Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Support for saving goal weight #713

Merged
merged 1 commit into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions examples/fitness/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ part 'settings.dart';

abstract class UserData {
BackupMode get backupMode;
double get goalWeight;
List<FitnessItem> get items;
}

Expand All @@ -32,31 +33,29 @@ class UserDataImpl extends UserData {

BackupMode _backupMode;
BackupMode get backupMode => _backupMode;
void setBackupModeAndSave(BackupMode value) {
void set backupMode(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 addAndSave(FitnessItem item) {
void add(FitnessItem item) {
_items.add(item);
sort();
save();
}

void removeAndSave(FitnessItem item) {
void remove(FitnessItem item) {
_items.remove(item);
save();
}

Future save() => saveFitnessData(this);
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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();
});
}

Expand Down
98 changes: 90 additions & 8 deletions examples/fitness/lib/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,35 @@
part of fitness;

typedef void SettingsUpdater({
BackupMode backup
BackupMode backup,
double goalWeight
});

class SettingsFragment extends Component {
class SettingsFragment extends StatefulComponent {

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() {
Expand All @@ -30,6 +45,64 @@ class SettingsFragment extends Component {
);
}

String get goalWeightText {
if (userData.goalWeight == null || userData.goalWeight == 0.0)
return "None";
else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else is not needed here.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might reference https://github.com/domokit/sky_engine/issues/574 in the comment.

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: () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

() => navigator.pop(),

navigator.pop();
},
actions: [
new FlatButton(
child: new Text('CANCEL'),
onPressed: () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onPressed: () => navigator.pop()

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,
Expand All @@ -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)
]
),
])
)
)
Expand Down