Browse Source

get rid of listener between tab manager and presenter. invert the dependency between them.

master
Anthony Restaino 9 years ago
parent
commit
ba3edc00e8
  1. 4
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 53
      app/src/main/java/acr/browser/lightning/activity/TabsManager.java
  3. 40
      app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java

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

@ -948,7 +948,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
*/ */
// TODO move to presenter // TODO move to presenter
private synchronized void showTab(final int position) { private synchronized void showTab(final int position) {
mTabsManager.switchToTab(position); mPresenter.tabChanged(position);
} }
private static void removeViewFromParent(@Nullable View view) { private static void removeViewFromParent(@Nullable View view) {
@ -1107,6 +1107,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
protected void onDestroy() { protected void onDestroy() {
Log.d(Constants.TAG, "onDestroy"); Log.d(Constants.TAG, "onDestroy");
mPresenter.shutdown();
if (mHistoryDatabase != null) { if (mHistoryDatabase != null) {
mHistoryDatabase.close(); mHistoryDatabase.close();
mHistoryDatabase = null; mHistoryDatabase = null;

53
app/src/main/java/acr/browser/lightning/activity/TabsManager.java

@ -45,28 +45,9 @@ public class TabsManager {
@Inject Bus mEventBus; @Inject Bus mEventBus;
@Inject Application mApp; @Inject Application mApp;
@Nullable private TabChangeListener mListener;
public interface TabChangeListener {
void tabChanged(LightningView newTab);
}
@Inject @Inject
public TabsManager() {} public TabsManager() {}
/**
* Sets the {@link TabChangeListener} to a new
* listener. Can be set to null if the caller
* wishes to unregister the listener.
*
* @param listener the new listener to use or
* null if the caller wishes to
* unregister the listener.
*/
public void setTabChangeListener(@Nullable TabChangeListener listener) {
mListener = listener;
}
/** /**
* Restores old tabs that were open before the browser * Restores old tabs that were open before the browser
* was closed. Handles the intent used to open the browser. * was closed. Handles the intent used to open the browser.
@ -156,7 +137,6 @@ public class TabsManager {
* released for garbage collection. * released for garbage collection.
*/ */
public synchronized void shutdown() { public synchronized void shutdown() {
sendTabChangedEvent(null);
for (LightningView tab : mTabList) { for (LightningView tab : mTabList) {
tab.onDestroy(); tab.onDestroy();
} }
@ -247,7 +227,7 @@ public class TabsManager {
* *
* @param position the position of the tab to delete. * @param position the position of the tab to delete.
*/ */
public synchronized void deleteTab(int position) { public synchronized boolean deleteTab(int position) {
Log.d(TAG, "Delete tab: " + position); Log.d(TAG, "Delete tab: " + position);
final LightningView currentTab = getCurrentTab(); final LightningView currentTab = getCurrentTab();
int current = positionOf(currentTab); int current = positionOf(currentTab);
@ -261,10 +241,11 @@ public class TabsManager {
} else { } else {
mCurrentTab = getTabAtPosition(current - 1); mCurrentTab = getTabAtPosition(current - 1);
} }
sendTabChangedEvent(mCurrentTab);
removeTab(current); removeTab(current);
return true;
} else { } else {
removeTab(position); removeTab(position);
return false;
} }
} }
@ -333,6 +314,16 @@ public class TabsManager {
return mCurrentTab != null ? mCurrentTab.getWebView() : null; return mCurrentTab != null ? mCurrentTab.getWebView() : null;
} }
/**
* Returns the index of the current tab.
*
* @return Return the index of the current tab, or -1 if the
* current tab is null.
*/
public int indexOfCurrentTab() {
return mTabList.indexOf(mCurrentTab);
}
/** /**
* Return the current {@link LightningView} or null if * Return the current {@link LightningView} or null if
* no current tab has been set. * no current tab has been set.
@ -356,32 +347,14 @@ public class TabsManager {
Log.d(TAG, "switch to tab: " + position); Log.d(TAG, "switch to tab: " + position);
if (position < 0 || position >= mTabList.size()) { if (position < 0 || position >= mTabList.size()) {
Log.e(TAG, "Returning a null LightningView requested for position: " + position); Log.e(TAG, "Returning a null LightningView requested for position: " + position);
sendTabChangedEvent(null);
return null; return null;
} else { } else {
final LightningView tab = mTabList.get(position); final LightningView tab = mTabList.get(position);
if (tab != null) { if (tab != null) {
mCurrentTab = tab; mCurrentTab = tab;
} }
sendTabChangedEvent(tab);
return tab; return tab;
} }
} }
/**
* Send the event that the current tab has changed to
* a new tab to the {@link TabChangeListener} if it is
* not null.
*
* @param newView the new tab that we have switcted to,
* can be null if the last tab has been
* deleted.
*/
private void sendTabChangedEvent(@Nullable LightningView newView) {
Log.d(TAG, "NULL: " + (mListener == null));
if (mListener != null) {
mListener.tabChanged(newView);
}
}
} }

40
app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java

@ -13,7 +13,6 @@ import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.activity.TabsManager; import acr.browser.lightning.activity.TabsManager;
import acr.browser.lightning.activity.TabsManager.TabChangeListener;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
@ -40,25 +39,10 @@ public class BrowserPresenter {
private boolean mIsIncognito; private boolean mIsIncognito;
private boolean mIsNewIntent; private boolean mIsNewIntent;
private static class TabListener implements TabChangeListener {
private BrowserPresenter mPresenter;
public TabListener(@NonNull BrowserPresenter presenter) {
mPresenter = presenter;
}
@Override
public void tabChanged(@Nullable LightningView newTab) {
mPresenter.onTabChanged(newTab);
}
}
public BrowserPresenter(@NonNull BrowserView view, boolean isIncognito) { public BrowserPresenter(@NonNull BrowserView view, boolean isIncognito) {
BrowserApp.getAppComponent().inject(this); BrowserApp.getAppComponent().inject(this);
mView = view; mView = view;
mIsIncognito = isIncognito; mIsIncognito = isIncognito;
mTabsModel.setTabChangeListener(new TabListener(this));
} }
private void onTabChanged(@Nullable LightningView newTab) { private void onTabChanged(@Nullable LightningView newTab) {
@ -115,7 +99,10 @@ public class BrowserPresenter {
if (isShown) { if (isShown) {
mView.removeTabView(); mView.removeTabView();
} }
mTabsModel.deleteTab(position); boolean currentDeleted = mTabsModel.deleteTab(position);
if (currentDeleted) {
tabChanged(mTabsModel.indexOfCurrentTab());
}
} }
final LightningView afterTab = mTabsModel.getCurrentTab(); final LightningView afterTab = mTabsModel.getCurrentTab();
if (afterTab == null) { if (afterTab == null) {
@ -178,6 +165,18 @@ public class BrowserPresenter {
currentTab.loadUrl(url); currentTab.loadUrl(url);
} }
public void shutdown() {
onTabChanged(null);
}
public void tabChanged(int position) {
if (position < 0) {
return;
}
LightningView tab = mTabsModel.switchToTab(position);
onTabChanged(tab);
}
public synchronized boolean newTab(String url, boolean show) { public synchronized boolean newTab(String url, boolean show) {
// Limit number of tabs for limited version of app // Limit number of tabs for limited version of app
if (!Constants.FULL_VERSION && mTabsModel.size() >= 10) { if (!Constants.FULL_VERSION && mTabsModel.size() >= 10) {
@ -191,7 +190,8 @@ public class BrowserPresenter {
} }
if (show) { if (show) {
mTabsModel.switchToTab(mTabsModel.size() - 1); LightningView tab = mTabsModel.switchToTab(mTabsModel.size() - 1);
onTabChanged(tab);
} }
// TODO Restore this // TODO Restore this
@ -205,8 +205,4 @@ public class BrowserPresenter {
return true; return true;
} }
public void destroy() {
mTabsModel.setTabChangeListener(null);
}
} }

Loading…
Cancel
Save