From 5657df8db212aa926971cebca73230295af172a7 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Fri, 21 Apr 2023 18:34:00 +0200 Subject: [PATCH 01/45] feat: move Settingsfragment to settings folder --- .../joinmastodon/android/fragments/HomeTabFragment.java | 2 +- .../fragments/onboarding/AccountActivationFragment.java | 3 +-- .../fragments/{ => settings}/SettingsFragment.java | 8 +++----- 3 files changed, 5 insertions(+), 8 deletions(-) rename mastodon/src/main/java/org/joinmastodon/android/fragments/{ => settings}/SettingsFragment.java (99%) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTabFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTabFragment.java index fd1a199ce..4e0a09659 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTabFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/HomeTabFragment.java @@ -1,7 +1,6 @@ package org.joinmastodon.android.fragments; import static org.joinmastodon.android.GlobalUserPreferences.reduceMotion; -import static org.joinmastodon.android.GlobalUserPreferences.showNewPostsButton; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; @@ -48,6 +47,7 @@ import org.joinmastodon.android.events.HashtagUpdatedEvent; import org.joinmastodon.android.events.ListDeletedEvent; import org.joinmastodon.android.events.ListUpdatedCreatedEvent; import org.joinmastodon.android.events.SelfUpdateStateChangedEvent; +import org.joinmastodon.android.fragments.settings.SettingsFragment; import org.joinmastodon.android.model.Announcement; import org.joinmastodon.android.model.Hashtag; import org.joinmastodon.android.model.HeaderPaginationList; diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/onboarding/AccountActivationFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/onboarding/AccountActivationFragment.java index 7a20eaa68..d30a93db3 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/onboarding/AccountActivationFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/onboarding/AccountActivationFragment.java @@ -23,8 +23,7 @@ import org.joinmastodon.android.api.requests.accounts.UpdateAccountCredentials; import org.joinmastodon.android.api.session.AccountActivationInfo; import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.api.session.AccountSessionManager; -import org.joinmastodon.android.fragments.HomeFragment; -import org.joinmastodon.android.fragments.SettingsFragment; +import org.joinmastodon.android.fragments.settings.SettingsFragment; import org.joinmastodon.android.model.Account; import org.joinmastodon.android.ui.AccountSwitcherSheet; import org.joinmastodon.android.ui.utils.UiUtils; diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsFragment.java similarity index 99% rename from mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java rename to mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsFragment.java index 5285c8e0d..639261ea1 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsFragment.java @@ -1,4 +1,4 @@ -package org.joinmastodon.android.fragments; +package org.joinmastodon.android.fragments.settings; import android.animation.ObjectAnimator; import android.annotation.SuppressLint; @@ -12,7 +12,6 @@ import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.text.TextUtils; -import android.util.Log; import android.util.LruCache; import android.util.TypedValue; import android.view.Gravity; @@ -46,7 +45,6 @@ import org.joinmastodon.android.GlobalUserPreferences; import org.joinmastodon.android.GlobalUserPreferences.ColorPreference; import org.joinmastodon.android.MainActivity; import org.joinmastodon.android.MastodonApp; -import org.joinmastodon.android.PushNotificationReceiver; import org.joinmastodon.android.R; import org.joinmastodon.android.api.MastodonAPIController; import org.joinmastodon.android.api.PushSubscriptionManager; @@ -55,6 +53,7 @@ import org.joinmastodon.android.api.session.AccountActivationInfo; import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.events.SelfUpdateStateChangedEvent; +import org.joinmastodon.android.fragments.MastodonToolbarFragment; import org.joinmastodon.android.fragments.onboarding.InstanceRulesFragment; import org.joinmastodon.android.model.ContentType; import org.joinmastodon.android.model.Instance; @@ -68,7 +67,6 @@ import org.joinmastodon.android.updater.GithubSelfUpdater; import org.parceler.Parcels; import java.util.ArrayList; -import java.util.Map; import java.util.function.Consumer; import androidx.annotation.DrawableRes; @@ -85,7 +83,7 @@ import me.grishka.appkit.utils.BindableViewHolder; import me.grishka.appkit.utils.V; import me.grishka.appkit.views.UsableRecyclerView; -public class SettingsFragment extends MastodonToolbarFragment{ +public class SettingsFragment extends MastodonToolbarFragment { private View view; private UsableRecyclerView list; private ArrayList items=new ArrayList<>(); From dc9e977d40e977396f6200d7d6609ccf66093807 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Fri, 21 Apr 2023 21:17:20 +0200 Subject: [PATCH 02/45] refactor: add abstract settingsbase fragment --- .../settings/SettingsBaseFragment.java | 415 ++++++++++++++++++ 1 file changed, 415 insertions(+) create mode 100644 mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java 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 new file mode 100644 index 000000000..f00bdd2fb --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java @@ -0,0 +1,415 @@ +package org.joinmastodon.android.fragments.settings; + +import android.annotation.SuppressLint; +import android.graphics.Rect; +import android.os.Build; +import android.os.Bundle; +import android.util.TypedValue; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.WindowInsets; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.Switch; +import android.widget.TextView; + +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.StringRes; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import org.joinmastodon.android.DomainManager; +import org.joinmastodon.android.R; +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.ui.utils.UiUtils; + +import java.util.ArrayList; +import java.util.function.Consumer; + +import me.grishka.appkit.utils.BindableViewHolder; +import me.grishka.appkit.utils.V; +import me.grishka.appkit.views.UsableRecyclerView; + +public abstract class SettingsBaseFragment extends MastodonToolbarFragment implements DomainDisplay { + protected View view; + private UsableRecyclerView list; + private ArrayList items=new ArrayList<>(); + private String accountID; + + public abstract void addItems(ArrayList items); + + @Override + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) + setRetainInstance(true); + setTitle(R.string.settings); + + accountID=getArguments().getString("account"); + AccountSession session = AccountSessionManager.getInstance().getAccount(accountID); + DomainManager.getInstance().setCurrentDomain(session.domain + "/settings"); + + addItems(items); + } + + @Override + public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ + list=new UsableRecyclerView(getActivity()); + list.setLayoutManager(new LinearLayoutManager(getActivity())); + list.setAdapter(new SettingsAdapter()); + list.setBackgroundColor(UiUtils.getThemeColor(getActivity(), android.R.attr.colorBackground)); + list.setPadding(0, V.dp(16), 0, V.dp(12)); + list.setClipToPadding(false); + list.addItemDecoration(new RecyclerView.ItemDecoration(){ + @Override + public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state){ + // Add 32dp gaps between sections + RecyclerView.ViewHolder holder=parent.getChildViewHolder(view); + if((holder instanceof HeaderViewHolder || holder instanceof FooterViewHolder) && holder.getAbsoluteAdapterPosition()>1) + outRect.top=V.dp(32); + } + }); + return list; + } + + @Override + public void onApplyWindowInsets(WindowInsets insets){ + if(Build.VERSION.SDK_INT>=29 && insets.getTappableElementInsets().bottom==0){ + list.setPadding(0, V.dp(16), 0, V.dp(12)+insets.getSystemWindowInsetBottom()); + insets=insets.inset(0, 0, 0, insets.getSystemWindowInsetBottom()); + } + super.onApplyWindowInsets(insets); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState){ + super.onViewCreated(view, savedInstanceState); + this.view = view; + } + + + private static abstract class Item{ + public abstract int getViewType(); + } + + private class HeaderItem extends Item{ + private String text; + + public HeaderItem(@StringRes int text){ + this.text=getString(text); + } + + public HeaderItem(String text){ + this.text=text; + } + + @Override + public int getViewType(){ + return Type.HEADER.ordinal(); + } + } + + private class SwitchItem extends Item{ + private String text; + private int icon; + private boolean checked; + private Consumer onChanged; + private boolean enabled=true; + + public SwitchItem(@StringRes int text, @DrawableRes int icon, boolean checked, Consumer onChanged){ + this.text=getString(text); + this.icon=icon; + this.checked=checked; + this.onChanged=onChanged; + } + + public SwitchItem(@StringRes int text, @DrawableRes int icon, boolean checked, Consumer onChanged, boolean enabled){ + this.text=getString(text); + this.icon=icon; + this.checked=checked; + this.onChanged=onChanged; + this.enabled=enabled; + } + + @Override + public int getViewType(){ + return Type.SWITCH.ordinal(); + } + } + + public class ButtonItem extends Item{ + private int text; + private int icon; + private Consumer