diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-09-16 15:45:18 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-09-16 15:45:18 +0000 |
commit | 4e036da000a6242d2f37a00f09f3c938c13e640e (patch) | |
tree | c73afc8aa9855b588860a8e16a3b746a44adfc31 /app/src/main/java | |
parent | 7e96130d200f7791ab4c3e2deb0cc2ca9f152e93 (diff) | |
download | infinity-for-reddit-4e036da000a6242d2f37a00f09f3c938c13e640e.tar infinity-for-reddit-4e036da000a6242d2f37a00f09f3c938c13e640e.tar.gz infinity-for-reddit-4e036da000a6242d2f37a00f09f3c938c13e640e.tar.bz2 infinity-for-reddit-4e036da000a6242d2f37a00f09f3c938c13e640e.tar.lz infinity-for-reddit-4e036da000a6242d2f37a00f09f3c938c13e640e.tar.xz infinity-for-reddit-4e036da000a6242d2f37a00f09f3c938c13e640e.tar.zst infinity-for-reddit-4e036da000a6242d2f37a00f09f3c938c13e640e.zip |
Fetch subscriptions in MainActivity. Check if the account exists before inserting subscriptions to the database.
Diffstat (limited to 'app/src/main/java')
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<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) 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, 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); 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, 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(); } 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> 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); 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<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("")) { 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> subscribedUserData, ArrayList<SubredditData> 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> subscribedUserData, ArrayList<SubredditData> subredditData) { new InsertSubscribedThingsAsyncTask( - mRedditDataRoomDatabase.subscribedSubredditDao(), - mRedditDataRoomDatabase.subscribedUserDao(), - mRedditDataRoomDatabase.subredditDao(), + mRedditDataRoomDatabase, + mAccountName, subscribedSubredditData, subscribedUserData, subredditData, |