Browse Source

pulling out event bus in more places

master
Anthony Restaino 8 years ago
parent
commit
df7fd47235
  1. 2
      app/build.gradle
  2. 162
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  3. 4
      app/src/main/java/acr/browser/lightning/browser/BookmarksView.java
  4. 6
      app/src/main/java/acr/browser/lightning/bus/BookmarkEvents.java
  5. 40
      app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java
  6. 46
      app/src/main/java/acr/browser/lightning/bus/TabEvents.java
  7. 13
      app/src/main/java/acr/browser/lightning/controller/UIController.java
  8. 25
      app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java
  9. 11
      app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java

2
app/build.gradle

@ -4,7 +4,7 @@ apply plugin: 'com.getkeepsafe.dexcount' @@ -4,7 +4,7 @@ apply plugin: 'com.getkeepsafe.dexcount'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
buildToolsVersion "24.0.3"
defaultConfig {
minSdkVersion 14

162
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -100,7 +100,6 @@ import acr.browser.lightning.browser.BrowserView; @@ -100,7 +100,6 @@ 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.TabEvents;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
@ -398,7 +397,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -398,7 +397,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}
mPresenter.setupTabs(intent);
setIntent(null);
mProxyUtils.checkForProxy(BrowserActivity.this);
mProxyUtils.checkForProxy(this);
}
}
@ -801,7 +800,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -801,7 +800,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
: null;
if (item != null && mBookmarkManager.addBookmark(item)) {
mSuggestionsAdapter.refreshBookmarks();
mEventBus.post(new BrowserEvents.BookmarkAdded(title, url));
mBookmarksView.handleUpdatedUrl(url);
}
}
@ -811,7 +810,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -811,7 +810,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
: null;
if (item != null && mBookmarkManager.deleteBookmark(item)) {
mSuggestionsAdapter.refreshBookmarks();
mEventBus.post(new BrowserEvents.CurrentPageUrl(url));
mBookmarksView.handleUpdatedUrl(url);
}
}
@ -1031,6 +1030,63 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1031,6 +1030,63 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
Utils.showSnackbar(this, resource);
}
@Override
public void tabCloseClicked(int position) {
mPresenter.deleteTab(position);
}
@Override
public void tabClicked(int position) {
showTab(position);
}
@Override
public void newTabButtonClicked() {
mPresenter.newTab(null, true);
}
@Override
public void newTabButtonLongClicked() {
String url = mPreferences.getSavedUrl();
if (url != null) {
newTab(url, true);
Utils.showSnackbar(this, R.string.deleted_tab);
}
mPreferences.setSavedUrl(null);
}
@Override
public void bookmarkButtonClicked() {
final LightningView currentTab = mTabsManager.getCurrentTab();
final String url = currentTab != null ? currentTab.getUrl() : null;
final String title = currentTab != null ? currentTab.getTitle() : null;
if (url == null) {
return;
}
if (!UrlUtils.isSpecialUrl(url)) {
if (!mBookmarkManager.isBookmark(url)) {
addBookmark(title, url);
} else {
deleteBookmark(title, url);
}
}
}
@Override
public void bookmarkItemClicked(@NonNull HistoryItem item) {
mPresenter.loadUrlInCurrentView(item.getUrl());
// keep any jank from happening when the drawer is closed after the
// URL starts to load
mDrawerHandler.postDelayed(new Runnable() {
@Override
public void run() {
closeDrawers(null);
}
}, 150);
}
/**
* displays the WebView contained in the LightningView Also handles the
* removal of previous views
@ -1076,11 +1132,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1076,11 +1132,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
return mPresenter.newTab(url, show);
}
@Override
public void newTabClicked() {
mPresenter.newTab(null, true);
}
void performExitCleanUp() {
final LightningView currentTab = mTabsManager.getCurrentTab();
if (mPreferences.getClearCacheExit() && currentTab != null && !isIncognito()) {
@ -1385,7 +1436,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1385,7 +1436,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
return;
}
final LightningView currentTab = mTabsManager.getCurrentTab();
mEventBus.post(new BrowserEvents.CurrentPageUrl(url));
mBookmarksView.handleUpdatedUrl(url);
if (shortUrl && !UrlUtils.isSpecialUrl(url)) {
switch (mPreferences.getUrlBoxContentChoice()) {
case 0: // Default, show only the domain
@ -1466,7 +1517,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1466,7 +1517,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
if (urlString != null) {
url = urlString.toString();
}
if (url == null || url.startsWith(BrowserActivity.this.getString(R.string.suggestion))) {
if (url == null || url.startsWith(getString(R.string.suggestion))) {
CharSequence searchString = ((TextView) view.findViewById(R.id.title)).getText();
if (searchString != null) {
url = searchString.toString();
@ -2137,26 +2188,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2137,26 +2188,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private final Object mBusEventListener = new Object() {
/**
* Load the given url in the current tab, used by the the
* {@link acr.browser.lightning.fragment.BookmarksFragment} and by the
* {@link LightningDialogBuilder}
*
* @param event Bus event indicating that the user has clicked a bookmark
*/
@Subscribe
public void loadUrlInCurrentTab(final BrowserEvents.OpenUrlInCurrentTab event) {
mPresenter.loadUrlInCurrentView(event.url);
// keep any jank from happening when the drawer is closed after the
// URL starts to load
mDrawerHandler.postDelayed(new Runnable() {
@Override
public void run() {
closeDrawers(null);
}
}, 150);
}
@Subscribe
public void loadHistory(final BrowserEvents.OpenHistoryInCurrentTab event) {
new HistoryPage(mTabsManager.getCurrentTab(), getApplication(), mHistoryDatabase).load();
@ -2174,9 +2205,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2174,9 +2205,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
public void loadUrlInNewTab(final BrowserEvents.OpenUrlInNewTab event) {
mDrawerLayout.closeDrawers();
if (event.location == BrowserEvents.OpenUrlInNewTab.Location.NEW_TAB) {
BrowserActivity.this.newTab(event.url, true);
newTab(event.url, true);
} else if (event.location == BrowserEvents.OpenUrlInNewTab.Location.BACKGROUND) {
BrowserActivity.this.newTab(event.url, false);
newTab(event.url, false);
} else if (event.location == BrowserEvents.OpenUrlInNewTab.Location.INCOGNITO) {
Intent intent = new Intent(BrowserActivity.this, IncognitoActivity.class);
intent.setData(Uri.parse(event.url));
@ -2185,31 +2216,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2185,31 +2216,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}
}
/**
* When receive a {@link BookmarkEvents.ToggleBookmarkForCurrentPage}
* message this receiver answer firing the
* {@link BrowserEvents.BookmarkAdded} message
*
* @param event an event that the user wishes to bookmark the current page
*/
@Subscribe
public void bookmarkCurrentPage(final BookmarkEvents.ToggleBookmarkForCurrentPage event) {
final LightningView currentTab = mTabsManager.getCurrentTab();
final String url = currentTab != null ? currentTab.getUrl() : null;
final String title = currentTab != null ? currentTab.getTitle() : null;
if (url == null) {
return;
}
if (!UrlUtils.isSpecialUrl(url)) {
if (!mBookmarkManager.isBookmark(url)) {
addBookmark(title, url);
} else {
deleteBookmark(title, url);
}
}
}
/**
* This method is called when the user edits a bookmark.
*
@ -2223,7 +2229,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2223,7 +2229,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
currentTab.loadBookmarkpage();
}
if (currentTab != null) {
mEventBus.post(new BrowserEvents.CurrentPageUrl(currentTab.getUrl()));
mBookmarksView.handleUpdatedUrl(currentTab.getUrl());
}
}
@ -2240,44 +2246,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -2240,44 +2246,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
currentTab.loadBookmarkpage();
}
if (currentTab != null) {
mEventBus.post(new BrowserEvents.CurrentPageUrl(currentTab.getUrl()));
}
}
/**
* The user wants to close a tab
*
* @param event contains the position inside the tabs adapter
*/
@Subscribe
public void closeTab(final TabEvents.CloseTab event) {
mPresenter.deleteTab(event.position);
mBookmarksView.handleUpdatedUrl(currentTab.getUrl());
}
/**
* The user clicked on a tab, let's show it
*
* @param event contains the tab position in the tabs adapter
*/
@Subscribe
public void showTab(final TabEvents.ShowTab event) {
BrowserActivity.this.showTab(event.position);
}
/**
* The user long pressed the new tab button
*
* @param event a marker
*/
@Subscribe
public void newTabLongPress(final TabEvents.NewTabLongPress event) {
String url = mPreferences.getSavedUrl();
if (url != null) {
BrowserActivity.this.newTab(url, true);
Utils.showSnackbar(BrowserActivity.this, R.string.deleted_tab);
}
mPreferences.setSavedUrl(null);
}
@Subscribe

4
app/src/main/java/acr/browser/lightning/browser/BookmarksView.java

@ -1,7 +1,11 @@ @@ -1,7 +1,11 @@
package acr.browser.lightning.browser;
import android.support.annotation.NonNull;
public interface BookmarksView {
void navigateBack();
void handleUpdatedUrl(@NonNull String url);
}

6
app/src/main/java/acr/browser/lightning/bus/BookmarkEvents.java

@ -19,12 +19,6 @@ public final class BookmarkEvents { @@ -19,12 +19,6 @@ public final class BookmarkEvents {
}
}
/**
* The user ask to add/del a bookmark to the currently displayed page
*/
public static class ToggleBookmarkForCurrentPage {
}
/**
* Sended when a bookmark is edited
*/

40
app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java

@ -9,35 +9,6 @@ public final class BrowserEvents { @@ -9,35 +9,6 @@ public final class BrowserEvents {
// No instances
}
/**
* The {@link acr.browser.lightning.activity.BrowserActivity} signal a new bookmark was added
* (mainly to the {@link acr.browser.lightning.fragment.BookmarksFragment}).
*/
public static class BookmarkAdded {
public final String title, url;
public BookmarkAdded(final String title, final String url) {
this.title = title;
this.url = url;
}
}
/**
* Notify the current page has a new url. This is generally used to update the
* {@link acr.browser.lightning.fragment.BookmarksFragment} interface.
*/
public static class CurrentPageUrl {
public final String url;
public CurrentPageUrl(final String url) {
this.url = url;
}
}
/**
*
*/
/**
* Notify the Browser to display a SnackBar in the main activity
*/
@ -60,17 +31,6 @@ public final class BrowserEvents { @@ -60,17 +31,6 @@ public final class BrowserEvents {
public final static class OpenHistoryInCurrentTab {
}
/**
* The user want to open the given url in the current tab
*/
public final static class OpenUrlInCurrentTab {
public final String url;
public OpenUrlInCurrentTab(final String url) {
this.url = url;
}
}
/**
* The user ask to open the given url as new tab
*/

46
app/src/main/java/acr/browser/lightning/bus/TabEvents.java

@ -1,46 +0,0 @@ @@ -1,46 +0,0 @@
package acr.browser.lightning.bus;
/**
* A collection of events been sent by {@link acr.browser.lightning.fragment.TabsFragment}
*
* @author Stefano Pacifici
* @date 2015/09/14
*/
public final class TabEvents {
private TabEvents() {
// No instances
}
/**
* Sended by {@link acr.browser.lightning.fragment.TabsFragment} when the user click on the
* tab exit button
*/
public static class CloseTab {
public final int position;
public CloseTab(int position) {
this.position = position;
}
}
/**
* Sended by {@link acr.browser.lightning.fragment.TabsFragment} when the user click on the
* tab itself.
*/
public static class ShowTab {
public final int position;
public ShowTab(int position) {
this.position = position;
}
}
/**
* Sended by {@link acr.browser.lightning.fragment.TabsFragment} when the user long presses on
* new tab button.
*/
public static class NewTabLongPress {
}
}

13
app/src/main/java/acr/browser/lightning/controller/UIController.java

@ -15,6 +15,7 @@ import android.webkit.ValueCallback; @@ -15,6 +15,7 @@ import android.webkit.ValueCallback;
import android.webkit.WebChromeClient.CustomViewCallback;
import acr.browser.lightning.activity.TabsManager;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.view.LightningView;
public interface UIController {
@ -54,7 +55,17 @@ public interface UIController { @@ -54,7 +55,17 @@ public interface UIController {
void showCloseDialog(int position);
void newTabClicked();
void newTabButtonClicked();
void tabCloseClicked(int position);
void tabClicked(int position);
void newTabButtonLongClicked();
void bookmarkButtonClicked();
void bookmarkItemClicked(@NonNull HistoryItem item);
void closeBookmarksDrawer();

25
app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java

@ -137,7 +137,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -137,7 +137,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
mScrollIndex = mBookmarksListView.getFirstVisiblePosition();
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(item.getTitle(), true), true);
} else {
mEventBus.post(new BrowserEvents.OpenUrlInCurrentTab(item.getUrl()));
mUiController.bookmarkItemClicked(item);
}
}
};
@ -219,20 +219,6 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -219,20 +219,6 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
ThemeUtils.getIconLightThemeColor(activity);
}
@Subscribe
public void addBookmark(@NonNull final BrowserEvents.BookmarkAdded event) {
updateBookmarkIndicator(event.url);
String folder = mBookmarkManager.getCurrentFolder();
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(folder, true), false);
}
@Subscribe
public void currentPageInfo(@NonNull final BrowserEvents.CurrentPageUrl event) {
updateBookmarkIndicator(event.url);
String folder = mBookmarkManager.getCurrentFolder();
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(folder, true), false);
}
@Subscribe
public void bookmarkChanged(BookmarkEvents.BookmarkChanged event) {
String folder = mBookmarkManager.getCurrentFolder();
@ -329,7 +315,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -329,7 +315,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
public void onClick(@NonNull View v) {
switch (v.getId()) {
case R.id.action_add_bookmark:
mEventBus.post(new BookmarkEvents.ToggleBookmarkForCurrentPage());
mUiController.bookmarkButtonClicked();
break;
case R.id.action_reading:
LightningView currentTab = mTabsManager.getCurrentTab();
@ -367,6 +353,13 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, @@ -367,6 +353,13 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
}
}
@Override
public void handleUpdatedUrl(@NonNull String url) {
updateBookmarkIndicator(url);
String folder = mBookmarkManager.getCurrentFolder();
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(folder, true), false);
}
private class BookmarkViewAdapter extends ArrayAdapter<HistoryItem> {
final Context context;

11
app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java

@ -39,7 +39,6 @@ import acr.browser.lightning.R; @@ -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.TabEvents;
import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.fragment.anim.HorizontalItemAnimator;
import acr.browser.lightning.fragment.anim.VerticalItemAnimator;
@ -123,7 +122,7 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View @@ -123,7 +122,7 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View
newTab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUiController.newTabClicked();
mUiController.newTabButtonClicked();
}
});
}
@ -214,7 +213,7 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View @@ -214,7 +213,7 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View
mUiController.showCloseDialog(mTabsManager.indexOfCurrentTab());
break;
case R.id.new_tab_button:
mUiController.newTabClicked();
mUiController.newTabButtonClicked();
break;
case R.id.action_back:
mUiController.onBackButtonPressed();
@ -233,7 +232,7 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View @@ -233,7 +232,7 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View
public boolean onLongClick(@NonNull View v) {
switch (v.getId()) {
case R.id.action_new_tab:
mBus.post(new TabEvents.NewTabLongPress());
mUiController.newTabButtonLongClicked();
break;
default:
break;
@ -406,10 +405,10 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View @@ -406,10 +405,10 @@ public class TabsFragment extends Fragment implements View.OnClickListener, View
@Override
public void onClick(View v) {
if (v == exitButton) {
mBus.post(new TabEvents.CloseTab(getAdapterPosition()));
mUiController.tabCloseClicked(getAdapterPosition());
}
if (v == layout) {
mBus.post(new TabEvents.ShowTab(getAdapterPosition()));
mUiController.tabClicked(getAdapterPosition());
}
}

Loading…
Cancel
Save