Skip to content

Commit 19c851a

Browse files
committed
Nullibility and style fixes
1 parent e2d7025 commit 19c851a

File tree

15 files changed

+156
-107
lines changed

15 files changed

+156
-107
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v1.4.1
2+
- Improved material components compatibility.
3+
- Fixed custom button style not working.
4+
15
## v1.4.0
26
- Migrated to AndroidX.
37

app/src/main/java/com/nmaltais/calcdialoglib/MainActivity.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,18 @@ protected void onSaveInstanceState(Bundle state) {
156156
}
157157

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

162162
this.value = value;
163163

164-
valueTxv.setText(value.toPlainString());
165-
signChk.setEnabled(value.compareTo(BigDecimal.ZERO) != 0);
164+
if (value == null) {
165+
valueTxv.setText(R.string.result_value_none);
166+
signChk.setEnabled(false);
167+
} else {
168+
valueTxv.setText(value.toPlainString());
169+
signChk.setEnabled(value.compareTo(BigDecimal.ZERO) != 0);
170+
}
166171
}
167172
}
168173

calcdialog/bintray.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ task javadocJar(type: Jar, dependsOn: javadoc) {
1919
from javadoc.destinationDir
2020
}
2121

22-
afterEvaluate {
23-
javadoc.classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
24-
}
25-
2622
artifacts {
2723
archives javadocJar
2824
archives sourcesJar

calcdialog/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ ext {
1313
siteUrl = 'https://github.com/maltaisn/calcdialoglib'
1414
gitUrl = 'https://github.com/maltaisn/calcdialoglib.git'
1515

16-
libraryVersionCode = 10
17-
libraryVersion = '1.4.0'
16+
libraryVersionCode = 11
17+
libraryVersion = '1.4.1'
1818

1919
developerId = 'maltaisn'
2020

calcdialog/src/main/java/com/nmaltais/calcdialog/CalcDialog.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
import android.graphics.Rect;
3131
import android.os.Build;
3232
import android.os.Bundle;
33-
import androidx.annotation.NonNull;
34-
import androidx.annotation.Nullable;
35-
import androidx.appcompat.app.AppCompatDialogFragment;
3633
import android.util.DisplayMetrics;
3734
import android.view.ContextThemeWrapper;
3835
import android.view.LayoutInflater;
@@ -47,6 +44,10 @@
4744
import java.math.RoundingMode;
4845
import java.util.Locale;
4946

47+
import androidx.annotation.NonNull;
48+
import androidx.annotation.Nullable;
49+
import androidx.appcompat.app.AppCompatDialogFragment;
50+
5051

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

154155
@SuppressLint("InflateParams")
155156
@Override
156-
public @NonNull Dialog onCreateDialog(final Bundle state) {
157+
public @NonNull
158+
Dialog onCreateDialog(final Bundle state) {
157159
LayoutInflater inflater = LayoutInflater.from(context);
158160
final View view = inflater.inflate(R.layout.dialog_calc, null);
159161

@@ -328,7 +330,9 @@ public void onSaveInstanceState(Bundle state) {
328330
@Override
329331
public void onDetach() {
330332
super.onDetach();
331-
presenter.detach();
333+
if (presenter != null) {
334+
presenter.detach();
335+
}
332336

333337
presenter = null;
334338
context = null;
@@ -409,6 +413,7 @@ void displayAnswerText() {
409413
}
410414

411415
////////// CALCULATOR SETTINGS //////////
416+
412417
/**
413418
* Set initial value to show
414419
* By default, initial value is null. That means value is 0 but if
@@ -437,7 +442,7 @@ public CalcDialog setMaxValue(@Nullable BigDecimal maxValue) {
437442
/**
438443
* Set max digits that can be entered on the calculator
439444
* Use {@link #MAX_DIGITS_UNLIMITED} for no limit
440-
* @param intPart Max digits for the integer part
445+
* @param intPart Max digits for the integer part
441446
* @param fracPart Max digits for the fractional part.
442447
* A value of 0 means the value can't have a fractional part
443448
* @return the dialog
@@ -465,8 +470,8 @@ public CalcDialog setRoundingMode(RoundingMode roundingMode) {
465470
* @param canBeChanged whether sign can be changed or not
466471
* if true, dialog can't be confirmed with a value of wrong sign
467472
* and an error will be shown
468-
* @param sign if canBeChanged is true, sign to force, -1 or 1
469-
* otherwise use any value
473+
* @param sign if canBeChanged is true, sign to force, -1 or 1
474+
* otherwise use any value
470475
* @return the dialog
471476
*/
472477
public CalcDialog setSignCanBeChanged(boolean canBeChanged, int sign) {
@@ -479,7 +484,7 @@ public CalcDialog setSignCanBeChanged(boolean canBeChanged, int sign) {
479484
* Use {@link #FORMAT_CHAR_DEFAULT} to use device locale's default symbol
480485
* By default, formatting will use locale's symbols
481486
* @param decimalSep decimal separator
482-
* @param groupSep grouping separator
487+
* @param groupSep grouping separator
483488
* @return the dialog
484489
*/
485490
public CalcDialog setFormatSymbols(char decimalSep, char groupSep) {
@@ -548,14 +553,15 @@ public CalcDialog setShowSignButton(boolean show) {
548553
public interface CalcDialogCallback {
549554
/**
550555
* Called when the dialog's OK button is clicked
551-
* @param value value entered.
552-
* To format the value to a String, use {@link BigDecimal#toPlainString()}.
553-
* To format the value to a currency String you could do:
554-
* {@code NumberFormat.getCurrencyInstance(Locale).format(BigDecimal)}
556+
* @param value value entered. May be null if no value was entered, in this case,
557+
* it should be interpreted as zero or no value.
558+
* To format the value to a String, use {@link BigDecimal#toPlainString()}.
559+
* To format the value to a currency String you could do:
560+
* {@code NumberFormat.getCurrencyInstance(Locale).format(BigDecimal)}
555561
* @param requestCode dialog request code given when dialog
556562
* was created with {@link #newInstance(int)}
557563
*/
558-
void onValueEntered(int requestCode, BigDecimal value);
564+
void onValueEntered(int requestCode, @Nullable BigDecimal value);
559565
}
560566

561567
}

calcdialog/src/main/java/com/nmaltais/calcdialog/CalcDialogUtils.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
import android.content.Context;
44
import android.os.Build;
5-
import androidx.annotation.NonNull;
6-
import androidx.annotation.Nullable;
75

86
import java.math.BigDecimal;
97
import java.util.Locale;
108

9+
import androidx.annotation.NonNull;
10+
import androidx.annotation.Nullable;
11+
1112
class CalcDialogUtils {
1213

1314
/**
1415
* Checks if a BigDecimal exceeds maximum value
1516
* @param value value to check for
1617
* @return true if value is greater than maximum value
17-
* maximum value is applied equally for positive and negative value
18+
* maximum value is applied equally for positive and negative value
1819
*/
1920
static boolean isValueOutOfBounds(@NonNull BigDecimal value, @Nullable BigDecimal maxValue) {
2021
return maxValue != null && (value.compareTo(maxValue) > 0 ||
@@ -27,9 +28,9 @@ static boolean isValueOutOfBounds(@NonNull BigDecimal value, @Nullable BigDecima
2728
* @return the default locale
2829
*/
2930
static Locale getDefaultLocale(Context context) {
30-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
31+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
3132
return context.getResources().getConfiguration().getLocales().get(0);
32-
} else{
33+
} else {
3334
//noinspection deprecation
3435
return context.getResources().getConfiguration().locale;
3536
}

calcdialog/src/main/java/com/nmaltais/calcdialog/CalcEraseButton.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@
2626
import android.content.Context;
2727
import android.content.res.TypedArray;
2828
import android.os.Handler;
29-
import androidx.annotation.Nullable;
30-
import androidx.appcompat.widget.AppCompatImageView;
3129
import android.util.AttributeSet;
3230
import android.view.HapticFeedbackConstants;
3331
import android.view.MotionEvent;
3432

33+
import androidx.annotation.Nullable;
34+
import androidx.appcompat.widget.AppCompatImageView;
35+
3536
/**
3637
* ImageView that triggers erase events when held down
3738
* Attributes:
3839
* - eraseBtnHoldDelay: Time view has to be held down to trigger quick erase (in ms)
39-
* Default value is 750ms. Use -1 for no quick erase and 0 for no delay
40+
* Default value is 750ms. Use -1 for no quick erase and 0 for no delay
4041
* - eraseBtnHoldSpeed: Time after which an erase event is triggered in quick erase mode (in ms)
41-
* Default value is 100ms
42+
* Default value is 100ms
4243
* - eraseAllOnHold: If true, holding button will trigger an erase all event instead of quick
43-
* erase mode if false. By default this is false.
44+
* erase mode if false. By default this is false.
4445
*/
4546
class CalcEraseButton extends AppCompatImageView {
4647

calcdialog/src/main/java/com/nmaltais/calcdialog/CalcPresenter.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.nmaltais.calcdialog;
22

33
import android.os.Bundle;
4-
import androidx.annotation.Nullable;
54

65
import java.math.BigDecimal;
76
import java.text.DecimalFormatSymbols;
87
import java.util.Locale;
98

10-
public class CalcPresenter {
9+
import androidx.annotation.NonNull;
10+
import androidx.annotation.Nullable;
11+
12+
class CalcPresenter {
1113

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

9799
void detach() {
98100
view = null;
101+
settings = null;
99102
}
100103

101104
void writeStateToBundle(Bundle bundle) {
@@ -117,11 +120,14 @@ void writeStateToBundle(Bundle bundle) {
117120
private void readStateFromBundle(Bundle bundle) {
118121
operation = bundle.getInt("operation");
119122
error = bundle.getInt("error");
120-
valueStr = new StringBuilder(bundle.getString("valueStr"));
123+
String value = bundle.getString("valueStr");
121124
resultIsDisplayed = bundle.getBoolean("resultIsDisplayed");
122125
overwriteValue = bundle.getBoolean("overwriteValue");
123126
currentIsAnswer = bundle.getBoolean("currentIsAnswer");
124127

128+
assert value != null;
129+
valueStr = new StringBuilder();
130+
125131
if (bundle.containsKey("resultValue")) {
126132
resultValue = new BigDecimal(bundle.getString("resultValue"));
127133
}
@@ -192,7 +198,7 @@ void onDigitBtnClicked(int digit) {
192198
|| valueStr.length() - pointPos - 1 < settings.maxFracDigits));
193199
boolean isValueZero = (pointPos == -1 && valueStr.length() == 1 && valueStr.charAt(0) == '0');
194200

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

275281
if (resultIsDisplayed) {
276-
//noinspection ConstantConditions
282+
assert resultValue != null && answerValue != null;
277283
resultValue = resultValue.negate();
278-
//noinspection ConstantConditions
279284
answerValue = answerValue.negate();
280285
}
281286

@@ -372,7 +377,7 @@ private void calculate() {
372377
BigDecimal operand = getCurrentValue();
373378

374379
if (operation == OPERATION_ADD) {
375-
//noinspection ConstantConditions
380+
assert resultValue != null;
376381
resultValue = resultValue.add(operand);
377382
} else if (operation == OPERATION_SUB) {
378383
resultValue = resultValue.subtract(operand);

calcdialog/src/main/java/com/nmaltais/calcdialog/CalcSettings.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package com.nmaltais.calcdialog;
22

33
import android.os.Bundle;
4-
import androidx.annotation.Nullable;
54

65
import java.math.BigDecimal;
76
import java.math.RoundingMode;
87

8+
import androidx.annotation.NonNull;
9+
import androidx.annotation.Nullable;
10+
911
class CalcSettings {
1012

1113
int requestCode;
1214

13-
BigDecimal initialValue;
15+
@Nullable BigDecimal initialValue;
1416

1517
@Nullable BigDecimal maxValue;
1618

1719
int maxIntDigits;
1820
int maxFracDigits;
1921

20-
RoundingMode roundingMode;
22+
@NonNull RoundingMode roundingMode;
2123

2224
boolean signCanBeChanged;
2325
int initialSign;
@@ -90,7 +92,7 @@ void readFromBundle(Bundle bundle) {
9092
}
9193

9294
void setValue(@Nullable BigDecimal value) {
93-
if (value != null && maxValue != null && CalcDialogUtils.isValueOutOfBounds(value, maxValue)) {
95+
if (value != null && CalcDialogUtils.isValueOutOfBounds(value, maxValue)) {
9496
value = (value.compareTo(BigDecimal.ZERO) > 0 ? maxValue : maxValue.negate());
9597
}
9698
initialValue = value;

calcdialog/src/main/res/drawable/calc_bg_elevation.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
android:left="0dp"
1212
android:right="0dp"
1313
android:top="0dp"
14-
android:bottom="2dp">
14+
android:bottom="2dp"
15+
>
1516
<shape android:shape="rectangle">
1617
<solid android:color="#ffffff"/>
1718
</shape>

0 commit comments

Comments
 (0)