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.
This commit is contained in:
flofriday 2024-06-22 13:38:18 +02:00 committed by GitHub
parent 14cf3cb1fc
commit da5972a3fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 45 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);
}