From 636d473fdb2e2892a792870fd0f6dd79334dc45a Mon Sep 17 00:00:00 2001 From: duy Date: Mon, 1 Jul 2019 02:13:57 +0700 Subject: [PATCH] Apply light text color for dark theme --- .../activities/base/BaseActivity.java | 8 +- .../symja/activities/ResultAdapter.java | 135 +++--- .../calculator/BasicCalculatorActivity.java | 427 +++++++++--------- .../{ThemeEngine.java => ThemeManager.java} | 10 +- .../java/com/duy/ncalc/utils/ViewUtils.java | 137 ++++++ .../com/duy/ncalc/view/MathFormulaView.java | 4 +- app/src/main/res/layout/list_item_result.xml | 26 +- .../duy/ncalc/document/StringHelperTest.java | 16 - 8 files changed, 447 insertions(+), 316 deletions(-) rename app/src/main/java/com/duy/ncalc/userinterface/{ThemeEngine.java => ThemeManager.java} (93%) create mode 100644 app/src/main/java/com/duy/ncalc/utils/ViewUtils.java delete mode 100644 app/src/test/java/com/duy/ncalc/document/StringHelperTest.java diff --git a/app/src/main/java/com/duy/calculator/activities/base/BaseActivity.java b/app/src/main/java/com/duy/calculator/activities/base/BaseActivity.java index 333d02de..91befb05 100644 --- a/app/src/main/java/com/duy/calculator/activities/base/BaseActivity.java +++ b/app/src/main/java/com/duy/calculator/activities/base/BaseActivity.java @@ -42,7 +42,7 @@ import com.duy.calculator.history.DatabaseHelper; import com.duy.ncalc.settings.CalculatorSetting; import com.duy.ncalc.userinterface.FontManager; -import com.duy.ncalc.userinterface.ThemeEngine; +import com.duy.ncalc.userinterface.ThemeManager; import com.duy.ncalc.utils.DLog; import com.kobakei.ratethisapp.RateThisApp; @@ -143,9 +143,9 @@ public void onBackPressed() { */ protected void setTheme(boolean recreate) { String name = mCalculatorSetting.getString(getResources().getString(R.string.key_pref_theme), ""); - ThemeEngine themeEngine = new ThemeEngine(getApplicationContext()); - int themeId = themeEngine.getTheme(name); - if (themeId != ThemeEngine.THEME_NOT_FOUND) { + ThemeManager themeManager = new ThemeManager(getApplicationContext()); + int themeId = themeManager.getTheme(name); + if (themeId != ThemeManager.THEME_NOT_FOUND) { super.setTheme(themeId); if (recreate) recreate(); Log.d(TAG, "Set theme ok"); diff --git a/app/src/main/java/com/duy/calculator/symja/activities/ResultAdapter.java b/app/src/main/java/com/duy/calculator/symja/activities/ResultAdapter.java index 24ae60fe..fccd236d 100644 --- a/app/src/main/java/com/duy/calculator/symja/activities/ResultAdapter.java +++ b/app/src/main/java/com/duy/calculator/symja/activities/ResultAdapter.java @@ -20,6 +20,8 @@ import android.app.Activity; import android.content.Intent; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.util.Log; @@ -30,6 +32,7 @@ import com.duy.calculator.R; import com.duy.calculator.history.ResultEntry; +import com.duy.ncalc.userinterface.ThemeManager; import com.duy.ncalc.utils.ClipboardManager; import java.util.ArrayList; @@ -40,76 +43,41 @@ * adapter for recycle view mResult */ public class ResultAdapter extends RecyclerView.Adapter { - final static String TAG = "ResultAdapter"; - private Activity mActivity; - private ArrayList mResults = new ArrayList<>(); + private final static String TAG = "ResultAdapter"; + @NonNull + private final LayoutInflater inflater; + private Activity activity; + private ArrayList resultEntries = new ArrayList<>(); + @Nullable private OnItemListener listener = null; - public ResultAdapter(Activity activity) { - mActivity = activity; - } - - public ArrayList getResults() { - return mResults; - } - - public void setResults(ArrayList mResults) { - this.mResults = mResults; - } - - public void addItem(ResultEntry item) { - mResults.add(0, item); - notifyItemInserted(0); - } - - public boolean removeItem(int position) { - if (position > mResults.size() - 1) { - return false; - } - mResults.remove(position); - notifyItemRemoved(position); - return true; - } - - public ResultEntry getResult(int position) { - if (position > mResults.size() - 1) { - return new ResultEntry("0", "0"); - } - return mResults.get(position); - } - - public void clear() { - int size = mResults.size(); - if (mResults.size() == 1) { - mResults.clear(); - notifyItemRemoved(0); - } else { - mResults.clear(); - notifyItemRangeRemoved(0, size); - } + this.activity = activity; + this.inflater = LayoutInflater.from(activity); } + @NonNull @Override - public ResultViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View view = LayoutInflater.from(mActivity).inflate(R.layout.list_item_result, parent, false); + public ResultViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = inflater.inflate(R.layout.list_item_result, parent, false); return new ResultViewHolder(view); } - public void setListener(OnItemListener listener) { - this.listener = listener; - } - @Override - public void onBindViewHolder(final ResultViewHolder holder, int position) { - final ResultEntry item = mResults.get(position); + public void onBindViewHolder(@NonNull final ResultViewHolder holder, int position) { + final ResultEntry item = resultEntries.get(position); Log.d(TAG, "onBindViewHolder: " + item.getExpression() + " = " + item.getResult()); + + holder.txtMath.setDarkTextColor(ThemeManager.isLightTheme(activity)); + holder.txtResult.setDarkTextColor(ThemeManager.isLightTheme(activity)); + holder.txtMath.setText(item.getExpression()); holder.txtResult.setText(item.getResult()); + holder.imgCopy.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - ClipboardManager.setClipboard(mActivity, item.getResult()); + ClipboardManager.setClipboard(activity, item.getResult()); } }); holder.imgShare.setOnClickListener(new View.OnClickListener() { @@ -119,13 +87,13 @@ public void onClick(View v) { intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, item.getExpression() + " = " + item.getResult()); intent.setType("text/plain"); - mActivity.startActivity(intent); + activity.startActivity(intent); } }); holder.imgEdit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (listener != null) listener.onEditExpr(item); + if (listener != null) listener.onEditClick(item); } }); @@ -139,23 +107,52 @@ public void onClick(View v) { @Override public int getItemCount() { - return mResults.size(); + return resultEntries.size(); } - private void updateEmptyView() { + public void setListener(@Nullable OnItemListener listener) { + this.listener = listener; + } + public ArrayList getResults() { + return resultEntries; } - public interface OnItemListener { - void onItemClickListener(View view, ResultEntry resultEntry); + public void setResults(ArrayList mResults) { + this.resultEntries = mResults; + } + + public void addItem(ResultEntry item) { + resultEntries.add(0, item); + notifyItemInserted(0); + } - void onItemLongClickListener(View view, ResultEntry resultEntry); + private void removeItem(int position) { + if (position > resultEntries.size() - 1) { + return; + } + resultEntries.remove(position); + notifyItemRemoved(position); + } + + public void clear() { + int size = resultEntries.size(); + if (resultEntries.size() == 1) { + resultEntries.clear(); + notifyItemRemoved(0); + } else { + resultEntries.clear(); + notifyItemRangeRemoved(0, size); + } + } + + public interface OnItemListener { - void onEditExpr(ResultEntry item); + void onEditClick(ResultEntry item); } - class ResultViewHolder extends RecyclerView.ViewHolder { + static class ResultViewHolder extends RecyclerView.ViewHolder { MathView txtMath; MathView txtResult; CardView cardView; @@ -164,14 +161,14 @@ class ResultViewHolder extends RecyclerView.ViewHolder { View imgDelete; View imgEdit; - public ResultViewHolder(View itemView) { + ResultViewHolder(View itemView) { super(itemView); - txtMath = (MathView) itemView.findViewById(R.id.txt_input); - txtResult = (MathView) itemView.findViewById(R.id.txt_result); - cardView = (CardView) itemView.findViewById(R.id.card_view); - imgShare = (ImageView) itemView.findViewById(R.id.img_share_item); + txtMath = itemView.findViewById(R.id.txt_input); + txtResult = itemView.findViewById(R.id.txt_result); + cardView = itemView.findViewById(R.id.card_view); + imgShare = itemView.findViewById(R.id.img_share_item); - imgCopy = (ImageView) itemView.findViewById(R.id.img_copy_item); + imgCopy = itemView.findViewById(R.id.img_copy_item); imgDelete = itemView.findViewById(R.id.img_delete_item); imgEdit = itemView.findViewById(R.id.img_edit); diff --git a/app/src/main/java/com/duy/ncalc/calculator/BasicCalculatorActivity.java b/app/src/main/java/com/duy/ncalc/calculator/BasicCalculatorActivity.java index ba749cbc..5d24bfb9 100644 --- a/app/src/main/java/com/duy/ncalc/calculator/BasicCalculatorActivity.java +++ b/app/src/main/java/com/duy/ncalc/calculator/BasicCalculatorActivity.java @@ -47,14 +47,12 @@ import android.widget.FrameLayout; import android.widget.Toast; -import com.duy.ncalc.utils.DLog; import com.duy.calculator.R; import com.duy.calculator.activities.base.AbstractCalculatorActivity; -import com.duy.ncalc.settings.CalculatorSetting; import com.duy.calculator.evaluator.EvaluateConfig; -import com.duy.calculator.evaluator.base.LogicEvaluator; import com.duy.calculator.evaluator.MathEvaluator; import com.duy.calculator.evaluator.base.Evaluator; +import com.duy.calculator.evaluator.base.LogicEvaluator; import com.duy.calculator.evaluator.thread.ResultCallback; import com.duy.calculator.history.HistoryActivity; import com.duy.calculator.history.ResultEntry; @@ -62,7 +60,10 @@ import com.duy.calculator.symja.models.ExprInput; import com.duy.calculator.symja.models.PrimeFactorItem; import com.duy.calculator.symja.models.SolveItem; +import com.duy.ncalc.settings.CalculatorSetting; +import com.duy.ncalc.userinterface.ThemeManager; import com.duy.ncalc.utils.ClipboardManager; +import com.duy.ncalc.utils.DLog; import com.duy.ncalc.view.AnimationFinishedListener; import com.duy.ncalc.view.CalculatorEditText; import com.duy.ncalc.view.RevealView; @@ -87,15 +88,15 @@ public class BasicCalculatorActivity extends AbstractCalculatorActivity */ private final InstantResultWatcher mFormulaTextWatcher = new InstantResultWatcher(); private final Handler mHandler = new Handler(); - public MathView mMathView; + public MathView mSecondaryResultView; public ContentLoadingProgressBar mProgress; - public FrameLayout mAnimateSolve; + public FrameLayout mSolveAnimView; private SwitchCompat mFractionSwitch; private FrameLayout mContainerSolve; private DrawerLayout mDrawerLayout; private CalculatorEditText mInputDisplay; private ViewGroup mDisplayForeground; - private MathView mReview; + private MathView mCalculatorResultView; private FloatingActionButton mFabClose; private View mCurrentButton = null; private CalculatorState mCurrentState = CalculatorState.INPUT; @@ -141,6 +142,43 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { showHelp(); } + public void insertText(String text) { + //set text display is null if not as operator + boolean b = mInputDisplay.getSelectionStart() == mInputDisplay.getCleanText().length() + 1; + if (mCurrentState == CalculatorState.RESULT && !Evaluator.isOperator(text) && b) { + mInputDisplay.clear(); + } + mInputDisplay.insert(text); + } + + /** + * insert text to display + * + * @param opt - operator + */ + public void insertOperator(String opt) { + if (mCurrentState == CalculatorState.INPUT) { + //do something + } else if (mCurrentState == CalculatorState.RESULT) { + //do some thing + } + insertText(opt); + } + + public String getTextClean() { + return mInputDisplay.getCleanText(); + } + + @Override + public void setTextDisplay(final String textDisplay) { + mInputDisplay.post(new Runnable() { + @Override + public void run() { + mInputDisplay.setText(mTokenizer.getLocalizedExpression(textDisplay)); + } + }); + } + @Override public void onClick(View v) { switch (v.getId()) { @@ -154,18 +192,22 @@ public void onClick(View v) { } } - private void bindView() { - mFabClose = (FloatingActionButton) findViewById(R.id.fab_close); - mReview = (MathView) findViewById(R.id.math_view); - mDisplayForeground = (ViewGroup) findViewById(R.id.the_clear_animation); - mInputDisplay = (CalculatorEditText) findViewById(R.id.txtDisplay); - mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); - mContainerSolve = (FrameLayout) findViewById(R.id.container_solve); - mFractionSwitch = (SwitchCompat) findViewById(R.id.sw_fraction); - mAnimateSolve = (FrameLayout) findViewById(R.id.result_animation); - mProgress = (ContentLoadingProgressBar) findViewById(R.id.progress_bar_main); - mMathView = (MathView) findViewById(R.id.math_result); + mFabClose = findViewById(R.id.fab_close); + + mCalculatorResultView = findViewById(R.id.math_view); + mCalculatorResultView.setDarkTextColor(ThemeManager.isLightTheme(this)); + + mDisplayForeground = findViewById(R.id.the_clear_animation); + mInputDisplay = findViewById(R.id.txtDisplay); + mDrawerLayout = findViewById(R.id.drawer_layout); + mContainerSolve = findViewById(R.id.container_solve); + mFractionSwitch = findViewById(R.id.sw_fraction); + mSolveAnimView = findViewById(R.id.result_animation); + mProgress = findViewById(R.id.progress_bar_main); + + mSecondaryResultView = findViewById(R.id.math_result); +// mResultView.setDarkTextColor(ThemeManager.isLightTheme(this)); mFractionSwitch.setChecked(mCalculatorSetting.useFraction()); mFractionSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @@ -205,27 +247,6 @@ private void initKeyboard() { ft.commitAllowingStateLoss(); } - @Override - public void onResume() { - super.onResume(); - - CalculatorSetting preferences = new CalculatorSetting(this); - String math = preferences.getString(CalculatorSetting.INPUT_MATH); - mInputDisplay.setText(math); - - ///receive data from another application - Intent intent = new Intent(); - String action = intent.getAction(); - String type = intent.getType(); - //process data - if (Intent.ACTION_SEND.equals(action) && type != null) { - if ("text/plain".equals(type)) { - mInputDisplay.setText(intent.getStringExtra(Intent.EXTRA_TEXT)); - } - } - if (mInputDisplay != null) mInputDisplay.requestFocus(); - } - private void showHelp() { if (mCalculatorSetting.getBoolean(BasicCalculatorActivity.class.getSimpleName())) { return; @@ -268,6 +289,11 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin } } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + return super.onOptionsItemSelected(item); + } + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); @@ -294,6 +320,15 @@ public void run() { } } + @Override + public void onPause() { + super.onPause(); + CalculatorSetting preferences = new CalculatorSetting(this); + String math = mInputDisplay.getCleanText(); + preferences.put(CalculatorSetting.INPUT_MATH, math); + mCalculatorResultView.setText(""); + } + /** * set input state * @@ -311,8 +346,17 @@ public void define(String var, String value) { mEvaluator.define(var, value); } - public void onDelete() { - mInputDisplay.backspace(); + public void setTextResult(String result) { + mCalculatorResultView.setText(result); + } + + public void setTextError(String msg) { + mCalculatorResultView.setText(msg); + } + + public void onResult(final String result) { + setTextDisplay(result.replace("\\", "").replace("\n", "")); + setTextResult(""); } public void onError(final String error) { @@ -334,17 +378,36 @@ public void onAnimationFinished() { } } - public void setTextResult(String result) { - mReview.setText(result); + public void onDelete() { + mInputDisplay.backspace(); } - public void setTextError(String msg) { - mReview.setText(msg); + public void clickClear() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + TypedValue typedValue = new TypedValue(); + Resources.Theme theme = this.getTheme(); + theme.resolveAttribute(R.attr.colorAccent, typedValue, true); + int color = typedValue.data; + animateRipple(mDisplayForeground, mCurrentButton, color, new AnimationFinishedListener() { + @Override + public void onAnimationFinished() { + setState(CalculatorState.INPUT); + mInputDisplay.clear(); + mCalculatorResultView.setText(""); + } + }, true); + } else { + setState(CalculatorState.INPUT); + mCalculatorResultView.setText(""); + mInputDisplay.clear(); + } } - public void onResult(final String result) { - setTextDisplay(result.replace("\\", "").replace("\n", "")); - setTextResult(""); + public void onEqual() { + String text = mInputDisplay.getCleanText(); + setState(CalculatorState.EVALUATE); + mEvaluator.evaluateWithResultNormal(text, BasicCalculatorActivity.this, + EvaluateConfig.loadFromSetting(this)); } /** @@ -412,84 +475,10 @@ public void onAnimationFinished() { playAnimation(revealAnimator); } - public void insertText(String text) { - //set text display is null if not as operator - boolean b = mInputDisplay.getSelectionStart() == mInputDisplay.getCleanText().length() + 1; - if (mCurrentState == CalculatorState.RESULT && !Evaluator.isOperator(text) && b) { - mInputDisplay.clear(); - } - mInputDisplay.insert(text); - } - - public void clickClear() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - TypedValue typedValue = new TypedValue(); - Resources.Theme theme = this.getTheme(); - theme.resolveAttribute(R.attr.colorAccent, typedValue, true); - int color = typedValue.data; - animateRipple(mDisplayForeground, mCurrentButton, color, new AnimationFinishedListener() { - @Override - public void onAnimationFinished() { - setState(CalculatorState.INPUT); - mInputDisplay.clear(); - mReview.setText(""); - } - }, true); - } else { - setState(CalculatorState.INPUT); - mReview.setText(""); - mInputDisplay.clear(); - } - } - - public void onEqual() { - String text = mInputDisplay.getCleanText(); - setState(CalculatorState.EVALUATE); - mEvaluator.evaluateWithResultNormal(text, BasicCalculatorActivity.this, - EvaluateConfig.loadFromSetting(this)); - } - void setState(CalculatorState state) { mCurrentState = state; } - /** - * insert text to display - * - * @param opt - operator - */ - public void insertOperator(String opt) { - if (mCurrentState == CalculatorState.INPUT) { - //do something - } else if (mCurrentState == CalculatorState.RESULT) { - //do some thing - } - insertText(opt); - } - - public String getTextClean() { - return mInputDisplay.getCleanText(); - } - - @Override - public void setTextDisplay(final String textDisplay) { - mInputDisplay.post(new Runnable() { - @Override - public void run() { - mInputDisplay.setText(mTokenizer.getLocalizedExpression(textDisplay)); - } - }); - } - - @Override - public void onPause() { - super.onPause(); - CalculatorSetting preferences = new CalculatorSetting(this); - String math = mInputDisplay.getCleanText(); - preferences.put(CalculatorSetting.INPUT_MATH, math); - mReview.setText(""); - } - @Override public void onEvaluated(String expr, String result, int resultId) { if (resultId == LogicEvaluator.RESULT_OK) { @@ -559,33 +548,68 @@ public void pasteText() { mInputDisplay.setText(ClipboardManager.getClipboard(this)); } - public void shareText() { - String s = mInputDisplay.getCleanText(); + protected boolean saveHistory(String expr, String result, boolean ensureResult) { + DLog.i("Save history: " + expr + " = " + result); + mHistoryDatabase.saveHistory(new ResultEntry(expr, result)); + return false; + } + + protected void onChangeModeFraction() { + Log.d(TAG, "onChangeModeFraction() called"); + + mEvaluator.evaluateWithResultAsTex(mInputDisplay.getCleanText(), + BasicCalculatorActivity.this, EvaluateConfig.loadFromSetting(BasicCalculatorActivity.this)); + } + + @Override + public void onBackPressed() { + if (mDrawerLayout != null) { + if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) { + mDrawerLayout.closeDrawers(); + return; + } + } + if (mCalculateState == CalculateState.RESULT) { + closeMathView(); + return; + } + super.onBackPressed(); + } + + @Override + public void onResume() { + super.onResume(); + + CalculatorSetting preferences = new CalculatorSetting(this); + String math = preferences.getString(CalculatorSetting.INPUT_MATH); + mInputDisplay.setText(math); + + ///receive data from another application Intent intent = new Intent(); - intent.setAction(Intent.ACTION_SEND); - intent.putExtra(Intent.EXTRA_TEXT, s); - intent.setType("text/plain"); - startActivity(intent); + String action = intent.getAction(); + String type = intent.getType(); + //process data + if (Intent.ACTION_SEND.equals(action) && type != null) { + if ("text/plain".equals(type)) { + mInputDisplay.setText(intent.getStringExtra(Intent.EXTRA_TEXT)); + } + } + if (mInputDisplay != null) mInputDisplay.requestFocus(); } - /** - * clickSolveEquation equation - */ - public void clickSolveEquation() { + public void clickDerivative() { String inp = mTokenizer.getNormalExpression(mInputDisplay.getCleanText()); if (inp.isEmpty()) { Toast.makeText(this, R.string.enter_expression, Toast.LENGTH_SHORT).show(); return; } - final SolveItem item = new SolveItem(inp); - if (!item.getInput().contains("x")) { - Toast.makeText(this, R.string.not_variable, Toast.LENGTH_SHORT).show(); - return; - } + + //check error before evaluate + DerivativeItem item = new DerivativeItem(mInputDisplay.getCleanText(), "x", "1"); new EvaluateTask(new ResultCallback() { @Override public void onSuccess(ArrayList result) { - mMathView.setText(result.get(0)); + mSecondaryResultView.setText(result.get(0)); mHandler.postDelayed(new Runnable() { @Override public void run() { @@ -603,36 +627,12 @@ public void onError(Exception e) { }).execute(item); } - - /** - * close Solve Result and animate - */ - public void closeMathView() { - setInputState(CalculateState.INPUT); - mHandler.post(new Runnable() { - @Override - public void run() { - mContainerSolve.setVisibility(View.GONE); - } - }); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - animateRipple(mAnimateSolve, null, -2, new AnimationFinishedListener() { - @Override - public void onAnimationFinished() { - } - }, false); - } - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - return super.onOptionsItemSelected(item); - } - - protected boolean saveHistory(String expr, String result, boolean ensureResult) { - DLog.i("Save history: " + expr + " = " + result); - mHistoryDatabase.saveHistory(new ResultEntry(expr, result)); - return false; + public void clickGraph() { +// Log.d(TAG, "clickGraph: "); +// String function = mInputFormula.getCleanText(); +// FastGraphView graph2DView = (FastGraphView) findViewById(R.id.graph_view); +// graph2DView.drawGraph(function); +// mGraphView.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED); } public void clickFactorPrime() { @@ -645,7 +645,7 @@ public void clickFactorPrime() { new EvaluateTask(new ResultCallback() { @Override public void onSuccess(ArrayList result) { - mMathView.setText(result.get(0)); + mSecondaryResultView.setText(result.get(0)); mHandler.postDelayed(new Runnable() { @Override public void run() { @@ -663,42 +663,24 @@ public void onError(Exception e) { }).execute(item); } - protected void onChangeModeFraction() { - Log.d(TAG, "onChangeModeFraction() called"); - - mEvaluator.evaluateWithResultAsTex(mInputDisplay.getCleanText(), - BasicCalculatorActivity.this, EvaluateConfig.loadFromSetting(BasicCalculatorActivity.this)); - } - - @Override - public void onBackPressed() { - if (mDrawerLayout != null) { - if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) { - mDrawerLayout.closeDrawers(); - return; - } - } - if (mCalculateState == CalculateState.RESULT) { - closeMathView(); - return; - } - super.onBackPressed(); - } - - - public void clickDerivative() { + /** + * clickSolveEquation equation + */ + public void clickSolveEquation() { String inp = mTokenizer.getNormalExpression(mInputDisplay.getCleanText()); if (inp.isEmpty()) { Toast.makeText(this, R.string.enter_expression, Toast.LENGTH_SHORT).show(); return; } - - //check error before evaluate - DerivativeItem item = new DerivativeItem(mInputDisplay.getCleanText(), "x", "1"); + final SolveItem item = new SolveItem(inp); + if (!item.getInput().contains("x")) { + Toast.makeText(this, R.string.not_variable, Toast.LENGTH_SHORT).show(); + return; + } new EvaluateTask(new ResultCallback() { @Override public void onSuccess(ArrayList result) { - mMathView.setText(result.get(0)); + mSecondaryResultView.setText(result.get(0)); mHandler.postDelayed(new Runnable() { @Override public void run() { @@ -716,18 +698,39 @@ public void onError(Exception e) { }).execute(item); } - public void clickGraph() { -// Log.d(TAG, "clickGraph: "); -// String function = mInputFormula.getCleanText(); -// FastGraphView graph2DView = (FastGraphView) findViewById(R.id.graph_view); -// graph2DView.drawGraph(function); -// mGraphView.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED); + /** + * close Solve Result and animate + */ + public void closeMathView() { + setInputState(CalculateState.INPUT); + mHandler.post(new Runnable() { + @Override + public void run() { + mContainerSolve.setVisibility(View.GONE); + } + }); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + animateRipple(mSolveAnimView, null, -2, new AnimationFinishedListener() { + @Override + public void onAnimationFinished() { + } + }, false); + } + } + + public void shareText() { + String s = mInputDisplay.getCleanText(); + Intent intent = new Intent(); + intent.setAction(Intent.ACTION_SEND); + intent.putExtra(Intent.EXTRA_TEXT, s); + intent.setType("text/plain"); + startActivity(intent); } public enum CalculatorState { INPUT, - EVALUATE, RESULT, ERROR; + EVALUATE, RESULT, ERROR } @@ -739,15 +742,6 @@ public EvaluateTask(ResultCallback resultCallback) { this.mCallback = resultCallback; } - @Override - protected void onPreExecute() { - super.onPreExecute(); - mContainerSolve.setVisibility(View.VISIBLE); - setInputState(CalculateState.RESULT); - mProgress.show(); - mFabClose.hide(); - } - @Override protected String doInBackground(ExprInput... params) { ExprInput item = params[0]; @@ -764,6 +758,15 @@ protected String doInBackground(ExprInput... params) { } } + @Override + protected void onPreExecute() { + super.onPreExecute(); + mContainerSolve.setVisibility(View.VISIBLE); + setInputState(CalculateState.RESULT); + mProgress.show(); + mFabClose.hide(); + } + @Override protected void onPostExecute(final String s) { super.onPostExecute(s); diff --git a/app/src/main/java/com/duy/ncalc/userinterface/ThemeEngine.java b/app/src/main/java/com/duy/ncalc/userinterface/ThemeManager.java similarity index 93% rename from app/src/main/java/com/duy/ncalc/userinterface/ThemeEngine.java rename to app/src/main/java/com/duy/ncalc/userinterface/ThemeManager.java index 2c9e9222..92528642 100644 --- a/app/src/main/java/com/duy/ncalc/userinterface/ThemeEngine.java +++ b/app/src/main/java/com/duy/ncalc/userinterface/ThemeManager.java @@ -22,11 +22,12 @@ import android.content.res.Resources; import com.duy.calculator.R; +import com.duy.ncalc.utils.ViewUtils; /** * Created by Duy on 3/7/2016 */ -public class ThemeEngine { +public class ThemeManager { public static final String NULL = ""; public static final int THEME_NOT_FOUND = -1; private final int mCyanTheme; @@ -45,7 +46,7 @@ public class ThemeEngine { private final int mGradientTheme; private Resources mResources; - public ThemeEngine(Context applicationContext) { + public ThemeManager(Context applicationContext) { this.mResources = applicationContext.getResources(); mCyanTheme = R.style.AppTheme_Cyan; mBlueGrayTheme = R.style.AppTheme_BlueGray; @@ -101,5 +102,10 @@ public int getTheme(String name) { } return mLightTheme; } + + + public static boolean isLightTheme(Context context) { + return ViewUtils.getBoolean(context, R.attr.isLightTheme, false); + } } diff --git a/app/src/main/java/com/duy/ncalc/utils/ViewUtils.java b/app/src/main/java/com/duy/ncalc/utils/ViewUtils.java new file mode 100644 index 00000000..1a3b93eb --- /dev/null +++ b/app/src/main/java/com/duy/ncalc/utils/ViewUtils.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2018 by Tran Le Duy + * + * 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. + */ + +package com.duy.ncalc.utils; + +import android.app.Activity; +import android.app.Dialog; +import android.content.Context; +import android.content.res.ColorStateList; +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.support.annotation.Nullable; +import android.support.annotation.StringRes; +import android.util.DisplayMetrics; +import android.util.TypedValue; +import android.widget.Toast; + +import com.duy.calculator.R; + + +/** + * Created by Duy on 2/5/2018. + */ + +public class ViewUtils { + + /** + * Show the view or text notification for a short period of time. This time + * could be user-definable. This is the default. + * + * @see Toast#setDuration + */ + public static final int LENGTH_SHORT = Toast.LENGTH_SHORT; + /** + * Show the view or text notification for a long period of time. This time + * could be user-definable. + * + * @see Toast#setDuration + */ + public static final int LENGTH_LONG = Toast.LENGTH_LONG; + public static float oneDpInPx = 1; + + public static void init(Context context) { + oneDpInPx = ViewUtils.dpToPx(context, 1); + } + + public static int dpToPx(Context context, int dp) { + DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); + return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); + } + + public static int spToPx(Context context, int sp) { + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics()); + } + + public static float pxToDp(Context context, int px) { + Resources resources = context.getResources(); + DisplayMetrics metrics = resources.getDisplayMetrics(); + float dp = px / ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); + return dp; + } + + public static int getAccentColor(Context context) { + TypedValue typedValue = new TypedValue(); + TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{R.attr.colorAccent}); + int color = a.getColor(0, 0); + a.recycle(); + return color; + } + + public static int getColor(Context context, int attr) { + TypedValue typedValue = new TypedValue(); + TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{attr}); + int color = a.getColor(0, 0); + a.recycle(); + return color; + } + + public static boolean getBoolean(Context context, int attr, boolean def) { + TypedValue typedValue = new TypedValue(); + TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{attr}); + boolean value = a.getBoolean(0, def); + a.recycle(); + return value; + } + + @Nullable + public static ColorStateList getColorStateList(Context context, int attr) { + TypedValue typedValue = new TypedValue(); + TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{attr}); + ColorStateList color = a.getColorStateList(0); + a.recycle(); + return color; + } + + public static boolean showDialog(Activity activity, Dialog dialog) { + if (activity == null) { + return false; + } + if (!activity.isFinishing()) { + dialog.show(); + return true; + } + return false; + } + + + @SuppressWarnings("UnusedReturnValue") + public static boolean showToast(@Nullable Context activity, @StringRes int idRes, int time) { + return showToast(activity, activity != null ? activity.getString(idRes) : null, time); + } + + @SuppressWarnings("UnusedReturnValue") + public static boolean showToast(@Nullable Context activity, String idRes, int time) { + if (activity == null) { + return false; + } + if (activity instanceof Activity && ((Activity) activity).isFinishing()) { + return false; + } + Toast.makeText(activity, idRes, time).show(); + return true; + } +} diff --git a/app/src/main/java/com/duy/ncalc/view/MathFormulaView.java b/app/src/main/java/com/duy/ncalc/view/MathFormulaView.java index 3356b091..d0a9ce7b 100644 --- a/app/src/main/java/com/duy/ncalc/view/MathFormulaView.java +++ b/app/src/main/java/com/duy/ncalc/view/MathFormulaView.java @@ -37,7 +37,9 @@ public class MathFormulaView extends MathView { public MathFormulaView(Context context, AttributeSet attrs) { super(context, attrs); - if (!isInEditMode()) setup(context); + if (!isInEditMode()) { + setup(context); + } } /** diff --git a/app/src/main/res/layout/list_item_result.xml b/app/src/main/res/layout/list_item_result.xml index 63be86bd..3287f836 100644 --- a/app/src/main/res/layout/list_item_result.xml +++ b/app/src/main/res/layout/list_item_result.xml @@ -16,6 +16,7 @@ - + app:srcCompat="@drawable/ic_mode_edit_white_24dp" + tools:visibility="visible" /> - + android:textColor="?android:textColorPrimary" + app:srcCompat="@drawable/ic_delete_black_24dp" /> - - android:tint="?imageTintColor" /> - - + android:textColor="?android:textColorPrimary" + app:srcCompat="@drawable/ic_share_white_24dp" /> diff --git a/app/src/test/java/com/duy/ncalc/document/StringHelperTest.java b/app/src/test/java/com/duy/ncalc/document/StringHelperTest.java deleted file mode 100644 index 9ef18093..00000000 --- a/app/src/test/java/com/duy/ncalc/document/StringHelperTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.duy.ncalc.document; - -import junit.framework.TestCase; - -public class StringHelperTest extends TestCase { - - public void testCapitalize() { - String result = StringHelper.capitalize("hello android"); - assertEquals(result, "Hello Android"); - } - - public void testAddSpace() { - String result = StringHelper.addSpaceBeforeUpperCase("HelloAndroid"); - assertEquals(result, "Hello Android"); - } -} \ No newline at end of file