Skip to content

Commit b301641

Browse files
committed
- improve state management in form
- improve state management in new_wallet_info - remove unnecessary annotations - remove hacks used to force rebuild
1 parent 842c1dc commit b301641

8 files changed

+56
-36
lines changed

lib/utils/form/pin_form_element.dart

+19-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import 'package:cupcake/utils/form/abstract_value_outcome.dart';
44
import 'package:cupcake/utils/secure_storage.dart';
55
import 'package:flutter/cupertino.dart';
66
import 'package:local_auth/local_auth.dart';
7+
import 'package:mobx/mobx.dart';
78

8-
class PinFormElement extends FormElement {
9-
PinFormElement({
9+
part 'pin_form_element.g.dart';
10+
11+
class PinFormElement = PinFormElementBase with _$PinFormElement;
12+
13+
abstract class PinFormElementBase extends FormElement with Store {
14+
PinFormElementBase({
1015
final String initialText = "",
1116
this.password = false,
1217
required this.validator,
@@ -19,6 +24,8 @@ class PinFormElement extends FormElement {
1924
}) : ctrl = TextEditingController(text: initialText),
2025
_errorHandler = errorHandler;
2126
final Future<void> Function(Object e) _errorHandler;
27+
28+
@action
2229
Future<void> loadSecureStorageValue(final VoidCallback callback) async {
2330
if (ctrl.text.isNotEmpty) return;
2431
if (!CupcakeConfig.instance.biometricEnabled) return;
@@ -46,27 +53,33 @@ class PinFormElement extends FormElement {
4653
}
4754

4855
TextEditingController ctrl;
49-
bool password;
50-
bool showNumboard;
56+
57+
final bool password;
58+
final bool showNumboard;
5159

5260
@override
53-
String label;
61+
final String label;
5462

55-
ValueOutcome valueOutcome;
63+
final ValueOutcome valueOutcome;
5664

65+
@computed
5766
@override
5867
Future<String> get value => valueOutcome.decode(ctrl.text);
5968

69+
@computed
6070
@override
6171
bool get isOk => validator(ctrl.text) == null;
6272

6373
Future<void> Function()? onChanged;
6474
Future<void> Function()? onConfirm;
75+
76+
@action
6577
Future<void> onConfirmInternal(final BuildContext context) async {
6678
await valueOutcome.encode(ctrl.text);
6779
isConfirmed = true;
6880
}
6981

82+
@observable
7083
bool isConfirmed = false;
7184
String? Function(String? input) validator;
7285

lib/utils/form/string_form_element.dart

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import 'package:cupcake/utils/form/abstract_form_element.dart';
22
import 'package:flutter/cupertino.dart';
3+
import 'package:mobx/mobx.dart';
34

4-
class StringFormElement extends FormElement {
5-
StringFormElement(
5+
part 'string_form_element.g.dart';
6+
7+
class StringFormElement = StringFormElementBase with _$StringFormElement;
8+
9+
abstract class StringFormElementBase extends FormElement with Store {
10+
StringFormElementBase(
611
this.label, {
712
final String initialText = "",
813
this.password = false,
@@ -19,12 +24,17 @@ class StringFormElement extends FormElement {
1924
bool password;
2025
@override
2126
String label;
27+
28+
@computed
2229
@override
2330
Future<String> get value => Future.value(ctrl.text);
2431

32+
@observable
2533
bool isExtra;
34+
2635
bool randomNameGenerator;
2736

37+
@computed
2838
@override
2939
bool get isOk => validator(ctrl.text) == null;
3040

lib/view_model/barcode_scanner_view_model.dart

-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ abstract class BarcodeScannerViewModelBase extends ViewModel with Store {
2727

2828
final MobileScannerController mobileScannerCtrl = MobileScannerController();
2929

30-
@action
3130
Future<void> handleUR() async {
3231
await callThrowable(
3332
() async {
@@ -59,7 +58,6 @@ abstract class BarcodeScannerViewModelBase extends ViewModel with Store {
5958
}
6059
}
6160

62-
@action
6361
List<int> urParts() {
6462
final List<int> l = [];
6563
for (final inp in URQRData.parse(urCodes).inputs) {

lib/view_model/create_wallet_view_model.dart

-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ abstract class CreateWalletViewModelBase extends ViewModel with Store {
140140

141141
final bool needsPasswordConfirm;
142142

143-
@action
144143
Future<void> completeSetup(final CoinWallet cw) async {
145144
await callThrowable(
146145
() async {
@@ -152,7 +151,6 @@ abstract class CreateWalletViewModelBase extends ViewModel with Store {
152151
);
153152
}
154153

155-
@action
156154
Future<void> createWallet() async {
157155
await callThrowable(
158156
() async {
@@ -162,7 +160,6 @@ abstract class CreateWalletViewModelBase extends ViewModel with Store {
162160
);
163161
}
164162

165-
@action
166163
Future<void> _createWallet() async {
167164
if (selectedCoin == null) throw Exception(L.error_selected_coin_null);
168165
if ((await walletName.value).isEmpty) {

lib/view_model/form_builder_view_model.dart

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ abstract class FormBuilderViewModelBase extends ViewModel with Store {
1818
_toggleIsPinSet = toggleIsPinSet,
1919
_isPinSet = isPinSet;
2020

21-
// Used to force a rebuild
22-
@observable
23-
int currentPageDoNotUse = 0;
24-
2521
@observable
2622
List<FormElement> formElements;
2723

lib/views/abstract.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class AbstractView extends StatefulWidget {
6262

6363
State<AbstractView>? state;
6464

65-
AppBar? get appBar => viewModel.screenName.isEmpty
65+
PreferredSizeWidget? get appBar => viewModel.screenName.isEmpty
6666
? null
6767
: AppBar(
6868
title: viewModel.screenName.toLowerCase() != "cupcake"

lib/views/new_wallet_info.dart

+24-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:cupcake/view_model/new_wallet_info_view_model.dart';
44
import 'package:cupcake/views/abstract.dart';
55
import 'package:cupcake/views/widgets/buttons/long_primary.dart';
66
import 'package:flutter/material.dart';
7+
import 'package:flutter_mobx/flutter_mobx.dart';
78

89
class NewWalletInfoScreen extends AbstractView {
910
NewWalletInfoScreen({
@@ -18,10 +19,15 @@ class NewWalletInfoScreen extends AbstractView {
1819
bool get canPop => false;
1920

2021
@override
21-
AppBar? get appBar => AppBar(
22-
title: Text(viewModel.screenName),
23-
automaticallyImplyLeading: canPop,
24-
actions: _getActionButton(),
22+
PreferredSizeWidget? get appBar => PreferredSize(
23+
preferredSize: const Size.fromHeight(kToolbarHeight),
24+
child: Observer(
25+
builder: (final context) => AppBar(
26+
title: Text(viewModel.screenName),
27+
automaticallyImplyLeading: canPop,
28+
actions: _getActionButton(),
29+
),
30+
),
2531
);
2632

2733
List<Widget>? _getActionButton() {
@@ -70,19 +76,21 @@ class NewWalletInfoScreen extends AbstractView {
7076
return SafeArea(
7177
child: Padding(
7278
padding: const EdgeInsets.only(left: 32, right: 32, top: 0, bottom: 16),
73-
child: Column(
74-
children: [
75-
if (viewModel.page.lottieAnimation != null) viewModel.page.lottieAnimation!,
76-
...viewModel.page.texts,
77-
const Spacer(),
78-
SizedBox(
79-
width: double.maxFinite,
80-
child: Row(
81-
mainAxisSize: MainAxisSize.max,
82-
children: _getBottomActionButtons(),
79+
child: Observer(
80+
builder: (final context) => Column(
81+
children: [
82+
if (viewModel.page.lottieAnimation != null) viewModel.page.lottieAnimation!,
83+
...viewModel.page.texts,
84+
const Spacer(),
85+
SizedBox(
86+
width: double.maxFinite,
87+
child: Row(
88+
mainAxisSize: MainAxisSize.max,
89+
children: _getBottomActionButtons(),
90+
),
8391
),
84-
),
85-
],
92+
],
93+
),
8694
),
8795
),
8896
);

lib/views/widgets/form_builder.dart

-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class FormBuilder extends StatelessWidget {
5151
}
5252

5353
Widget _build(final BuildContext context) {
54-
final _ = viewModel.currentPageDoNotUse; // very silly way to force a rebuild
5554
if (_displayPinFormElement()) {
5655
var e = viewModel.formElements.first as PinFormElement;
5756
int i = 0;
@@ -68,7 +67,6 @@ class FormBuilder extends StatelessWidget {
6867
try {
6968
await e.onConfirmInternal(context);
7069
if (!context.mounted) return;
71-
viewModel.currentPageDoNotUse++;
7270
viewModel.isPinSet = (count == i);
7371
await e.onConfirm?.call();
7472
} catch (err) {

0 commit comments

Comments
 (0)