From d5f3761973e20acb63e10bbfea529b2d1b3fbc85 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 28 Sep 2021 20:25:58 +0800 Subject: Fix subreddits disappear in anonymous multireddit after favoriting it. --- .../activities/MultiredditSelectionActivity.java | 2 +- .../activities/SubscribedThingListingActivity.java | 2 +- .../asynctasks/InsertMultireddit.java | 20 +++++++++++++++----- .../multireddit/CreateMultiReddit.java | 2 +- .../multireddit/EditMultiReddit.java | 5 +++-- .../multireddit/FavoriteMultiReddit.java | 2 +- 6 files changed, 22 insertions(+), 11 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java index 0e189274..943dbf2d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java @@ -168,7 +168,7 @@ public class MultiredditSelectionActivity extends BaseActivity implements Activi FetchMyMultiReddits.fetchMyMultiReddits(mOauthRetrofit, mAccessToken, new FetchMyMultiReddits.FetchMyMultiRedditsListener() { @Override public void success(ArrayList multiReddits) { - InsertMultireddit.insertMultireddit(mExecutor, new Handler(), mRedditDataRoomDatabase, + InsertMultireddit.insertMultireddits(mExecutor, new Handler(), mRedditDataRoomDatabase, multiReddits, mAccountName, () -> { mInsertSuccess = true; ((MultiRedditListingFragment) mFragment).stopRefreshProgressbar(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java index 850ada27..d4c4caee 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java @@ -298,7 +298,7 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti FetchMyMultiReddits.fetchMyMultiReddits(mOauthRetrofit, mAccessToken, new FetchMyMultiReddits.FetchMyMultiRedditsListener() { @Override public void success(ArrayList multiReddits) { - InsertMultireddit.insertMultireddit(mExecutor, new Handler(), mRedditDataRoomDatabase, multiReddits, mAccountName, () -> { + InsertMultireddit.insertMultireddits(mExecutor, new Handler(), mRedditDataRoomDatabase, multiReddits, mAccountName, () -> { mInsertMultiredditSuccess = true; sectionsPagerAdapter.stopMultiRedditRefreshProgressbar(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/InsertMultireddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/InsertMultireddit.java index 99d046c9..80fbb74e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/InsertMultireddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/InsertMultireddit.java @@ -7,15 +7,16 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.Executor; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.multireddit.AnonymousMultiredditSubreddit; import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import ml.docilealligator.infinityforreddit.multireddit.MultiRedditDao; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class InsertMultireddit { - public static void insertMultireddit(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, - ArrayList multiReddits, String accountName, - InsertMultiRedditListener insertMultiRedditListener) { + public static void insertMultireddits(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, + ArrayList multiReddits, String accountName, + InsertMultiRedditListener insertMultiRedditListener) { executor.execute(() -> { MultiRedditDao multiRedditDao = redditDataRoomDatabase.multiRedditDao(); List existingMultiReddits = multiRedditDao.getAllMultiRedditsList(accountName); @@ -39,7 +40,16 @@ public class InsertMultireddit { MultiReddit multiReddit, InsertMultiRedditListener insertMultiRedditListener) { executor.execute(() -> { - redditDataRoomDatabase.multiRedditDao().insert(multiReddit); + if (multiReddit.getOwner().equals("-")) { + ArrayList allAnonymousMultiRedditSubreddits = + (ArrayList) redditDataRoomDatabase.anonymousMultiredditSubredditDao().getAllAnonymousMultiRedditSubreddits(multiReddit.getPath()); + redditDataRoomDatabase.multiRedditDao().insert(multiReddit); + if (allAnonymousMultiRedditSubreddits != null) { + redditDataRoomDatabase.anonymousMultiredditSubredditDao().insertAll(allAnonymousMultiRedditSubreddits); + } + } else { + redditDataRoomDatabase.multiRedditDao().insert(multiReddit); + } handler.post(insertMultiRedditListener::success); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/CreateMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/CreateMultiReddit.java index 8eb52a66..1c3113ea 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/CreateMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/CreateMultiReddit.java @@ -70,7 +70,7 @@ public class CreateMultiReddit { redditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); } redditDataRoomDatabase.multiRedditDao().insert(new MultiReddit(multipath, name, name, description, - null, null, null, "-", 0, System.currentTimeMillis(), true, false, false)); + null, null, "private", "-", 0, System.currentTimeMillis(), true, false, false)); List anonymousMultiredditSubreddits = new ArrayList<>(); for (String s : subreddits) { anonymousMultiredditSubreddits.add(new AnonymousMultiredditSubreddit(multipath, s)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/EditMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/EditMultiReddit.java index 6efa6487..b19875f5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/EditMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/EditMultiReddit.java @@ -51,9 +51,10 @@ public class EditMultiReddit { MultiReddit multiReddit, EditMultiRedditListener editMultiRedditListener) { executor.execute(() -> { - redditDataRoomDatabase.multiRedditDao().insert(multiReddit); ArrayList anonymousMultiredditSubreddits = new ArrayList<>(); - for (String s : multiReddit.getSubreddits()) { + ArrayList subreddits = multiReddit.getSubreddits(); + redditDataRoomDatabase.multiRedditDao().insert(multiReddit); + for (String s : subreddits) { anonymousMultiredditSubreddits.add(new AnonymousMultiredditSubreddit(multiReddit.getPath(), s)); } redditDataRoomDatabase.anonymousMultiredditSubredditDao().insertAll(anonymousMultiredditSubreddits); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FavoriteMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FavoriteMultiReddit.java index 227b7665..2d78a388 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FavoriteMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FavoriteMultiReddit.java @@ -32,7 +32,7 @@ public class FavoriteMultiReddit { params.put(APIUtils.MAKE_FAVORITE_KEY, String.valueOf(makeFavorite)); params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); oauthRetrofit.create(RedditAPI.class).favoriteMultiReddit(APIUtils.getOAuthHeader(accessToken), - params).enqueue(new Callback() { + params).enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { -- cgit v1.2.3