From cd749e9c9cb8d3453f4739a53eed0535f254e5b2 Mon Sep 17 00:00:00 2001 From: LucasGGamerM Date: Sun, 23 Apr 2023 12:47:49 -0300 Subject: [PATCH] feat: rework SettingsCategoryItem, and add visual appearance settings --- .../ic_fluent_chat_settings_24_regular.xml | 9 - .../settings/SettingsAppearanceFragment.java | 57 ++++--- .../settings/SettingsBaseFragment.java | 154 +++++++++--------- .../settings/SettingsMainFragment.java | 15 +- 4 files changed, 121 insertions(+), 114 deletions(-) delete mode 100644 mastodon/src/github/res/drawable/ic_fluent_chat_settings_24_regular.xml diff --git a/mastodon/src/github/res/drawable/ic_fluent_chat_settings_24_regular.xml b/mastodon/src/github/res/drawable/ic_fluent_chat_settings_24_regular.xml deleted file mode 100644 index 703f8f901..000000000 --- a/mastodon/src/github/res/drawable/ic_fluent_chat_settings_24_regular.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAppearanceFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAppearanceFragment.java index 62968cece..fc870c20f 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAppearanceFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAppearanceFragment.java @@ -1,22 +1,29 @@ package org.joinmastodon.android.fragments.settings; +import android.graphics.Bitmap; +import android.graphics.Canvas; import android.os.Build; import android.view.Gravity; import android.view.MenuItem; +import android.view.View; +import android.view.WindowManager; +import android.widget.ImageView; import android.widget.PopupMenu; +import androidx.recyclerview.widget.RecyclerView; + import org.joinmastodon.android.GlobalUserPreferences; +import org.joinmastodon.android.MastodonApp; import org.joinmastodon.android.R; +import org.joinmastodon.android.ui.utils.UiUtils; import java.util.ArrayList; public class SettingsAppearanceFragment extends SettingsBaseFragment { - @Override public void addItems(ArrayList items) { - themeItem = new ThemeItem(); - items.add(themeItem); - items.add(new ButtonItem(R.string.sk_settings_color_palette, R.drawable.ic_fluent_color_24_regular, b -> { + items.add(themeItem = new SettingsBaseFragment.ThemeItem()); + items.add(new SettingsBaseFragment.ButtonItem(R.string.sk_settings_color_palette, R.drawable.ic_fluent_color_24_regular, b -> { PopupMenu popupMenu = new PopupMenu(getActivity(), b, Gravity.CENTER_HORIZONTAL); popupMenu.inflate(R.menu.color_palettes); popupMenu.getMenu().findItem(R.id.m3_color).setVisible(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S); @@ -35,43 +42,43 @@ public class SettingsAppearanceFragment extends SettingsBaseFragment { case NORD -> R.string.mo_color_palette_nord; }); })); - items.add(new SwitchItem(R.string.theme_true_black, R.drawable.ic_fluent_dark_theme_24_regular, GlobalUserPreferences.trueBlackTheme, this::onTrueBlackThemeChanged)); - items.add(new SwitchItem(R.string.sk_disable_marquee, R.drawable.ic_fluent_text_more_24_regular, GlobalUserPreferences.disableMarquee, i -> { + items.add(new SettingsBaseFragment.SwitchItem(R.string.theme_true_black, R.drawable.ic_fluent_dark_theme_24_regular, GlobalUserPreferences.trueBlackTheme, this::onTrueBlackThemeChanged)); + items.add(new SettingsBaseFragment.SwitchItem(R.string.sk_disable_marquee, R.drawable.ic_fluent_text_more_24_regular, GlobalUserPreferences.disableMarquee, i -> { GlobalUserPreferences.disableMarquee = i.checked; GlobalUserPreferences.save(); needAppRestart = true; })); - items.add(new SwitchItem(R.string.sk_settings_uniform_icon_for_notifications, R.drawable.ic_ntf_logo, GlobalUserPreferences.uniformNotificationIcon, i -> { + items.add(new SettingsBaseFragment.SwitchItem(R.string.sk_settings_uniform_icon_for_notifications, R.drawable.ic_ntf_logo, GlobalUserPreferences.uniformNotificationIcon, i -> { GlobalUserPreferences.uniformNotificationIcon = i.checked; GlobalUserPreferences.save(); })); - items.add(new SwitchItem(R.string.sk_settings_reduce_motion, R.drawable.ic_fluent_star_emphasis_24_regular, GlobalUserPreferences.reduceMotion, i -> { + items.add(new SettingsBaseFragment.SwitchItem(R.string.sk_settings_reduce_motion, R.drawable.ic_fluent_star_emphasis_24_regular, GlobalUserPreferences.reduceMotion, i -> { GlobalUserPreferences.reduceMotion = i.checked; GlobalUserPreferences.save(); needAppRestart = true; })); } - private boolean onColorPreferenceClick(MenuItem item) { - GlobalUserPreferences.ColorPreference pref = null; - int id = item.getItemId(); + protected boolean onColorPreferenceClick(MenuItem item){ + GlobalUserPreferences.ColorPreference pref = null; + int id = item.getItemId(); - if (id == R.id.m3_color) pref = GlobalUserPreferences.ColorPreference.MATERIAL3; - else if (id == R.id.pink_color) pref = GlobalUserPreferences.ColorPreference.PINK; - else if (id == R.id.purple_color) pref = GlobalUserPreferences.ColorPreference.PURPLE; - else if (id == R.id.green_color) pref = GlobalUserPreferences.ColorPreference.GREEN; - else if (id == R.id.blue_color) pref = GlobalUserPreferences.ColorPreference.BLUE; - else if (id == R.id.brown_color) pref = GlobalUserPreferences.ColorPreference.BROWN; - else if (id == R.id.red_color) pref = GlobalUserPreferences.ColorPreference.RED; - else if (id == R.id.yellow_color) pref = GlobalUserPreferences.ColorPreference.YELLOW; - else if (id == R.id.nord_color) pref = GlobalUserPreferences.ColorPreference.NORD; + if (id == R.id.m3_color) pref = GlobalUserPreferences.ColorPreference.MATERIAL3; + else if (id == R.id.pink_color) pref = GlobalUserPreferences.ColorPreference.PINK; + else if (id == R.id.purple_color) pref = GlobalUserPreferences.ColorPreference.PURPLE; + else if (id == R.id.green_color) pref = GlobalUserPreferences.ColorPreference.GREEN; + else if (id == R.id.blue_color) pref = GlobalUserPreferences.ColorPreference.BLUE; + else if (id == R.id.brown_color) pref = GlobalUserPreferences.ColorPreference.BROWN; + else if (id == R.id.red_color) pref = GlobalUserPreferences.ColorPreference.RED; + else if (id == R.id.yellow_color) pref = GlobalUserPreferences.ColorPreference.YELLOW; + else if (id == R.id.nord_color) pref = GlobalUserPreferences.ColorPreference.NORD; - if (pref == null) return false; + if (pref == null) return false; - GlobalUserPreferences.color=pref; - GlobalUserPreferences.save(); - restartActivityToApplyNewTheme(); - return true; + GlobalUserPreferences.color=pref; + GlobalUserPreferences.save(); + restartActivityToApplyNewTheme(); + return true; } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java index efb29350a..90fe16606 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java @@ -2,7 +2,6 @@ package org.joinmastodon.android.fragments.settings; import android.animation.ObjectAnimator; import android.annotation.SuppressLint; -import android.app.Fragment; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Rect; @@ -42,7 +41,6 @@ import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.fragments.DomainDisplay; import org.joinmastodon.android.fragments.MastodonToolbarFragment; -import org.joinmastodon.android.model.Instance; import org.joinmastodon.android.model.PushNotification; import org.joinmastodon.android.model.PushSubscription; import org.joinmastodon.android.ui.OutlineProviders; @@ -52,7 +50,6 @@ import org.joinmastodon.android.updater.GithubSelfUpdater; import java.util.ArrayList; import java.util.function.Consumer; -import me.grishka.appkit.Nav; import me.grishka.appkit.utils.BindableViewHolder; import me.grishka.appkit.utils.V; import me.grishka.appkit.views.UsableRecyclerView; @@ -66,15 +63,12 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple protected ThemeItem themeItem; protected boolean needAppRestart; - private Instance instance; - private String instanceName; - protected NotificationPolicyItem notificationPolicyItem; + protected SettingsBaseFragment.NotificationPolicyItem notificationPolicyItem; protected PushSubscription pushSubscription; protected ArrayList items=new ArrayList<>(); protected String accountID; - protected AccountSession session; protected boolean needUpdateNotificationSettings; @@ -88,9 +82,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple setTitle(R.string.settings); accountID=getArguments().getString("account"); - session = AccountSessionManager.getInstance().getAccount(accountID); - instance = AccountSessionManager.getInstance().getInstanceInfo(session.domain); - instanceName = UiUtils.getInstanceName(accountID); + AccountSession session = AccountSessionManager.getInstance().getAccount(accountID); DomainManager.getInstance().setCurrentDomain(session.domain + "/settings"); addItems(items); @@ -131,14 +123,6 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple this.view = view; } - protected Instance getInstance() { - return instance; - } - - protected String getInstanceName() { - return instanceName; - } - static abstract class Item{ public abstract int getViewType(); @@ -161,29 +145,12 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } } - protected class RedHeaderItem extends HeaderItem { - - public RedHeaderItem(int text){ - super(text); - } - - public RedHeaderItem(String text){ - super(text); - } - - @Override - public int getViewType(){ - return Type.RED_HEADER.ordinal(); - } - } - - protected class SwitchItem extends Item{ private String text; private int icon; boolean checked; private Consumer onChanged; - protected boolean enabled=true; + private boolean enabled=true; public SwitchItem(@StringRes int text, @DrawableRes int icon, boolean checked, Consumer onChanged){ this.text=getString(text); @@ -206,7 +173,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } } - protected class UpdateItem extends Item { + protected class UpdateItem extends SettingsBaseFragment.Item { @Override public int getViewType(){ @@ -214,7 +181,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } } - protected static class ThemeItem extends Item { + protected static class ThemeItem extends SettingsBaseFragment.Item { @Override public int getViewType(){ @@ -222,7 +189,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } } - protected static class NotificationPolicyItem extends Item { + protected static class NotificationPolicyItem extends SettingsBaseFragment.Item { @Override public int getViewType(){ @@ -263,7 +230,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple protected class TextItem extends Item{ private String text; - protected String secondaryText; + private String secondaryText; private Runnable onClick; private boolean loading; private int icon; @@ -307,20 +274,41 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple protected class SettingsCategoryItem extends Item{ private String text; + private String secondaryText; + private Runnable onClick; + private boolean loading; private int icon; - private Class fragmentClass; + public SettingsCategoryItem(@StringRes int text, Runnable onClick) { + this(text, null, onClick, false, 0); + } - public SettingsCategoryItem(@StringRes int text, Class fragmentClass, @DrawableRes int icon) { - this.text = getString(text); - this.fragmentClass=fragmentClass; - this.icon=icon; - } + public SettingsCategoryItem(@StringRes int text, Runnable onClick, @DrawableRes int icon) { + this(text, null, onClick, false, icon); + } - public SettingsCategoryItem(@StringRes int text, Class fragmentClass) { - this(text, fragmentClass, 0); - } + public SettingsCategoryItem(@StringRes int text, String secondaryText, Runnable onClick, @DrawableRes int icon) { + this(text, secondaryText, onClick, false, icon); + } + public SettingsCategoryItem(@StringRes int text, String secondaryText, Runnable onClick, boolean loading, @DrawableRes int icon){ + this.text=getString(text); + this.onClick=onClick; + this.loading=loading; + this.icon=icon; + this.secondaryText = secondaryText; + } + + public SettingsCategoryItem(String text, Runnable onClick){ + this.text=text; + this.onClick=onClick; + } + + public SettingsCategoryItem(String text, Runnable onClick, @DrawableRes int icon){ + this.text=text; + this.onClick=onClick; + this.icon=icon; + } @Override public int getViewType(){ @@ -346,7 +334,6 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple public enum Type{ HEADER, - RED_HEADER, SWITCH, THEME, TEXT, @@ -355,7 +342,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple BUTTON, SMALL_TEXT, UPDATER, - SETTINGS_CATEGORY; + SETTINGS_CATEGORY } @@ -367,7 +354,6 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple //noinspection unchecked return (BindableViewHolder) switch(Type.values()[viewType]){ case HEADER -> new HeaderViewHolder(); - case RED_HEADER -> new HeaderViewHolder(true); case SWITCH -> new SwitchViewHolder(); case THEME -> new ThemeViewHolder(); case TEXT -> new TextViewHolder(); @@ -403,19 +389,34 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple text=(TextView) itemView; } - public HeaderViewHolder(boolean red){ - super(getActivity(), R.layout.item_settings_header, list); - text=(TextView) itemView; - if(red) - text.setTextColor(getResources().getColor(UiUtils.isDarkTheme() ? R.color.error_400 : R.color.error_700)); - } - @Override public void onBind(HeaderItem item){ text.setText(item.text); } } + protected boolean onColorPreferenceClick(MenuItem item){ + GlobalUserPreferences.ColorPreference pref = null; + int id = item.getItemId(); + + if (id == R.id.m3_color) pref = GlobalUserPreferences.ColorPreference.MATERIAL3; + else if (id == R.id.pink_color) pref = GlobalUserPreferences.ColorPreference.PINK; + else if (id == R.id.purple_color) pref = GlobalUserPreferences.ColorPreference.PURPLE; + else if (id == R.id.green_color) pref = GlobalUserPreferences.ColorPreference.GREEN; + else if (id == R.id.blue_color) pref = GlobalUserPreferences.ColorPreference.BLUE; + else if (id == R.id.brown_color) pref = GlobalUserPreferences.ColorPreference.BROWN; + else if (id == R.id.red_color) pref = GlobalUserPreferences.ColorPreference.RED; + else if (id == R.id.yellow_color) pref = GlobalUserPreferences.ColorPreference.YELLOW; + else if (id == R.id.nord_color) pref = GlobalUserPreferences.ColorPreference.NORD; + + if (pref == null) return false; + + GlobalUserPreferences.color=pref; + GlobalUserPreferences.save(); + restartActivityToApplyNewTheme(); + return true; + } + protected void onThemePreferenceClick(GlobalUserPreferences.ThemePreference theme){ GlobalUserPreferences.theme=theme; GlobalUserPreferences.save(); @@ -455,13 +456,13 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } - protected void onTrueBlackThemeChanged(SwitchItem item){ + protected void onTrueBlackThemeChanged(SettingsBaseFragment.SwitchItem item){ GlobalUserPreferences.trueBlackTheme=item.checked; GlobalUserPreferences.save(); RecyclerView.ViewHolder themeHolder=list.findViewHolderForAdapterPosition(items.indexOf(themeItem)); if(themeHolder!=null){ - ((ThemeViewHolder)themeHolder).bindSubitems(); + ((SettingsBaseFragment.ThemeViewHolder)themeHolder).bindSubitems(); }else{ list.getAdapter().notifyItemChanged(items.indexOf(themeItem)); } @@ -503,7 +504,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } @Override - public void onBind(NotificationPolicyItem item){ + public void onBind(SettingsBaseFragment.NotificationPolicyItem item){ button.setText(switch(getPushSubscription().policy){ case ALL -> R.string.notify_anyone; case FOLLOWED -> R.string.notify_followed; @@ -522,7 +523,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple int index=items.indexOf(notificationPolicyItem); RecyclerView.ViewHolder policyHolder=list.findViewHolderForAdapterPosition(index); if(policyHolder!=null){ - ((NotificationPolicyViewHolder)policyHolder).rebind(); + ((SettingsBaseFragment.NotificationPolicyViewHolder)policyHolder).rebind(); }else{ list.getAdapter().notifyItemChanged(index); } @@ -532,7 +533,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple onNotificationsChanged(value, newState); } index++; - while(items.size() > index && items.get(index) instanceof SwitchItem si){ + while(items.get(index) instanceof SettingsBaseFragment.SwitchItem si){ si.enabled=si.checked=newState; RecyclerView.ViewHolder holder=list.findViewHolderForAdapterPosition(index); if(holder!=null) @@ -572,7 +573,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple return pushSubscription; } - protected class SwitchViewHolder extends BindableViewHolder implements UsableRecyclerView.DisableableClickable{ + private class SwitchViewHolder extends BindableViewHolder implements UsableRecyclerView.DisableableClickable{ private final TextView text; private final ImageView icon; private final Switch checkbox; @@ -621,7 +622,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } @Override - public void onBind(ThemeItem item){ + public void onBind(SettingsBaseFragment.ThemeItem item){ bindSubitems(); } @@ -671,7 +672,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } } - private class SettingsCategoryViewHolder extends BindableViewHolder implements UsableRecyclerView.Clickable{ + private class SettingsCategoryViewHolder extends BindableViewHolder implements UsableRecyclerView.DisableableClickable{ private final ImageView icon; private final TextView text; @@ -688,11 +689,16 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple icon.setImageResource(item.icon); } - @Override - public void onClick() { - Nav.go(getActivity(), item.fragmentClass, getArguments()); - } - } + @Override + public void onClick() { + item.onClick.run(); + } + + @Override + public boolean isEnabled(){ + return true; + } + } protected class ButtonViewHolder extends BindableViewHolder{ private final Button button; @@ -715,7 +721,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } } - protected class TextViewHolder extends BindableViewHolder implements UsableRecyclerView.Clickable{ + private class TextViewHolder extends BindableViewHolder implements UsableRecyclerView.Clickable{ private final TextView text, secondaryText; private final ProgressBar progress; private final ImageView icon; @@ -813,7 +819,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple } @Override - public void onBind(UpdateItem item){ + public void onBind(SettingsBaseFragment.UpdateItem item){ GithubSelfUpdater updater=GithubSelfUpdater.getInstance(); GithubSelfUpdater.UpdateState state=updater.getState(); if (state == GithubSelfUpdater.UpdateState.CHECKING) return; diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsMainFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsMainFragment.java index eb3ef9f67..9717a85dc 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsMainFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsMainFragment.java @@ -1,17 +1,20 @@ package org.joinmastodon.android.fragments.settings; +import android.os.Bundle; + import org.joinmastodon.android.R; import java.util.ArrayList; +import me.grishka.appkit.Nav; + public class SettingsMainFragment extends SettingsBaseFragment{ @Override public void addItems(ArrayList items) { - items.add(new SettingsCategoryItem(R.string.settings_theme, SettingsAppearanceFragment.class, R.drawable.ic_fluent_color_24_regular)); - items.add(new SettingsCategoryItem(R.string.settings_behavior, BehaviourFragment.class, R.drawable.ic_fluent_chat_settings_24_regular)); - items.add(new SettingsCategoryItem(R.string.sk_timelines, TimeLineFragment.class, R.drawable.ic_fluent_timeline_24_regular)); - items.add(new SettingsCategoryItem(R.string.settings_notifications, NotificationsFragment.class, R.drawable.ic_fluent_alert_28_regular_badged)); - items.add(new SettingsCategoryItem(R.string.settings_account, AccountFragment.class, R.drawable.ic_fluent_person_28_regular)); - items.add(new SettingsCategoryItem(R.string.sk_settings_about, AboutFragment.class, R.drawable.ic_fluent_info_24_regular)); + items.add(new SettingsBaseFragment.SettingsCategoryItem(R.string.settings_theme, () -> { + Bundle args = new Bundle(); + args.putString("account", accountID); + Nav.go(getActivity(), SettingsAppearanceFragment.class, args); + }, R.drawable.ic_fluent_color_24_regular)); } }