Fetch subscriptions in MainActivity. Check if the account exists before inserting subscriptions to the database.

This commit is contained in:
Alex Ning 2019-09-16 23:45:18 +08:00
parent 7e96130d20
commit 4e036da000
11 changed files with 68 additions and 26 deletions

View File

@ -254,7 +254,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
} else {
if(mPost.getSubredditIconUrl() == null) {
new LoadSubredditIconAsyncTask(
mRedditDataRoomDatabase.subredditDao(), mPost.getSubredditNamePrefixed().substring(2),
mRedditDataRoomDatabase, mPost.getSubredditNamePrefixed().substring(2),
mRetrofit, iconImageUrl -> {
if(iconImageUrl == null || iconImageUrl.equals("")) {
mGlide.load(R.drawable.subreddit_default_icon)

View File

@ -2,6 +2,8 @@ package ml.docilealligator.infinityforreddit;
import android.os.AsyncTask;
import androidx.annotation.Nullable;
import java.util.List;
import SubredditDatabase.SubredditDao;
@ -17,6 +19,8 @@ class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void> {
void insertSuccess();
}
private RedditDataRoomDatabase mRedditDataRoomDatabase;
private String mAccountName;
private SubscribedSubredditDao mSubscribedSubredditDao;
private SubscribedUserDao mUserDao;
private SubredditDao mSubredditDao;
@ -25,17 +29,16 @@ class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void> {
private List<SubredditData> subredditData;
private InsertSubscribedThingListener insertSubscribedThingListener;
InsertSubscribedThingsAsyncTask(SubscribedSubredditDao subscribedSubredditDao,
SubscribedUserDao userDao,
SubredditDao subredditDao,
InsertSubscribedThingsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, @Nullable String accountName,
List<SubscribedSubredditData> subscribedSubredditData,
List<SubscribedUserData> subscribedUserData,
List<SubredditData> subredditData,
InsertSubscribedThingListener insertSubscribedThingListener) {
mSubscribedSubredditDao = subscribedSubredditDao;
mUserDao = userDao;
mSubredditDao = subredditDao;
mRedditDataRoomDatabase = redditDataRoomDatabase;
mAccountName = accountName;
mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao();
mUserDao = redditDataRoomDatabase.subscribedUserDao();
mSubredditDao = redditDataRoomDatabase.subredditDao();
this.subscribedSubredditData = subscribedSubredditData;
this.subscribedUserData = subscribedUserData;
@ -45,6 +48,10 @@ class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(final Void... params) {
if(mAccountName != null && mRedditDataRoomDatabase.accountDao().getAccountData(mAccountName) == null) {
return null;
}
if(subscribedSubredditData != null) {
for (SubscribedSubredditData s : subscribedSubredditData) {
mSubscribedSubredditDao.insert(s);

View File

@ -13,6 +13,7 @@ class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
void loadIconSuccess(String iconImageUrl);
}
private RedditDataRoomDatabase redditDataRoomDatabase;
private SubredditDao subredditDao;
private String subredditName;
private Retrofit retrofit;
@ -20,9 +21,10 @@ class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
private boolean hasSubredditInDb;
private LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener;
LoadSubredditIconAsyncTask(SubredditDao subredditDao, String subredditName, Retrofit retrofit,
LoadSubredditIconAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName, Retrofit retrofit,
LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener) {
this.subredditDao = subredditDao;
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.subredditDao = redditDataRoomDatabase.subredditDao();
this.subredditName = subredditName;
this.retrofit = retrofit;
this.loadSubredditIconAsyncTaskListener = loadSubredditIconAsyncTaskListener;
@ -52,7 +54,7 @@ class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
ArrayList<SubredditData> singleSubredditDataList = new ArrayList<>();
singleSubredditDataList.add(subredditData);
new InsertSubscribedThingsAsyncTask(null, null, subredditDao,
new InsertSubscribedThingsAsyncTask(redditDataRoomDatabase, null,
null, null, singleSubredditDataList,
() -> loadSubredditIconAsyncTaskListener.loadIconSuccess(subredditData.getIconUrl())).execute();
}

View File

@ -51,6 +51,7 @@ import com.google.android.material.tabs.TabLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
@ -58,6 +59,9 @@ import javax.inject.Named;
import Account.Account;
import Account.AccountViewModel;
import SubredditDatabase.SubredditData;
import SubscribedSubredditDatabase.SubscribedSubredditData;
import SubscribedUserDatabase.SubscribedUserData;
import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
@ -77,6 +81,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN";
private static final String FETCH_USER_INFO_STATE = "FUIS";
private static final String FETCH_SUBSCRIPTIONS_STATE = "FSS";
private static final String DRAWER_ON_ACCOUNT_SWITCH_STATE = "DOASS";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
@ -135,6 +140,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
private String mBannerImageUrl;
private int mKarma;
private boolean mFetchUserInfoSuccess = false;
private boolean mFetchSubscriptionsSuccess = false;
private boolean mDrawerOnAccountSwitch = false;
private String mMessageFullname;
private String mNewAccountName;
@ -258,6 +264,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
if(savedInstanceState != null) {
mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE);
mFetchSubscriptionsSuccess = savedInstanceState.getBoolean(FETCH_SUBSCRIPTIONS_STATE);
mDrawerOnAccountSwitch = savedInstanceState.getBoolean(DRAWER_ON_ACCOUNT_SWITCH_STATE);
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
@ -435,6 +442,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
public void onPageScrollStateChanged(int state) {}
});
loadSubscriptions();
glide = Glide.with(this);
AccountRecyclerViewAdapter adapter = new AccountRecyclerViewAdapter(this, glide, mAccountName,
@ -634,6 +643,33 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
});
}
private void loadSubscriptions() {
if (mAccessToken != null && !mFetchSubscriptionsSuccess) {
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,
mAccountName,
subscribedSubredditData,
subscribedUserData,
subredditData,
() -> mFetchSubscriptionsSuccess = true).execute();
}
@Override
public void onFetchSubscribedThingFail() {
mFetchSubscriptionsSuccess = false;
}
});
}
}
private void loadUserData() {
if (!mFetchUserInfoSuccess) {
FetchMyInfo.fetchAccountInfo(mOauthRetrofit, mAccessToken, new FetchMyInfo.FetchUserMyListener() {
@ -782,6 +818,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(FETCH_USER_INFO_STATE, mFetchUserInfoSuccess);
outState.putBoolean(FETCH_SUBSCRIPTIONS_STATE, mFetchSubscriptionsSuccess);
outState.putBoolean(DRAWER_ON_ACCOUNT_SWITCH_STATE, mDrawerOnAccountSwitch);
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);

View File

@ -370,8 +370,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
}
private void loadSubredditIcon() {
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase.subredditDao(),
subredditName, mRetrofit, iconImageUrl -> {
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase, subredditName, mRetrofit, iconImageUrl -> {
iconUrl = iconImageUrl;
displaySubredditIcon();
loadSubredditIconSuccessful = true;

View File

@ -303,8 +303,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
}
private void loadSubredditIcon() {
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase.subredditDao(),
subredditName, mRetrofit, iconImageUrl -> {
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase, subredditName, mRetrofit, iconImageUrl -> {
iconUrl = iconImageUrl;
displaySubredditIcon();
loadSubredditIconSuccessful = true;

View File

@ -58,6 +58,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
private Retrofit mRetrofit;
private String mAccessToken;
private RequestManager mGlide;
private RedditDataRoomDatabase mRedditDataRoomDatabase;
private SubredditDao mSubredditDao;
private UserDao mUserDao;
private boolean canStartActivity = true;
@ -89,6 +90,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
mPostType = postType;
mDisplaySubredditName = displaySubredditName;
mGlide = Glide.with(mContext.getApplicationContext());
mRedditDataRoomDatabase = redditDataRoomDatabase;
mSubredditDao = redditDataRoomDatabase.subredditDao();
mUserDao = redditDataRoomDatabase.userDao();
mCallback = callback;
@ -192,7 +194,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
}
} else {
if(post.getSubredditIconUrl() == null) {
new LoadSubredditIconAsyncTask(mSubredditDao, subredditName, mRetrofit,
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase, subredditName, mRetrofit,
iconImageUrl -> {
if(mContext != null && getItemCount() > 0) {
if(iconImageUrl == null || iconImageUrl.equals("")) {

View File

@ -307,8 +307,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
}
private void loadSubredditIcon() {
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase.subredditDao(),
subredditName, mRetrofit, iconImageUrl -> {
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase, subredditName, mRetrofit, iconImageUrl -> {
iconUrl = iconImageUrl;
displaySubredditIcon();
loadSubredditIconSuccessful = true;

View File

@ -379,8 +379,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
}
private void loadSubredditIcon() {
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase.subredditDao(),
subredditName, mRetrofit, iconImageUrl -> {
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase, subredditName, mRetrofit, iconImageUrl -> {
iconUrl = iconImageUrl;
displaySubredditIcon();
loadSubredditIconSuccessful = true;

View File

@ -214,9 +214,8 @@ public class SubredditSelectionActivity extends AppCompatActivity {
ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData) {
new InsertSubscribedThingsAsyncTask(
mRedditDataRoomDatabase.subscribedSubredditDao(),
mRedditDataRoomDatabase.subscribedUserDao(),
mRedditDataRoomDatabase.subredditDao(),
mRedditDataRoomDatabase,
mAccountName,
subscribedSubredditData,
subscribedUserData,
subredditData,

View File

@ -217,9 +217,8 @@ public class SubscribedThingListingActivity extends AppCompatActivity {
ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData) {
new InsertSubscribedThingsAsyncTask(
mRedditDataRoomDatabase.subscribedSubredditDao(),
mRedditDataRoomDatabase.subscribedUserDao(),
mRedditDataRoomDatabase.subredditDao(),
mRedditDataRoomDatabase,
mAccountName,
subscribedSubredditData,
subscribedUserData,
subredditData,