Load subscriptions in SubredditSelectionActivity. Minor bugs fixed.

This commit is contained in:
Alex Ning 2019-08-08 13:44:27 +08:00
parent eb1d243f2c
commit 5b5da3d3fd
3 changed files with 75 additions and 14 deletions

View File

@ -48,6 +48,10 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
private static final String ACCOUNT_NAME_STATE = "ANS";
private static final String ACCOUNT_PROFILE_IMAGE_URL_STATE = "APIUS";
private static final String ACCOUNT_BANNER_IMAGE_URL_STATE = "ABIUS";
private static final String ACCOUNT_KARMA_STATE = "AKS";
private static final int LOGIN_ACTIVITY_REQUEST_CODE = 0;
@ -129,6 +133,10 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
mProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL_STATE);
mBannerImageUrl = savedInstanceState.getString(ACCOUNT_BANNER_IMAGE_URL_STATE);
mKarma = savedInstanceState.getString(ACCOUNT_KARMA_STATE);
if(!mNullAccessToken && mAccessToken == null) {
getCurrentAccountAndBindView();
} else {
@ -353,6 +361,10 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mName);
outState.putString(ACCOUNT_PROFILE_IMAGE_URL_STATE, mProfileImageUrl);
outState.putString(ACCOUNT_BANNER_IMAGE_URL_STATE, mBannerImageUrl);
outState.putString(ACCOUNT_KARMA_STATE, mKarma);
}
@Override

View File

@ -12,10 +12,17 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import javax.inject.Inject;
import java.util.ArrayList;
import javax.inject.Inject;
import javax.inject.Named;
import SubredditDatabase.SubredditData;
import SubscribedSubredditDatabase.SubscribedSubredditData;
import SubscribedUserDatabase.SubscribedUserData;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Retrofit;
public class SubredditSelectionActivity extends AppCompatActivity {
@ -25,19 +32,27 @@ public class SubredditSelectionActivity extends AppCompatActivity {
static final String EXTRA_RETURN_SUBREDDIT_IS_USER = "ERSIU";
private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 0;
private static final String NULL_ACCOUNT_NAME_STATE = "NATS";
private static final String ACCOUNT_NAME_STATE = "ATS";
private static final String INSERT_SUBSCRIBED_SUBREDDIT_STATE = "ISSS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
private static final String ACCOUNT_NAME_STATE = "ANS";
private static final String ACCOUNT_PROFILE_IMAGE_URL = "APIU";
private static final String FRAGMENT_OUT_STATE = "FOS";
@BindView(R.id.toolbar_subreddit_selection_activity) Toolbar toolbar;
private boolean mNullAccountName = false;
private boolean mNullAccessToken = false;
private String mAccessToken;
private String mAccountName;
private String mAccountProfileImageUrl;
private boolean mInsertSuccess = false;
private Fragment mFragment;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@ -54,14 +69,16 @@ public class SubredditSelectionActivity extends AppCompatActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(savedInstanceState == null) {
getCurrentAccountAndInitializeFragment();
getCurrentAccountAndBindView();
} else {
mNullAccountName = savedInstanceState.getBoolean(NULL_ACCOUNT_NAME_STATE);
mInsertSuccess = savedInstanceState.getBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE);
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
mAccountProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL);
if(!mNullAccountName && mAccountName == null) {
getCurrentAccountAndInitializeFragment();
if(!mNullAccessToken && mAccountName == null) {
getCurrentAccountAndBindView();
} else {
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit();
@ -69,19 +86,22 @@ public class SubredditSelectionActivity extends AppCompatActivity {
}
}
private void getCurrentAccountAndInitializeFragment() {
private void getCurrentAccountAndBindView() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccountName = true;
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountProfileImageUrl = account.getProfileImageUrl();
}
initializeFragment();
bindView();
}).execute();
}
private void initializeFragment() {
private void bindView() {
loadSubscriptions();
mFragment = new SubscribedSubredditsListingFragment();
Bundle bundle = new Bundle();
bundle.putString(SubscribedSubredditsListingFragment.EXTRA_ACCOUNT_NAME, mAccountName);
@ -95,6 +115,34 @@ public class SubredditSelectionActivity extends AppCompatActivity {
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit();
}
private void loadSubscriptions() {
if (!mInsertSuccess) {
FetchSubscribedThing.fetchSubscribedThing(mOauthRetrofit, mAccessToken, mAccountName, null,
new ArrayList<>(), new ArrayList<>(),
new ArrayList<>(),
new FetchSubscribedThing.FetchSubscribedThingListener() {
@Override
public void onFetchSubscribedThingSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData) {
new InsertSubscribedThingsAsyncTask(
mRedditDataRoomDatabase.subscribedSubredditDao(),
mRedditDataRoomDatabase.subscribedUserDao(),
mRedditDataRoomDatabase.subredditDao(),
subscribedSubredditData,
subscribedUserData,
subredditData,
() -> mInsertSuccess = true).execute();
}
@Override
public void onFetchSubscribedThingFail() {
mInsertSuccess = false;
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.subreddit_selection_activity, menu);
@ -149,7 +197,9 @@ public class SubredditSelectionActivity extends AppCompatActivity {
if (mFragment != null) {
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment);
}
outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName);
outState.putBoolean(INSERT_SUBSCRIBED_SUBREDDIT_STATE, mInsertSuccess);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
outState.putString(ACCOUNT_PROFILE_IMAGE_URL, mAccountProfileImageUrl);
}

View File

@ -29,7 +29,6 @@ import retrofit2.Retrofit;
public class SubscribedThingListingActivity extends AppCompatActivity {
private static final String INSERT_SUBSCRIBED_SUBREDDIT_STATE = "ISSS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
private static final String ACCOUNT_NAME_STATE = "ANS";