From d62cea49e9f999fd1c2d08be87aa643cdb2f54da Mon Sep 17 00:00:00 2001 From: anthony restaino Date: Sun, 18 Jun 2017 11:08:51 -0400 Subject: [PATCH] Breaking out search box display logic into a ui model --- .../lightning/activity/BrowserActivity.java | 37 +++------- .../browser/lightning/app/AppComponent.java | 4 +- .../lightning/browser/BrowserPresenter.java | 2 +- .../lightning/browser/BrowserView.java | 5 +- .../lightning/browser/SearchBoxModel.java | 71 +++++++++++++++++++ .../lightning/controller/UIController.java | 2 +- .../lightning/view/LightningWebClient.java | 4 +- 7 files changed, 90 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/acr/browser/lightning/browser/SearchBoxModel.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 625f0e9..34a4161 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -95,6 +95,7 @@ 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.SearchBoxModel; import acr.browser.lightning.browser.TabsView; import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.DownloadsPage; @@ -189,6 +190,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @Inject LightningDialogBuilder mBookmarksDialogBuilder; + @Inject SearchBoxModel mSearchBoxModel; + private TabsManager mTabsManager; // Image @@ -510,7 +513,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements final LightningView currentView = mTabsManager.getCurrentTab(); if (!hasFocus && currentView != null) { setIsLoading(currentView.getProgress() < 100); - updateUrl(currentView.getUrl(), true); + updateUrl(currentView.getUrl(), false); } else if (hasFocus && currentView != null) { // Hack to make sure the text gets selected @@ -1112,7 +1115,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } @Override - public void showBlockedLocalFileDialog(DialogInterface.OnClickListener listener) { + public void showBlockedLocalFileDialog(@NonNull DialogInterface.OnClickListener listener) { AlertDialog.Builder builder = new AlertDialog.Builder(this); Dialog dialog = builder.setCancelable(true) .setTitle(R.string.title_warning) @@ -1534,36 +1537,16 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } @Override - public void updateUrl(@Nullable String url, boolean shortUrl) { + public void updateUrl(@Nullable String url, boolean isLoading) { if (url == null || mSearch == null || mSearch.hasFocus()) { return; } final LightningView currentTab = mTabsManager.getCurrentTab(); mBookmarksView.handleUpdatedUrl(url); - if (shortUrl && !UrlUtils.isSpecialUrl(url)) { - switch (mPreferences.getUrlBoxContentChoice()) { - case 0: // Default, show only the domain - url = url.replaceFirst(Constants.HTTP, ""); - url = Utils.getDomainName(url); - mSearch.setText(url); - break; - case 1: // URL, show the entire URL - mSearch.setText(url); - break; - case 2: // Title, show the page's title - if (currentTab != null && !currentTab.getTitle().isEmpty()) { - mSearch.setText(currentTab.getTitle()); - } else { - mSearch.setText(mUntitledTitle); - } - break; - } - } else { - if (UrlUtils.isSpecialUrl(url)) { - url = ""; - } - mSearch.setText(url); - } + + String currentTitle = currentTab != null ? currentTab.getTitle() : null; + + mSearch.setText(mSearchBoxModel.getDisplayContent(url, currentTitle, isLoading)); } @Override diff --git a/app/src/main/java/acr/browser/lightning/app/AppComponent.java b/app/src/main/java/acr/browser/lightning/app/AppComponent.java index c47e3f4..4b74766 100644 --- a/app/src/main/java/acr/browser/lightning/app/AppComponent.java +++ b/app/src/main/java/acr/browser/lightning/app/AppComponent.java @@ -8,11 +8,11 @@ import acr.browser.lightning.activity.TabsManager; import acr.browser.lightning.activity.ThemableBrowserActivity; import acr.browser.lightning.activity.ThemableSettingsActivity; import acr.browser.lightning.browser.BrowserPresenter; +import acr.browser.lightning.browser.SearchBoxModel; import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.DownloadsPage; import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.StartPage; -import acr.browser.lightning.database.history.HistoryDatabase; import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.download.DownloadHandler; import acr.browser.lightning.download.LightningDownloadListener; @@ -83,6 +83,6 @@ public interface AppComponent { void inject(DownloadHandler downloadHandler); - HistoryDatabase historyDatabase(); + void inject(SearchBoxModel searchBoxModel); } 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 691d742..c53258c 100644 --- a/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java +++ b/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java @@ -121,7 +121,7 @@ public class BrowserPresenter { mView.updateProgress(newTab.getProgress()); mView.setBackButtonEnabled(newTab.canGoBack()); mView.setForwardButtonEnabled(newTab.canGoForward()); - mView.updateUrl(newTab.getUrl(), true); + mView.updateUrl(newTab.getUrl(), false); mView.setTabView(newTab.getWebView()); int index = mTabsModel.indexOfTab(newTab); if (index >= 0) { diff --git a/app/src/main/java/acr/browser/lightning/browser/BrowserView.java b/app/src/main/java/acr/browser/lightning/browser/BrowserView.java index b701bbb..e301a0c 100644 --- a/app/src/main/java/acr/browser/lightning/browser/BrowserView.java +++ b/app/src/main/java/acr/browser/lightning/browser/BrowserView.java @@ -2,6 +2,7 @@ package acr.browser.lightning.browser; import android.content.DialogInterface; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.view.View; @@ -11,7 +12,7 @@ public interface BrowserView { void removeTabView(); - void updateUrl(String url, boolean shortUrl); + void updateUrl(@Nullable String url, boolean isLoading); void updateProgress(int progress); @@ -21,7 +22,7 @@ public interface BrowserView { void closeActivity(); - void showBlockedLocalFileDialog(DialogInterface.OnClickListener listener); + void showBlockedLocalFileDialog(@NonNull DialogInterface.OnClickListener listener); void showSnackbar(@StringRes int resource); diff --git a/app/src/main/java/acr/browser/lightning/browser/SearchBoxModel.java b/app/src/main/java/acr/browser/lightning/browser/SearchBoxModel.java new file mode 100644 index 0000000..c426677 --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/browser/SearchBoxModel.java @@ -0,0 +1,71 @@ +package acr.browser.lightning.browser; + +import android.app.Application; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.text.TextUtils; + +import javax.inject.Inject; + +import acr.browser.lightning.R; +import acr.browser.lightning.app.BrowserApp; +import acr.browser.lightning.preference.PreferenceManager; +import acr.browser.lightning.utils.UrlUtils; +import acr.browser.lightning.utils.Utils; + +/** + * A UI model for the search box. + */ +public class SearchBoxModel { + + @Inject PreferenceManager mPreferences; + @Inject Application mApplication; + + @NonNull private final String mUntitledTitle; + + @Inject + public SearchBoxModel() { + BrowserApp.getAppComponent().inject(this); + mUntitledTitle = mApplication.getString(R.string.untitled); + } + + /** + * Returns the contents of the search box based on a variety of factors. + *
  • + * + * + * + *
  • + * This method uses the URL, title, and loading information to determine what + * should be displayed by the search box. + * + * @param url the URL of the current page. + * @param title the title of the current page, if known. + * @param isLoading whether the page is currently loading or not. + * @return the string that should be displayed by the search box. + */ + @NonNull + public String getDisplayContent(@NonNull String url, @Nullable String title, boolean isLoading) { + if (UrlUtils.isSpecialUrl(url)) { + return ""; + } else if (isLoading) { + return url; + } else { + switch (mPreferences.getUrlBoxContentChoice()) { + default: + case 0: // Default, show only the domain + String domain = Utils.getDomainName(url); + return domain != null ? domain : url; + case 1: // URL, show the entire URL + return url; + case 2: // Title, show the page's title + if (!TextUtils.isEmpty(title)) { + return title; + } else { + return mUntitledTitle; + } + } + } + } + +} 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 a9021a7..c708585 100644 --- a/app/src/main/java/acr/browser/lightning/controller/UIController.java +++ b/app/src/main/java/acr/browser/lightning/controller/UIController.java @@ -28,7 +28,7 @@ public interface UIController { boolean getUseDarkTheme(); - void updateUrl(@Nullable String title, boolean shortUrl); + void updateUrl(@Nullable String title, boolean isLoading); void updateProgress(int n); diff --git a/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java b/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java index ae959bd..5378019 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java @@ -102,7 +102,7 @@ public class LightningWebClient extends WebViewClient { @Override public void onPageFinished(@NonNull WebView view, String url) { if (view.isShown()) { - mUIController.updateUrl(url, true); + mUIController.updateUrl(url, false); mUIController.setBackButtonEnabled(view.canGoBack()); mUIController.setForwardButtonEnabled(view.canGoForward()); view.postInvalidate(); @@ -123,7 +123,7 @@ public class LightningWebClient extends WebViewClient { public void onPageStarted(WebView view, String url, Bitmap favicon) { mLightningView.getTitleInfo().setFavicon(null); if (mLightningView.isShown()) { - mUIController.updateUrl(url, false); + mUIController.updateUrl(url, true); mUIController.showActionBar(); } mUIController.tabChanged(mLightningView);