summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorflofriday <flohacksfriday@gmail.com>2024-06-22 13:38:18 +0200
committerGitHub <noreply@github.com>2024-06-22 13:38:18 +0200
commitda5972a3fd3fdddb0e3882886792bbc1183a332c (patch)
treefa37340cf236af5411431e7eec17f72aeb1b6a2f
parent14cf3cb1fca763dd5b9137e5e00906161131fc57 (diff)
downloadAntennaPod-da5972a3fd3fdddb0e3882886792bbc1183a332c.zip
Refactor ToolbarIconTintManager to a simpler API (#7240)
The original API used a callback where the caller needed to specify the icons of the menu items. However, the manager can find them on it's own. Since the callback was never used for anything else, I also removed it, which now means the ToolbarIconTintManager can now be used on it's own instead of instantiating an anonymous class that only overwrites the callback.
-rw-r--r--app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedInfoFragment.java13
-rw-r--r--app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedItemlistFragment.java14
-rw-r--r--app/src/main/java/de/danoeh/antennapod/ui/screen/feed/ToolbarIconTintManager.java43
3 files changed, 25 insertions, 45 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedInfoFragment.java b/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedInfoFragment.java
index f73b69d75..56164a819 100644
--- a/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedInfoFragment.java
+++ b/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedInfoFragment.java
@@ -20,7 +20,6 @@ import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
-import androidx.appcompat.content.res.AppCompatResources;
import androidx.documentfile.provider.DocumentFile;
import androidx.fragment.app.Fragment;
import com.bumptech.glide.Glide;
@@ -104,16 +103,8 @@ public class FeedInfoFragment extends Fragment implements MaterialToolbar.OnMenu
viewBinding.toolbar.setOnMenuItemClickListener(this);
refreshToolbarState();
- ToolbarIconTintManager iconTintManager = new ToolbarIconTintManager(getContext(),
- viewBinding.toolbar, viewBinding.collapsingToolbar) {
- @Override
- protected void doTint(Context themedContext) {
- viewBinding.toolbar.getMenu().findItem(R.id.visit_website_item)
- .setIcon(AppCompatResources.getDrawable(themedContext, R.drawable.ic_web));
- viewBinding.toolbar.getMenu().findItem(R.id.share_item)
- .setIcon(AppCompatResources.getDrawable(themedContext, R.drawable.ic_share));
- }
- };
+ ToolbarIconTintManager iconTintManager =
+ new ToolbarIconTintManager(viewBinding.toolbar, viewBinding.collapsingToolbar);
iconTintManager.updateTint();
viewBinding.appBar.addOnOffsetChangedListener(iconTintManager);
diff --git a/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedItemlistFragment.java b/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedItemlistFragment.java
index 2c8be5390..6dbab44a8 100644
--- a/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedItemlistFragment.java
+++ b/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/FeedItemlistFragment.java
@@ -1,6 +1,5 @@
package de.danoeh.antennapod.ui.screen.feed;
-import android.content.Context;
import android.content.res.Configuration;
import android.graphics.LightingColorFilter;
import android.os.Bundle;
@@ -15,7 +14,6 @@ import android.widget.AdapterView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
-import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.util.Pair;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
@@ -161,16 +159,8 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
swipeActions = new SwipeActions(this, TAG).attachTo(viewBinding.recyclerView);
viewBinding.progressBar.setVisibility(View.VISIBLE);
- ToolbarIconTintManager iconTintManager = new ToolbarIconTintManager(
- viewBinding.toolbar.getContext(), viewBinding.toolbar, viewBinding.collapsingToolbar) {
- @Override
- protected void doTint(Context themedContext) {
- viewBinding.toolbar.getMenu().findItem(R.id.refresh_item)
- .setIcon(AppCompatResources.getDrawable(themedContext, R.drawable.ic_refresh));
- viewBinding.toolbar.getMenu().findItem(R.id.action_search)
- .setIcon(AppCompatResources.getDrawable(themedContext, R.drawable.ic_search));
- }
- };
+ ToolbarIconTintManager iconTintManager =
+ new ToolbarIconTintManager(viewBinding.toolbar, viewBinding.collapsingToolbar);
iconTintManager.updateTint();
viewBinding.appBar.addOnOffsetChangedListener(iconTintManager);
diff --git a/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/ToolbarIconTintManager.java b/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/ToolbarIconTintManager.java
index e158053d0..7b2104fba 100644
--- a/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/ToolbarIconTintManager.java
+++ b/app/src/main/java/de/danoeh/antennapod/ui/screen/feed/ToolbarIconTintManager.java
@@ -1,23 +1,24 @@
package de.danoeh.antennapod.ui.screen.feed;
-import android.content.Context;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
-import android.view.ContextThemeWrapper;
+import android.view.Menu;
+
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
-import de.danoeh.antennapod.R;
-public abstract class ToolbarIconTintManager implements AppBarLayout.OnOffsetChangedListener {
- private final Context context;
+/**
+ * A manager that automatically finds all icons in a collapsable toolbar and tints them according to the collapse state
+ * of the toolbar.
+ */
+public class ToolbarIconTintManager implements AppBarLayout.OnOffsetChangedListener {
private final CollapsingToolbarLayout collapsingToolbar;
private final MaterialToolbar toolbar;
private boolean isTinted = false;
- public ToolbarIconTintManager(Context context, MaterialToolbar toolbar, CollapsingToolbarLayout collapsingToolbar) {
- this.context = context;
+ public ToolbarIconTintManager(MaterialToolbar toolbar, CollapsingToolbarLayout collapsingToolbar) {
this.collapsingToolbar = collapsingToolbar;
this.toolbar = toolbar;
}
@@ -32,16 +33,20 @@ public abstract class ToolbarIconTintManager implements AppBarLayout.OnOffsetCha
}
public void updateTint() {
+ PorterDuffColorFilter filter = null;
if (isTinted) {
- doTint(new ContextThemeWrapper(context, R.style.Theme_AntennaPod_Dark));
- safeSetColorFilter(toolbar.getNavigationIcon(), new PorterDuffColorFilter(0xffffffff, Mode.SRC_ATOP));
- safeSetColorFilter(toolbar.getOverflowIcon(), new PorterDuffColorFilter(0xffffffff, Mode.SRC_ATOP));
- safeSetColorFilter(toolbar.getCollapseIcon(), new PorterDuffColorFilter(0xffffffff, Mode.SRC_ATOP));
- } else {
- doTint(context);
- safeSetColorFilter(toolbar.getNavigationIcon(), null);
- safeSetColorFilter(toolbar.getOverflowIcon(), null);
- safeSetColorFilter(toolbar.getCollapseIcon(), null);
+ filter = new PorterDuffColorFilter(0xffffffff, Mode.SRC_ATOP);
+ }
+
+ safeSetColorFilter(toolbar.getNavigationIcon(), filter);
+ safeSetColorFilter(toolbar.getOverflowIcon(), filter);
+ safeSetColorFilter(toolbar.getCollapseIcon(), filter);
+
+ Menu menu = toolbar.getMenu();
+ for (int i = 0; i < menu.size(); i++) {
+ Drawable icon = menu.getItem(i).getIcon();
+ safeSetColorFilter(icon, filter);
+ menu.getItem(i).setIcon(icon);
}
}
@@ -50,10 +55,4 @@ public abstract class ToolbarIconTintManager implements AppBarLayout.OnOffsetCha
icon.setColorFilter(filter);
}
}
-
- /**
- * View expansion was changed. Icons need to be tinted
- * @param themedContext ContextThemeWrapper with dark theme while expanded
- */
- protected abstract void doTint(Context themedContext);
}