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 { } else {
if(mPost.getSubredditIconUrl() == null) { if(mPost.getSubredditIconUrl() == null) {
new LoadSubredditIconAsyncTask( new LoadSubredditIconAsyncTask(
mRedditDataRoomDatabase.subredditDao(), mPost.getSubredditNamePrefixed().substring(2), mRedditDataRoomDatabase, mPost.getSubredditNamePrefixed().substring(2),
mRetrofit, iconImageUrl -> { mRetrofit, iconImageUrl -> {
if(iconImageUrl == null || iconImageUrl.equals("")) { if(iconImageUrl == null || iconImageUrl.equals("")) {
mGlide.load(R.drawable.subreddit_default_icon) mGlide.load(R.drawable.subreddit_default_icon)

View File

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

View File

@ -13,6 +13,7 @@ class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
void loadIconSuccess(String iconImageUrl); void loadIconSuccess(String iconImageUrl);
} }
private RedditDataRoomDatabase redditDataRoomDatabase;
private SubredditDao subredditDao; private SubredditDao subredditDao;
private String subredditName; private String subredditName;
private Retrofit retrofit; private Retrofit retrofit;
@ -20,9 +21,10 @@ class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
private boolean hasSubredditInDb; private boolean hasSubredditInDb;
private LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener; private LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener;
LoadSubredditIconAsyncTask(SubredditDao subredditDao, String subredditName, Retrofit retrofit, LoadSubredditIconAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String subredditName, Retrofit retrofit,
LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener) { LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener) {
this.subredditDao = subredditDao; this.redditDataRoomDatabase = redditDataRoomDatabase;
this.subredditDao = redditDataRoomDatabase.subredditDao();
this.subredditName = subredditName; this.subredditName = subredditName;
this.retrofit = retrofit; this.retrofit = retrofit;
this.loadSubredditIconAsyncTaskListener = loadSubredditIconAsyncTaskListener; this.loadSubredditIconAsyncTaskListener = loadSubredditIconAsyncTaskListener;
@ -52,7 +54,7 @@ class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) {
ArrayList<SubredditData> singleSubredditDataList = new ArrayList<>(); ArrayList<SubredditData> singleSubredditDataList = new ArrayList<>();
singleSubredditDataList.add(subredditData); singleSubredditDataList.add(subredditData);
new InsertSubscribedThingsAsyncTask(null, null, subredditDao, new InsertSubscribedThingsAsyncTask(redditDataRoomDatabase, null,
null, null, singleSubredditDataList, null, null, singleSubredditDataList,
() -> loadSubredditIconAsyncTaskListener.loadIconSuccess(subredditData.getIconUrl())).execute(); () -> 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.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
@ -58,6 +59,9 @@ import javax.inject.Named;
import Account.Account; import Account.Account;
import Account.AccountViewModel; import Account.AccountViewModel;
import SubredditDatabase.SubredditData;
import SubscribedSubredditDatabase.SubscribedSubredditData;
import SubscribedUserDatabase.SubscribedUserData;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation; 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"; static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN";
private static final String FETCH_USER_INFO_STATE = "FUIS"; 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 DRAWER_ON_ACCOUNT_SWITCH_STATE = "DOASS";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
@ -135,6 +140,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
private String mBannerImageUrl; private String mBannerImageUrl;
private int mKarma; private int mKarma;
private boolean mFetchUserInfoSuccess = false; private boolean mFetchUserInfoSuccess = false;
private boolean mFetchSubscriptionsSuccess = false;
private boolean mDrawerOnAccountSwitch = false; private boolean mDrawerOnAccountSwitch = false;
private String mMessageFullname; private String mMessageFullname;
private String mNewAccountName; private String mNewAccountName;
@ -258,6 +264,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
if(savedInstanceState != null) { if(savedInstanceState != null) {
mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE); mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE);
mFetchSubscriptionsSuccess = savedInstanceState.getBoolean(FETCH_SUBSCRIPTIONS_STATE);
mDrawerOnAccountSwitch = savedInstanceState.getBoolean(DRAWER_ON_ACCOUNT_SWITCH_STATE); mDrawerOnAccountSwitch = savedInstanceState.getBoolean(DRAWER_ON_ACCOUNT_SWITCH_STATE);
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
@ -435,6 +442,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
public void onPageScrollStateChanged(int state) {} public void onPageScrollStateChanged(int state) {}
}); });
loadSubscriptions();
glide = Glide.with(this); glide = Glide.with(this);
AccountRecyclerViewAdapter adapter = new AccountRecyclerViewAdapter(this, glide, mAccountName, 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() { private void loadUserData() {
if (!mFetchUserInfoSuccess) { if (!mFetchUserInfoSuccess) {
FetchMyInfo.fetchAccountInfo(mOauthRetrofit, mAccessToken, new FetchMyInfo.FetchUserMyListener() { FetchMyInfo.fetchAccountInfo(mOauthRetrofit, mAccessToken, new FetchMyInfo.FetchUserMyListener() {
@ -782,6 +818,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putBoolean(FETCH_USER_INFO_STATE, mFetchUserInfoSuccess); outState.putBoolean(FETCH_USER_INFO_STATE, mFetchUserInfoSuccess);
outState.putBoolean(FETCH_SUBSCRIPTIONS_STATE, mFetchSubscriptionsSuccess);
outState.putBoolean(DRAWER_ON_ACCOUNT_SWITCH_STATE, mDrawerOnAccountSwitch); outState.putBoolean(DRAWER_ON_ACCOUNT_SWITCH_STATE, mDrawerOnAccountSwitch);
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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