From 40d25ad8ef1bd2c75268932ee2ccfd9973aec07f Mon Sep 17 00:00:00 2001 From: fkloft Date: Mon, 2 Dec 2013 23:06:18 +0100 Subject: [PATCH] Replace Pane enum with class --- .../cradle/iitc_mobile/IITC_JSInterface.java | 4 +- .../cradle/iitc_mobile/IITC_MapSettings.java | 1 - .../com/cradle/iitc_mobile/IITC_Mobile.java | 4 +- .../iitc_mobile/IITC_NavigationHelper.java | 198 +++++++++--------- 4 files changed, 98 insertions(+), 109 deletions(-) diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java b/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java index 0222fbe9..d86e3e0e 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java @@ -13,8 +13,6 @@ import android.widget.Toast; import com.cradle.iitc_mobile.IITC_NavigationHelper.Pane; import com.cradle.iitc_mobile.share.ShareActivity; -import java.util.Locale; - // provide communication between IITC script and android app public class IITC_JSInterface { // context of main activity @@ -96,7 +94,7 @@ public class IITC_JSInterface { public void run() { Pane pane; try { - pane = Pane.valueOf(id.toUpperCase(Locale.getDefault())); + pane = mIitc.getNavigationHelper().getPane(id); } catch (IllegalArgumentException e) { pane = Pane.MAP; } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_MapSettings.java b/mobile/src/com/cradle/iitc_mobile/IITC_MapSettings.java index 1e06f9e1..687aff13 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_MapSettings.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_MapSettings.java @@ -12,7 +12,6 @@ import android.widget.ArrayAdapter; import android.widget.CheckedTextView; import android.widget.ListView; import android.widget.Spinner; -import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index 92bcc447..46c59ef2 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -35,7 +35,6 @@ import com.cradle.iitc_mobile.IITC_NavigationHelper.Pane; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; -import java.util.Locale; import java.util.Stack; public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeListener, LocationListener { @@ -412,8 +411,7 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis } public void switchToPane(Pane pane) { - String name = pane.name().toLowerCase(Locale.getDefault()); - mIitcWebView.loadUrl("javascript: window.show('" + name + "');"); + mIitcWebView.loadUrl("javascript: window.show('" + pane.name + "');"); } @Override diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_NavigationHelper.java b/mobile/src/com/cradle/iitc_mobile/IITC_NavigationHelper.java index 0d15cc85..35b8a504 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_NavigationHelper.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_NavigationHelper.java @@ -34,69 +34,6 @@ public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnIt // Makes the icon/title clickable // getActionBar().setHomeButtonEnabled(enabled); - private class NavigationAdapter extends ArrayAdapter { - public NavigationAdapter() { - super(mIitc, R.layout.list_item_selectable); - - add(Pane.MAP); - add(Pane.INFO); - add(Pane.FULL); - add(Pane.COMPACT); - add(Pane.PUBLIC); - add(Pane.FACTION); - - if (mPrefs.getBoolean("pref_advanced_menu", false)) { - add(Pane.DEBUG); - } - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - TextView view = (TextView) super.getView(position, convertView, parent); - Pane item = getItem(position); - if (item == Pane.MAP) { - view.setText("Map"); - } else { - view.setText(getPaneTitle(item)); - } - - int icon = 0; - switch (item) { - case MAP: - icon = R.drawable.location_map; - break; - case INFO: - icon = R.drawable.action_about; - break; - case FULL: - icon = R.drawable.collections_view_as_list; - break; - case COMPACT: - icon = R.drawable.collections_view_as_list_compact; - break; - case PUBLIC: - icon = R.drawable.social_group; - break; - case FACTION: - icon = R.drawable.social_cc_bcc; - break; - case DEBUG: - icon = R.drawable.ic_debug; - break; - } - - if (icon != 0) { - view.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0); - } - - return view; - } - } - - public static enum Pane { - COMPACT, DEBUG, FACTION, FULL, INFO, MAP, PUBLIC - } - public static final int NOTICE_DRAWERS = 1 << 0; public static final int NOTICE_INFO = 1 << 1; // next one would be 1<<2; (this results in 1,2,4,8,...) @@ -140,9 +77,7 @@ public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnIt } private void showNotice(final int which) { - if ((mPrefs.getInt("pref_messages", 0) & which) != 0) { - return; - } + if ((mPrefs.getInt("pref_messages", 0) & which) != 0) return; String text; switch (which) { @@ -185,10 +120,12 @@ public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnIt dialog.show(); } - private void updateActionBar() { + private void updateViews() { int position = mNavigationAdapter.getPosition(mPane); if (position >= 0 && position < mNavigationAdapter.getCount()) { mDrawerLeft.setItemChecked(position, true); + } else { + mDrawerLeft.setItemChecked(mDrawerLeft.getCheckedItemPosition(), false); } if (mDesktopMode) { @@ -219,7 +156,7 @@ public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnIt if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) { mActionBar.setTitle(mIitc.getString(R.string.app_name)); } else { - mActionBar.setTitle(getPaneTitle(mPane)); + mActionBar.setTitle(mPane.label); } } @@ -235,23 +172,17 @@ public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnIt mDrawerLayout.closeDrawers(); } - public String getPaneTitle(Pane pane) { - switch (pane) { - case INFO: - return "Info"; - case FULL: - return "Full"; - case COMPACT: - return "Compact"; - case PUBLIC: - return "Public"; - case FACTION: - return "Faction"; - case DEBUG: - return "Debug"; - default: - return mIitc.getString(R.string.app_name); + public Pane getPane(String id) { + for (int i = 0; i < mNavigationAdapter.getCount(); i++) { + Pane pane = mNavigationAdapter.getItem(i); + if (pane.name.equals(id)) + return pane; } + throw new IllegalArgumentException("Unknown pane: " + id); + } + + public void hideActionBar() { + mActionBar.hide(); } public boolean isDrawerOpened() { @@ -268,7 +199,7 @@ public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnIt @Override public void run() { mIitc.invalidateOptionsMenu(); - updateActionBar(); + updateViews(); } }, 200); } @@ -278,7 +209,7 @@ public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnIt super.onDrawerOpened(drawerView); mIitc.getWebView().onWindowFocusChanged(false); mIitc.invalidateOptionsMenu(); - updateActionBar(); + updateViews(); mDrawerLayout.closeDrawer(drawerView.equals(mDrawerLeft) ? mDrawerRight : mDrawerLeft); } @@ -310,7 +241,7 @@ public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnIt public void onPrefChanged() { mDesktopMode = mPrefs.getBoolean("pref_force_desktop", false); - updateActionBar(); + updateViews(); } public void openRightDrawer() { @@ -321,37 +252,100 @@ public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnIt public void reset() { mPane = Pane.MAP; - updateActionBar(); + mNavigationAdapter.reset(); + updateViews(); } public void setDebugMode(boolean enabled) { - mNavigationAdapter.remove(Pane.DEBUG); // avoid duplicates - if (enabled) { - mNavigationAdapter.add(Pane.DEBUG); - } + mNavigationAdapter.reset(); } public void setHighlighter(String name) { mHighlighter = name; - updateActionBar(); + updateViews(); } public void setLoadingState(boolean isLoading) { mIsLoading = isLoading; - updateActionBar(); - } - - public void switchTo(Pane pane) { - mPane = pane; - - updateActionBar(); + updateViews(); } public void showActionBar() { mActionBar.show(); } - public void hideActionBar() { - mActionBar.hide(); + public void switchTo(Pane pane) { + mPane = pane; + + updateViews(); + } + + private class NavigationAdapter extends ArrayAdapter { + public NavigationAdapter() { + super(mIitc, R.layout.list_item_selectable); + + reset(); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + TextView view = (TextView) super.getView(position, convertView, parent); + Pane item = getItem(position); + view.setText(item.label); + + if (item.icon != 0) { + view.setCompoundDrawablesWithIntrinsicBounds(item.icon, 0, 0, 0); + } + + return view; + } + + public void reset() { + clear(); + + add(Pane.INFO); + add(Pane.FULL); + add(Pane.COMPACT); + add(Pane.PUBLIC); + add(Pane.FACTION); + + if (mPrefs.getBoolean("pref_advanced_menu", false)) { + add(Pane.DEBUG); + } + } + } + + public static class Pane { + public static final Pane COMPACT = new Pane("compact", "Compact", R.drawable.collections_view_as_list_compact); + public static final Pane DEBUG = new Pane("debug", "Debug", R.drawable.ic_debug); + public static final Pane FACTION = new Pane("faction", "Faction", R.drawable.social_cc_bcc); + public static final Pane FULL = new Pane("full", "Full", R.drawable.collections_view_as_list); + public static final Pane INFO = new Pane("info", "Info", R.drawable.action_about); + public static final Pane MAP = new Pane("map", "IITC Mobile", R.drawable.location_map); + public static final Pane PUBLIC = new Pane("public", "Public", R.drawable.social_group); + + private int icon; + public String label; + public String name; + + public Pane(String name, String label, int icon) { + this.name = name; + this.label = label; + this.icon = icon; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (o.getClass() != getClass()) return false; + + Pane pane = (Pane) o; + return name.equals(pane.name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } } }