From 4e036da000a6242d2f37a00f09f3c938c13e640e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 16 Sep 2019 23:45:18 +0800 Subject: Fetch subscriptions in MainActivity. Check if the account exists before inserting subscriptions to the database. --- .../CommentAndPostRecyclerViewAdapter.java | 2 +- .../InsertSubscribedThingsAsyncTask.java | 21 ++++++++---- .../LoadSubredditIconAsyncTask.java | 8 +++-- .../infinityforreddit/MainActivity.java | 37 ++++++++++++++++++++++ .../infinityforreddit/PostImageActivity.java | 3 +- .../infinityforreddit/PostLinkActivity.java | 3 +- .../infinityforreddit/PostRecyclerViewAdapter.java | 4 ++- .../infinityforreddit/PostTextActivity.java | 3 +- .../infinityforreddit/PostVideoActivity.java | 3 +- .../SubredditSelectionActivity.java | 5 ++- .../SubscribedThingListingActivity.java | 5 ++- 11 files changed, 68 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index eb815582..1cbc1466 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -254,7 +254,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { if(iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/InsertSubscribedThingsAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/InsertSubscribedThingsAsyncTask.java index c5b517c2..22b79e85 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/InsertSubscribedThingsAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/InsertSubscribedThingsAsyncTask.java @@ -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 insertSuccess(); } + private RedditDataRoomDatabase mRedditDataRoomDatabase; + private String mAccountName; private SubscribedSubredditDao mSubscribedSubredditDao; private SubscribedUserDao mUserDao; private SubredditDao mSubredditDao; @@ -25,17 +29,16 @@ class InsertSubscribedThingsAsyncTask extends AsyncTask { private List subredditData; private InsertSubscribedThingListener insertSubscribedThingListener; - InsertSubscribedThingsAsyncTask(SubscribedSubredditDao subscribedSubredditDao, - SubscribedUserDao userDao, - SubredditDao subredditDao, + InsertSubscribedThingsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, @Nullable String accountName, List subscribedSubredditData, List subscribedUserData, List 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 { @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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java index db44d980..c35c9f3f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadSubredditIconAsyncTask.java @@ -13,6 +13,7 @@ class LoadSubredditIconAsyncTask extends AsyncTask { void loadIconSuccess(String iconImageUrl); } + private RedditDataRoomDatabase redditDataRoomDatabase; private SubredditDao subredditDao; private String subredditName; private Retrofit retrofit; @@ -20,9 +21,10 @@ class LoadSubredditIconAsyncTask extends AsyncTask { 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 { public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { ArrayList singleSubredditDataList = new ArrayList<>(); singleSubredditDataList.add(subredditData); - new InsertSubscribedThingsAsyncTask(null, null, subredditDao, + new InsertSubscribedThingsAsyncTask(redditDataRoomDatabase, null, null, null, singleSubredditDataList, () -> loadSubredditIconAsyncTaskListener.loadIconSuccess(subredditData.getIconUrl())).execute(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index f4c90da5..389dc1d5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -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, + ArrayList subscribedUserData, + ArrayList 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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index aa9a34d3..27c27fd2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index 426eb062..cab3288d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index eff6c6c2..04a81c87 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -58,6 +58,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter { if(mContext != null && getItemCount() > 0) { if(iconImageUrl == null || iconImageUrl.equals("")) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index 257c97ef..b8e967f2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index ec5b1bca..eb3e51ff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index ee452eca..4bb67e27 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -214,9 +214,8 @@ public class SubredditSelectionActivity extends AppCompatActivity { ArrayList subscribedUserData, ArrayList subredditData) { new InsertSubscribedThingsAsyncTask( - mRedditDataRoomDatabase.subscribedSubredditDao(), - mRedditDataRoomDatabase.subscribedUserDao(), - mRedditDataRoomDatabase.subredditDao(), + mRedditDataRoomDatabase, + mAccountName, subscribedSubredditData, subscribedUserData, subredditData, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java index 305e6bee..35d49eeb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java @@ -217,9 +217,8 @@ public class SubscribedThingListingActivity extends AppCompatActivity { ArrayList subscribedUserData, ArrayList subredditData) { new InsertSubscribedThingsAsyncTask( - mRedditDataRoomDatabase.subscribedSubredditDao(), - mRedditDataRoomDatabase.subscribedUserDao(), - mRedditDataRoomDatabase.subredditDao(), + mRedditDataRoomDatabase, + mAccountName, subscribedSubredditData, subscribedUserData, subredditData, -- cgit v1.2.3