From 77ae5dad9dee480bb054f52bb1225676ba3a6ecd Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 16 Jan 2016 12:41:29 +0100 Subject: [PATCH] History changes --- .../calculator/CalculatorApplication.java | 10 +- .../history/BaseHistoryFragment.java | 108 ++++++------------ .../history/EditHistoryFragment.java | 7 +- .../history/RecentHistoryFragment.java | 31 +++++ .../history/SavedHistoryFragment.java | 36 ++++++ app/src/main/res/layout/history_edit.xml | 10 +- 6 files changed, 111 insertions(+), 91 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java index e7c17cd2..8c18473c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorApplication.java @@ -26,10 +26,8 @@ import android.content.SharedPreferences; import android.os.Handler; import android.preference.PreferenceManager; import android.util.Log; - import com.squareup.leakcanary.LeakCanary; import com.squareup.otto.Bus; - import org.acra.ACRA; import org.acra.ACRAConfiguration; import org.acra.sender.HttpSender; @@ -43,15 +41,14 @@ import org.solovyev.android.calculator.plot.AndroidCalculatorPlotter; import org.solovyev.android.calculator.plot.CalculatorPlotterImpl; import org.solovyev.common.msg.MessageType; +import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.inject.Named; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.concurrent.Executor; -import javax.annotation.Nonnull; -import javax.inject.Inject; -import javax.inject.Named; - public class CalculatorApplication extends android.app.Application implements SharedPreferences.OnSharedPreferenceChangeListener { @Inject @@ -107,7 +104,6 @@ public class CalculatorApplication extends android.app.Application implements Sh .build(); component.inject(this); history.init(initThread); - display.init(); onPostCreate(preferences, languages); } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java index 7c267d65..790bb8ee 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java @@ -30,37 +30,22 @@ import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.ListFragment; import android.text.ClipboardManager; -import android.view.ContextMenu; -import android.view.LayoutInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; +import android.view.*; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; -import android.widget.Toast; - import com.melnykov.fab.FloatingActionButton; import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; - -import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.CalculatorActivity; -import org.solovyev.android.calculator.CalculatorApplication; -import org.solovyev.android.calculator.CalculatorFragmentType; -import org.solovyev.android.calculator.FragmentUi; -import org.solovyev.android.calculator.R; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.common.text.Strings; -import java.util.ArrayList; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; - -import static android.view.Menu.NONE; +import java.util.ArrayList; +import java.util.List; public abstract class BaseHistoryFragment extends ListFragment { @@ -167,73 +152,52 @@ public abstract class BaseHistoryFragment extends ListFragment { } @Override - public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + public final void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; final HistoryState state = (HistoryState) getListView().getItemAtPosition(info.position); - if (!isRecentHistory()) { - menu.add(NONE, R.string.c_use, NONE, R.string.c_use); - menu.add(NONE, R.string.c_copy_expression, NONE, R.string.c_copy_expression); - if (shouldHaveCopyResult(state)) { - menu.add(NONE, R.string.c_copy_result, NONE, R.string.c_copy_result); - } - menu.add(NONE, R.string.c_edit, NONE, R.string.c_edit); - menu.add(NONE, R.string.c_remove, NONE, R.string.c_remove); - } else { - menu.add(NONE, R.string.c_use, NONE, R.string.c_use); - menu.add(NONE, R.string.c_copy_expression, NONE, R.string.c_copy_expression); - if (shouldHaveCopyResult(state)) { - menu.add(NONE, R.string.c_copy_result, NONE, R.string.c_copy_result); - } - menu.add(NONE, R.string.c_save, NONE, R.string.c_save); - } + onCreateContextMenu(menu, state); } + protected abstract void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull HistoryState state); + @Override - public boolean onContextItemSelected(MenuItem item) { - final Context context = getActivity(); + public final boolean onContextItemSelected(MenuItem item) { final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); final HistoryState state = (HistoryState) getListView().getItemAtPosition(info.position); - switch (item.getItemId()) { - case R.string.c_use: - useState(state); - return true; - case R.string.c_copy_expression: - final String editorText = state.editor.getTextString(); - if (!Strings.isEmpty(editorText)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(editorText); - Toast.makeText(context, context.getText(R.string.c_expression_copied), Toast.LENGTH_SHORT).show(); - } - return true; - case R.string.c_copy_result: - final String displayText = state.display.text; - if (!Strings.isEmpty(displayText)) { - final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); - clipboard.setText(displayText); - Toast.makeText(context, context.getText(R.string.c_result_copied), Toast.LENGTH_SHORT).show(); - } - return true; - case R.string.c_save: - EditHistoryFragment.show(state, true, getFragmentManager()); - return true; - case R.string.c_edit: - EditHistoryFragment.show(state, false, getFragmentManager()); - return true; - case R.string.c_remove: - getAdapter().remove(state); - history.removeSaved(state); - Toast.makeText(context, context.getText(R.string.c_history_was_removed), Toast.LENGTH_LONG).show(); - getAdapter().notifyDataSetChanged(); - return true; - + if (onContextItemSelected(item, state)) { + return true; } return super.onContextItemSelected(item); } - private boolean shouldHaveCopyResult(@Nonnull HistoryState state) { + protected abstract boolean onContextItemSelected(@Nonnull MenuItem item, @Nonnull HistoryState state); + + @SuppressWarnings("deprecation") + protected final void copyResult(@Nonnull HistoryState state) { + final Context context = getActivity(); + final String displayText = state.display.text; + if (Strings.isEmpty(displayText)) { + return; + } + final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(displayText); + } + + @SuppressWarnings("deprecation") + protected final void copyExpression(@Nonnull HistoryState state) { + final Context context = getActivity(); + final String editorText = state.editor.getTextString(); + if (Strings.isEmpty(editorText)) { + return; + } + final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE); + clipboard.setText(editorText); + } + + protected final boolean shouldHaveCopyResult(@Nonnull HistoryState state) { return !state.display.valid || !Strings.isEmpty(state.display.text); } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java index 26ad3c49..a0099ffb 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java @@ -12,16 +12,14 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.TextView; - +import butterknife.Bind; +import butterknife.ButterKnife; import org.solovyev.android.calculator.AppComponent; import org.solovyev.android.calculator.BaseDialogFragment; import org.solovyev.android.calculator.R; import javax.inject.Inject; -import butterknife.Bind; -import butterknife.ButterKnife; - public class EditHistoryFragment extends BaseDialogFragment { public static final String ARG_STATE = "state"; @@ -76,7 +74,6 @@ public class EditHistoryFragment extends BaseDialogFragment { @Override protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) { - builder.setTitle(newState ? R.string.c_save_history : R.string.c_edit_history); builder.setNegativeButton(R.string.c_cancel, null); builder.setPositiveButton(R.string.c_save, new DialogInterface.OnClickListener() { @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/history/RecentHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistoryFragment.java index 531599a7..00f325ff 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/RecentHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/RecentHistoryFragment.java @@ -22,12 +22,16 @@ package org.solovyev.android.calculator.history; +import android.view.ContextMenu; +import android.view.MenuItem; import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.R; import javax.annotation.Nonnull; import java.util.List; +import static android.view.Menu.NONE; + public class RecentHistoryFragment extends BaseHistoryFragment { public RecentHistoryFragment() { @@ -55,4 +59,31 @@ public class RecentHistoryFragment extends BaseHistoryFragment { protected boolean isRecentHistory() { return true; } + + protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull HistoryState state) { + menu.add(NONE, R.string.c_use, NONE, R.string.c_use); + menu.add(NONE, R.string.c_copy_expression, NONE, R.string.c_copy_expression); + if (shouldHaveCopyResult(state)) { + menu.add(NONE, R.string.c_copy_result, NONE, R.string.c_copy_result); + } + menu.add(NONE, R.string.c_save, NONE, R.string.c_save); + } + + protected boolean onContextItemSelected(@Nonnull MenuItem item, @Nonnull HistoryState state) { + switch (item.getItemId()) { + case R.string.c_use: + useState(state); + return true; + case R.string.c_copy_expression: + copyExpression(state); + return true; + case R.string.c_copy_result: + copyResult(state); + return true; + case R.string.c_save: + EditHistoryFragment.show(state, true, getFragmentManager()); + return true; + } + return false; + } } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java index 7b5eb7eb..3b0d761a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/SavedHistoryFragment.java @@ -22,12 +22,16 @@ package org.solovyev.android.calculator.history; +import android.view.ContextMenu; +import android.view.MenuItem; import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.R; import javax.annotation.Nonnull; import java.util.List; +import static android.view.Menu.NONE; + public class SavedHistoryFragment extends BaseHistoryFragment { public SavedHistoryFragment() { @@ -55,4 +59,36 @@ public class SavedHistoryFragment extends BaseHistoryFragment { protected boolean isRecentHistory() { return false; } + + protected void onCreateContextMenu(@Nonnull ContextMenu menu, @Nonnull HistoryState state) { + menu.add(NONE, R.string.c_use, NONE, R.string.c_use); + menu.add(NONE, R.string.c_copy_expression, NONE, R.string.c_copy_expression); + if (shouldHaveCopyResult(state)) { + menu.add(NONE, R.string.c_copy_result, NONE, R.string.c_copy_result); + } + menu.add(NONE, R.string.c_edit, NONE, R.string.c_edit); + menu.add(NONE, R.string.c_remove, NONE, R.string.c_remove); + } + + protected boolean onContextItemSelected(@Nonnull MenuItem item, @Nonnull HistoryState state) { + switch (item.getItemId()) { + case R.string.c_use: + useState(state); + return true; + case R.string.c_copy_expression: + copyExpression(state); + return true; + case R.string.c_copy_result: + copyResult(state); + return true; + case R.string.c_edit: + EditHistoryFragment.show(state, false, getFragmentManager()); + return true; + case R.string.c_remove: + history.removeSaved(state); + return true; + + } + return false; + } } diff --git a/app/src/main/res/layout/history_edit.xml b/app/src/main/res/layout/history_edit.xml index 79206315..3fd5e26e 100644 --- a/app/src/main/res/layout/history_edit.xml +++ b/app/src/main/res/layout/history_edit.xml @@ -34,13 +34,11 @@ @@ -51,12 +49,10 @@ + a:inputType="text" />