From f50f8b599b4ccf2868fa7f98afa999531a3381ff Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 4 Feb 2016 17:19:25 +0100 Subject: [PATCH] Remove CalculatorMenu --- .../android/calculator/ActivityLauncher.java | 74 +++++--------- .../android/calculator/AppComponent.java | 2 +- .../calculator/CalculatorActivity.java | 2 +- .../calculator/CalculatorFragmentType.java | 2 +- .../android/calculator/CalculatorMenu.java | 99 ------------------- ...ditorFragment.java => EditorFragment.java} | 89 +++++++++-------- .../android/calculator/FragmentUi.java | 2 +- app/src/main/res/menu/main.xml | 29 ++++++ .../main/res/menu/{plot_menu.xml => plot.xml} | 0 9 files changed, 104 insertions(+), 195 deletions(-) delete mode 100644 app/src/main/java/org/solovyev/android/calculator/CalculatorMenu.java rename app/src/main/java/org/solovyev/android/calculator/{CalculatorEditorFragment.java => EditorFragment.java} (66%) create mode 100644 app/src/main/res/menu/main.xml rename app/src/main/res/menu/{plot_menu.xml => plot.xml} (100%) diff --git a/app/src/main/java/org/solovyev/android/calculator/ActivityLauncher.java b/app/src/main/java/org/solovyev/android/calculator/ActivityLauncher.java index f91929cd..08ae167a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ActivityLauncher.java +++ b/app/src/main/java/org/solovyev/android/calculator/ActivityLauncher.java @@ -69,57 +69,16 @@ public final class ActivityLauncher implements CalculatorEventListener { public ActivityLauncher() { } - private static void showActivity(@Nonnull final Context context, @NonNull Class activity) { - showActivity(context, new Intent(context, activity)); + private static void show(@Nonnull final Context context, @NonNull Class activity) { + show(context, new Intent(context, activity)); } - private static void showActivity(@Nonnull Context context, @NonNull Intent intent) { + private static void show(@Nonnull Context context, @NonNull Intent intent) { final boolean detached = !(context instanceof Activity); Activities.addIntentFlags(intent, detached, context); context.startActivity(intent); } - public void showHistory() { - showHistory(getContext()); - } - - public void showSettings() { - showSettings(getContext()); - } - - public void showWidgetSettings() { - final Context context = getContext(); - showActivity(context, PreferencesActivity.makeIntent(context, R.xml.preferences_widget, R.string.prefs_widget_title)); - } - - public static void showHistory(@Nonnull final Context context) { - show(context, HistoryActivity.class); - } - - private static void show(@Nonnull Context context, Class activity) { - showActivity(context, activity); - } - - public void showOperators() { - showActivity(getContext(), OperatorsActivity.class); - } - - public static void showSettings(@Nonnull final Context context) { - showActivity(context, PreferencesActivity.class); - } - - public static void showAbout(@Nonnull final Context context) { - context.startActivity(new Intent(context, AboutActivity.class)); - } - - public static void showFunctions(@Nonnull final Context context) { - showActivity(context, FunctionsActivity.class); - } - - public static void showVariables(@Nonnull final Context context) { - showActivity(context, VariablesActivity.class); - } - public static void tryCreateVar(@Nonnull final Context context) { final Display display = App.getDisplay(); final DisplayState state = display.getState(); @@ -190,6 +149,27 @@ public final class ActivityLauncher implements CalculatorEventListener { } } + public void showHistory() { + show(getContext(), HistoryActivity.class); + } + + public void showSettings() { + show(getContext(), PreferencesActivity.class); + } + + public void showWidgetSettings() { + final Context context = getContext(); + show(context, PreferencesActivity.makeIntent(context, R.xml.preferences_widget, R.string.prefs_widget_title)); + } + + public void showOperators() { + show(getContext(), OperatorsActivity.class); + } + + public void showAbout() { + show(getContext(), AboutActivity.class); + } + public void openFacebook() { final Uri uri = Uri.parse(application.getString(R.string.cpp_share_link)); final Intent intent = new Intent(Intent.ACTION_VIEW, uri); @@ -209,7 +189,7 @@ public final class ActivityLauncher implements CalculatorEventListener { } public void show(@NonNull Class activity) { - showActivity(getContext(), activity); + show(getContext(), activity); } @NonNull @@ -259,11 +239,11 @@ public final class ActivityLauncher implements CalculatorEventListener { } public void showFunctions() { - showFunctions(getContext()); + show(getContext(), FunctionsActivity.class); } public void showVariables() { - showVariables(getContext()); + show(getContext(), VariablesActivity.class); } public void openApp() { diff --git a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java index 1a165145..6add75bc 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java @@ -19,7 +19,7 @@ import dagger.Component; @Component(modules = AppModule.class) public interface AppComponent { void inject(CalculatorApplication application); - void inject(CalculatorEditorFragment fragment); + void inject(EditorFragment fragment); void inject(BaseUi ui); void inject(CalculatorOnscreenService service); void inject(BaseHistoryFragment fragment); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java index 8a86249c..b993dd37 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivity.java @@ -173,7 +173,7 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference } } - FragmentUtils.createFragment(this, CalculatorEditorFragment.class, R.id.editorContainer, "editor"); + FragmentUtils.createFragment(this, EditorFragment.class, R.id.editorContainer, "editor"); FragmentUtils.createFragment(this, CalculatorDisplayFragment.class, R.id.displayContainer, "display"); FragmentUtils.createFragment(this, CalculatorKeyboardFragment.class, R.id.keyboardContainer, "keyboard"); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java index d14e94b2..bfb9c250 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorFragmentType.java @@ -43,7 +43,7 @@ import javax.annotation.Nonnull; */ public enum CalculatorFragmentType { - editor(CalculatorEditorFragment.class, R.layout.cpp_app_editor, R.string.editor), + editor(EditorFragment.class, R.layout.cpp_app_editor, R.string.editor), //display(CalculatorHistoryFragment.class, "history", R.layout.fragment_history, R.string.c_history), //keyboard(CalculatorHistoryFragment.class, "history", R.layout.fragment_history, R.string.c_history), history(RecentHistoryFragment.class, R.layout.fragment_history, R.string.cpp_history_tab_recent), diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorMenu.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorMenu.java deleted file mode 100644 index e28b047d..00000000 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorMenu.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2013 serso aka se.solovyev - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - * Contact details - * - * Email: se.solovyev@gmail.com - * Site: http://se.solovyev.org - */ - -package org.solovyev.android.calculator; - -import android.app.Activity; -import android.content.Context; -import android.util.Log; -import android.view.MenuItem; - -import org.solovyev.android.calculator.view.NumeralBaseConverterDialog; -import org.solovyev.android.menu.LabeledMenuItem; - -import javax.annotation.Nonnull; - -/** - * User: serso - * Date: 4/23/12 - * Time: 2:25 PM - */ -enum CalculatorMenu implements LabeledMenuItem { - - settings(R.string.c_settings) { - @Override - public void onClick(@Nonnull MenuItem data, @Nonnull Context context) { - ActivityLauncher.showSettings(context); - } - }, - - history(R.string.c_history) { - @Override - public void onClick(@Nonnull MenuItem data, @Nonnull Context context) { - ActivityLauncher.showHistory(context); - } - }, - - plotter(R.string.cpp_plotter) { - @Override - public void onClick(@Nonnull MenuItem data, @Nonnull Context context) { - Locator.getInstance().getPlotter().plot(); - } - }, - - conversion_tool(R.string.c_conversion_tool) { - @Override - public void onClick(@Nonnull MenuItem data, @Nonnull Context context) { - new NumeralBaseConverterDialog(null).show(context); - } - }, - - exit(R.string.c_exit) { - @Override - public void onClick(@Nonnull MenuItem data, @Nonnull Context context) { - if (context instanceof Activity) { - ((Activity) context).finish(); - } else { - Log.e(CalculatorActivity.TAG, "Activity menu used with context"); - } - } - }, - - about(R.string.c_about) { - @Override - public void onClick(@Nonnull MenuItem data, @Nonnull Context context) { - ActivityLauncher.showAbout(context); - } - }; - - private final int captionResId; - - CalculatorMenu(int captionResId) { - this.captionResId = captionResId; - } - - @Nonnull - @Override - public String getCaption(@Nonnull Context context) { - return context.getString(captionResId); - } -} diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java b/app/src/main/java/org/solovyev/android/calculator/EditorFragment.java similarity index 66% rename from app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java rename to app/src/main/java/org/solovyev/android/calculator/EditorFragment.java index 9dbee527..c91547e7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEditorFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/EditorFragment.java @@ -33,41 +33,26 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import org.solovyev.android.menu.ActivityMenu; -import org.solovyev.android.menu.AndroidMenuHelper; -import org.solovyev.android.menu.ListActivityMenu; +import org.solovyev.android.calculator.view.NumeralBaseConverterDialog; -import javax.annotation.Nonnull; import javax.inject.Inject; import static org.solovyev.android.calculator.App.cast; -public class CalculatorEditorFragment extends Fragment { +public class EditorFragment extends Fragment { - private FragmentUi fragmentUi; - - @Nonnull - private ActivityMenu menu = ListActivityMenu.fromEnum(CalculatorMenu.class, AndroidMenuHelper.getInstance()); + private FragmentUi ui; private EditorView editorView; @Inject Editor editor; - @Inject SharedPreferences preferences; + @Inject + ActivityLauncher launcher; - public CalculatorEditorFragment() { - } - - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - - fragmentUi.onViewCreated(this, view); - - editorView = (EditorView) view.findViewById(R.id.calculator_editor); - editor.setView(editorView); + public EditorFragment() { } @Override @@ -83,42 +68,52 @@ public class CalculatorEditorFragment extends Fragment { final Preferences.Gui.Layout layout = Preferences.Gui.getLayout(preferences); if (!layout.optimized) { - fragmentUi = new FragmentUi(R.layout.cpp_app_editor_mobile, R.string.editor); + ui = new FragmentUi(R.layout.cpp_app_editor_mobile, R.string.editor); } else { - fragmentUi = new FragmentUi(R.layout.cpp_app_editor, R.string.editor); + ui = new FragmentUi(R.layout.cpp_app_editor, R.string.editor); } - fragmentUi.onCreate(this); + ui.onCreate(this); setHasOptionsMenu(true); } + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + ui.onViewCreated(this, view); + + editorView = (EditorView) view.findViewById(R.id.calculator_editor); + editor.setView(editorView); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return fragmentUi.onCreateView(this, inflater, container); + return ui.onCreateView(this, inflater, container); } @Override public void onResume() { super.onResume(); - this.fragmentUi.onResume(this); + this.ui.onResume(this); } @Override public void onPause() { - this.fragmentUi.onPause(this); + this.ui.onPause(this); super.onPause(); } @Override public void onDestroyView() { editor.clearView(editorView); - fragmentUi.onDestroyView(this); + ui.onDestroyView(this); super.onDestroyView(); } @Override public void onDestroy() { - fragmentUi.onDestroy(this); + ui.onDestroy(this); super.onDestroy(); } @@ -127,26 +122,30 @@ public class CalculatorEditorFragment extends Fragment { super.onDetach(); } - /* - ********************************************************************** - * - * MENU - * - ********************************************************************** - */ - - @Override + @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - this.menu.onCreateOptionsMenu(this.getActivity(), menu); - } - - @Override - public void onPrepareOptionsMenu(Menu menu) { - this.menu.onPrepareOptionsMenu(this.getActivity(), menu); + inflater.inflate(R.menu.main, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { - return this.menu.onOptionsItemSelected(this.getActivity(), item); + switch (item.getItemId()) { + case R.id.menu_settings: + launcher.showSettings(); + return true; + case R.id.menu_history: + launcher.showHistory(); + return true; + case R.id.menu_plotter: + Locator.getInstance().getPlotter().plot(); + return true; + case R.id.menu_conversion_tool: + new NumeralBaseConverterDialog(null).show(getActivity()); + return true; + case R.id.menu_about: + launcher.showAbout(); + return true; + } + return false; } } diff --git a/app/src/main/java/org/solovyev/android/calculator/FragmentUi.java b/app/src/main/java/org/solovyev/android/calculator/FragmentUi.java index 7305cab7..64a7d7f9 100644 --- a/app/src/main/java/org/solovyev/android/calculator/FragmentUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/FragmentUi.java @@ -144,7 +144,7 @@ public class FragmentUi extends BaseUi { adView = (AdView) root.findViewById(R.id.ad); final ViewGroup mainFragmentLayout = (ViewGroup) root.findViewById(R.id.main_fragment_layout); - if (fragment instanceof CalculatorDisplayFragment || fragment instanceof CalculatorEditorFragment || fragment instanceof CalculatorKeyboardFragment) { + if (fragment instanceof CalculatorDisplayFragment || fragment instanceof EditorFragment || fragment instanceof CalculatorKeyboardFragment) { // no ads in those fragments } else { if (adView != null) { diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml new file mode 100644 index 00000000..6136ce6a --- /dev/null +++ b/app/src/main/res/menu/main.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/plot_menu.xml b/app/src/main/res/menu/plot.xml similarity index 100% rename from app/src/main/res/menu/plot_menu.xml rename to app/src/main/res/menu/plot.xml