From f71ebd26434cdf49ea8809468126ba94c9c0533a Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Fri, 7 Oct 2016 22:06:52 -0400 Subject: [PATCH] Use interface instead of event bus, moving some stuff out of activity and into presenter --- .../lightning/activity/BrowserActivity.java | 169 ++++++------------ .../lightning/browser/BookmarksView.java | 7 + .../lightning/browser/BrowserPresenter.java | 18 ++ .../browser/lightning/bus/BookmarkEvents.java | 8 - .../browser/lightning/bus/BrowserEvents.java | 6 - .../lightning/bus/NavigationEvents.java | 34 ---- .../acr/browser/lightning/bus/TabEvents.java | 19 -- .../lightning/controller/UIController.java | 8 + .../lightning/fragment/BookmarksFragment.java | 42 ++--- .../lightning/fragment/TabsFragment.java | 12 +- 10 files changed, 116 insertions(+), 207 deletions(-) create mode 100644 app/src/main/java/acr/browser/lightning/browser/BookmarksView.java delete mode 100644 app/src/main/java/acr/browser/lightning/bus/NavigationEvents.java diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index fdd4897..2fb39e4 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -61,7 +61,6 @@ import android.view.ViewTreeObserver; import android.view.Window; import android.view.WindowManager; import android.view.animation.Animation; -import android.view.animation.DecelerateInterpolator; import android.view.animation.Transformation; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; @@ -95,12 +94,12 @@ import javax.inject.Inject; import acr.browser.lightning.R; import acr.browser.lightning.app.BrowserApp; +import acr.browser.lightning.browser.BookmarksView; import acr.browser.lightning.browser.BrowserPresenter; import acr.browser.lightning.browser.BrowserView; import acr.browser.lightning.browser.TabsView; import acr.browser.lightning.bus.BookmarkEvents; import acr.browser.lightning.bus.BrowserEvents; -import acr.browser.lightning.bus.NavigationEvents; import acr.browser.lightning.bus.TabEvents; import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.Constants; @@ -208,6 +207,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements private BrowserPresenter mPresenter; private TabsView mTabsView; + private BookmarksView mBookmarksView; // Proxy @Inject ProxyUtils mProxyUtils; @@ -301,6 +301,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements tabsFragment.setArguments(tabsFragmentArguments); final BookmarksFragment bookmarksFragment = new BookmarksFragment(); + mBookmarksView = bookmarksFragment; final Bundle bookmarksFragmentArguments = new Bundle(); bookmarksFragmentArguments.putBoolean(BookmarksFragment.INCOGNITO_MODE, isIncognito()); bookmarksFragment.setArguments(bookmarksFragmentArguments); @@ -338,7 +339,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } updateTabNumber(0); } else { - mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mSwapBookmarksAndTabs ? mDrawerRight : mDrawerLeft); + mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, getTabDrawer()); mArrowImage.setImageResource(R.drawable.ic_action_home); mArrowImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); } @@ -719,8 +720,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements // Handle action buttons switch (item.getItemId()) { case android.R.id.home: - if (mDrawerLayout.isDrawerOpen(mDrawerRight)) { - mDrawerLayout.closeDrawer(mDrawerRight); + if (mDrawerLayout.isDrawerOpen(getBookmarkDrawer())) { + mDrawerLayout.closeDrawer(getBookmarkDrawer()); } return true; case R.id.action_back: @@ -858,17 +859,14 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @Override public void onClick(String text) { if (!TextUtils.isEmpty(text)) { - showSearchInterfaceBar(text); + mPresenter.findInPage(text); + showFindInPageControls(text); } } }); } - private void showSearchInterfaceBar(String text) { - final LightningView currentView = mTabsManager.getCurrentTab(); - if (currentView != null) { - currentView.find(text); - } + private void showFindInPageControls(@NonNull String text) { mSearchBar.setVisibility(View.VISIBLE); TextView tw = (TextView) findViewById(R.id.search_query); @@ -898,7 +896,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements new BrowserDialog.Item(R.string.close_tab) { @Override public void onClick() { - deleteTab(position); + mPresenter.deleteTab(position); } }, new BrowserDialog.Item(R.string.close_other_tabs) { @@ -1060,21 +1058,16 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @Override public void closeEmptyTab() { - final WebView currentWebView = mTabsManager.getCurrentWebView(); - if (currentWebView != null && currentWebView.copyBackForwardList().getSize() == 0) { - closeCurrentTab(); - } - } - - private void closeCurrentTab() { - // don't delete the tab because the browser will close and mess stuff up + // Currently do nothing + // Possibly closing the current tab might close the browser + // and mess stuff up } @Override public void onTrimMemory(int level) { if (level > TRIM_MEMORY_MODERATE && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { Log.d(TAG, "Low Memory, Free Memory"); - mTabsManager.freeMemory(); + mPresenter.onAppLowMemory(); } } @@ -1088,11 +1081,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mPresenter.newTab(null, true); } - // TODO move this to presenter - private synchronized void deleteTab(int position) { - mPresenter.deleteTab(position); - } - void performExitCleanUp() { final LightningView currentTab = mTabsManager.getCurrentTab(); if (mPreferences.getClearCacheExit() && currentTab != null && !isIncognito()) { @@ -1178,7 +1166,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements if (mDrawerLayout.isDrawerOpen(getTabDrawer())) { mDrawerLayout.closeDrawer(getTabDrawer()); } else if (mDrawerLayout.isDrawerOpen(getBookmarkDrawer())) { - mEventBus.post(new BrowserEvents.UserPressedBack()); + mBookmarksView.navigateBack(); } else { if (currentTab != null) { Log.d(TAG, "onBackPressed"); @@ -1194,7 +1182,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements if (mCustomView != null || mCustomViewCallback != null) { onHideCustomView(); } else { - deleteTab(mTabsManager.positionOf(currentTab)); + mPresenter.deleteTab(mTabsManager.positionOf(currentTab)); } } } else { @@ -1491,10 +1479,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements searchTheWeb(url); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getUrl.getWindowToken(), 0); - final LightningView currentTab = mTabsManager.getCurrentTab(); - if (currentTab != null) { - currentTab.requestFocus(); - } + mPresenter.onAutoCompleteItemPressed(); } }); @@ -1763,6 +1748,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } } + @Override + public void closeBookmarksDrawer() { + mDrawerLayout.closeDrawer(getBookmarkDrawer()); + } + @Override public void onHideCustomView() { final LightningView currentTab = mTabsManager.getCurrentTab(); @@ -1837,6 +1827,37 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } } + @Override + public void onBackButtonPressed() { + final LightningView currentTab = mTabsManager.getCurrentTab(); + if (currentTab != null) { + if (currentTab.canGoBack()) { + currentTab.goBack(); + } else { + mPresenter.deleteTab(mTabsManager.positionOf(currentTab)); + } + } + } + + @Override + public void onForwardButtonPressed() { + final LightningView currentTab = mTabsManager.getCurrentTab(); + if (currentTab != null) { + if (currentTab.canGoForward()) { + currentTab.goForward(); + } + } + } + + @Override + public void onHomeButtonPressed() { + final LightningView currentTab = mTabsManager.getCurrentTab(); + if (currentTab != null) { + currentTab.loadHomepage(); + closeDrawers(null); + } + } + /** * This method sets whether or not the activity will display * in full-screen mode (i.e. the ActionBar will be hidden) and @@ -1908,7 +1929,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements */ @Override public void onCloseWindow(LightningView view) { - deleteTab(mTabsManager.positionOf(view)); + mPresenter.deleteTab(mTabsManager.positionOf(view)); } /** @@ -1957,10 +1978,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements height = mToolbarLayout.getMeasuredHeight(); } - final LightningView currentTab = mTabsManager.getCurrentTab(); - if (currentTab == null) - return; - final int totalHeight = height; if (mToolbarLayout.getTranslationY() < -(height - 0.01f)) { Animation show = new Animation() { @@ -2135,7 +2152,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mDrawerHandler.postDelayed(new Runnable() { @Override public void run() { - mDrawerLayout.closeDrawer(mDrawerRight); + closeDrawers(null); } }, 150); } @@ -2227,19 +2244,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } } - /** - * The {@link acr.browser.lightning.fragment.BookmarksFragment} send this message on reply - * to {@link acr.browser.lightning.bus.BrowserEvents.UserPressedBack} message if the - * fragement is showing the boomarks root folder. - * - * @param event an event notifying the browser that the bookmark drawer - * should be closed. - */ - @Subscribe - public void closeBookmarks(final BookmarkEvents.CloseBookmarks event) { - mDrawerLayout.closeDrawer(mDrawerRight); - } - /** * The user wants to close a tab * @@ -2247,7 +2251,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements */ @Subscribe public void closeTab(final TabEvents.CloseTab event) { - deleteTab(event.position); + mPresenter.deleteTab(event.position); } /** @@ -2260,67 +2264,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements BrowserActivity.this.showTab(event.position); } - /** - * The user long pressed on a tab, ask him if he want to close the tab - * - * @param event contains the tab position in the tabs adapter - */ - @Subscribe - public void showCloseDialog(final TabEvents.ShowCloseDialog event) { - BrowserActivity.this.showCloseDialog(event.position); - } - - /** - * The user wants to create a new tab - * - * @param event a marker - */ - @Subscribe - public void newTab(final TabEvents.NewTab event) { - BrowserActivity.this.newTab(null, true); - } - - /** - * The user wants to go back on current tab - * - * @param event a marker - */ - @Subscribe - public void goBack(final NavigationEvents.GoBack event) { - final LightningView currentTab = mTabsManager.getCurrentTab(); - if (currentTab != null) { - if (currentTab.canGoBack()) { - currentTab.goBack(); - } else { - deleteTab(mTabsManager.positionOf(currentTab)); - } - } - } - - /** - * The user wants to go forward on current tab - * - * @param event a marker - */ - @Subscribe - public void goForward(final NavigationEvents.GoForward event) { - final LightningView currentTab = mTabsManager.getCurrentTab(); - if (currentTab != null) { - if (currentTab.canGoForward()) { - currentTab.goForward(); - } - } - } - - @Subscribe - public void goHome(final NavigationEvents.GoHome event) { - final LightningView currentTab = mTabsManager.getCurrentTab(); - if (currentTab != null) { - currentTab.loadHomepage(); - closeDrawers(null); - } - } - /** * The user long pressed the new tab button * diff --git a/app/src/main/java/acr/browser/lightning/browser/BookmarksView.java b/app/src/main/java/acr/browser/lightning/browser/BookmarksView.java new file mode 100644 index 0000000..22b7a85 --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/browser/BookmarksView.java @@ -0,0 +1,7 @@ +package acr.browser.lightning.browser; + +public interface BookmarksView { + + void navigateBack(); + +} diff --git a/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java b/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java index 689cd46..2c19fbf 100644 --- a/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java +++ b/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java @@ -341,4 +341,22 @@ public class BrowserPresenter { return true; } + public void onAutoCompleteItemPressed() { + final LightningView currentTab = mTabsModel.getCurrentTab(); + if (currentTab != null) { + currentTab.requestFocus(); + } + } + + public void findInPage(@NonNull String query) { + final LightningView currentView = mTabsModel.getCurrentTab(); + if (currentView != null) { + currentView.find(query); + } + } + + public void onAppLowMemory() { + mTabsModel.freeMemory(); + } + } diff --git a/app/src/main/java/acr/browser/lightning/bus/BookmarkEvents.java b/app/src/main/java/acr/browser/lightning/bus/BookmarkEvents.java index 20c10e5..2bf69ce 100644 --- a/app/src/main/java/acr/browser/lightning/bus/BookmarkEvents.java +++ b/app/src/main/java/acr/browser/lightning/bus/BookmarkEvents.java @@ -25,14 +25,6 @@ public final class BookmarkEvents { public static class ToggleBookmarkForCurrentPage { } - /** - * Sended by the {@link acr.browser.lightning.fragment.BookmarksFragment} when it wants to close - * itself (generally in reply to a {@link acr.browser.lightning.bus.BrowserEvents.UserPressedBack} - * event. - */ - public static class CloseBookmarks { - } - /** * Sended when a bookmark is edited */ diff --git a/app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java b/app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java index 6adc03d..813f557 100644 --- a/app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java +++ b/app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java @@ -34,12 +34,6 @@ public final class BrowserEvents { } } - /** - * Notify the BookmarksFragment and TabsFragment that the user pressed the back button - */ - public static class UserPressedBack { - } - /** * */ diff --git a/app/src/main/java/acr/browser/lightning/bus/NavigationEvents.java b/app/src/main/java/acr/browser/lightning/bus/NavigationEvents.java deleted file mode 100644 index 5f7519f..0000000 --- a/app/src/main/java/acr/browser/lightning/bus/NavigationEvents.java +++ /dev/null @@ -1,34 +0,0 @@ -package acr.browser.lightning.bus; - -/** - * Collections of navigation events, like go back or go forward - * - * @author Stefano Pacifici - * @date 2015/09/15 - */ -public class NavigationEvents { - private NavigationEvents() { - // No instances please - } - - /** - * Fired by {@link acr.browser.lightning.fragment.TabsFragment} when the user presses back - * button. - */ - public static class GoBack { - } - - /** - * Fired by {@link acr.browser.lightning.fragment.TabsFragment} when the user presses forward - * button. - */ - public static class GoForward { - } - - /** - * Fired by {@link acr.browser.lightning.fragment.TabsFragment} when the user presses the home - * button. - */ - public static class GoHome { - } -} diff --git a/app/src/main/java/acr/browser/lightning/bus/TabEvents.java b/app/src/main/java/acr/browser/lightning/bus/TabEvents.java index a2bac5c..49de1ec 100644 --- a/app/src/main/java/acr/browser/lightning/bus/TabEvents.java +++ b/app/src/main/java/acr/browser/lightning/bus/TabEvents.java @@ -37,25 +37,6 @@ public final class TabEvents { } } - /** - * Sended by {@link acr.browser.lightning.fragment.TabsFragment} when the user long press on the - * tab itself. - */ - public static class ShowCloseDialog { - public final int position; - - public ShowCloseDialog(int position) { - this.position = position; - } - } - - /** - * Sended by {@link acr.browser.lightning.fragment.TabsFragment} when the user want to create a - * new tab. - */ - public static class NewTab { - } - /** * Sended by {@link acr.browser.lightning.fragment.TabsFragment} when the user long presses on * new tab button. diff --git a/app/src/main/java/acr/browser/lightning/controller/UIController.java b/app/src/main/java/acr/browser/lightning/controller/UIController.java index e28ca13..5ac1c7f 100644 --- a/app/src/main/java/acr/browser/lightning/controller/UIController.java +++ b/app/src/main/java/acr/browser/lightning/controller/UIController.java @@ -56,6 +56,8 @@ public interface UIController { void newTabClicked(); + void closeBookmarksDrawer(); + void setForwardButtonEnabled(boolean enabled); void setBackButtonEnabled(boolean enabled); @@ -64,4 +66,10 @@ public interface UIController { TabsManager getTabModel(); + void onBackButtonPressed(); + + void onForwardButtonPressed(); + + void onHomeButtonPressed(); + } 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 79e641e..3f5be3a 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java @@ -27,6 +27,11 @@ import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; +import com.anthonycr.bonsai.Action; +import com.anthonycr.bonsai.Observable; +import com.anthonycr.bonsai.OnSubscribe; +import com.anthonycr.bonsai.Schedulers; +import com.anthonycr.bonsai.Subscriber; import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; @@ -40,6 +45,8 @@ import acr.browser.lightning.activity.ReadingActivity; import acr.browser.lightning.activity.TabsManager; import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.async.AsyncExecutor; +import acr.browser.lightning.async.ImageDownloadTask; +import acr.browser.lightning.browser.BookmarksView; import acr.browser.lightning.bus.BookmarkEvents; import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.constant.Constants; @@ -48,18 +55,10 @@ import acr.browser.lightning.database.BookmarkManager; import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.preference.PreferenceManager; -import acr.browser.lightning.async.ImageDownloadTask; - -import com.anthonycr.bonsai.Action; -import com.anthonycr.bonsai.Observable; -import com.anthonycr.bonsai.OnSubscribe; -import com.anthonycr.bonsai.Schedulers; -import com.anthonycr.bonsai.Subscriber; - import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.view.LightningView; -public class BookmarksFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener { +public class BookmarksFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener, BookmarksView { private final static String TAG = BookmarksFragment.class.getSimpleName(); @@ -78,6 +77,8 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, private TabsManager mTabsManager; + private UIController mUiController; + // Adapter private BookmarkViewAdapter mBookmarkAdapter; @@ -117,7 +118,8 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, BrowserApp.getAppComponent().inject(this); final Bundle arguments = getArguments(); final Context context = getContext(); - mTabsManager = ((UIController) context).getTabModel(); + mUiController = (UIController) context; + mTabsManager = mUiController.getTabModel(); mIsIncognito = arguments.getBoolean(INCOGNITO_MODE, false); boolean darkTheme = mPreferenceManager.getUseTheme() != 0 || mIsIncognito; mWebpageBitmap = ThemeUtils.getThemedBitmap(context, R.drawable.ic_webpage, darkTheme); @@ -247,16 +249,6 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, } } - @Subscribe - public void userPressedBack(final BrowserEvents.UserPressedBack event) { - if (mBookmarkManager.isRootFolder()) { - mEventBus.post(new BookmarkEvents.CloseBookmarks()); - } else { - setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true); - mBookmarksListView.setSelection(mScrollIndex); - } - } - @Subscribe public void bookmarkDeleted(@NonNull final BookmarkEvents.Deleted event) { mBookmarks.remove(event.item); @@ -365,6 +357,16 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, return false; } + @Override + public void navigateBack() { + if (mBookmarkManager.isRootFolder()) { + mUiController.closeBookmarksDrawer(); + } else { + setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true); + mBookmarksListView.setSelection(mScrollIndex); + } + } + private class BookmarkViewAdapter extends ArrayAdapter { final Context context; 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 cf716fc..c45859d 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java @@ -39,7 +39,6 @@ import acr.browser.lightning.R; import acr.browser.lightning.activity.TabsManager; import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.browser.TabsView; -import acr.browser.lightning.bus.NavigationEvents; import acr.browser.lightning.bus.TabEvents; import acr.browser.lightning.controller.UIController; import acr.browser.lightning.fragment.anim.HorizontalItemAnimator; @@ -215,16 +214,16 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View mUiController.showCloseDialog(mTabsManager.indexOfCurrentTab()); break; case R.id.new_tab_button: - mBus.post(new TabEvents.NewTab()); + mUiController.newTabClicked(); break; case R.id.action_back: - mBus.post(new NavigationEvents.GoBack()); + mUiController.onBackButtonPressed(); break; case R.id.action_forward: - mBus.post(new NavigationEvents.GoForward()); + mUiController.onForwardButtonPressed(); break; case R.id.action_home: - mBus.post(new NavigationEvents.GoHome()); + mUiController.onHomeButtonPressed(); default: break; } @@ -416,8 +415,7 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View @Override public boolean onLongClick(View v) { - // Show close dialog - mBus.post(new TabEvents.ShowCloseDialog(getAdapterPosition())); + mUiController.showCloseDialog(getAdapterPosition()); return true; } }