settings.dart 3.63 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
part of fitness;
6 7

typedef void SettingsUpdater({
8 9
  BackupMode backup,
  double goalWeight
10 11
});

12
class SettingsFragment extends StatefulComponent {
13

14
  SettingsFragment({ this.navigator, this.userData, this.updater });
15

16 17 18 19
  Navigator navigator;
  UserData userData;
  SettingsUpdater updater;

20
  void syncConstructorArguments(SettingsFragment source) {
21 22 23 24
    navigator = source.navigator;
    userData = source.userData;
    updater = source.updater;
  }
25 26

  void _handleBackupChanged(bool value) {
27 28 29 30
    assert(updater != null);
    updater(backup: value ? BackupMode.enabled : BackupMode.disabled);
  }

31 32 33 34 35 36 37 38 39
  Widget buildToolBar() {
    return new ToolBar(
      left: new IconButton(
        icon: "navigation/arrow_back",
        onPressed: navigator.pop),
      center: new Text('Settings')
    );
  }

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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;
  }

98 99 100 101 102 103
  Widget buildSettingsPane() {
    return new Material(
      type: MaterialType.canvas,
      child: new ScrollableViewport(
        child: new Container(
          padding: const EdgeDims.symmetric(vertical: 20.0),
104
          child: new BlockBody([
105
            new DrawerItem(
106
              onPressed: () { _handleBackupChanged(!(userData.backupMode == BackupMode.enabled)); },
107
              child: new Flex([
108
                new Flexible(child: new Text('Back up data to the cloud')),
109 110
                new Switch(value: userData.backupMode == BackupMode.enabled, onChanged: _handleBackupChanged),
              ])
111 112 113
            ),
            new DrawerItem(
              onPressed: () => _handleGoalWeightPressed(),
114
              child: new Flex([
115 116 117 118 119 120
                  new Text('Goal Weight'),
                  new Text(goalWeightText, style: Theme.of(this).text.caption),
                ],
                direction: FlexDirection.vertical,
                alignItems: FlexAlignItems.start
              )
121
            ),
122 123 124 125 126 127 128 129 130 131 132 133 134
          ])
        )
      )
    );
  }

  Widget build() {
    return new Scaffold(
      toolbar: buildToolBar(),
      body: buildSettingsPane()
    );
  }
}