From 5f8470c5778e8ae3c61d00b79da7e7eafc5b416d Mon Sep 17 00:00:00 2001 From: anthony restaino Date: Sat, 22 Apr 2017 20:37:01 -0400 Subject: [PATCH] Using butterknife to bind views in fragments --- .../lightning/dialog/BrowserDialog.java | 8 +-- .../lightning/fragment/BookmarksFragment.java | 61 +++++++++++-------- .../lightning/fragment/TabsFragment.java | 23 ++++--- 3 files changed, 51 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java b/app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java index 7fbf632..9a3f180 100644 --- a/app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java +++ b/app/src/main/java/acr/browser/lightning/dialog/BrowserDialog.java @@ -45,10 +45,6 @@ import acr.browser.lightning.utils.Utils; */ public class BrowserDialog { - public interface Listener { - void onClick(); - } - public interface EditorListener { void onClick(String text); } @@ -58,12 +54,12 @@ public class BrowserDialog { private final int mTitle; private boolean mCondition = true; - public Item(@StringRes int title, boolean condition) { + Item(@StringRes int title, boolean condition) { this(title); mCondition = condition; } - public Item(@StringRes int title) { + protected Item(@StringRes int title) { mTitle = title; } diff --git a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java index 9de7337..9f963c3 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java @@ -56,6 +56,8 @@ import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.view.LightningView; +import butterknife.Bind; +import butterknife.ButterKnife; public class BookmarksFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener, BookmarksView { @@ -90,8 +92,9 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, private final List mBookmarks = new ArrayList<>(); // Views - private ListView mBookmarksListView; - private ImageView mBookmarkTitleImage, mBookmarkImage; + @Bind(R.id.right_drawer_list) ListView mBookmarksListView; + @Bind(R.id.starIcon) ImageView mBookmarkTitleImage; + @Bind(R.id.icon_star) ImageView mBookmarkImage; // Colors private int mIconColor, mScrollIndex; @@ -126,7 +129,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, mWebpageBitmap = ThemeUtils.getThemedBitmap(context, R.drawable.ic_webpage, darkTheme); mFolderBitmap = ThemeUtils.getThemedBitmap(context, R.drawable.ic_folder, darkTheme); mIconColor = darkTheme ? ThemeUtils.getIconDarkThemeColor(context) : - ThemeUtils.getIconLightThemeColor(context); + ThemeUtils.getIconLightThemeColor(context); mFaviconModel = new FaviconModel(); } @@ -173,12 +176,10 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.bookmark_drawer, container, false); - mBookmarksListView = (ListView) view.findViewById(R.id.right_drawer_list); + ButterKnife.bind(this, view); mBookmarksListView.setOnItemClickListener(mItemClickListener); mBookmarksListView.setOnItemLongClickListener(mItemLongClickListener); - mBookmarkTitleImage = (ImageView) view.findViewById(R.id.starIcon); mBookmarkTitleImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); - mBookmarkImage = (ImageView) view.findViewById(R.id.icon_star); final View backView = view.findViewById(R.id.bookmark_back_button); backView.setOnClickListener(new View.OnClickListener() { @Override @@ -195,16 +196,22 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop); initBookmarkManager().subscribeOn(Schedulers.io()) - .observeOn(Schedulers.main()) - .subscribe(new SingleOnSubscribe() { - @Override - public void onItem(@Nullable BookmarkViewAdapter item) { - mBookmarksListView.setAdapter(mBookmarkAdapter); - } - }); + .observeOn(Schedulers.main()) + .subscribe(new SingleOnSubscribe() { + @Override + public void onItem(@Nullable BookmarkViewAdapter item) { + mBookmarksListView.setAdapter(mBookmarkAdapter); + } + }); return view; } + @Override + public void onDestroyView() { + super.onDestroyView(); + ButterKnife.unbind(this); + } + @Override public void onStart() { super.onStart(); @@ -226,7 +233,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, mWebpageBitmap = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_webpage, darkTheme); mFolderBitmap = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, darkTheme); mIconColor = darkTheme ? ThemeUtils.getIconDarkThemeColor(activity) : - ThemeUtils.getIconLightThemeColor(activity); + ThemeUtils.getIconLightThemeColor(activity); mFaviconModel = new FaviconModel(); } @@ -407,20 +414,20 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, final WeakReference imageViewReference = new WeakReference<>(holder.favicon); mFaviconModel.faviconForUrl(url, mWebpageBitmap, true) - .subscribeOn(Schedulers.worker()) - .observeOn(Schedulers.main()) - .subscribe(new SingleOnSubscribe() { - @Override - public void onItem(@Nullable Bitmap item) { - ImageView imageView = imageViewReference.get(); - Object tag = imageView != null ? imageView.getTag() : null; - if (tag != null && tag.equals(url.hashCode())) { - imageView.setImageBitmap(item); - } - - web.setBitmap(item); + .subscribeOn(Schedulers.worker()) + .observeOn(Schedulers.main()) + .subscribe(new SingleOnSubscribe() { + @Override + public void onItem(@Nullable Bitmap item) { + ImageView imageView = imageViewReference.get(); + Object tag = imageView != null ? imageView.getTag() : null; + if (tag != null && tag.equals(url.hashCode())) { + imageView.setImageBitmap(item); } - }); + + web.setBitmap(item); + } + }); } else { holder.favicon.setImageBitmap(web.getBitmap()); } diff --git a/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java b/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java index fdb9799..4877117 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java @@ -48,6 +48,8 @@ import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.Utils; import acr.browser.lightning.view.BackgroundDrawable; import acr.browser.lightning.view.LightningView; +import butterknife.Bind; +import butterknife.ButterKnife; /** * A fragment that holds and manages the tabs and interaction with the tabs. @@ -74,7 +76,8 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View @Nullable private LightningViewAdapter mTabsAdapter; private UIController mUiController; - private RecyclerView mRecyclerView; + + @Bind(R.id.tabs_list) RecyclerView mRecyclerView; private TabsManager mTabsManager; @Inject Bus mBus; @@ -126,7 +129,9 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View } }); } - mRecyclerView = (RecyclerView) view.findViewById(R.id.tabs_list); + + ButterKnife.bind(this, view); + SimpleItemAnimator animator; if (mShowInNavigationDrawer) { animator = new VerticalItemAnimator(); @@ -144,9 +149,17 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View mTabsAdapter = new LightningViewAdapter(mShowInNavigationDrawer); mRecyclerView.setAdapter(mTabsAdapter); mRecyclerView.setHasFixedSize(true); + return view; } + @Override + public void onDestroyView() { + super.onDestroyView(); + ButterKnife.unbind(this); + mTabsAdapter = null; + } + private TabsManager getTabsManager() { if (mTabsManager == null) { mTabsManager = mUiController.getTabModel(); @@ -163,12 +176,6 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View buttonImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); } - @Override - public void onDestroyView() { - super.onDestroyView(); - mTabsAdapter = null; - } - @Override public void onStart() { super.onStart();