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

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

@ -45,28 +45,9 @@ public class TabsManager { @@ -45,28 +45,9 @@ public class TabsManager {
@Inject Bus mEventBus;
@Inject Application mApp;
@Nullable private TabChangeListener mListener;
public interface TabChangeListener {
void tabChanged(LightningView newTab);
}
@Inject
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
* was closed. Handles the intent used to open the browser.
@ -156,7 +137,6 @@ public class TabsManager { @@ -156,7 +137,6 @@ public class TabsManager {
* released for garbage collection.
*/
public synchronized void shutdown() {
sendTabChangedEvent(null);
for (LightningView tab : mTabList) {
tab.onDestroy();
}
@ -247,7 +227,7 @@ public class TabsManager { @@ -247,7 +227,7 @@ public class TabsManager {
*
* @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);
final LightningView currentTab = getCurrentTab();
int current = positionOf(currentTab);
@ -261,10 +241,11 @@ public class TabsManager { @@ -261,10 +241,11 @@ public class TabsManager {
} else {
mCurrentTab = getTabAtPosition(current - 1);
}
sendTabChangedEvent(mCurrentTab);
removeTab(current);
return true;
} else {
removeTab(position);
return false;
}
}
@ -333,6 +314,16 @@ public class TabsManager { @@ -333,6 +314,16 @@ public class TabsManager {
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
* no current tab has been set.
@ -356,32 +347,14 @@ public class TabsManager { @@ -356,32 +347,14 @@ public class TabsManager {
Log.d(TAG, "switch to tab: " + position);
if (position < 0 || position >= mTabList.size()) {
Log.e(TAG, "Returning a null LightningView requested for position: " + position);
sendTabChangedEvent(null);
return null;
} else {
final LightningView tab = mTabList.get(position);
if (tab != null) {
mCurrentTab = tab;
}
sendTabChangedEvent(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; @@ -13,7 +13,6 @@ import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.activity.TabsManager;
import acr.browser.lightning.activity.TabsManager.TabChangeListener;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.constant.Constants;
@ -40,25 +39,10 @@ public class BrowserPresenter { @@ -40,25 +39,10 @@ public class BrowserPresenter {
private boolean mIsIncognito;
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) {
BrowserApp.getAppComponent().inject(this);
mView = view;
mIsIncognito = isIncognito;
mTabsModel.setTabChangeListener(new TabListener(this));
}
private void onTabChanged(@Nullable LightningView newTab) {
@ -115,7 +99,10 @@ public class BrowserPresenter { @@ -115,7 +99,10 @@ public class BrowserPresenter {
if (isShown) {
mView.removeTabView();
}
mTabsModel.deleteTab(position);
boolean currentDeleted = mTabsModel.deleteTab(position);
if (currentDeleted) {
tabChanged(mTabsModel.indexOfCurrentTab());
}
}
final LightningView afterTab = mTabsModel.getCurrentTab();
if (afterTab == null) {
@ -178,6 +165,18 @@ public class BrowserPresenter { @@ -178,6 +165,18 @@ public class BrowserPresenter {
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) {
// Limit number of tabs for limited version of app
if (!Constants.FULL_VERSION && mTabsModel.size() >= 10) {
@ -191,7 +190,8 @@ public class BrowserPresenter { @@ -191,7 +190,8 @@ public class BrowserPresenter {
}
if (show) {
mTabsModel.switchToTab(mTabsModel.size() - 1);
LightningView tab = mTabsModel.switchToTab(mTabsModel.size() - 1);
onTabChanged(tab);
}
// TODO Restore this
@ -205,8 +205,4 @@ public class BrowserPresenter { @@ -205,8 +205,4 @@ public class BrowserPresenter {
return true;
}
public void destroy() {
mTabsModel.setTabChangeListener(null);
}
}

Loading…
Cancel
Save