From 408e819d321951a060dbafafa7dbfd94295bf4dd Mon Sep 17 00:00:00 2001 From: Stypox Date: Mon, 7 Sep 2020 15:25:26 +0200 Subject: [PATCH] Extract duplicate setActivityTitle code to own function --- .../settings/BasePreferenceFragment.java | 22 ++++++------------- .../PeertubeInstanceListFragment.java | 14 ++---------- .../settings/tabs/ChooseTabsFragment.java | 14 ++---------- .../org/schabi/newpipe/util/ThemeHelper.java | 20 +++++++++++++++++ 4 files changed, 31 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/BasePreferenceFragment.java b/app/src/main/java/org/schabi/newpipe/settings/BasePreferenceFragment.java index d8b686297..911d27403 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/BasePreferenceFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/BasePreferenceFragment.java @@ -5,12 +5,12 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.AppCompatActivity; import androidx.preference.PreferenceFragmentCompat; import org.schabi.newpipe.MainActivity; +import org.schabi.newpipe.util.ThemeHelper; public abstract class BasePreferenceFragment extends PreferenceFragmentCompat { protected final String TAG = getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()); @@ -25,24 +25,16 @@ public abstract class BasePreferenceFragment extends PreferenceFragmentCompat { } @Override - public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); + public void onViewCreated(@NonNull final View rootView, + @Nullable final Bundle savedInstanceState) { + super.onViewCreated(rootView, savedInstanceState); setDivider(null); - updateTitle(); + ThemeHelper.setTitleToAppCompatActivity(getActivity(), getPreferenceScreen().getTitle()); } @Override public void onResume() { super.onResume(); - updateTitle(); - } - - private void updateTitle() { - if (getActivity() instanceof AppCompatActivity) { - final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(getPreferenceScreen().getTitle()); - } - } + ThemeHelper.setTitleToAppCompatActivity(getActivity(), getPreferenceScreen().getTitle()); } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/PeertubeInstanceListFragment.java b/app/src/main/java/org/schabi/newpipe/settings/PeertubeInstanceListFragment.java index dfa975eef..5dddb14fd 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/PeertubeInstanceListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/PeertubeInstanceListFragment.java @@ -22,9 +22,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.widget.AppCompatImageView; import androidx.fragment.app.Fragment; @@ -117,7 +115,8 @@ public class PeertubeInstanceListFragment extends Fragment { @Override public void onResume() { super.onResume(); - updateTitle(); + ThemeHelper.setTitleToAppCompatActivity(getActivity(), + getString(R.string.peertube_instance_url_title)); } @Override @@ -176,15 +175,6 @@ public class PeertubeInstanceListFragment extends Fragment { sharedPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, true).apply(); } - private void updateTitle() { - if (getActivity() instanceof AppCompatActivity) { - final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(R.string.peertube_instance_url_title); - } - } - } - private void saveChanges() { final JsonStringWriter jsonWriter = JsonWriter.string().object().array("instances"); for (final PeertubeInstance instance : instanceList) { diff --git a/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java index 44fe987ee..2554ecc5c 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/tabs/ChooseTabsFragment.java @@ -16,9 +16,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.widget.AppCompatImageView; import androidx.fragment.app.Fragment; @@ -92,7 +90,8 @@ public class ChooseTabsFragment extends Fragment { @Override public void onResume() { super.onResume(); - updateTitle(); + ThemeHelper.setTitleToAppCompatActivity(getActivity(), + getString(R.string.main_page_content)); } @Override @@ -137,15 +136,6 @@ public class ChooseTabsFragment extends Fragment { tabList.addAll(tabsManager.getTabs()); } - private void updateTitle() { - if (getActivity() instanceof AppCompatActivity) { - final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(R.string.main_page_content); - } - } - } - private void saveChanges() { tabsManager.saveTabs(tabList); } diff --git a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java index 88e1a31cd..6b8e884a2 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ThemeHelper.java @@ -19,6 +19,7 @@ package org.schabi.newpipe.util; +import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.preference.PreferenceManager; @@ -26,7 +27,10 @@ import android.util.TypedValue; import android.view.ContextThemeWrapper; import androidx.annotation.AttrRes; +import androidx.annotation.Nullable; import androidx.annotation.StyleRes; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; import org.schabi.newpipe.R; @@ -231,4 +235,20 @@ public final class ThemeHelper { return PreferenceManager.getDefaultSharedPreferences(context) .getString(themeKey, defaultTheme); } + + /** + * Sets the title to the activity, if the activity is an {@link AppCompatActivity} and has an + * action bar. + * @param activity the activity to set the title of + * @param title the title to set to the activity + */ + public static void setTitleToAppCompatActivity(@Nullable final Activity activity, + final CharSequence title) { + if (activity instanceof AppCompatActivity) { + final ActionBar actionBar = ((AppCompatActivity) activity).getSupportActionBar(); + if (actionBar != null) { + actionBar.setTitle(title); + } + } + } }