Commit 4d696e62 authored by Ian Hickson's avatar Ian Hickson

Merge pull request #1165 from Hixie/stock-settings

Refactor the Stocks app's settings
parents 71c2ccbf 34cfb608
......@@ -37,6 +37,12 @@ class StocksAppState extends State<StocksApp> {
final Map<String, Stock> _stocks = <String, Stock>{};
final List<String> _symbols = <String>[];
StockConfiguration _configuration = new StockConfiguration(
stockMode: StockMode.optimistic,
backupMode: BackupMode.enabled,
showGrid: false
);
void initState() {
super.initState();
new StockDataFetcher((StockData data) {
......@@ -46,27 +52,14 @@ class StocksAppState extends State<StocksApp> {
});
}
StockMode _optimismSetting = StockMode.optimistic;
BackupMode _backupSetting = BackupMode.disabled;
bool _showGridSetting = false;
void modeUpdater(StockMode optimism) {
setState(() {
_optimismSetting = optimism;
});
}
void settingsUpdater({ StockMode optimism, BackupMode backup, bool showGrid }) {
void configurationUpdater(StockConfiguration value) {
setState(() {
if (optimism != null)
_optimismSetting = optimism;
if (backup != null)
_backupSetting = backup;
if (showGrid != null)
_showGridSetting = showGrid;
_configuration = value;
});
}
ThemeData get theme {
switch (_optimismSetting) {
switch (_configuration.stockMode) {
case StockMode.optimistic:
return new ThemeData(
brightness: ThemeBrightness.light,
......@@ -108,10 +101,10 @@ class StocksAppState extends State<StocksApp> {
return new MaterialApp(
title: 'Stocks',
theme: theme,
debugShowMaterialGrid: _showGridSetting,
debugShowMaterialGrid: _configuration.showGrid,
routes: <String, RouteBuilder>{
'/': (RouteArguments args) => new StockHome(_stocks, _symbols, _optimismSetting, modeUpdater),
'/settings': (RouteArguments args) => new StockSettings(_optimismSetting, _backupSetting, _showGridSetting, settingsUpdater)
'/': (RouteArguments args) => new StockHome(_stocks, _symbols, _configuration, configurationUpdater),
'/settings': (RouteArguments args) => new StockSettings(_configuration, configurationUpdater)
},
onGenerateRoute: _getRoute,
onLocaleChanged: _onLocaleChanged
......
......@@ -9,12 +9,12 @@ typedef void ModeUpdater(StockMode mode);
enum StockHomeTab { market, portfolio }
class StockHome extends StatefulComponent {
StockHome(this.stocks, this.symbols, this.stockMode, this.modeUpdater);
const StockHome(this.stocks, this.symbols, this.configuration, this.updater);
final Map<String, Stock> stocks;
final List<String> symbols;
final StockMode stockMode;
final ModeUpdater modeUpdater;
final StockConfiguration configuration;
final ValueChanged<StockConfiguration> updater;
StockHomeState createState() => new StockHomeState();
}
......@@ -25,10 +25,6 @@ class StockHomeState extends State<StockHome> {
bool _isSearching = false;
String _searchQuery;
void initState() {
super.initState();
}
void _handleSearchBegin() {
ModalRoute.of(context).addLocalHistoryEntry(new LocalHistoryEntry(
onRemove: () {
......@@ -61,8 +57,8 @@ class StockHomeState extends State<StockHome> {
}
void _handleStockModeChange(StockMode value) {
if (config.modeUpdater != null)
config.modeUpdater(value);
if (config.updater != null)
config.updater(config.configuration.copyWith(stockMode: value));
}
void _handleMenuShow() {
......@@ -120,7 +116,7 @@ class StockHomeState extends State<StockHome> {
onPressed: () => _handleStockModeChange(StockMode.optimistic),
child: new Row(<Widget>[
new Flexible(child: new Text('Optimistic')),
new Radio<StockMode>(value: StockMode.optimistic, groupValue: config.stockMode, onChanged: _handleStockModeChange)
new Radio<StockMode>(value: StockMode.optimistic, groupValue: config.configuration.stockMode, onChanged: _handleStockModeChange)
])
),
new DrawerItem(
......@@ -128,7 +124,7 @@ class StockHomeState extends State<StockHome> {
onPressed: () => _handleStockModeChange(StockMode.pessimistic),
child: new Row(<Widget>[
new Flexible(child: new Text('Pessimistic')),
new Radio<StockMode>(value: StockMode.pessimistic, groupValue: config.stockMode, onChanged: _handleStockModeChange)
new Radio<StockMode>(value: StockMode.pessimistic, groupValue: config.configuration.stockMode, onChanged: _handleStockModeChange)
])
),
new DrawerDivider(),
......
......@@ -4,19 +4,11 @@
part of stocks;
typedef void SettingsUpdater({
StockMode optimism,
BackupMode backup,
bool showGrid
});
class StockSettings extends StatefulComponent {
const StockSettings(this.optimism, this.backup, this.showGrid, this.updater);
const StockSettings(this.configuration, this.updater);
final StockMode optimism;
final BackupMode backup;
final SettingsUpdater updater;
final bool showGrid;
final StockConfiguration configuration;
final ValueChanged<StockConfiguration> updater;
StockSettingsState createState() => new StockSettingsState();
}
......@@ -24,19 +16,19 @@ class StockSettings extends StatefulComponent {
class StockSettingsState extends State<StockSettings> {
void _handleOptimismChanged(bool value) {
value ??= false;
sendUpdates(value ? StockMode.optimistic : StockMode.pessimistic, config.backup, config.showGrid);
sendUpdates(config.configuration.copyWith(stockMode: value ? StockMode.optimistic : StockMode.pessimistic));
}
void _handleBackupChanged(bool value) {
sendUpdates(config.optimism, value ? BackupMode.enabled : BackupMode.disabled, config.showGrid);
sendUpdates(config.configuration.copyWith(backupMode: value ? BackupMode.enabled : BackupMode.disabled));
}
void _handleShowGridChanged(bool value) {
sendUpdates(config.optimism, config.backup, value);
sendUpdates(config.configuration.copyWith(showGrid: value));
}
void _confirmOptimismChange() {
switch (config.optimism) {
switch (config.configuration.stockMode) {
case StockMode.optimistic:
_handleOptimismChanged(false);
break;
......@@ -66,13 +58,9 @@ class StockSettingsState extends State<StockSettings> {
}
}
void sendUpdates(StockMode optimism, BackupMode backup, bool showGrid) {
void sendUpdates(StockConfiguration value) {
if (config.updater != null)
config.updater(
optimism: optimism,
backup: backup,
showGrid: showGrid
);
config.updater(value);
}
Widget buildToolBar(BuildContext context) {
......@@ -82,8 +70,6 @@ class StockSettingsState extends State<StockSettings> {
}
Widget buildSettingsPane(BuildContext context) {
// TODO(ianh): Once we have the gesture API hooked up, fix https://github.com/domokit/mojo/issues/281
// (whereby tapping the widgets below causes both the widget and the menu item to fire their callbacks)
List<Widget> rows = <Widget>[
new DrawerItem(
icon: 'action/thumb_up',
......@@ -91,18 +77,18 @@ class StockSettingsState extends State<StockSettings> {
child: new Row(<Widget>[
new Flexible(child: new Text('Everything is awesome')),
new Checkbox(
value: config.optimism == StockMode.optimistic,
value: config.configuration.stockMode == StockMode.optimistic,
onChanged: (bool value) => _confirmOptimismChange()
),
])
),
new DrawerItem(
icon: 'action/backup',
onPressed: () { _handleBackupChanged(!(config.backup == BackupMode.enabled)); },
onPressed: () { _handleBackupChanged(!(config.configuration.backupMode == BackupMode.enabled)); },
child: new Row(<Widget>[
new Flexible(child: new Text('Back up stock list to the cloud')),
new Switch(
value: config.backup == BackupMode.enabled,
value: config.configuration.backupMode == BackupMode.enabled,
onChanged: _handleBackupChanged
),
])
......@@ -113,11 +99,11 @@ class StockSettingsState extends State<StockSettings> {
rows.add(
new DrawerItem(
icon: 'editor/border_clear',
onPressed: () { _handleShowGridChanged(!config.showGrid); },
onPressed: () { _handleShowGridChanged(!config.configuration.showGrid); },
child: new Row(<Widget>[
new Flexible(child: new Text('Show material grid (for debugging)')),
new Switch(
value: config.showGrid,
value: config.configuration.showGrid,
onChanged: _handleShowGridChanged
),
])
......
......@@ -6,3 +6,31 @@ part of stocks;
enum StockMode { optimistic, pessimistic }
enum BackupMode { enabled, disabled }
class StockConfiguration {
StockConfiguration({
this.stockMode,
this.backupMode,
this.showGrid
}) {
assert(stockMode != null);
assert(backupMode != null);
assert(showGrid != null);
}
final StockMode stockMode;
final BackupMode backupMode;
final bool showGrid;
StockConfiguration copyWith({
StockMode stockMode,
BackupMode backupMode,
bool showGrid
}) {
return new StockConfiguration(
stockMode: stockMode ?? this.stockMode,
backupMode: backupMode ?? this.backupMode,
showGrid: showGrid ?? this.showGrid
);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment