Material You.

This commit is contained in:
Alex Ning 2021-06-16 17:24:17 +08:00
parent 761217a87f
commit dfe2ca8081
5 changed files with 126 additions and 5 deletions

View File

@ -30,6 +30,7 @@ import androidx.constraintlayout.widget.Barrier;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet; import androidx.constraintlayout.widget.ConstraintSet;
import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.graphics.drawable.DrawableCompat;
import androidx.paging.PagedListAdapter;
import androidx.paging.PagingDataAdapter; import androidx.paging.PagingDataAdapter;
import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.ItemTouchHelper;
@ -104,7 +105,7 @@ import retrofit2.Retrofit;
* Created by alex on 2/25/18. * Created by alex on 2/25/18.
*/ */
public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerView.ViewHolder> implements CacheManager { public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHolder> implements CacheManager {
private static final int VIEW_TYPE_POST_CARD_VIDEO_AUTOPLAY_TYPE = 1; private static final int VIEW_TYPE_POST_CARD_VIDEO_AUTOPLAY_TYPE = 1;
private static final int VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE = 2; private static final int VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE = 2;
private static final int VIEW_TYPE_POST_CARD_IMAGE_AND_GIF_AUTOPLAY_TYPE = 3; private static final int VIEW_TYPE_POST_CARD_IMAGE_AND_GIF_AUTOPLAY_TYPE = 3;

View File

@ -1,11 +1,17 @@
package ml.docilealligator.infinityforreddit.settings; package ml.docilealligator.infinityforreddit.settings;
import android.app.WallpaperColors;
import android.app.WallpaperManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
@ -19,16 +25,19 @@ import androidx.preference.SwitchPreference;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import javax.inject.Inject; 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.activities.CustomThemeListingActivity; import ml.docilealligator.infinityforreddit.activities.CustomThemeListingActivity;
import ml.docilealligator.infinityforreddit.activities.CustomizeThemeActivity; import ml.docilealligator.infinityforreddit.activities.CustomizeThemeActivity;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeViewModel; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeViewModel;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
@ -44,9 +53,20 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat {
private AppCompatActivity activity; private AppCompatActivity activity;
@Inject @Inject
@Named("light_theme")
SharedPreferences lightThemeSharedPreferences;
@Inject
@Named("dark_theme")
SharedPreferences darkThemeSharedPreferences;
@Inject
@Named("amoled_theme")
SharedPreferences amoledThemeSharedPreferences;
@Inject
RedditDataRoomDatabase redditDataRoomDatabase; RedditDataRoomDatabase redditDataRoomDatabase;
@Inject @Inject
CustomThemeWrapper customThemeWrapper; CustomThemeWrapper customThemeWrapper;
@Inject
Executor executor;
public CustomThemeViewModel customThemeViewModel; public CustomThemeViewModel customThemeViewModel;
@Override @Override
@ -61,6 +81,7 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat {
Preference customizeDarkThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_DARK_THEME); Preference customizeDarkThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_DARK_THEME);
Preference customizeAmoledThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_AMOLED_THEME); Preference customizeAmoledThemePreference = findPreference(SharedPreferencesUtils.CUSTOMIZE_AMOLED_THEME);
Preference selectAndCustomizeThemePreference = findPreference(SharedPreferencesUtils.MANAGE_THEMES); Preference selectAndCustomizeThemePreference = findPreference(SharedPreferencesUtils.MANAGE_THEMES);
SwitchPreference enableMaterialYouSwitchPreference = findPreference(SharedPreferencesUtils.ENABLE_MATERIAL_YOU);
boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
if (themePreference != null && amoledDarkSwitch != null) { if (themePreference != null && amoledDarkSwitch != null) {
@ -151,6 +172,70 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat {
}); });
} }
if (enableMaterialYouSwitchPreference != null) {
Handler handler = new Handler();
enableMaterialYouSwitchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (true) {
executor.execute(() -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity);
WallpaperColors wallpaperColors = wallpaperManager.getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
if (wallpaperColors != null) {
int colorPrimaryInt = shiftColorTo255(wallpaperColors.getPrimaryColor().toArgb(), 0.4);
int colorPrimaryDarkInt = shiftColorTo0(colorPrimaryInt, 0.4);
int backgroundColor = shiftColorTo255(colorPrimaryInt, 0.6);
int cardViewBackgroundColor = shiftColorTo255(colorPrimaryInt, 0.9);
Color colorAccent = wallpaperColors.getSecondaryColor();
int colorAccentInt = shiftColorTo255(colorAccent == null ? customThemeWrapper.getColorAccent() : colorAccent.toArgb(), 0.4);
int colorPrimaryAppropriateTextColor = getAppropriateTextColor(colorPrimaryInt);
int backgroundColorAppropriateTextColor = getAppropriateTextColor(backgroundColor);
lightThemeSharedPreferences.edit().putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY, colorPrimaryInt)
.putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_DARK, colorPrimaryDarkInt)
.putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorAccentInt)
.putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt)
.putInt(CustomThemeSharedPreferencesUtils.BACKGROUND_COLOR, backgroundColor)
.putInt(CustomThemeSharedPreferencesUtils.CARD_VIEW_BACKGROUND_COLOR, cardViewBackgroundColor)
.putInt(CustomThemeSharedPreferencesUtils.BOTTOM_APP_BAR_BACKGROUND_COLOR, colorPrimaryInt)
.putInt(CustomThemeSharedPreferencesUtils.NAV_BAR_COLOR, colorPrimaryInt)
.putInt(CustomThemeSharedPreferencesUtils.PRIMARY_TEXT_COLOR, backgroundColorAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.BOTTOM_APP_BAR_ICON_COLOR, colorPrimaryAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.PRIMARY_ICON_COLOR, backgroundColorAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.FAB_ICON_COLOR, colorPrimaryAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.TOOLBAR_PRIMARY_TEXT_AND_ICON_COLOR, colorPrimaryAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.TOOLBAR_SECONDARY_TEXT_COLOR, colorPrimaryAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_INDICATOR, colorPrimaryAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TEXT_COLOR, colorPrimaryAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_COLLAPSED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, colorPrimaryInt)
.putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_BACKGROUND, colorPrimaryInt)
.putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_INDICATOR, colorPrimaryAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TEXT_COLOR, colorPrimaryAppropriateTextColor)
.putInt(CustomThemeSharedPreferencesUtils.CIRCULAR_PROGRESS_BAR_BACKGROUND, colorPrimaryInt)
.putBoolean(CustomThemeSharedPreferencesUtils.LIGHT_STATUS_BAR, getAppropriateTextColor(colorPrimaryDarkInt) == Color.toArgb(Color.BLACK))
.apply();
darkThemeSharedPreferences.edit()
.putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorPrimaryInt)
.putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt)
.apply();
amoledThemeSharedPreferences.edit()
.putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorPrimaryInt)
.putInt(CustomThemeSharedPreferencesUtils.COLOR_PRIMARY_LIGHT_THEME, colorPrimaryInt)
.apply();
handler.post(() -> EventBus.getDefault().post(new RecreateActivityEvent()));
}
}
});
}
return true;
}
});
}
customThemeViewModel = new ViewModelProvider(this, customThemeViewModel = new ViewModelProvider(this,
new CustomThemeViewModel.Factory(redditDataRoomDatabase)) new CustomThemeViewModel.Factory(redditDataRoomDatabase))
.get(CustomThemeViewModel.class); .get(CustomThemeViewModel.class);
@ -186,6 +271,28 @@ public class ThemePreferenceFragment extends PreferenceFragmentCompat {
}); });
} }
private int shiftColorTo255(int color, double ratio) {
int offset = (int) (Math.min(Math.min(255 - Color.red(color), 255 - Color.green(color)), 255 - Color.blue(color)) * ratio);
return Color.argb(Color.alpha(color), Color.red(color) + offset,
Color.green(color) + offset,
Color.blue(color) + offset);
}
private int shiftColorTo0(int color, double ratio) {
int offset = (int) (Math.min(Math.min(Color.red(color), Color.green(color)), Color.blue(color)) * ratio);
return Color.argb(Color.alpha(color), Color.red(color) - offset,
Color.green(color) - offset,
Color.blue(color) - offset);
}
@ColorInt
public int getAppropriateTextColor(@ColorInt int color) {
// Counting the perceptive luminance - human eye favors green color...
double luminance = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
return luminance < 0.5 ? Color.BLACK : Color.WHITE;
}
@Override @Override
public void onAttach(@NonNull Context context) { public void onAttach(@NonNull Context context) {
super.onAttach(context); super.onAttach(context);

View File

@ -180,6 +180,7 @@ public class SharedPreferencesUtils {
public static final String NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_CARD_LAYOUT_2 = "number_of_columns_in_post_feed_landscape_card_layout_2"; public static final String NUMBER_OF_COLUMNS_IN_POST_FEED_LANDSCAPE_CARD_LAYOUT_2 = "number_of_columns_in_post_feed_landscape_card_layout_2";
public static final String DISABLE_NSFW_FOREVER = "disable_nsfw_forever"; public static final String DISABLE_NSFW_FOREVER = "disable_nsfw_forever";
public static final String SHOW_ONLY_ONE_COMMENT_LEVEL_INDICATOR = "show_only_one_comment_level_indicator"; public static final String SHOW_ONLY_ONE_COMMENT_LEVEL_INDICATOR = "show_only_one_comment_level_indicator";
public static final String ENABLE_MATERIAL_YOU = "enable_material_you";
public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences"; public static final String DEFAULT_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit_preferences";
public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs";

View File

@ -457,6 +457,9 @@
<string name="settings_customize_dark_theme_title">Dark Theme</string> <string name="settings_customize_dark_theme_title">Dark Theme</string>
<string name="settings_customize_amoled_theme_title">Amoled Theme</string> <string name="settings_customize_amoled_theme_title">Amoled Theme</string>
<string name="settings_manage_themes_title">Manage Themes</string> <string name="settings_manage_themes_title">Manage Themes</string>
<string name="settings_category_material_you_title">Material You</string>
<string name="settings_enable_material_you_title">Enable Material You</string>
<string name="settings_enable_material_you_summary">Personalize Infinity based on Your Wallpaper</string>
<string name="settings_custom_theme_cannot_apply_to_settings_page_summary">Custom themes cannot be applied to settings page (except toolbar, status bar and navigation bar).</string> <string name="settings_custom_theme_cannot_apply_to_settings_page_summary">Custom themes cannot be applied to settings page (except toolbar, status bar and navigation bar).</string>
<string name="settings_advanced_master_title">Advanced</string> <string name="settings_advanced_master_title">Advanced</string>
<string name="settings_delete_all_subreddits_data_in_database_title">Delete All Subreddits in Database</string> <string name="settings_delete_all_subreddits_data_in_database_title">Delete All Subreddits in Database</string>

View File

@ -40,6 +40,15 @@
app:icon="@drawable/ic_edit_24dp" app:icon="@drawable/ic_edit_24dp"
app:title="@string/settings_manage_themes_title" /> app:title="@string/settings_manage_themes_title" />
<PreferenceCategory
app:title="@string/settings_category_material_you_title" />
<SwitchPreference
app:defaultValue="false"
app:key="enable_material_you"
app:title="@string/settings_enable_material_you_title"
app:summary="@string/settings_enable_material_you_summary" />
<Preference <Preference
app:icon="@drawable/ic_info_preference_24dp" app:icon="@drawable/ic_info_preference_24dp"
app:summary="@string/settings_custom_theme_cannot_apply_to_settings_page_summary" app:summary="@string/settings_custom_theme_cannot_apply_to_settings_page_summary"