Remove CalculatorMenu

This commit is contained in:
serso 2016-02-04 17:19:25 +01:00
parent 07a1608a80
commit f50f8b599b
9 changed files with 104 additions and 195 deletions

View File

@ -69,57 +69,16 @@ public final class ActivityLauncher implements CalculatorEventListener {
public ActivityLauncher() { public ActivityLauncher() {
} }
private static void showActivity(@Nonnull final Context context, @NonNull Class<? extends Activity> activity) { private static void show(@Nonnull final Context context, @NonNull Class<? extends Activity> activity) {
showActivity(context, new Intent(context, 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); final boolean detached = !(context instanceof Activity);
Activities.addIntentFlags(intent, detached, context); Activities.addIntentFlags(intent, detached, context);
context.startActivity(intent); 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<HistoryActivity> 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) { public static void tryCreateVar(@Nonnull final Context context) {
final Display display = App.getDisplay(); final Display display = App.getDisplay();
final DisplayState state = display.getState(); 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() { public void openFacebook() {
final Uri uri = Uri.parse(application.getString(R.string.cpp_share_link)); final Uri uri = Uri.parse(application.getString(R.string.cpp_share_link));
final Intent intent = new Intent(Intent.ACTION_VIEW, uri); final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
@ -209,7 +189,7 @@ public final class ActivityLauncher implements CalculatorEventListener {
} }
public void show(@NonNull Class<HistoryActivity> activity) { public void show(@NonNull Class<HistoryActivity> activity) {
showActivity(getContext(), activity); show(getContext(), activity);
} }
@NonNull @NonNull
@ -259,11 +239,11 @@ public final class ActivityLauncher implements CalculatorEventListener {
} }
public void showFunctions() { public void showFunctions() {
showFunctions(getContext()); show(getContext(), FunctionsActivity.class);
} }
public void showVariables() { public void showVariables() {
showVariables(getContext()); show(getContext(), VariablesActivity.class);
} }
public void openApp() { public void openApp() {

View File

@ -19,7 +19,7 @@ import dagger.Component;
@Component(modules = AppModule.class) @Component(modules = AppModule.class)
public interface AppComponent { public interface AppComponent {
void inject(CalculatorApplication application); void inject(CalculatorApplication application);
void inject(CalculatorEditorFragment fragment); void inject(EditorFragment fragment);
void inject(BaseUi ui); void inject(BaseUi ui);
void inject(CalculatorOnscreenService service); void inject(CalculatorOnscreenService service);
void inject(BaseHistoryFragment fragment); void inject(BaseHistoryFragment fragment);

View File

@ -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, CalculatorDisplayFragment.class, R.id.displayContainer, "display");
FragmentUtils.createFragment(this, CalculatorKeyboardFragment.class, R.id.keyboardContainer, "keyboard"); FragmentUtils.createFragment(this, CalculatorKeyboardFragment.class, R.id.keyboardContainer, "keyboard");

View File

@ -43,7 +43,7 @@ import javax.annotation.Nonnull;
*/ */
public enum CalculatorFragmentType { 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), //display(CalculatorHistoryFragment.class, "history", R.layout.fragment_history, R.string.c_history),
//keyboard(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), history(RecentHistoryFragment.class, R.layout.fragment_history, R.string.cpp_history_tab_recent),

View File

@ -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<MenuItem> {
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);
}
}

View File

@ -33,41 +33,26 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.solovyev.android.menu.ActivityMenu; import org.solovyev.android.calculator.view.NumeralBaseConverterDialog;
import org.solovyev.android.menu.AndroidMenuHelper;
import org.solovyev.android.menu.ListActivityMenu;
import javax.annotation.Nonnull;
import javax.inject.Inject; import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast; import static org.solovyev.android.calculator.App.cast;
public class CalculatorEditorFragment extends Fragment { public class EditorFragment extends Fragment {
private FragmentUi fragmentUi; private FragmentUi ui;
@Nonnull
private ActivityMenu<Menu, MenuItem> menu = ListActivityMenu.fromEnum(CalculatorMenu.class, AndroidMenuHelper.getInstance());
private EditorView editorView; private EditorView editorView;
@Inject @Inject
Editor editor; Editor editor;
@Inject @Inject
SharedPreferences preferences; SharedPreferences preferences;
@Inject
ActivityLauncher launcher;
public CalculatorEditorFragment() { public EditorFragment() {
}
@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);
} }
@Override @Override
@ -83,42 +68,52 @@ public class CalculatorEditorFragment extends Fragment {
final Preferences.Gui.Layout layout = Preferences.Gui.getLayout(preferences); final Preferences.Gui.Layout layout = Preferences.Gui.getLayout(preferences);
if (!layout.optimized) { 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 { } 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); 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 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return fragmentUi.onCreateView(this, inflater, container); return ui.onCreateView(this, inflater, container);
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
this.fragmentUi.onResume(this); this.ui.onResume(this);
} }
@Override @Override
public void onPause() { public void onPause() {
this.fragmentUi.onPause(this); this.ui.onPause(this);
super.onPause(); super.onPause();
} }
@Override @Override
public void onDestroyView() { public void onDestroyView() {
editor.clearView(editorView); editor.clearView(editorView);
fragmentUi.onDestroyView(this); ui.onDestroyView(this);
super.onDestroyView(); super.onDestroyView();
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
fragmentUi.onDestroy(this); ui.onDestroy(this);
super.onDestroy(); super.onDestroy();
} }
@ -127,26 +122,30 @@ public class CalculatorEditorFragment extends Fragment {
super.onDetach(); super.onDetach();
} }
/*
**********************************************************************
*
* MENU
*
**********************************************************************
*/
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
this.menu.onCreateOptionsMenu(this.getActivity(), menu); inflater.inflate(R.menu.main, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
this.menu.onPrepareOptionsMenu(this.getActivity(), menu);
} }
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { 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;
} }
} }

View File

@ -144,7 +144,7 @@ public class FragmentUi extends BaseUi {
adView = (AdView) root.findViewById(R.id.ad); adView = (AdView) root.findViewById(R.id.ad);
final ViewGroup mainFragmentLayout = (ViewGroup) root.findViewById(R.id.main_fragment_layout); 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 // no ads in those fragments
} else { } else {
if (adView != null) { if (adView != null) {

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_settings"
android:title="@string/c_settings"
app:showAsAction="never" />
<item
android:id="@+id/menu_history"
android:title="@string/c_history"
app:showAsAction="never" />
<item
android:id="@+id/menu_plotter"
android:title="@string/cpp_plotter"
app:showAsAction="never" />
<item
android:id="@+id/menu_conversion_tool"
android:title="@string/c_conversion_tool"
app:showAsAction="never" />
<item
android:id="@+id/menu_about"
android:title="@string/c_about"
app:showAsAction="never" />
</menu>