Skip to content

Commit

Permalink
Nullibility and style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maltaisn committed Jan 29, 2019
1 parent e2d7025 commit 19c851a
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 107 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.4.1
- Improved material components compatibility.
- Fixed custom button style not working.

## v1.4.0
- Migrated to AndroidX.

Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/com/nmaltais/calcdialoglib/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,18 @@ protected void onSaveInstanceState(Bundle state) {
}

@Override
public void onValueEntered(int requestCode, BigDecimal value) {
// if (requestCode == DIALOG_REQUEST_CODE) {} <-- If there's many dialogs
public void onValueEntered(int requestCode, @Nullable BigDecimal value) {
// if (requestCode == DIALOG_REQUEST_CODE) {} <-- If there are many dialogs

this.value = value;

valueTxv.setText(value.toPlainString());
signChk.setEnabled(value.compareTo(BigDecimal.ZERO) != 0);
if (value == null) {
valueTxv.setText(R.string.result_value_none);
signChk.setEnabled(false);
} else {
valueTxv.setText(value.toPlainString());
signChk.setEnabled(value.compareTo(BigDecimal.ZERO) != 0);
}
}
}

4 changes: 0 additions & 4 deletions calcdialog/bintray.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ task javadocJar(type: Jar, dependsOn: javadoc) {
from javadoc.destinationDir
}

afterEvaluate {
javadoc.classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
}

artifacts {
archives javadocJar
archives sourcesJar
Expand Down
4 changes: 2 additions & 2 deletions calcdialog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ ext {
siteUrl = 'https://github.com/maltaisn/calcdialoglib'
gitUrl = 'https://github.com/maltaisn/calcdialoglib.git'

libraryVersionCode = 10
libraryVersion = '1.4.0'
libraryVersionCode = 11
libraryVersion = '1.4.1'

developerId = 'maltaisn'

Expand Down
34 changes: 20 additions & 14 deletions calcdialog/src/main/java/com/nmaltais/calcdialog/CalcDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import android.util.DisplayMetrics;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
Expand All @@ -47,6 +44,10 @@
import java.math.RoundingMode;
import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;


/**
* Dialog with calculator for entering and calculating a number
Expand Down Expand Up @@ -153,7 +154,8 @@ public void onCreate(Bundle state) {

@SuppressLint("InflateParams")
@Override
public @NonNull Dialog onCreateDialog(final Bundle state) {
public @NonNull
Dialog onCreateDialog(final Bundle state) {
LayoutInflater inflater = LayoutInflater.from(context);
final View view = inflater.inflate(R.layout.dialog_calc, null);

Expand Down Expand Up @@ -328,7 +330,9 @@ public void onSaveInstanceState(Bundle state) {
@Override
public void onDetach() {
super.onDetach();
presenter.detach();
if (presenter != null) {
presenter.detach();
}

presenter = null;
context = null;
Expand Down Expand Up @@ -409,6 +413,7 @@ void displayAnswerText() {
}

////////// CALCULATOR SETTINGS //////////

/**
* Set initial value to show
* By default, initial value is null. That means value is 0 but if
Expand Down Expand Up @@ -437,7 +442,7 @@ public CalcDialog setMaxValue(@Nullable BigDecimal maxValue) {
/**
* Set max digits that can be entered on the calculator
* Use {@link #MAX_DIGITS_UNLIMITED} for no limit
* @param intPart Max digits for the integer part
* @param intPart Max digits for the integer part
* @param fracPart Max digits for the fractional part.
* A value of 0 means the value can't have a fractional part
* @return the dialog
Expand Down Expand Up @@ -465,8 +470,8 @@ public CalcDialog setRoundingMode(RoundingMode roundingMode) {
* @param canBeChanged whether sign can be changed or not
* if true, dialog can't be confirmed with a value of wrong sign
* and an error will be shown
* @param sign if canBeChanged is true, sign to force, -1 or 1
* otherwise use any value
* @param sign if canBeChanged is true, sign to force, -1 or 1
* otherwise use any value
* @return the dialog
*/
public CalcDialog setSignCanBeChanged(boolean canBeChanged, int sign) {
Expand All @@ -479,7 +484,7 @@ public CalcDialog setSignCanBeChanged(boolean canBeChanged, int sign) {
* Use {@link #FORMAT_CHAR_DEFAULT} to use device locale's default symbol
* By default, formatting will use locale's symbols
* @param decimalSep decimal separator
* @param groupSep grouping separator
* @param groupSep grouping separator
* @return the dialog
*/
public CalcDialog setFormatSymbols(char decimalSep, char groupSep) {
Expand Down Expand Up @@ -548,14 +553,15 @@ public CalcDialog setShowSignButton(boolean show) {
public interface CalcDialogCallback {
/**
* Called when the dialog's OK button is clicked
* @param value value entered.
* To format the value to a String, use {@link BigDecimal#toPlainString()}.
* To format the value to a currency String you could do:
* {@code NumberFormat.getCurrencyInstance(Locale).format(BigDecimal)}
* @param value value entered. May be null if no value was entered, in this case,
* it should be interpreted as zero or no value.
* To format the value to a String, use {@link BigDecimal#toPlainString()}.
* To format the value to a currency String you could do:
* {@code NumberFormat.getCurrencyInstance(Locale).format(BigDecimal)}
* @param requestCode dialog request code given when dialog
* was created with {@link #newInstance(int)}
*/
void onValueEntered(int requestCode, BigDecimal value);
void onValueEntered(int requestCode, @Nullable BigDecimal value);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import android.content.Context;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.math.BigDecimal;
import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

class CalcDialogUtils {

/**
* Checks if a BigDecimal exceeds maximum value
* @param value value to check for
* @return true if value is greater than maximum value
* maximum value is applied equally for positive and negative value
* maximum value is applied equally for positive and negative value
*/
static boolean isValueOutOfBounds(@NonNull BigDecimal value, @Nullable BigDecimal maxValue) {
return maxValue != null && (value.compareTo(maxValue) > 0 ||
Expand All @@ -27,9 +28,9 @@ static boolean isValueOutOfBounds(@NonNull BigDecimal value, @Nullable BigDecima
* @return the default locale
*/
static Locale getDefaultLocale(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.getResources().getConfiguration().getLocales().get(0);
} else{
} else {
//noinspection deprecation
return context.getResources().getConfiguration().locale;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

/**
* ImageView that triggers erase events when held down
* Attributes:
* - eraseBtnHoldDelay: Time view has to be held down to trigger quick erase (in ms)
* Default value is 750ms. Use -1 for no quick erase and 0 for no delay
* Default value is 750ms. Use -1 for no quick erase and 0 for no delay
* - eraseBtnHoldSpeed: Time after which an erase event is triggered in quick erase mode (in ms)
* Default value is 100ms
* Default value is 100ms
* - eraseAllOnHold: If true, holding button will trigger an erase all event instead of quick
* erase mode if false. By default this is false.
* erase mode if false. By default this is false.
*/
class CalcEraseButton extends AppCompatImageView {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.nmaltais.calcdialog;

import android.os.Bundle;
import androidx.annotation.Nullable;

import java.math.BigDecimal;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class CalcPresenter {
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

class CalcPresenter {

private static final int OPERATION_NONE = -1;
private static final int OPERATION_ADD = 0;
Expand Down Expand Up @@ -96,6 +98,7 @@ void attach(CalcDialog v, Bundle state) {

void detach() {
view = null;
settings = null;
}

void writeStateToBundle(Bundle bundle) {
Expand All @@ -117,11 +120,14 @@ void writeStateToBundle(Bundle bundle) {
private void readStateFromBundle(Bundle bundle) {
operation = bundle.getInt("operation");
error = bundle.getInt("error");
valueStr = new StringBuilder(bundle.getString("valueStr"));
String value = bundle.getString("valueStr");
resultIsDisplayed = bundle.getBoolean("resultIsDisplayed");
overwriteValue = bundle.getBoolean("overwriteValue");
currentIsAnswer = bundle.getBoolean("currentIsAnswer");

assert value != null;
valueStr = new StringBuilder();

if (bundle.containsKey("resultValue")) {
resultValue = new BigDecimal(bundle.getString("resultValue"));
}
Expand Down Expand Up @@ -192,7 +198,7 @@ void onDigitBtnClicked(int digit) {
|| valueStr.length() - pointPos - 1 < settings.maxFracDigits));
boolean isValueZero = (pointPos == -1 && valueStr.length() == 1 && valueStr.charAt(0) == '0');

if ((withinMaxInt || withinMaxFrac) && (!isValueZero || digit != 0)) {
if ((withinMaxInt || withinMaxFrac) && (!isValueZero || digit != 0)) {
// If max int or max frac digits have not already been reached
// Concatenate current value with new digit
if (isValueZero) {
Expand Down Expand Up @@ -273,9 +279,8 @@ void onSignBtnClicked() {
}

if (resultIsDisplayed) {
//noinspection ConstantConditions
assert resultValue != null && answerValue != null;
resultValue = resultValue.negate();
//noinspection ConstantConditions
answerValue = answerValue.negate();
}

Expand Down Expand Up @@ -372,7 +377,7 @@ private void calculate() {
BigDecimal operand = getCurrentValue();

if (operation == OPERATION_ADD) {
//noinspection ConstantConditions
assert resultValue != null;
resultValue = resultValue.add(operand);
} else if (operation == OPERATION_SUB) {
resultValue = resultValue.subtract(operand);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package com.nmaltais.calcdialog;

import android.os.Bundle;
import androidx.annotation.Nullable;

import java.math.BigDecimal;
import java.math.RoundingMode;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

class CalcSettings {

int requestCode;

BigDecimal initialValue;
@Nullable BigDecimal initialValue;

@Nullable BigDecimal maxValue;

int maxIntDigits;
int maxFracDigits;

RoundingMode roundingMode;
@NonNull RoundingMode roundingMode;

boolean signCanBeChanged;
int initialSign;
Expand Down Expand Up @@ -90,7 +92,7 @@ void readFromBundle(Bundle bundle) {
}

void setValue(@Nullable BigDecimal value) {
if (value != null && maxValue != null && CalcDialogUtils.isValueOutOfBounds(value, maxValue)) {
if (value != null && CalcDialogUtils.isValueOutOfBounds(value, maxValue)) {
value = (value.compareTo(BigDecimal.ZERO) > 0 ? maxValue : maxValue.negate());
}
initialValue = value;
Expand Down
3 changes: 2 additions & 1 deletion calcdialog/src/main/res/drawable/calc_bg_elevation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
android:bottom="2dp"
>
<shape android:shape="rectangle">
<solid android:color="#ffffff"/>
</shape>
Expand Down
12 changes: 7 additions & 5 deletions calcdialog/src/main/res/drawable/calc_ic_backspace.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24.00dp"
android:height="24.00dp"
android:viewportWidth="24.00"
android:viewportHeight="24.00">
android:width="24.00dp"
android:height="24.00dp"
android:viewportWidth="24.00"
android:viewportHeight="24.00"
>

<path
android:fillColor="#000000"
android:strokeWidth="1"
android:strokeLineJoin="round"
android:pathData="M 22.0025,2.9978L 7.0025,2.9978C 6.30875,2.9978 5.77125,3.35 5.41,3.88125L 0,12.0025L 5.41,20.1125C 5.77125,20.64 6.30875,21.0012 7.0025,21.0012L 22.0025,21.0012C 23.1,21.0012 23.9987,20.0975 23.9987,18.9988L 23.9987,5C 23.9987,3.90125 23.1,2.9978 22.0025,2.9978 Z M 18.9988,15.5913L 17.5875,17.0025L 13.9987,13.4088L 10.41,17.0025L 8.99875,15.5913L 12.5875,12.0025L 8.99875,8.40875L 10.41,7.0025L 13.9987,10.5913L 17.5875,7.0025L 18.9988,8.40875L 15.41,12.0025" />
android:pathData="M 22.0025,2.9978L 7.0025,2.9978C 6.30875,2.9978 5.77125,3.35 5.41,3.88125L 0,12.0025L 5.41,20.1125C 5.77125,20.64 6.30875,21.0012 7.0025,21.0012L 22.0025,21.0012C 23.1,21.0012 23.9987,20.0975 23.9987,18.9988L 23.9987,5C 23.9987,3.90125 23.1,2.9978 22.0025,2.9978 Z M 18.9988,15.5913L 17.5875,17.0025L 13.9987,13.4088L 10.41,17.0025L 8.99875,15.5913L 12.5875,12.0025L 8.99875,8.40875L 10.41,7.0025L 13.9987,10.5913L 17.5875,7.0025L 18.9988,8.40875L 15.41,12.0025"
/>
</vector>
Loading

0 comments on commit 19c851a

Please sign in to comment.