Breaking out search box display logic into a ui model
This commit is contained in:
parent
fe7dc75973
commit
d62cea49e9
@ -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
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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.
|
||||
* <li>
|
||||
* <ul>The user's preference to show either the URL, domain, or page title</ul>
|
||||
* <ul>Whether or not the current page is loading</ul>
|
||||
* <ul>Whether or not the current page is a Lightning generated page.</ul>
|
||||
* </li>
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user