Fragments

This commit is contained in:
serso 2012-09-25 12:10:11 +04:00
parent 2f2914dd46
commit 5242d65e3a
12 changed files with 1970 additions and 1938 deletions

View File

@ -85,6 +85,11 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
</dependency>
<dependency> <dependency>
<groupId>net.sf.opencsv</groupId> <groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId> <artifactId>opencsv</artifactId>

View File

@ -15,7 +15,10 @@
a:layout_gravity="center" a:layout_gravity="center"
a:background="#ff000000"> a:background="#ff000000">
<include layout="@layout/calc_editor"/> <LinearLayout a:id="@+id/editorContainer"
a:layout_weight="2"
a:layout_width="match_parent"
a:layout_height="0dp"/>
<LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp"> <LinearLayout a:layout_weight="1" a:layout_width="fill_parent" a:layout_height="0dp">
@ -24,10 +27,10 @@
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent"/> a:layout_height="fill_parent"/>
<include layout="@layout/calc_display" <LinearLayout a:id="@+id/displayContainer"
a:layout_weight="4" a:layout_weight="4"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_height="fill_parent"/> a:layout_height="match_parent"/>
</LinearLayout> </LinearLayout>

View File

@ -7,9 +7,8 @@
--> -->
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_weight="2" a:layout_width="match_parent"
a:layout_width="fill_parent" a:layout_height="wrap_content">
a:layout_height="0dp">
<org.solovyev.android.calculator.AndroidCalculatorEditorView <org.solovyev.android.calculator.AndroidCalculatorEditorView
a:id="@+id/calculatorEditor" a:id="@+id/calculatorEditor"

View File

@ -1,7 +1,11 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.content.SharedPreferences; import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import jscl.NumeralBase; import jscl.NumeralBase;
import jscl.math.Generic; import jscl.math.Generic;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -22,17 +26,44 @@ public class AndroidCalculator implements Calculator {
@NotNull @NotNull
private final Calculator calculator = new CalculatorImpl(); private final Calculator calculator = new CalculatorImpl();
public void init(@NotNull final Activity activity, @NotNull SharedPreferences preferences) { public static void showEvaluationError(@NotNull Context context, @NotNull final String errorMessage) {
final AndroidCalculatorEditorView editorView = (AndroidCalculatorEditorView) activity.findViewById(R.id.calculatorEditor); final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
editorView.init(preferences);
preferences.registerOnSharedPreferenceChangeListener(editorView);
CalculatorLocatorImpl.getInstance().getEditor().setView(editorView);
final View errorMessageView = layoutInflater.inflate(R.layout.display_error_message, null);
((TextView) errorMessageView.findViewById(R.id.error_message_text_view)).setText(errorMessage);
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setPositiveButton(R.string.c_cancel, null)
.setView(errorMessageView);
builder.create().show();
}
public void init(@NotNull final Activity activity) {
setEditor(activity);
setDisplay(activity);
}
public void setDisplay(@NotNull Activity activity) {
final AndroidCalculatorDisplayView displayView = (AndroidCalculatorDisplayView) activity.findViewById(R.id.calculatorDisplay); final AndroidCalculatorDisplayView displayView = (AndroidCalculatorDisplayView) activity.findViewById(R.id.calculatorDisplay);
displayView.setOnClickListener(new CalculatorDisplayOnClickListener(activity)); setDisplay(activity, displayView);
}
public void setDisplay(@NotNull Context context, @NotNull AndroidCalculatorDisplayView displayView) {
displayView.init(context);
CalculatorLocatorImpl.getInstance().getDisplay().setView(displayView); CalculatorLocatorImpl.getInstance().getDisplay().setView(displayView);
} }
public void setEditor(@NotNull Activity activity) {
final AndroidCalculatorEditorView editorView = (AndroidCalculatorEditorView) activity.findViewById(R.id.calculatorEditor);
setEditor(activity, editorView);
}
public void setEditor(@NotNull Context context, @NotNull AndroidCalculatorEditorView editorView) {
editorView.init(context);
CalculatorLocatorImpl.getInstance().getEditor().setView(editorView);
}
/* /*
********************************************************************** **********************************************************************

View File

@ -55,6 +55,8 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
@NotNull @NotNull
private final Handler handler = new Handler(); private final Handler handler = new Handler();
private volatile boolean initialized = false;
/* /*
********************************************************************** **********************************************************************
* *
@ -170,6 +172,14 @@ public class AndroidCalculatorDisplayView extends AutoResizeTextView implements
} }
} }
public synchronized void init(@NotNull Context context) {
if (!initialized) {
this.setOnClickListener(new CalculatorDisplayOnClickListener(context));
this.initialized = true;
}
}
private final class TextWatcherImpl implements TextWatcher { private final class TextWatcherImpl implements TextWatcher {
@Override @Override

View File

@ -10,6 +10,7 @@ import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.Editable; import android.text.Editable;
import android.text.Html; import android.text.Html;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -33,6 +34,8 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
private static final String CALC_COLOR_DISPLAY_KEY = "org.solovyev.android.calculator.CalculatorModel_color_display"; private static final String CALC_COLOR_DISPLAY_KEY = "org.solovyev.android.calculator.CalculatorModel_color_display";
private static final boolean CALC_COLOR_DISPLAY_DEFAULT = true; private static final boolean CALC_COLOR_DISPLAY_DEFAULT = true;
private volatile boolean initialized = false;
private boolean highlightText = true; private boolean highlightText = true;
@NotNull @NotNull
@ -52,20 +55,16 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
public AndroidCalculatorEditorView(Context context) { public AndroidCalculatorEditorView(Context context) {
super(context); super(context);
this.addTextChangedListener(new TextWatcherImpl());
} }
public AndroidCalculatorEditorView(Context context, AttributeSet attrs) { public AndroidCalculatorEditorView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
this.addTextChangedListener(new TextWatcherImpl());
} }
public AndroidCalculatorEditorView(Context context, AttributeSet attrs, int defStyle) { public AndroidCalculatorEditorView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
this.addTextChangedListener(new TextWatcherImpl());
} }
@Override @Override
public boolean onCheckIsTextEditor() { public boolean onCheckIsTextEditor() {
// NOTE: code below can be used carefully and should not be copied without special intention // NOTE: code below can be used carefully and should not be copied without special intention
@ -138,8 +137,18 @@ public class AndroidCalculatorEditorView extends EditText implements SharedPrefe
} }
} }
public void init(@NotNull SharedPreferences preferences) { public synchronized void init(@NotNull Context context) {
if (!initialized) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences.registerOnSharedPreferenceChangeListener(this);
this.addTextChangedListener(new TextWatcherImpl());
onSharedPreferenceChanged(preferences, CALC_COLOR_DISPLAY_KEY); onSharedPreferenceChanged(preferences, CALC_COLOR_DISPLAY_KEY);
initialized = true;
}
} }
@Override @Override

View File

@ -7,6 +7,9 @@ package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.res.Configuration; import android.content.res.Configuration;
@ -68,8 +71,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
@NotNull @NotNull
private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class); private final Announcer<DragPreferencesChangeListener> dpclRegister = new Announcer<DragPreferencesChangeListener>(DragPreferencesChangeListener.class);
private volatile boolean initialized;
@NotNull @NotNull
private CalculatorPreferences.Gui.Theme theme; private CalculatorPreferences.Gui.Theme theme;
@ -105,6 +106,18 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setLayout(preferences); setLayout(preferences);
final FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
final CalculatorEditorFragment editorFragment = new CalculatorEditorFragment();
fragmentTransaction.add(R.id.editorContainer, editorFragment);
fragmentTransaction.commit();
fragmentTransaction = fragmentManager.beginTransaction();
final CalculatorDisplayFragment displayFragment = new CalculatorDisplayFragment();
fragmentTransaction.add(R.id.displayContainer, displayFragment);
fragmentTransaction.commit();
if (customTitleSupported) { if (customTitleSupported) {
try { try {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title);
@ -120,15 +133,14 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
billingObserver = new CalculatorBillingObserver(this); billingObserver = new CalculatorBillingObserver(this);
BillingController.registerObserver(billingObserver); BillingController.registerObserver(billingObserver);
firstTimeInit(preferences); this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences);
firstTimeInit(preferences, this);
// init billing controller // init billing controller
BillingController.checkBillingSupported(this); BillingController.checkBillingSupported(this);
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE); vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
getCalculator().init(this, preferences);
dpclRegister.clear(); dpclRegister.clear();
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this); final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(preferences, this);
@ -207,7 +219,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
toggleOrientationChange(preferences); toggleOrientationChange(preferences);
toggleEqualsButton(preferences); // todo serso: continue
//toggleEqualsButton(preferences);
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
} }
@ -263,14 +276,16 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
int orientation = AndroidUtils.getScreenOrientation(this); int orientation = AndroidUtils.getScreenOrientation(this);
if (orientation == Configuration.ORIENTATION_PORTRAIT) { if (orientation == Configuration.ORIENTATION_PORTRAIT) {
setMarginsForView(equalsButton, marginLeft, marginBottom); setMarginsForView(equalsButton, marginLeft, marginBottom);
setMarginsForView(getCalculatorDisplayView(), marginLeft, marginBottom); // todo serso: continue
//setMarginsForView(getCalculatorDisplayView(), marginLeft, marginBottom);
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
setMarginsForView(leftButton, marginLeft, marginBottom); setMarginsForView(leftButton, marginLeft, marginBottom);
setMarginsForView(eraseButton, marginLeft, marginBottom); setMarginsForView(eraseButton, marginLeft, marginBottom);
setMarginsForView(clearButton, marginLeft, marginBottom); setMarginsForView(clearButton, marginLeft, marginBottom);
setMarginsForView(rightButton, marginLeft, marginBottom); setMarginsForView(rightButton, marginLeft, marginBottom);
// magic magic magic // magic magic magic
setMarginsForView(getCalculatorDisplayView(), 3 * marginLeft, marginBottom); // todo serso: continue
//setMarginsForView(getCalculatorDisplayView(), 3 * marginLeft, marginBottom);
} }
} }
@ -423,14 +438,10 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
private synchronized void setTheme(@NotNull SharedPreferences preferences) { private synchronized void setTheme(@NotNull SharedPreferences preferences) {
theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences); theme = CalculatorPreferences.Gui.theme.getPreferenceNoError(preferences);
setTheme(theme.getThemeId()); setTheme(theme.getThemeId());
} }
private synchronized void firstTimeInit(@NotNull SharedPreferences preferences) { private static void firstTimeInit(@NotNull SharedPreferences preferences, @NotNull Context context) {
if (!initialized) {
this.useBackAsPrev = CalculatorPreferences.Gui.usePrevAsBack.getPreference(preferences);
final Integer appOpenedCounter = CalculatorPreferences.appOpenedCounter.getPreference(preferences); final Integer appOpenedCounter = CalculatorPreferences.appOpenedCounter.getPreference(preferences);
if (appOpenedCounter != null) { if (appOpenedCounter != null) {
CalculatorPreferences.appOpenedCounter.putPreference(preferences, appOpenedCounter + 1); CalculatorPreferences.appOpenedCounter.putPreference(preferences, appOpenedCounter + 1);
@ -438,14 +449,14 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
final Integer savedVersion = CalculatorPreferences.appVersion.getPreference(preferences); final Integer savedVersion = CalculatorPreferences.appVersion.getPreference(preferences);
final int appVersion = AndroidUtils.getAppVersionCode(this, CalculatorActivity.class.getPackage().getName()); final int appVersion = AndroidUtils.getAppVersionCode(context, CalculatorActivity.class.getPackage().getName());
CalculatorPreferences.appVersion.putPreference(preferences, appVersion); CalculatorPreferences.appVersion.putPreference(preferences, appVersion);
boolean dialogShown = false; boolean dialogShown = false;
if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) { if (EqualsTool.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) {
// new start // new start
final AlertDialog.Builder builder = new AlertDialog.Builder(this).setMessage(R.string.c_first_start_text); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(R.string.c_first_start_text);
builder.setPositiveButton(android.R.string.ok, null); builder.setPositiveButton(android.R.string.ok, null);
builder.setTitle(R.string.c_first_start_text_title); builder.setTitle(R.string.c_first_start_text_title);
builder.create().show(); builder.create().show();
@ -454,9 +465,9 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
if (savedVersion < appVersion) { if (savedVersion < appVersion) {
final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences); final boolean showReleaseNotes = CalculatorPreferences.Gui.showReleaseNotes.getPreference(preferences);
if (showReleaseNotes) { if (showReleaseNotes) {
final String releaseNotes = CalculatorReleaseNotesActivity.getReleaseNotes(this, savedVersion + 1); final String releaseNotes = CalculatorReleaseNotesActivity.getReleaseNotes(context, savedVersion + 1);
if (!StringUtils.isEmpty(releaseNotes)) { if (!StringUtils.isEmpty(releaseNotes)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this).setMessage(Html.fromHtml(releaseNotes)); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setMessage(Html.fromHtml(releaseNotes));
builder.setPositiveButton(android.R.string.ok, null); builder.setPositiveButton(android.R.string.ok, null);
builder.setTitle(R.string.c_release_notes); builder.setTitle(R.string.c_release_notes);
builder.create().show(); builder.create().show();
@ -469,31 +480,28 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
//Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!"); //Log.d(this.getClass().getName(), "Application was opened " + appOpenedCounter + " time!");
if (!dialogShown) { if (!dialogShown) {
if ( appOpenedCounter != null && appOpenedCounter > 10 ) { if (appOpenedCounter != null && appOpenedCounter > 10) {
dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText); dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.feedbackWindowShown, R.layout.feedback, R.id.feedbackText, context);
} }
} }
if ( !dialogShown ) { if (!dialogShown) {
dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.notesppAnnounceShown, R.layout.notespp_announce, R.id.notespp_announce); dialogShown = showSpecialWindow(preferences, CalculatorPreferences.Gui.notesppAnnounceShown, R.layout.notespp_announce, R.id.notespp_announce, context);
}
initialized = true;
} }
} }
private boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference<Boolean> specialWindowShownPref, int layoutId, int textViewId) { private static boolean showSpecialWindow(@NotNull SharedPreferences preferences, @NotNull Preference<Boolean> specialWindowShownPref, int layoutId, int textViewId, @NotNull Context context) {
boolean result = false; boolean result = false;
final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences); final Boolean specialWindowShown = specialWindowShownPref.getPreference(preferences);
if ( specialWindowShown != null && !specialWindowShown ) { if ( specialWindowShown != null && !specialWindowShown ) {
final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
final View view = layoutInflater.inflate(layoutId, null); final View view = layoutInflater.inflate(layoutId, null);
final TextView feedbackTextView = (TextView) view.findViewById(textViewId); final TextView feedbackTextView = (TextView) view.findViewById(textViewId);
feedbackTextView.setMovementMethod(LinkMovementMethod.getInstance()); feedbackTextView.setMovementMethod(LinkMovementMethod.getInstance());
final AlertDialog.Builder builder = new AlertDialog.Builder(this).setView(view); final AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(view);
builder.setPositiveButton(android.R.string.ok, null); builder.setPositiveButton(android.R.string.ok, null);
builder.create().show(); builder.create().show();

View File

@ -0,0 +1,32 @@
package org.solovyev.android.calculator;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* User: Solovyev_S
* Date: 25.09.12
* Time: 12:03
*/
public class CalculatorDisplayFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.calc_display, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setDisplay(getActivity());
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}

View File

@ -1,6 +1,6 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.Activity; import android.content.Context;
import android.view.View; import android.view.View;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.menu.AMenuBuilder; import org.solovyev.android.menu.AMenuBuilder;
@ -17,10 +17,10 @@ import java.util.List;
public class CalculatorDisplayOnClickListener implements View.OnClickListener { public class CalculatorDisplayOnClickListener implements View.OnClickListener {
@NotNull @NotNull
private final Activity activity; private final Context context;
public CalculatorDisplayOnClickListener(@NotNull Activity activity) { public CalculatorDisplayOnClickListener(@NotNull Context context) {
this.activity = activity; this.context = context;
} }
@Override @Override
@ -39,13 +39,13 @@ public class CalculatorDisplayOnClickListener implements View.OnClickListener {
} }
if (!filteredMenuItems.isEmpty()) { if (!filteredMenuItems.isEmpty()) {
AMenuBuilder.newInstance(activity, MenuImpl.newInstance(filteredMenuItems)).create(displayViewState).show(); AMenuBuilder.newInstance(context, MenuImpl.newInstance(filteredMenuItems)).create(displayViewState).show();
} }
} else { } else {
final String errorMessage = displayViewState.getErrorMessage(); final String errorMessage = displayViewState.getErrorMessage();
if (errorMessage != null) { if (errorMessage != null) {
CalculatorModel.showEvaluationError(activity, errorMessage); AndroidCalculator.showEvaluationError(context, errorMessage);
} }
} }
} }

View File

@ -0,0 +1,32 @@
package org.solovyev.android.calculator;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* User: Solovyev_S
* Date: 25.09.12
* Time: 10:49
*/
public class CalculatorEditorFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.calc_editor, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((AndroidCalculator) CalculatorLocatorImpl.getInstance().getCalculator()).setEditor(getActivity());
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}

View File

@ -1,103 +0,0 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
*/
package org.solovyev.android.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.jscl.JsclOperation;
import org.solovyev.common.gui.CursorControl;
/**
* User: serso
* Date: 9/12/11
* Time: 11:15 PM
*/
public enum CalculatorModel implements CalculatorEngineControl, CursorControl {
instance;
@NotNull
private final CalculatorEditor editor;
@NotNull
private final CalculatorDisplay display;
private CalculatorModel() {
display = CalculatorLocatorImpl.getInstance().getDisplay();
editor = CalculatorLocatorImpl.getInstance().getEditor();
}
public CalculatorModel attachViews(@NotNull final Activity activity, @NotNull SharedPreferences preferences) {
Log.d(this.getClass().getName(), "CalculatorModel initialization with activity: " + activity);
final AndroidCalculatorEditorView editorView = (AndroidCalculatorEditorView) activity.findViewById(R.id.calculatorEditor);
editorView.init(preferences);
preferences.registerOnSharedPreferenceChangeListener(editorView);
editor.setView(editorView);
final AndroidCalculatorDisplayView displayView = (AndroidCalculatorDisplayView) activity.findViewById(R.id.calculatorDisplay);
displayView.setOnClickListener(new CalculatorDisplayOnClickListener(activity));
display.setView(displayView);
return this;
}
public static void showEvaluationError(@NotNull Activity activity, @NotNull final String errorMessage) {
final LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View errorMessageView = layoutInflater.inflate(R.layout.display_error_message, null);
((TextView) errorMessageView.findViewById(R.id.error_message_text_view)).setText(errorMessage);
final AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setPositiveButton(R.string.c_cancel, null)
.setView(errorMessageView);
builder.create().show();
}
@Override
public void setCursorOnStart() {
this.editor.setCursorOnStart();
}
@Override
public void setCursorOnEnd() {
this.editor.setCursorOnEnd();
}
@Override
public void moveCursorLeft() {
this.editor.moveCursorLeft();
}
@Override
public void moveCursorRight() {
this.editor.moveCursorRight();
}
@Override
public void evaluate() {
CalculatorLocatorImpl.getInstance().getCalculator().evaluate(JsclOperation.numeric, this.editor.getViewState().getText());
}
@Override
public void simplify() {
CalculatorLocatorImpl.getInstance().getCalculator().evaluate(JsclOperation.simplify, this.editor.getViewState().getText());
}
@NotNull
public CalculatorDisplay getDisplay() {
return display;
}
}

View File

@ -109,6 +109,12 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
<version>r7</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.android</groupId> <groupId>com.google.android</groupId>
<artifactId>android-test</artifactId> <artifactId>android-test</artifactId>