Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
1217f3b5
Commit
1217f3b5
authored
Aug 20, 2015
by
Collin Jackson
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #713 from collinjackson/goalweight
Support for saving goal weight
parents
de966121
3e3f6242
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
115 additions
and
23 deletions
+115
-23
main.dart
examples/fitness/lib/main.dart
+25
-15
settings.dart
examples/fitness/lib/settings.dart
+90
-8
No files found.
examples/fitness/lib/main.dart
View file @
1217f3b5
...
...
@@ -22,6 +22,7 @@ part 'settings.dart';
abstract
class
UserData
{
BackupMode
get
backupMode
;
double
get
goalWeight
;
List
<
FitnessItem
>
get
items
;
}
...
...
@@ -32,31 +33,29 @@ class UserDataImpl extends UserData {
BackupMode
_backupMode
;
BackupMode
get
backupMode
=>
_backupMode
;
void
set
BackupModeAndSav
e
(
BackupMode
value
)
{
void
set
backupMod
e
(
BackupMode
value
)
{
_backupMode
=
value
;
save
();
}
List
<
FitnessItem
>
get
items
=>
_items
;
void
set
items
(
List
<
FitnessItem
>
newItems
)
{
_items
=
[];
_items
.
addAll
(
newItems
);
sort
();
double
_goalWeight
;
double
get
goalWeight
=>
_goalWeight
;
void
set
goalWeight
(
double
value
)
{
_goalWeight
=
value
;
}
List
<
FitnessItem
>
get
items
=>
_items
;
void
sort
()
{
_items
.
sort
((
a
,
b
)
=>
-
a
.
when
.
compareTo
(
b
.
when
));
}
void
add
AndSave
(
FitnessItem
item
)
{
void
add
(
FitnessItem
item
)
{
_items
.
add
(
item
);
sort
();
save
();
}
void
remove
AndSave
(
FitnessItem
item
)
{
void
remove
(
FitnessItem
item
)
{
_items
.
remove
(
item
);
save
();
}
Future
save
()
=>
saveFitnessData
(
this
);
...
...
@@ -72,12 +71,14 @@ class UserDataImpl extends UserData {
}
catch
(
e
)
{
print
(
"Failed to load backup mode:
${e}
"
);
}
_goalWeight
=
json
[
'goalWeight'
];
}
Map
toJson
()
{
Map
json
=
new
Map
();
json
[
'items'
]
=
_items
.
map
((
item
)
=>
item
.
toJson
()).
toList
();
json
[
'backupMode'
]
=
_backupMode
.
toString
();
json
[
'goalWeight'
]
=
_goalWeight
;
return
json
;
}
}
...
...
@@ -139,17 +140,26 @@ class FitnessApp extends App {
}
void
_handleItemCreated
(
FitnessItem
item
)
{
setState
(()
=>
_userData
.
addAndSave
(
item
));
setState
(()
{
_userData
.
add
(
item
);
_userData
.
save
();
});
}
void
_handleItemDeleted
(
FitnessItem
item
)
{
setState
(()
=>
_userData
.
removeAndSave
(
item
));
setState
(()
{
_userData
.
remove
(
item
);
_userData
.
save
();
});
}
void
settingsUpdater
({
BackupMode
backup
})
{
void
settingsUpdater
({
BackupMode
backup
,
double
goalWeight
})
{
setState
(()
{
if
(
backup
!=
null
)
_userData
.
setBackupModeAndSave
(
backup
);
_userData
.
backupMode
=
backup
;
if
(
goalWeight
!=
null
)
_userData
.
goalWeight
=
goalWeight
;
_userData
.
save
();
});
}
...
...
examples/fitness/lib/settings.dart
View file @
1217f3b5
...
...
@@ -5,20 +5,35 @@
part of
fitness
;
typedef
void
SettingsUpdater
(
{
BackupMode
backup
BackupMode
backup
,
double
goalWeight
});
class
SettingsFragment
extends
Component
{
class
SettingsFragment
extends
Stateful
Component
{
SettingsFragment
({
this
.
navigator
,
this
.
userData
,
this
.
updater
});
final
Navigator
navigator
;
final
UserData
userData
;
final
SettingsUpdater
updater
;
Navigator
navigator
;
UserData
userData
;
SettingsUpdater
updater
;
void
syncFields
(
SettingsFragment
source
)
{
navigator
=
source
.
navigator
;
userData
=
source
.
userData
;
updater
=
source
.
updater
;
}
void
_handleBackupChanged
(
bool
value
)
{
if
(
updater
!=
null
)
updater
(
backup:
value
?
BackupMode
.
enabled
:
BackupMode
.
disabled
);
assert
(
updater
!=
null
);
updater
(
backup:
value
?
BackupMode
.
enabled
:
BackupMode
.
disabled
);
}
void
_goalWeightChanged
(
double
value
)
{
assert
(
updater
!=
null
);
setState
(()
{
optimism
=
value
?
StockMode
.
optimistic
:
StockMode
.
pessimistic
;
});
sendUpdates
();
}
Widget
buildToolBar
()
{
...
...
@@ -30,6 +45,64 @@ class SettingsFragment extends Component {
);
}
String
get
goalWeightText
{
if
(
userData
.
goalWeight
==
null
||
userData
.
goalWeight
==
0.0
)
return
"None"
;
else
return
"
${userData.goalWeight}
"
;
}
static
final
GlobalKey
weightGoalKey
=
new
GlobalKey
();
double
_goalWeight
;
void
_handleGoalWeightChanged
(
String
goalWeight
)
{
// TODO(jackson): Looking for null characters to detect enter key is a hack
if
(
goalWeight
.
endsWith
(
"
\
u{0}"
))
{
navigator
.
pop
(
double
.
parse
(
goalWeight
.
replaceAll
(
"
\
u{0}"
,
""
)));
}
else
{
setState
(()
{
try
{
_goalWeight
=
double
.
parse
(
goalWeight
);
}
on
FormatException
{
_goalWeight
=
0.0
;
}
});
}
}
EventDisposition
_handleGoalWeightPressed
()
{
showDialog
(
navigator
,
(
navigator
)
{
return
new
Dialog
(
title:
new
Text
(
"Goal Weight"
),
content:
new
Input
(
key:
weightGoalKey
,
placeholder:
'Goal weight in lbs'
,
keyboardType:
KeyboardType_NUMBER
,
onChanged:
_handleGoalWeightChanged
),
onDismiss:
()
{
navigator
.
pop
();
},
actions:
[
new
FlatButton
(
child:
new
Text
(
'CANCEL'
),
onPressed:
()
{
navigator
.
pop
();
}
),
new
FlatButton
(
child:
new
Text
(
'SAVE'
),
onPressed:
()
{
navigator
.
pop
(
_goalWeight
);
}
),
]
);
}).
then
((
double
goalWeight
)
=>
updater
(
goalWeight:
goalWeight
));
return
EventDisposition
.
processed
;
}
Widget
buildSettingsPane
()
{
return
new
Material
(
type:
MaterialType
.
canvas
,
...
...
@@ -43,7 +116,16 @@ class SettingsFragment extends Component {
new
Flexible
(
child:
new
Text
(
'Back up data to the cloud'
)),
new
Switch
(
value:
userData
.
backupMode
==
BackupMode
.
enabled
,
onChanged:
_handleBackupChanged
)
]
)
),
new
DrawerItem
(
onPressed:
()
=>
_handleGoalWeightPressed
(),
children:
[
new
Flex
([
new
Text
(
'Goal Weight'
),
new
Text
(
goalWeightText
,
style:
Theme
.
of
(
this
).
text
.
caption
),
],
direction:
FlexDirection
.
vertical
,
alignItems:
FlexAlignItems
.
start
)
]
),
])
)
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment