From 9c4889b1b02a6bfa6486ce1c3bfc0c6b590f1f4e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 20 Mar 2020 19:50:45 +0800 Subject: [PATCH] Support orientation change in CustomizeThemeActivity. --- .../Activity/CustomizeThemeActivity.java | 167 +++++++++++------- .../Activity/SettingsActivity.java | 17 ++ .../AsyncTask/GetCustomThemeAsyncTask.java | 25 ++- .../AsyncTask/InsertCustomThemeAsyncTask.java | 12 +- .../CustomTheme/CustomThemeDao.java | 6 +- .../CustomTheme/CustomThemeRepository.java | 18 -- .../CustomTheme/CustomThemeSettingsItem.java | 36 +++- .../CustomTheme/CustomThemeViewModel.java | 18 -- .../CustomTheme/CustomThemeWrapper.java | 2 +- .../CustomThemeSharedPreferencesUtils.java | 76 +++++++- 10 files changed, 266 insertions(+), 111 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomizeThemeActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomizeThemeActivity.java index b496502f..468c55d9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomizeThemeActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CustomizeThemeActivity.java @@ -9,13 +9,13 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.appbar.AppBarLayout; +import org.greenrobot.eventbus.EventBus; + import java.util.ArrayList; import javax.inject.Inject; @@ -30,6 +30,7 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomTheme; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeSettingsItem; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeViewModel; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; @@ -38,10 +39,11 @@ import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUt public class CustomizeThemeActivity extends BaseActivity { public static final String EXTRA_THEME_TYPE = "ETT"; - public static final int EXTRA_LIGHT_THEME = 0; - public static final int EXTRA_DARK_THEME = 1; - public static final int EXTRA_AMOLED_THEME = 2; + public static final int EXTRA_LIGHT_THEME = CustomThemeSharedPreferencesUtils.LIGHT; + public static final int EXTRA_DARK_THEME = CustomThemeSharedPreferencesUtils.DARK; + public static final int EXTRA_AMOLED_THEME = CustomThemeSharedPreferencesUtils.AMOLED; public static final String EXTRA_THEME_NAME = "ETN"; + private static final String CUSTOM_THEME_SETTINGS_ITEMS_STATE = "CTSIS"; @BindView(R.id.appbar_layout_customize_theme_activity) AppBarLayout appBarLayout; @@ -53,6 +55,15 @@ public class CustomizeThemeActivity extends BaseActivity { @Named("default") SharedPreferences sharedPreferences; @Inject + @Named("light_theme") + SharedPreferences lightThemeSharedPreferences; + @Inject + @Named("dark_theme") + SharedPreferences darkThemeSharedPreferences; + @Inject + @Named("amoled_theme") + SharedPreferences amoledThemeSharedPreferences; + @Inject RedditDataRoomDatabase redditDataRoomDatabase; @Inject CustomThemeWrapper customThemeWrapper; @@ -82,68 +93,78 @@ public class CustomizeThemeActivity extends BaseActivity { recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); - customThemeSettingsItems = new ArrayList<>(); + if (savedInstanceState != null) { + customThemeSettingsItems = savedInstanceState.getParcelableArrayList(CUSTOM_THEME_SETTINGS_ITEMS_STATE); + } int androidVersion = Build.VERSION.SDK_INT; - if (getIntent().hasExtra(EXTRA_THEME_TYPE)) { - customThemeViewModel = new ViewModelProvider(this, new CustomThemeViewModel.Factory(redditDataRoomDatabase)) - .get(CustomThemeViewModel.class); - LiveData customThemeLiveData; - themeType = getIntent().getIntExtra(EXTRA_THEME_TYPE, EXTRA_LIGHT_THEME); - switch (themeType) { - case EXTRA_DARK_THEME: - setTitle(getString(R.string.customize_dark_theme_fragment_title)); - customThemeLiveData = customThemeViewModel.getDarkCustomTheme(); - break; - case EXTRA_AMOLED_THEME: - setTitle(getString(R.string.customize_amoled_theme_fragment_title)); - customThemeLiveData = customThemeViewModel.getAmoledCustomTheme(); - break; - default: - setTitle(getString(R.string.customize_light_theme_fragment_title)); - customThemeLiveData = customThemeViewModel.getLightCustomTheme(); - break; - } - customThemeLiveData.observe(this, customTheme -> { - if (customTheme == null) { - switch (themeType) { - case EXTRA_DARK_THEME: - customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem( - CustomizeThemeActivity.this, - CustomThemeWrapper.getIndigoDark(CustomizeThemeActivity.this)); - break; - case EXTRA_AMOLED_THEME: - customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem( - CustomizeThemeActivity.this, - CustomThemeWrapper.getIndigoAmoled(CustomizeThemeActivity.this)); - break; - default: - customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem( - CustomizeThemeActivity.this, - CustomThemeWrapper.getIndigo(CustomizeThemeActivity.this)); + if (customThemeSettingsItems == null) { + if (getIntent().hasExtra(EXTRA_THEME_TYPE)) { + themeType = getIntent().getIntExtra(EXTRA_THEME_TYPE, EXTRA_LIGHT_THEME); + + new GetCustomThemeAsyncTask(redditDataRoomDatabase, themeType, customTheme -> { + if (customTheme == null) { + switch (themeType) { + case EXTRA_DARK_THEME: + customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem( + CustomizeThemeActivity.this, + CustomThemeWrapper.getIndigoDark(CustomizeThemeActivity.this)); + break; + case EXTRA_AMOLED_THEME: + customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem( + CustomizeThemeActivity.this, + CustomThemeWrapper.getIndigoAmoled(CustomizeThemeActivity.this)); + break; + default: + customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem( + CustomizeThemeActivity.this, + CustomThemeWrapper.getIndigo(CustomizeThemeActivity.this)); + } + } else { + customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem(CustomizeThemeActivity.this, customTheme); } - } else { - customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem(CustomizeThemeActivity.this, customTheme); - } - if (androidVersion < Build.VERSION_CODES.O) { - customThemeSettingsItems.get(customThemeSettingsItems.size() - 2).itemDetails = getString(R.string.theme_item_available_on_android_8); - } - if (androidVersion < Build.VERSION_CODES.M) { - customThemeSettingsItems.get(customThemeSettingsItems.size() - 3).itemDetails = getString(R.string.theme_item_available_on_android_6); - } + if (androidVersion < Build.VERSION_CODES.O) { + customThemeSettingsItems.get(customThemeSettingsItems.size() - 2).itemDetails = getString(R.string.theme_item_available_on_android_8); + } + if (androidVersion < Build.VERSION_CODES.M) { + customThemeSettingsItems.get(customThemeSettingsItems.size() - 3).itemDetails = getString(R.string.theme_item_available_on_android_6); + } - adapter.setCustomThemeSettingsItem(customThemeSettingsItems); - }); + adapter.setCustomThemeSettingsItem(customThemeSettingsItems); + }).execute(); + + switch (themeType) { + case EXTRA_DARK_THEME: + setTitle(getString(R.string.customize_dark_theme_fragment_title)); + break; + case EXTRA_AMOLED_THEME: + setTitle(getString(R.string.customize_amoled_theme_fragment_title)); + break; + default: + setTitle(getString(R.string.customize_light_theme_fragment_title)); + break; + } + } else { + themeName = getIntent().getStringExtra(EXTRA_THEME_NAME); + setTitle(themeName); + new GetCustomThemeAsyncTask(redditDataRoomDatabase, themeName, + customTheme -> { + customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem(CustomizeThemeActivity.this, customTheme); + + if (androidVersion < Build.VERSION_CODES.O) { + customThemeSettingsItems.get(customThemeSettingsItems.size() - 2).itemDetails = getString(R.string.theme_item_available_on_android_8); + } + if (androidVersion < Build.VERSION_CODES.M) { + customThemeSettingsItems.get(customThemeSettingsItems.size() - 3).itemDetails = getString(R.string.theme_item_available_on_android_6); + } + + adapter.setCustomThemeSettingsItem(customThemeSettingsItems); + }).execute(); + } } else { - themeName = getIntent().getStringExtra(EXTRA_THEME_NAME); - setTitle(themeName); - new GetCustomThemeAsyncTask(redditDataRoomDatabase, themeName, - customTheme -> { - customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem(CustomizeThemeActivity.this, customTheme); - adapter.setCustomThemeSettingsItem(customThemeSettingsItems); - }).execute(); + adapter.setCustomThemeSettingsItem(customThemeSettingsItems); } } @@ -181,29 +202,45 @@ public class CustomizeThemeActivity extends BaseActivity { customTheme.isLightTheme = false; customTheme.isDarkTheme = true; customTheme.isAmoledTheme = false; + new InsertCustomThemeAsyncTask(redditDataRoomDatabase, darkThemeSharedPreferences, customTheme, () -> { + Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new RecreateActivityEvent()); + finish(); + }).execute(); break; case CustomThemeSharedPreferencesUtils.AMOLED: customTheme.isLightTheme = false; customTheme.isDarkTheme = false; customTheme.isAmoledTheme = true; + new InsertCustomThemeAsyncTask(redditDataRoomDatabase, amoledThemeSharedPreferences, customTheme, () -> { + Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new RecreateActivityEvent()); + finish(); + }).execute(); break; default: customTheme.isLightTheme = true; customTheme.isDarkTheme = false; customTheme.isAmoledTheme = false; + new InsertCustomThemeAsyncTask(redditDataRoomDatabase, lightThemeSharedPreferences, customTheme, () -> { + Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new RecreateActivityEvent()); + finish(); + }).execute(); } - new InsertCustomThemeAsyncTask(redditDataRoomDatabase, customTheme, () -> { - Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); - finish(); - }).execute(); - return true; } return false; } + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putParcelableArrayList(CUSTOM_THEME_SETTINGS_ITEMS_STATE, customThemeSettingsItems); + } + @Override protected SharedPreferences getSharedPreferences() { return sharedPreferences; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java index c8ad39c5..ef91f34a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java @@ -13,12 +13,16 @@ import androidx.preference.PreferenceFragmentCompat; import com.google.android.material.appbar.AppBarLayout; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import javax.inject.Inject; import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.Settings.AboutPreferenceFragment; @@ -53,6 +57,8 @@ public class SettingsActivity extends BaseActivity implements ButterKnife.bind(this); + EventBus.getDefault().register(this); + applyCustomTheme(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) { @@ -136,4 +142,15 @@ public class SettingsActivity extends BaseActivity implements setTitle(pref.getTitle()); return true; } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onRecreateActivityEvent(RecreateActivityEvent recreateActivityEvent) { + recreate(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/GetCustomThemeAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/GetCustomThemeAsyncTask.java index 9a8469f5..ffc8771e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/GetCustomThemeAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/GetCustomThemeAsyncTask.java @@ -4,10 +4,12 @@ import android.os.AsyncTask; import ml.docilealligator.infinityforreddit.CustomTheme.CustomTheme; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils; public class GetCustomThemeAsyncTask extends AsyncTask { private RedditDataRoomDatabase redditDataRoomDatabase; private String customThemeName; + private int themeType; private GetCustomThemeAsyncTaskListener getCustomThemeAsyncTaskListener; private CustomTheme customTheme; @@ -23,9 +25,30 @@ public class GetCustomThemeAsyncTask extends AsyncTask { this.getCustomThemeAsyncTaskListener = getCustomThemeAsyncTaskListener; } + public GetCustomThemeAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, + int themeType, + GetCustomThemeAsyncTaskListener getCustomThemeAsyncTaskListener) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.themeType = themeType; + this.getCustomThemeAsyncTaskListener = getCustomThemeAsyncTaskListener; + } + @Override protected Void doInBackground(Void... voids) { - customTheme = redditDataRoomDatabase.customThemeDao().getCustomTheme(customThemeName); + if (customThemeName != null) { + customTheme = redditDataRoomDatabase.customThemeDao().getCustomTheme(customThemeName); + } else { + switch (themeType) { + case CustomThemeSharedPreferencesUtils.DARK: + customTheme = redditDataRoomDatabase.customThemeDao().getDarkCustomTheme(); + break; + case CustomThemeSharedPreferencesUtils.AMOLED: + customTheme = redditDataRoomDatabase.customThemeDao().getAmoledCustomTheme(); + break; + default: + customTheme = redditDataRoomDatabase.customThemeDao().getLightCustomTheme(); + } + } return null; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertCustomThemeAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertCustomThemeAsyncTask.java index 2bdb149d..c8c29a03 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertCustomThemeAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertCustomThemeAsyncTask.java @@ -1,22 +1,27 @@ package ml.docilealligator.infinityforreddit.AsyncTask; +import android.content.SharedPreferences; import android.os.AsyncTask; import ml.docilealligator.infinityforreddit.CustomTheme.CustomTheme; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils; public class InsertCustomThemeAsyncTask extends AsyncTask { private RedditDataRoomDatabase redditDataRoomDatabase; - private InsertCustomThemeAsyncTaskListener insertCustomThemeAsyncTaskListener; + private SharedPreferences themeSharedPreferences; private CustomTheme customTheme; + private InsertCustomThemeAsyncTaskListener insertCustomThemeAsyncTaskListener; public interface InsertCustomThemeAsyncTaskListener { void success(); } - public InsertCustomThemeAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, CustomTheme customTheme, - InsertCustomThemeAsyncTaskListener insertCustomThemeAsyncTaskListener) { + public InsertCustomThemeAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, + SharedPreferences themeSharedPreferences, CustomTheme customTheme, + InsertCustomThemeAsyncTaskListener insertCustomThemeAsyncTaskListener) { this.redditDataRoomDatabase = redditDataRoomDatabase; + this.themeSharedPreferences = themeSharedPreferences; this.customTheme = customTheme; this.insertCustomThemeAsyncTaskListener = insertCustomThemeAsyncTaskListener; } @@ -24,6 +29,7 @@ public class InsertCustomThemeAsyncTask extends AsyncTask { @Override protected Void doInBackground(Void... voids) { redditDataRoomDatabase.customThemeDao().insert(customTheme); + CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, themeSharedPreferences); return null; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java index 552cc8a7..42102bcb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java @@ -17,13 +17,13 @@ public interface CustomThemeDao { LiveData> getAllCustomThemes(); @Query("SELECT * FROM custom_themes WHERE is_light_theme = 1 LIMIT 1") - LiveData getLightCustomTheme(); + CustomTheme getLightCustomTheme(); @Query("SELECT * FROM custom_themes WHERE is_dark_theme = 1 LIMIT 1") - LiveData getDarkCustomTheme(); + CustomTheme getDarkCustomTheme(); @Query("SELECT * FROM custom_themes WHERE is_amoled_theme = 1 LIMIT 1") - LiveData getAmoledCustomTheme(); + CustomTheme getAmoledCustomTheme(); @Query("SELECT * FROM custom_themes WHERE name = :name COLLATE NOCASE LIMIT 1") CustomTheme getCustomTheme(String name); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeRepository.java index cedc4513..e2c0b692 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeRepository.java @@ -8,30 +8,12 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class CustomThemeRepository { private LiveData> mAllCustomThemes; - private LiveData mLightCustomTheme; - private LiveData mDarkCustomTheme; - private LiveData mAmoledCustomTheme; CustomThemeRepository(RedditDataRoomDatabase redditDataRoomDatabase) { mAllCustomThemes = redditDataRoomDatabase.customThemeDao().getAllCustomThemes(); - mLightCustomTheme = redditDataRoomDatabase.customThemeDao().getLightCustomTheme(); - mDarkCustomTheme = redditDataRoomDatabase.customThemeDao().getDarkCustomTheme(); - mAmoledCustomTheme = redditDataRoomDatabase.customThemeDao().getAmoledCustomTheme(); } LiveData> getAllCustomThemes() { return mAllCustomThemes; } - - public LiveData getLightCustomTheme() { - return mLightCustomTheme; - } - - public LiveData getDarkCustomTheme() { - return mDarkCustomTheme; - } - - public LiveData getAmoledCustomTheme() { - return mAmoledCustomTheme; - } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeSettingsItem.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeSettingsItem.java index 5741d1f1..97f5d10e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeSettingsItem.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeSettingsItem.java @@ -1,12 +1,14 @@ package ml.docilealligator.infinityforreddit.CustomTheme; import android.content.Context; +import android.os.Parcel; +import android.os.Parcelable; import java.util.ArrayList; import ml.docilealligator.infinityforreddit.R; -public class CustomThemeSettingsItem { +public class CustomThemeSettingsItem implements Parcelable { public String itemName; public String itemDetails; public int colorValue; @@ -23,6 +25,25 @@ public class CustomThemeSettingsItem { this.isEnabled = isEnabled; } + protected CustomThemeSettingsItem(Parcel in) { + itemName = in.readString(); + itemDetails = in.readString(); + colorValue = in.readInt(); + isEnabled = in.readByte() != 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public CustomThemeSettingsItem createFromParcel(Parcel in) { + return new CustomThemeSettingsItem(in); + } + + @Override + public CustomThemeSettingsItem[] newArray(int size) { + return new CustomThemeSettingsItem[size]; + } + }; + public static ArrayList convertCustomThemeToSettingsItem(Context context, CustomTheme customTheme) { ArrayList customThemeSettingsItems = new ArrayList<>(); @@ -289,4 +310,17 @@ public class CustomThemeSettingsItem { customTheme.isChangeStatusBarIconColorAfterToolbarCollapsedInImmersiveInterface)); return customThemeSettingsItems; } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(itemName); + parcel.writeString(itemDetails); + parcel.writeInt(colorValue); + parcel.writeByte((byte) (isEnabled ? 1 : 0)); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeViewModel.java index 227e641e..c9b1fde2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeViewModel.java @@ -11,34 +11,16 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class CustomThemeViewModel extends ViewModel { private LiveData> mAllCustomThemes; - private LiveData mLightCustomTheme; - private LiveData mDarkCustomTheme; - private LiveData mAmoledCustomTheme; public CustomThemeViewModel(RedditDataRoomDatabase redditDataRoomDatabase) { CustomThemeRepository customThemeRepository = new CustomThemeRepository(redditDataRoomDatabase); mAllCustomThemes = customThemeRepository.getAllCustomThemes(); - mLightCustomTheme = customThemeRepository.getLightCustomTheme(); - mDarkCustomTheme = customThemeRepository.getDarkCustomTheme(); - mAmoledCustomTheme = customThemeRepository.getAmoledCustomTheme(); } public LiveData> getAllCustomThemes() { return mAllCustomThemes; } - public LiveData getLightCustomTheme() { - return mLightCustomTheme; - } - - public LiveData getDarkCustomTheme() { - return mDarkCustomTheme; - } - - public LiveData getAmoledCustomTheme() { - return mAmoledCustomTheme; - } - public static class Factory extends ViewModelProvider.NewInstanceFactory { private RedditDataRoomDatabase mRedditDataRoomDatabase; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeWrapper.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeWrapper.java index b6416f53..8244ce4f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeWrapper.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeWrapper.java @@ -288,7 +288,7 @@ public class CustomThemeWrapper { } public int getSingleCommentThreadBackgroundColor() { - return getThemeSharedPreferences().getInt(CustomThemeSharedPreferencesUtils.SINGLE_COMMENT_THREAD_BACKGROUND, + return getThemeSharedPreferences().getInt(CustomThemeSharedPreferencesUtils.SINGLE_COMMENT_THREAD_BACKGROUND_COLOR, getDefaultColor("#B3E5F9", "#123E77", "#123E77")); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CustomThemeSharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CustomThemeSharedPreferencesUtils.java index a4631521..4a82a964 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CustomThemeSharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/CustomThemeSharedPreferencesUtils.java @@ -1,5 +1,9 @@ package ml.docilealligator.infinityforreddit.Utils; +import android.content.SharedPreferences; + +import ml.docilealligator.infinityforreddit.CustomTheme.CustomTheme; + public class CustomThemeSharedPreferencesUtils { public static final int LIGHT = 0; public static final int DARK = 1; @@ -58,7 +62,7 @@ public class CustomThemeSharedPreferencesUtils { public static final String AUTHOR_FLAIR_TEXT_COLOR = "authorFlairTextColor"; public static final String SUBMITTER = "submitter"; public static final String MODERATOR = "moderator"; - public static final String SINGLE_COMMENT_THREAD_BACKGROUND = "singleCommentThreadBackgroundColor"; + public static final String SINGLE_COMMENT_THREAD_BACKGROUND_COLOR = "singleCommentThreadBackgroundColor"; public static final String UNREAD_MESSAGE_BACKGROUND_COLOR = "unreadMessageBackgroundColor"; public static final String DIVIDER_COLOR = "dividerColor"; public static final String NO_PREVIEW_LINK_BACKGROUND_COLOR = "noPreviewLinkBackgroundColor"; @@ -72,4 +76,74 @@ public class CustomThemeSharedPreferencesUtils { public static final String COMMENT_VERTICAL_BAR_COLOR_7 = "commentVerticalBarColor7"; public static final String FAB_ICON_COLOR = "fabIconColor"; public static final String CHIP_TEXT_COLOR = "chipTextColor"; + + public static void insertThemeToSharedPreferences(CustomTheme customTheme, SharedPreferences themeSharedPreferences) { + SharedPreferences.Editor editor = themeSharedPreferences.edit(); + editor.putInt(COLOR_PRIMARY, customTheme.colorPrimary); + editor.putInt(COLOR_PRIMARY_DARK, customTheme.colorPrimaryDark); + editor.putInt(COLOR_ACCENT, customTheme.colorAccent); + editor.putInt(COLOR_PRIMARY_LIGHT_THEME, customTheme.colorPrimaryLightTheme); + editor.putInt(PRIMARY_TEXT_COLOR, customTheme.primaryTextColor); + editor.putInt(SECONDARY_TEXT_COLOR, customTheme.secondaryTextColor); + editor.putInt(POST_TITLE_COLOR, customTheme.postTitleColor); + editor.putInt(POST_CONTENT_COLOR, customTheme.postContentColor); + editor.putInt(COMMENT_COLOR, customTheme.commentColor); + editor.putInt(BUTTON_TEXT_COLOR, customTheme.buttonTextColor); + editor.putInt(BACKGROUND_COLOR, customTheme.backgroundColor); + editor.putInt(CARD_VIEW_BACKGROUND_COLOR, customTheme.cardViewBackgroundColor); + editor.putInt(COMMENT_BACKGROUND_COLOR, customTheme.commentBackgroundColor); + editor.putInt(BOTTOM_APP_BAR_BACKGROUND_COLOR, customTheme.bottomAppBarBackgroundColor); + editor.putInt(PRIMARY_ICON_COLOR, customTheme.primaryIconColor); + editor.putInt(POST_ICON_AND_INFO_COLOR, customTheme.postIconAndInfoColor); + editor.putInt(COMMENT_ICON_AND_INFO_COLOR, customTheme.commentIconAndInfoColor); + editor.putInt(TOOLBAR_PRIMARY_TEXT_AND_ICON_COLOR, customTheme.toolbarPrimaryTextAndIconColor); + editor.putInt(TOOLBAR_SECONDARY_TEXT_COLOR, customTheme.toolbarSecondaryTextColor); + editor.putInt(CIRCULAR_PROGRESS_BAR_BACKGROUND, customTheme.circularProgressBarBackground); + editor.putInt(TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, customTheme.tabLayoutWithExpandedCollapsingToolbarTabBackground); + editor.putInt(TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TEXT_COLOR, customTheme.tabLayoutWithExpandedCollapsingToolbarTextColor); + editor.putInt(TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_INDICATOR, customTheme.tabLayoutWithExpandedCollapsingToolbarTabIndicator); + editor.putInt(TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, customTheme.tabLayoutWithCollapsedCollapsingToolbarTabBackground); + editor.putInt(TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TEXT_COLOR, customTheme.tabLayoutWithCollapsedCollapsingToolbarTextColor); + editor.putInt(TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_INDICATOR, customTheme.tabLayoutWithCollapsedCollapsingToolbarTabIndicator); + editor.putInt(NAV_BAR_COLOR, customTheme.navBarColor); + editor.putInt(UPVOTED, customTheme.upvoted); + editor.putInt(DOWNVOTED, customTheme.downvoted); + editor.putInt(POST_TYPE_BACKGROUND_COLOR, customTheme.postTypeBackgroundColor); + editor.putInt(POST_TYPE_TEXT_COLOR, customTheme.postTypeTextColor); + editor.putInt(SPOILER_BACKGROUND_COLOR, customTheme.spoilerBackgroundColor); + editor.putInt(SPOILER_TEXT_COLOR, customTheme.spoilerTextColor); + editor.putInt(NSFW_BACKGROUND_COLOR, customTheme.nsfwBackgroundColor); + editor.putInt(NSFW_TEXT_COLOR, customTheme.nsfwTextColor); + editor.putInt(FLAIR_BACKGROUND_COLOR, customTheme.flairBackgroundColor); + editor.putInt(FLAIR_TEXT_COLOR, customTheme.flairTextColor); + editor.putInt(ARCHIVED_ICON_TINT, customTheme.archivedTint); + editor.putInt(LOCKED_ICON_TINT, customTheme.lockedIconTint); + editor.putInt(CROSSPOST_ICON_TINT, customTheme.crosspostIconTint); + editor.putInt(STICKIED_POST_ICON_TINT, customTheme.stickiedPostIconTint); + editor.putInt(SUBSCRIBED, customTheme.subscribed); + editor.putInt(UNSUBSCRIBED, customTheme.unsubscribed); + editor.putInt(USERNAME, customTheme.username); + editor.putInt(SUBREDDIT, customTheme.subreddit); + editor.putInt(AUTHOR_FLAIR_TEXT_COLOR, customTheme.authorFlairTextColor); + editor.putInt(SUBMITTER, customTheme.submitter); + editor.putInt(MODERATOR, customTheme.moderator); + editor.putInt(SINGLE_COMMENT_THREAD_BACKGROUND_COLOR, customTheme.singleCommentThreadBackgroundColor); + editor.putInt(UNREAD_MESSAGE_BACKGROUND_COLOR, customTheme.unreadMessageBackgroundColor); + editor.putInt(DIVIDER_COLOR, customTheme.dividerColor); + editor.putInt(NO_PREVIEW_LINK_BACKGROUND_COLOR, customTheme.noPreviewLinkBackgroundColor); + editor.putInt(VOTE_AND_REPLY_UNAVAILABLE_BUTTON_COLOR, customTheme.voteAndReplyUnavailableButtonColor); + editor.putInt(COMMENT_VERTICAL_BAR_COLOR_1, customTheme.commentVerticalBarColor1); + editor.putInt(COMMENT_VERTICAL_BAR_COLOR_2, customTheme.commentVerticalBarColor2); + editor.putInt(COMMENT_VERTICAL_BAR_COLOR_3, customTheme.commentVerticalBarColor3); + editor.putInt(COMMENT_VERTICAL_BAR_COLOR_4, customTheme.commentVerticalBarColor4); + editor.putInt(COMMENT_VERTICAL_BAR_COLOR_5, customTheme.commentVerticalBarColor5); + editor.putInt(COMMENT_VERTICAL_BAR_COLOR_6, customTheme.commentVerticalBarColor6); + editor.putInt(COMMENT_VERTICAL_BAR_COLOR_7, customTheme.commentVerticalBarColor7); + editor.putInt(FAB_ICON_COLOR, customTheme.fabIconColor); + editor.putInt(CHIP_TEXT_COLOR, customTheme.chipTextColor); + editor.putBoolean(LIGHT_STATUS_BAR, customTheme.isLightStatusBar); + editor.putBoolean(LIGHT_NAV_BAR, customTheme.isLightNavBar); + editor.putBoolean(CHANGE_STATUS_BAR_ICON_COLOR_AFTER_TOOLBAR_COLLAPSED_IN_IMMERSIVE_INTERFACE, customTheme.isChangeStatusBarIconColorAfterToolbarCollapsedInImmersiveInterface); + editor.apply(); + } }