55part of fitness;
66
77typedef void SettingsUpdater ({
8- BackupMode backup
8+ BackupMode backup,
9+ double goalWeight
910});
1011
11- class SettingsFragment extends Component {
12+ class SettingsFragment extends StatefulComponent {
1213
1314 SettingsFragment ({ this .navigator, this .userData, this .updater });
1415
15- final Navigator navigator;
16- final UserData userData;
17- final SettingsUpdater updater;
16+ Navigator navigator;
17+ UserData userData;
18+ SettingsUpdater updater;
19+
20+ void syncFields (SettingsFragment source) {
21+ navigator = source.navigator;
22+ userData = source.userData;
23+ updater = source.updater;
24+ }
1825
1926 void _handleBackupChanged (bool value) {
20- if (updater != null )
21- updater (backup: value ? BackupMode .enabled : BackupMode .disabled);
27+ assert (updater != null );
28+ updater (backup: value ? BackupMode .enabled : BackupMode .disabled);
29+ }
30+
31+ void _goalWeightChanged (double value) {
32+ assert (updater != null );
33+ setState (() {
34+ optimism = value ? StockMode .optimistic : StockMode .pessimistic;
35+ });
36+ sendUpdates ();
2237 }
2338
2439 Widget buildToolBar () {
@@ -30,6 +45,64 @@ class SettingsFragment extends Component {
3045 );
3146 }
3247
48+ String get goalWeightText {
49+ if (userData.goalWeight == null || userData.goalWeight == 0.0 )
50+ return "None" ;
51+ else
52+ return "${userData .goalWeight }" ;
53+ }
54+
55+ static final GlobalKey weightGoalKey = new GlobalKey ();
56+
57+ double _goalWeight;
58+
59+ void _handleGoalWeightChanged (String goalWeight) {
60+ // TODO(jackson): Looking for null characters to detect enter key is a hack
61+ if (goalWeight.endsWith ("\u {0}" )) {
62+ navigator.pop (double .parse (goalWeight.replaceAll ("\u {0}" , "" )));
63+ } else {
64+ setState (() {
65+ try {
66+ _goalWeight = double .parse (goalWeight);
67+ } on FormatException {
68+ _goalWeight = 0.0 ;
69+ }
70+ });
71+ }
72+ }
73+
74+ EventDisposition _handleGoalWeightPressed () {
75+ showDialog (navigator, (navigator) {
76+ return new Dialog (
77+ title: new Text ("Goal Weight" ),
78+ content: new Input (
79+ key: weightGoalKey,
80+ placeholder: 'Goal weight in lbs' ,
81+ keyboardType: KeyboardType_NUMBER ,
82+ onChanged: _handleGoalWeightChanged
83+ ),
84+ onDismiss: () {
85+ navigator.pop ();
86+ },
87+ actions: [
88+ new FlatButton (
89+ child: new Text ('CANCEL' ),
90+ onPressed: () {
91+ navigator.pop ();
92+ }
93+ ),
94+ new FlatButton (
95+ child: new Text ('SAVE' ),
96+ onPressed: () {
97+ navigator.pop (_goalWeight);
98+ }
99+ ),
100+ ]
101+ );
102+ }).then ((double goalWeight) => updater (goalWeight: goalWeight));
103+ return EventDisposition .processed;
104+ }
105+
33106 Widget buildSettingsPane () {
34107 return new Material (
35108 type: MaterialType .canvas,
@@ -43,7 +116,16 @@ class SettingsFragment extends Component {
43116 new Flexible (child: new Text ('Back up data to the cloud' )),
44117 new Switch (value: userData.backupMode == BackupMode .enabled, onChanged: _handleBackupChanged)
45118 ]
46- )
119+ ),
120+ new DrawerItem (
121+ onPressed: () => _handleGoalWeightPressed (),
122+ children: [
123+ new Flex ([
124+ new Text ('Goal Weight' ),
125+ new Text (goalWeightText, style: Theme .of (this ).text.caption),
126+ ], direction: FlexDirection .vertical, alignItems: FlexAlignItems .start)
127+ ]
128+ ),
47129 ])
48130 )
49131 )
0 commit comments