From adb77f44d0e01480d80aa354a6af0587401cfdbf Mon Sep 17 00:00:00 2001
From: Docile-Alligator <chineseperson5@gmail.com>
Date: Wed, 1 Jun 2022 14:53:30 +0800
Subject: Fix app crashes when applying Material You theme after changing
 wallpaper. Tweak the design of the fast scroller.

---
 app/src/main/AndroidManifest.xml                   |   3 -
 .../infinityforreddit/AppComponent.java            |   5 +-
 .../infinityforreddit/MaterialYouWorker.java       |  55 ++++
 .../infinityforreddit/activities/MainActivity.java |   3 +-
 .../activities/SettingsActivity.java               |   3 +-
 .../WallpaperChangeReceiver.java                   |  11 +-
 .../fragments/FollowedUsersListingFragment.java    |   2 +-
 .../fragments/MultiRedditListingFragment.java      |   2 +-
 .../SubscribedSubredditsListingFragment.java       |   2 +-
 .../services/MaterialYouService.java               |  97 -------
 .../settings/ThemePreferenceFragment.java          |   4 +-
 .../infinityforreddit/utils/MaterialYouUtils.java  | 320 +++++++++++----------
 12 files changed, 244 insertions(+), 263 deletions(-)
 create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/MaterialYouWorker.java
 delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java

(limited to 'app/src')

diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 654a9708..d343bfe6 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -438,9 +438,6 @@
             android:name=".services.SubmitPostService"
             android:enabled="true"
             android:exported="false" />
-        <service
-            android:name=".services.MaterialYouService"
-            android:exported="false" />
         <service
             android:name=".services.EditProfileService"
             android:enabled="true"
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
index 144d0565..6b899a13 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
@@ -81,7 +81,6 @@ import ml.docilealligator.infinityforreddit.fragments.ViewRedditGalleryVideoFrag
 import ml.docilealligator.infinityforreddit.services.DownloadMediaService;
 import ml.docilealligator.infinityforreddit.services.DownloadRedditVideoService;
 import ml.docilealligator.infinityforreddit.services.EditProfileService;
-import ml.docilealligator.infinityforreddit.services.MaterialYouService;
 import ml.docilealligator.infinityforreddit.services.SubmitPostService;
 import ml.docilealligator.infinityforreddit.settings.AdvancedPreferenceFragment;
 import ml.docilealligator.infinityforreddit.settings.CommentPreferenceFragment;
@@ -276,8 +275,6 @@ public interface AppComponent {
 
     void inject(LockScreenActivity lockScreenActivity);
 
-    void inject(MaterialYouService materialYouService);
-
     void inject(RPANActivity rpanActivity);
 
     void inject(ViewRPANBroadcastFragment viewRPANBroadcastFragment);
@@ -301,4 +298,6 @@ public interface AppComponent {
     void inject(PostPollActivity postPollActivity);
 
     void inject(AccountChooserBottomSheetFragment accountChooserBottomSheetFragment);
+
+    void inject(MaterialYouWorker materialYouWorker);
 }
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MaterialYouWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MaterialYouWorker.java
new file mode 100644
index 00000000..afa7d7ce
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MaterialYouWorker.java
@@ -0,0 +1,55 @@
+package ml.docilealligator.infinityforreddit;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+
+import androidx.annotation.NonNull;
+import androidx.work.Worker;
+import androidx.work.WorkerParameters;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
+import ml.docilealligator.infinityforreddit.utils.MaterialYouUtils;
+import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
+
+public class MaterialYouWorker extends Worker {
+    public static final String UNIQUE_WORKER_NAME = "MYWT";
+
+    @Inject
+    @Named("default")
+    SharedPreferences mSharedPreferences;
+    @Inject
+    @Named("light_theme")
+    SharedPreferences lightThemeSharedPreferences;
+    @Inject
+    @Named("dark_theme")
+    SharedPreferences darkThemeSharedPreferences;
+    @Inject
+    @Named("amoled_theme")
+    SharedPreferences amoledThemeSharedPreferences;
+    @Inject
+    RedditDataRoomDatabase redditDataRoomDatabase;
+    @Inject
+    CustomThemeWrapper customThemeWrapper;
+    private Context context;
+
+    public MaterialYouWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
+        super(context, workerParams);
+        this.context = context;
+        ((Infinity) context.getApplicationContext()).getAppComponent().inject(this);
+    }
+
+    @NonNull
+    @Override
+    public Result doWork() {
+        if (mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_MATERIAL_YOU, false)) {
+            MaterialYouUtils.changeThemeSync(context, redditDataRoomDatabase,
+                    customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences,
+                    amoledThemeSharedPreferences);
+        }
+
+        return Result.success();
+    }
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java
index 02bff5d4..26c67ca9 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java
@@ -61,6 +61,7 @@ import com.google.android.material.textfield.TextInputEditText;
 
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
+import org.greenrobot.eventbus.ThreadMode;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -1210,7 +1211,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
         }
     }
 
-    @Subscribe
+    @Subscribe(threadMode = ThreadMode.MAIN)
     public void onRecreateActivityEvent(RecreateActivityEvent recreateActivityEvent) {
         ActivityCompat.recreate(this);
     }
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java
index 067e44a1..5705e3eb 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java
@@ -17,6 +17,7 @@ import com.google.android.material.appbar.CollapsingToolbarLayout;
 
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
+import org.greenrobot.eventbus.ThreadMode;
 
 import javax.inject.Inject;
 import javax.inject.Named;
@@ -189,7 +190,7 @@ public class SettingsActivity extends BaseActivity implements
         EventBus.getDefault().unregister(this);
     }
 
-    @Subscribe
+    @Subscribe(threadMode = ThreadMode.MAIN)
     public void onRecreateActivityEvent(RecreateActivityEvent recreateActivityEvent) {
         ActivityCompat.recreate(this);
     }
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java
index e65b97f8..f92c79ef 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/WallpaperChangeReceiver.java
@@ -5,9 +5,11 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 
-import androidx.core.content.ContextCompat;
+import androidx.work.ExistingWorkPolicy;
+import androidx.work.OneTimeWorkRequest;
+import androidx.work.WorkManager;
 
-import ml.docilealligator.infinityforreddit.services.MaterialYouService;
+import ml.docilealligator.infinityforreddit.MaterialYouWorker;
 import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
 
 public class WallpaperChangeReceiver extends BroadcastReceiver {
@@ -20,8 +22,9 @@ public class WallpaperChangeReceiver extends BroadcastReceiver {
     @Override
     public void onReceive(Context context, Intent intent) {
         if (sharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_MATERIAL_YOU, false)) {
-            Intent materialYouIntent = new Intent(context, MaterialYouService.class);
-            ContextCompat.startForegroundService(context, materialYouIntent);
+            OneTimeWorkRequest materialYouRequest = OneTimeWorkRequest.from(MaterialYouWorker.class);
+            WorkManager.getInstance(context).enqueueUniqueWork(MaterialYouWorker.UNIQUE_WORKER_NAME,
+                    ExistingWorkPolicy.REPLACE, materialYouRequest);
         }
     }
 }
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/FollowedUsersListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/FollowedUsersListingFragment.java
index bd2eed16..7ed43e60 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/FollowedUsersListingFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/FollowedUsersListingFragment.java
@@ -117,7 +117,7 @@ public class FollowedUsersListingFragment extends Fragment implements FragmentCo
         FollowedUsersRecyclerViewAdapter adapter = new FollowedUsersRecyclerViewAdapter(mActivity,
                 mExecutor, mOauthRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, accessToken);
         mRecyclerView.setAdapter(adapter);
-        new FastScrollerBuilder(mRecyclerView).build();
+        new FastScrollerBuilder(mRecyclerView).useMd2Style().build();
 
         mSubscribedUserViewModel = new ViewModelProvider(this,
                 new SubscribedUserViewModel.Factory(mActivity.getApplication(), mRedditDataRoomDatabase, getArguments().getString(EXTRA_ACCOUNT_NAME)))
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java
index 1b7d15a9..962fadc4 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MultiRedditListingFragment.java
@@ -154,7 +154,7 @@ public class MultiRedditListingFragment extends Fragment implements FragmentComm
                 }
             });
         }
-        new FastScrollerBuilder(mRecyclerView).build();
+        new FastScrollerBuilder(mRecyclerView).useMd2Style().build();
 
         mMultiRedditViewModel = new ViewModelProvider(this,
                 new MultiRedditViewModel.Factory(mActivity.getApplication(), mRedditDataRoomDatabase, accountName))
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java
index 3ee37f7e..3a18b05e 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java
@@ -132,7 +132,7 @@ public class SubscribedSubredditsListingFragment extends Fragment implements Fra
         }
 
         mRecyclerView.setAdapter(adapter);
-        new FastScrollerBuilder(mRecyclerView).build();
+        new FastScrollerBuilder(mRecyclerView).useMd2Style().build();
 
         mSubscribedSubredditViewModel = new ViewModelProvider(this,
                 new SubscribedSubredditViewModel.Factory(mActivity.getApplication(), mRedditDataRoomDatabase, accountName))
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java
deleted file mode 100644
index 40059987..00000000
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/MaterialYouService.java
+++ /dev/null
@@ -1,97 +0,0 @@
-package ml.docilealligator.infinityforreddit.services;
-
-import android.app.Notification;
-import android.app.Service;
-import android.content.Intent;
-import android.content.SharedPreferences;
-import android.os.Handler;
-import android.os.IBinder;
-
-import androidx.annotation.Nullable;
-import androidx.core.app.NotificationChannelCompat;
-import androidx.core.app.NotificationCompat;
-import androidx.core.app.NotificationManagerCompat;
-
-import java.util.concurrent.Executor;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-
-import ml.docilealligator.infinityforreddit.Infinity;
-import ml.docilealligator.infinityforreddit.R;
-import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
-import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
-import ml.docilealligator.infinityforreddit.utils.MaterialYouUtils;
-import ml.docilealligator.infinityforreddit.utils.NotificationUtils;
-import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
-
-public class MaterialYouService extends Service {
-
-    @Inject
-    @Named("default")
-    SharedPreferences mSharedPreferences;
-    @Inject
-    @Named("light_theme")
-    SharedPreferences lightThemeSharedPreferences;
-    @Inject
-    @Named("dark_theme")
-    SharedPreferences darkThemeSharedPreferences;
-    @Inject
-    @Named("amoled_theme")
-    SharedPreferences amoledThemeSharedPreferences;
-    @Inject
-    RedditDataRoomDatabase redditDataRoomDatabase;
-    @Inject
-    CustomThemeWrapper customThemeWrapper;
-    @Inject
-    Executor executor;
-
-    public MaterialYouService() {
-    }
-
-    @Nullable
-    @Override
-    public IBinder onBind(Intent intent) {
-        return null;
-    }
-
-    @Override
-    public int onStartCommand(Intent intent, int flags, int startId) {
-        ((Infinity) getApplication()).getAppComponent().inject(this);
-
-        NotificationChannelCompat serviceChannel =
-                new NotificationChannelCompat.Builder(
-                        NotificationUtils.CHANNEL_ID_MATERIAL_YOU,
-                        NotificationManagerCompat.IMPORTANCE_LOW)
-                        .setName(NotificationUtils.CHANNEL_MATERIAL_YOU)
-                        .build();
-
-        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
-        manager.createNotificationChannel(serviceChannel);
-
-        Notification notification = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_MATERIAL_YOU)
-                .setContentTitle(getString(R.string.material_you_notification_title))
-                .setContentText(getString(R.string.please_wait))
-                .setSmallIcon(R.drawable.ic_notification)
-                .setColor(customThemeWrapper.getColorPrimaryLightTheme())
-                .build();
-        startForeground(NotificationUtils.MATERIAL_YOU_NOTIFICATION_ID, notification);
-
-        if (mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_MATERIAL_YOU, false)) {
-            MaterialYouUtils.changeTheme(this, executor, new Handler(), redditDataRoomDatabase,
-                    customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences,
-                    amoledThemeSharedPreferences, () -> {
-                        stopService();
-                    });
-        } else {
-            stopService();
-        }
-
-        return START_NOT_STICKY;
-    }
-
-    private void stopService() {
-        stopForeground(true);
-        stopSelf();
-    }
-}
\ No newline at end of file
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java
index cb0c759b..65bea6ef 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/ThemePreferenceFragment.java
@@ -179,7 +179,7 @@ public class ThemePreferenceFragment extends CustomFontPreferenceFragmentCompat
 
             enableMaterialYouSwitchPreference.setOnPreferenceChangeListener((preference, newValue) -> {
                 if ((Boolean) newValue) {
-                    MaterialYouUtils.changeTheme(activity, executor, new Handler(),
+                    MaterialYouUtils.changeThemeASync(activity, executor, new Handler(),
                             redditDataRoomDatabase, customThemeWrapper,
                             lightThemeSharedPreferences, darkThemeSharedPreferences,
                             amoledThemeSharedPreferences, null);
@@ -191,7 +191,7 @@ public class ThemePreferenceFragment extends CustomFontPreferenceFragmentCompat
             });
 
             applyMaterialYouPreference.setOnPreferenceClickListener(preference -> {
-                MaterialYouUtils.changeTheme(activity, executor, new Handler(),
+                MaterialYouUtils.changeThemeASync(activity, executor, new Handler(),
                         redditDataRoomDatabase, customThemeWrapper,
                         lightThemeSharedPreferences, darkThemeSharedPreferences,
                         amoledThemeSharedPreferences, null);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java
index 9d5f27cb..7268cb2b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java
@@ -40,83 +40,185 @@ public class MaterialYouUtils {
         });
     }
 
-    public static void changeTheme(Context context, Executor executor, Handler handler,
-                                   RedditDataRoomDatabase redditDataRoomDatabase,
-                                   CustomThemeWrapper customThemeWrapper,
-                                   SharedPreferences lightThemeSharedPreferences,
-                                   SharedPreferences darkThemeSharedPreferences,
-                                   SharedPreferences amoledThemeSharedPreferences,
-                                   @Nullable MaterialYouListener materialYouListener) {
+    public static void changeThemeSync(Context context,
+                                        RedditDataRoomDatabase redditDataRoomDatabase,
+                                        CustomThemeWrapper customThemeWrapper,
+                                        SharedPreferences lightThemeSharedPreferences,
+                                        SharedPreferences darkThemeSharedPreferences,
+                                        SharedPreferences amoledThemeSharedPreferences) {
+        try {
+            Thread.sleep(2000);
+        } catch (InterruptedException ignored) { }
+        if (changeTheme(context, redditDataRoomDatabase, customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences)) {
+            EventBus.getDefault().post(new RecreateActivityEvent());
+        }
+    }
+
+    public static void changeThemeASync(Context context, Executor executor, Handler handler,
+                                        RedditDataRoomDatabase redditDataRoomDatabase,
+                                        CustomThemeWrapper customThemeWrapper,
+                                        SharedPreferences lightThemeSharedPreferences,
+                                        SharedPreferences darkThemeSharedPreferences,
+                                        SharedPreferences amoledThemeSharedPreferences,
+                                        @Nullable MaterialYouListener materialYouListener) {
         executor.execute(() -> {
             try {
                 Thread.sleep(2000);
             } catch (InterruptedException ignored) { }
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+            if (changeTheme(context, redditDataRoomDatabase, customThemeWrapper, lightThemeSharedPreferences, darkThemeSharedPreferences, amoledThemeSharedPreferences)) {
+                handler.post(() -> {
+                    if (materialYouListener != null) {
+                        materialYouListener.applied();
+                    }
+                    EventBus.getDefault().post(new RecreateActivityEvent());
+                });
+            }
+        });
+    }
+
+    private static boolean changeTheme(Context context,
+                             RedditDataRoomDatabase redditDataRoomDatabase,
+                             CustomThemeWrapper customThemeWrapper,
+                             SharedPreferences lightThemeSharedPreferences,
+                             SharedPreferences darkThemeSharedPreferences,
+                             SharedPreferences amoledThemeSharedPreferences) {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+            CustomTheme lightTheme = CustomThemeWrapper.getIndigo(context);
+            CustomTheme darkTheme = CustomThemeWrapper.getIndigoDark(context);
+            CustomTheme amoledTheme = CustomThemeWrapper.getIndigoAmoled(context);
+
+            lightTheme.colorPrimary = context.getColor(android.R.color.system_accent1_100);
+            lightTheme.colorPrimaryDark = lightTheme.colorPrimary;
+            lightTheme.colorAccent = context.getColor(android.R.color.system_accent3_300);
+            lightTheme.colorPrimaryLightTheme = lightTheme.colorPrimary;
+            lightTheme.backgroundColor = context.getColor(android.R.color.system_neutral1_100);
+            lightTheme.cardViewBackgroundColor = context.getColor(android.R.color.system_neutral2_50);
+            lightTheme.commentBackgroundColor = context.getColor(android.R.color.system_neutral2_50);
+            lightTheme.awardedCommentBackgroundColor = context.getColor(android.R.color.system_neutral2_50);
+            lightTheme.bottomAppBarBackgroundColor = lightTheme.colorPrimary;
+            lightTheme.navBarColor = lightTheme.colorPrimary;
+            lightTheme.primaryTextColor = context.getColor(android.R.color.system_neutral1_900);
+            lightTheme.secondaryTextColor = context.getColor(android.R.color.system_neutral1_700);
+            lightTheme.buttonTextColor = context.getColor(android.R.color.system_accent1_800);
+            lightTheme.bottomAppBarIconColor = lightTheme.buttonTextColor;
+            lightTheme.primaryIconColor = context.getColor(android.R.color.system_accent1_400);
+            lightTheme.fabIconColor = lightTheme.buttonTextColor;
+            lightTheme.toolbarPrimaryTextAndIconColor = lightTheme.buttonTextColor;
+            lightTheme.toolbarSecondaryTextColor = lightTheme.buttonTextColor;
+            lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabIndicator = lightTheme.buttonTextColor;
+            lightTheme.tabLayoutWithCollapsedCollapsingToolbarTextColor = lightTheme.buttonTextColor;
+            lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabBackground = lightTheme.colorPrimary;
+            lightTheme.tabLayoutWithExpandedCollapsingToolbarTabBackground = lightTheme.backgroundColor;
+            lightTheme.tabLayoutWithExpandedCollapsingToolbarTabIndicator = lightTheme.buttonTextColor;
+            lightTheme.tabLayoutWithExpandedCollapsingToolbarTextColor = lightTheme.buttonTextColor;
+            lightTheme.circularProgressBarBackground = context.getColor(android.R.color.system_accent1_10);
+            lightTheme.dividerColor = context.getColor(android.R.color.system_neutral1_400);
+            lightTheme.isLightStatusBar = true;
+            lightTheme.isChangeStatusBarIconColorAfterToolbarCollapsedInImmersiveInterface = true;
+            lightTheme.name = "Material You";
+
+            darkTheme.colorPrimary = context.getColor(android.R.color.system_accent2_800);
+            darkTheme.colorPrimaryDark = darkTheme.colorPrimary;
+            darkTheme.colorAccent = context.getColor(android.R.color.system_accent3_100);
+            darkTheme.colorPrimaryLightTheme = context.getColor(android.R.color.system_accent1_300);
+            darkTheme.backgroundColor = context.getColor(android.R.color.system_neutral1_900);
+            darkTheme.cardViewBackgroundColor = context.getColor(android.R.color.system_neutral2_800);
+            darkTheme.commentBackgroundColor = context.getColor(android.R.color.system_neutral2_800);
+            darkTheme.awardedCommentBackgroundColor = context.getColor(android.R.color.system_neutral2_800);
+            darkTheme.bottomAppBarBackgroundColor = context.getColor(android.R.color.system_accent2_800);
+            darkTheme.navBarColor = darkTheme.colorPrimary;
+            darkTheme.primaryTextColor = context.getColor(android.R.color.system_neutral1_10);
+            darkTheme.secondaryTextColor = context.getColor(android.R.color.system_neutral1_10);
+            darkTheme.bottomAppBarIconColor = context.getColor(android.R.color.system_accent1_100);;
+            darkTheme.primaryIconColor = context.getColor(android.R.color.system_accent1_100);
+            darkTheme.fabIconColor = context.getColor(android.R.color.system_neutral1_900);
+            darkTheme.toolbarPrimaryTextAndIconColor = context.getColor(android.R.color.system_accent2_100);
+            darkTheme.toolbarSecondaryTextColor = darkTheme.toolbarPrimaryTextAndIconColor;
+            darkTheme.tabLayoutWithCollapsedCollapsingToolbarTabIndicator = darkTheme.toolbarPrimaryTextAndIconColor;
+            darkTheme.tabLayoutWithCollapsedCollapsingToolbarTextColor = darkTheme.toolbarPrimaryTextAndIconColor;
+            darkTheme.tabLayoutWithCollapsedCollapsingToolbarTabBackground = darkTheme.colorPrimary;
+            darkTheme.tabLayoutWithExpandedCollapsingToolbarTabBackground = darkTheme.backgroundColor;
+            darkTheme.tabLayoutWithExpandedCollapsingToolbarTabIndicator = darkTheme.bottomAppBarIconColor;
+            darkTheme.tabLayoutWithExpandedCollapsingToolbarTextColor = darkTheme.bottomAppBarIconColor;
+            darkTheme.circularProgressBarBackground = context.getColor(android.R.color.system_accent1_900);
+            darkTheme.dividerColor = context.getColor(android.R.color.system_neutral1_600);
+            darkTheme.isChangeStatusBarIconColorAfterToolbarCollapsedInImmersiveInterface = true;
+            darkTheme.name = "Material You Dark";
+
+            amoledTheme.colorAccent = context.getColor(android.R.color.system_accent1_100);
+            amoledTheme.colorPrimaryLightTheme = context.getColor(android.R.color.system_accent1_300);
+            amoledTheme.fabIconColor = context.getColor(android.R.color.system_neutral1_900);
+            amoledTheme.name = "Material You Amoled";
+
+            redditDataRoomDatabase.customThemeDao().unsetLightTheme();
+            redditDataRoomDatabase.customThemeDao().unsetDarkTheme();
+            redditDataRoomDatabase.customThemeDao().unsetAmoledTheme();
+
+            redditDataRoomDatabase.customThemeDao().insert(lightTheme);
+            redditDataRoomDatabase.customThemeDao().insert(darkTheme);
+            redditDataRoomDatabase.customThemeDao().insert(amoledTheme);
+
+            CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(lightTheme, lightThemeSharedPreferences);
+            CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(darkTheme, darkThemeSharedPreferences);
+            CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(amoledTheme, amoledThemeSharedPreferences);
+
+            return true;
+        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
+            WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
+            WallpaperColors wallpaperColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
+
+            if (wallpaperColors != null) {
+                int colorPrimaryInt = lightenColor(wallpaperColors.getPrimaryColor().toArgb(), 0.4);
+                int colorPrimaryDarkInt = darkenColor(colorPrimaryInt, 0.3);
+                int backgroundColor = lightenColor(colorPrimaryInt, 0.2);
+                int cardViewBackgroundColor = lightenColor(colorPrimaryInt, 0.6);
+                Color colorAccent = wallpaperColors.getSecondaryColor();
+                int colorAccentInt = lightenColor(colorAccent == null ? customThemeWrapper.getColorAccent() : colorAccent.toArgb(), 0.4);
+
+                int colorPrimaryAppropriateTextColor = getAppropriateTextColor(colorPrimaryInt);
+                int backgroundColorAppropriateTextColor = getAppropriateTextColor(backgroundColor);
+
                 CustomTheme lightTheme = CustomThemeWrapper.getIndigo(context);
                 CustomTheme darkTheme = CustomThemeWrapper.getIndigoDark(context);
                 CustomTheme amoledTheme = CustomThemeWrapper.getIndigoAmoled(context);
 
-                lightTheme.colorPrimary = context.getColor(android.R.color.system_accent1_100);
-                lightTheme.colorPrimaryDark = lightTheme.colorPrimary;
-                lightTheme.colorAccent = context.getColor(android.R.color.system_accent3_300);
-                lightTheme.colorPrimaryLightTheme = lightTheme.colorPrimary;
-                lightTheme.backgroundColor = context.getColor(android.R.color.system_neutral1_100);
-                lightTheme.cardViewBackgroundColor = context.getColor(android.R.color.system_neutral2_50);
-                lightTheme.commentBackgroundColor = context.getColor(android.R.color.system_neutral2_50);
-                lightTheme.awardedCommentBackgroundColor = context.getColor(android.R.color.system_neutral2_50);
-                lightTheme.bottomAppBarBackgroundColor = lightTheme.colorPrimary;
-                lightTheme.navBarColor = lightTheme.colorPrimary;
-                lightTheme.primaryTextColor = context.getColor(android.R.color.system_neutral1_900);
-                lightTheme.secondaryTextColor = context.getColor(android.R.color.system_neutral1_700);
-                lightTheme.buttonTextColor = context.getColor(android.R.color.system_accent1_800);
-                lightTheme.bottomAppBarIconColor = lightTheme.buttonTextColor;
-                lightTheme.primaryIconColor = context.getColor(android.R.color.system_accent1_400);
-                lightTheme.fabIconColor = lightTheme.buttonTextColor;
-                lightTheme.toolbarPrimaryTextAndIconColor = lightTheme.buttonTextColor;
-                lightTheme.toolbarSecondaryTextColor = lightTheme.buttonTextColor;
-                lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabIndicator = lightTheme.buttonTextColor;
-                lightTheme.tabLayoutWithCollapsedCollapsingToolbarTextColor = lightTheme.buttonTextColor;
-                lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabBackground = lightTheme.colorPrimary;
-                lightTheme.tabLayoutWithExpandedCollapsingToolbarTabBackground = lightTheme.backgroundColor;
-                lightTheme.tabLayoutWithExpandedCollapsingToolbarTabIndicator = lightTheme.buttonTextColor;
-                lightTheme.tabLayoutWithExpandedCollapsingToolbarTextColor = lightTheme.buttonTextColor;
-                lightTheme.circularProgressBarBackground = context.getColor(android.R.color.system_accent1_10);
-                lightTheme.dividerColor = context.getColor(android.R.color.system_neutral1_400);
-                lightTheme.isLightStatusBar = true;
-                lightTheme.isChangeStatusBarIconColorAfterToolbarCollapsedInImmersiveInterface = true;
+                lightTheme.colorPrimary = colorPrimaryInt;
+                lightTheme.colorPrimaryDark = colorPrimaryDarkInt;
+                lightTheme.colorAccent = colorAccentInt;
+                lightTheme.colorPrimaryLightTheme = colorPrimaryInt;
+                lightTheme.backgroundColor = backgroundColor;
+                lightTheme.cardViewBackgroundColor = cardViewBackgroundColor;
+                lightTheme.commentBackgroundColor = cardViewBackgroundColor;
+                lightTheme.awardedCommentBackgroundColor = cardViewBackgroundColor;
+                lightTheme.bottomAppBarBackgroundColor = colorPrimaryInt;
+                lightTheme.navBarColor = colorPrimaryInt;
+                lightTheme.primaryTextColor = backgroundColorAppropriateTextColor;
+                lightTheme.secondaryTextColor = backgroundColorAppropriateTextColor == Color.BLACK ? Color.parseColor("#8A000000") : Color.parseColor("#B3FFFFFF");
+                lightTheme.bottomAppBarIconColor = colorPrimaryAppropriateTextColor;
+                lightTheme.primaryIconColor = backgroundColorAppropriateTextColor;
+                lightTheme.fabIconColor = colorPrimaryAppropriateTextColor;
+                lightTheme.toolbarPrimaryTextAndIconColor = colorPrimaryAppropriateTextColor;
+                lightTheme.toolbarSecondaryTextColor = colorPrimaryAppropriateTextColor;
+                lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabIndicator = colorPrimaryAppropriateTextColor;
+                lightTheme.tabLayoutWithCollapsedCollapsingToolbarTextColor = colorPrimaryAppropriateTextColor;
+                lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabBackground = colorPrimaryInt;
+                lightTheme.tabLayoutWithExpandedCollapsingToolbarTabBackground = backgroundColor;
+                lightTheme.tabLayoutWithExpandedCollapsingToolbarTabIndicator = colorPrimaryAppropriateTextColor;
+                lightTheme.tabLayoutWithExpandedCollapsingToolbarTextColor = colorPrimaryAppropriateTextColor;
+                lightTheme.circularProgressBarBackground = colorPrimaryInt;
+                lightTheme.dividerColor = backgroundColorAppropriateTextColor == Color.BLACK ? Color.parseColor("#E0E0E0") : Color.parseColor("69666C");
+                lightTheme.isLightStatusBar = colorPrimaryAppropriateTextColor == Color.BLACK;
+                lightTheme.isChangeStatusBarIconColorAfterToolbarCollapsedInImmersiveInterface =
+                        (lightTheme.isLightStatusBar && getAppropriateTextColor(cardViewBackgroundColor) == Color.WHITE)
+                                || (!lightTheme.isLightStatusBar && getAppropriateTextColor(cardViewBackgroundColor) == Color.BLACK);
                 lightTheme.name = "Material You";
 
-                darkTheme.colorPrimary = context.getColor(android.R.color.system_accent2_800);
-                darkTheme.colorPrimaryDark = darkTheme.colorPrimary;
-                darkTheme.colorAccent = context.getColor(android.R.color.system_accent3_100);
-                darkTheme.colorPrimaryLightTheme = context.getColor(android.R.color.system_accent1_300);
-                darkTheme.backgroundColor = context.getColor(android.R.color.system_neutral1_900);
-                darkTheme.cardViewBackgroundColor = context.getColor(android.R.color.system_neutral2_800);
-                darkTheme.commentBackgroundColor = context.getColor(android.R.color.system_neutral2_800);
-                darkTheme.awardedCommentBackgroundColor = context.getColor(android.R.color.system_neutral2_800);
-                darkTheme.bottomAppBarBackgroundColor = context.getColor(android.R.color.system_accent2_800);
-                darkTheme.navBarColor = darkTheme.colorPrimary;
-                darkTheme.primaryTextColor = context.getColor(android.R.color.system_neutral1_10);
-                darkTheme.secondaryTextColor = context.getColor(android.R.color.system_neutral1_10);
-                darkTheme.bottomAppBarIconColor = context.getColor(android.R.color.system_accent1_100);;
-                darkTheme.primaryIconColor = context.getColor(android.R.color.system_accent1_100);
-                darkTheme.fabIconColor = context.getColor(android.R.color.system_neutral1_900);
-                darkTheme.toolbarPrimaryTextAndIconColor = context.getColor(android.R.color.system_accent2_100);
-                darkTheme.toolbarSecondaryTextColor = darkTheme.toolbarPrimaryTextAndIconColor;
-                darkTheme.tabLayoutWithCollapsedCollapsingToolbarTabIndicator = darkTheme.toolbarPrimaryTextAndIconColor;
-                darkTheme.tabLayoutWithCollapsedCollapsingToolbarTextColor = darkTheme.toolbarPrimaryTextAndIconColor;
-                darkTheme.tabLayoutWithCollapsedCollapsingToolbarTabBackground = darkTheme.colorPrimary;
-                darkTheme.tabLayoutWithExpandedCollapsingToolbarTabBackground = darkTheme.backgroundColor;
-                darkTheme.tabLayoutWithExpandedCollapsingToolbarTabIndicator = darkTheme.bottomAppBarIconColor;
-                darkTheme.tabLayoutWithExpandedCollapsingToolbarTextColor = darkTheme.bottomAppBarIconColor;
-                darkTheme.circularProgressBarBackground = context.getColor(android.R.color.system_accent1_900);
-                darkTheme.dividerColor = context.getColor(android.R.color.system_neutral1_600);
-                darkTheme.isChangeStatusBarIconColorAfterToolbarCollapsedInImmersiveInterface = true;
+                darkTheme.colorAccent = colorPrimaryInt;
+                darkTheme.colorPrimaryLightTheme = colorPrimaryInt;
                 darkTheme.name = "Material You Dark";
 
-                amoledTheme.colorAccent = context.getColor(android.R.color.system_accent1_100);
-                amoledTheme.colorPrimaryLightTheme = context.getColor(android.R.color.system_accent1_300);
-                amoledTheme.fabIconColor = context.getColor(android.R.color.system_neutral1_900);
+                amoledTheme.colorAccent = colorPrimaryInt;
+                amoledTheme.colorPrimaryLightTheme = colorPrimaryInt;
                 amoledTheme.name = "Material You Amoled";
 
                 redditDataRoomDatabase.customThemeDao().unsetLightTheme();
@@ -131,91 +233,11 @@ public class MaterialYouUtils {
                 CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(darkTheme, darkThemeSharedPreferences);
                 CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(amoledTheme, amoledThemeSharedPreferences);
 
-                handler.post(() -> {
-                    if (materialYouListener != null) {
-                        materialYouListener.applied();
-                    }
-                    EventBus.getDefault().post(new RecreateActivityEvent());
-                });
-            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
-                WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
-                WallpaperColors wallpaperColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
-
-                if (wallpaperColors != null) {
-                    int colorPrimaryInt = lightenColor(wallpaperColors.getPrimaryColor().toArgb(), 0.4);
-                    int colorPrimaryDarkInt = darkenColor(colorPrimaryInt, 0.3);
-                    int backgroundColor = lightenColor(colorPrimaryInt, 0.2);
-                    int cardViewBackgroundColor = lightenColor(colorPrimaryInt, 0.6);
-                    Color colorAccent = wallpaperColors.getSecondaryColor();
-                    int colorAccentInt = lightenColor(colorAccent == null ? customThemeWrapper.getColorAccent() : colorAccent.toArgb(), 0.4);
-
-                    int colorPrimaryAppropriateTextColor = getAppropriateTextColor(colorPrimaryInt);
-                    int backgroundColorAppropriateTextColor = getAppropriateTextColor(backgroundColor);
-
-                    CustomTheme lightTheme = CustomThemeWrapper.getIndigo(context);
-                    CustomTheme darkTheme = CustomThemeWrapper.getIndigoDark(context);
-                    CustomTheme amoledTheme = CustomThemeWrapper.getIndigoAmoled(context);
-
-                    lightTheme.colorPrimary = colorPrimaryInt;
-                    lightTheme.colorPrimaryDark = colorPrimaryDarkInt;
-                    lightTheme.colorAccent = colorAccentInt;
-                    lightTheme.colorPrimaryLightTheme = colorPrimaryInt;
-                    lightTheme.backgroundColor = backgroundColor;
-                    lightTheme.cardViewBackgroundColor = cardViewBackgroundColor;
-                    lightTheme.commentBackgroundColor = cardViewBackgroundColor;
-                    lightTheme.awardedCommentBackgroundColor = cardViewBackgroundColor;
-                    lightTheme.bottomAppBarBackgroundColor = colorPrimaryInt;
-                    lightTheme.navBarColor = colorPrimaryInt;
-                    lightTheme.primaryTextColor = backgroundColorAppropriateTextColor;
-                    lightTheme.secondaryTextColor = backgroundColorAppropriateTextColor == Color.BLACK ? Color.parseColor("#8A000000") : Color.parseColor("#B3FFFFFF");
-                    lightTheme.bottomAppBarIconColor = colorPrimaryAppropriateTextColor;
-                    lightTheme.primaryIconColor = backgroundColorAppropriateTextColor;
-                    lightTheme.fabIconColor = colorPrimaryAppropriateTextColor;
-                    lightTheme.toolbarPrimaryTextAndIconColor = colorPrimaryAppropriateTextColor;
-                    lightTheme.toolbarSecondaryTextColor = colorPrimaryAppropriateTextColor;
-                    lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabIndicator = colorPrimaryAppropriateTextColor;
-                    lightTheme.tabLayoutWithCollapsedCollapsingToolbarTextColor = colorPrimaryAppropriateTextColor;
-                    lightTheme.tabLayoutWithCollapsedCollapsingToolbarTabBackground = colorPrimaryInt;
-                    lightTheme.tabLayoutWithExpandedCollapsingToolbarTabBackground = backgroundColor;
-                    lightTheme.tabLayoutWithExpandedCollapsingToolbarTabIndicator = colorPrimaryAppropriateTextColor;
-                    lightTheme.tabLayoutWithExpandedCollapsingToolbarTextColor = colorPrimaryAppropriateTextColor;
-                    lightTheme.circularProgressBarBackground = colorPrimaryInt;
-                    lightTheme.dividerColor = backgroundColorAppropriateTextColor == Color.BLACK ? Color.parseColor("#E0E0E0") : Color.parseColor("69666C");
-                    lightTheme.isLightStatusBar = colorPrimaryAppropriateTextColor == Color.BLACK;
-                    lightTheme.isChangeStatusBarIconColorAfterToolbarCollapsedInImmersiveInterface =
-                            (lightTheme.isLightStatusBar && getAppropriateTextColor(cardViewBackgroundColor) == Color.WHITE)
-                                    || (!lightTheme.isLightStatusBar && getAppropriateTextColor(cardViewBackgroundColor) == Color.BLACK);
-                    lightTheme.name = "Material You";
-
-                    darkTheme.colorAccent = colorPrimaryInt;
-                    darkTheme.colorPrimaryLightTheme = colorPrimaryInt;
-                    darkTheme.name = "Material You Dark";
-
-                    amoledTheme.colorAccent = colorPrimaryInt;
-                    amoledTheme.colorPrimaryLightTheme = colorPrimaryInt;
-                    amoledTheme.name = "Material You Amoled";
-
-                    redditDataRoomDatabase.customThemeDao().unsetLightTheme();
-                    redditDataRoomDatabase.customThemeDao().unsetDarkTheme();
-                    redditDataRoomDatabase.customThemeDao().unsetAmoledTheme();
-
-                    redditDataRoomDatabase.customThemeDao().insert(lightTheme);
-                    redditDataRoomDatabase.customThemeDao().insert(darkTheme);
-                    redditDataRoomDatabase.customThemeDao().insert(amoledTheme);
-
-                    CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(lightTheme, lightThemeSharedPreferences);
-                    CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(darkTheme, darkThemeSharedPreferences);
-                    CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(amoledTheme, amoledThemeSharedPreferences);
-
-                    handler.post(() -> {
-                        if (materialYouListener != null) {
-                            materialYouListener.applied();
-                        }
-                        EventBus.getDefault().post(new RecreateActivityEvent());
-                    });
-                }
+                return true;
             }
-        });
+        }
+
+        return false;
     }
 
     private static int lightenColor(int color, double ratio) {
-- 
cgit v1.2.3