mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Refactor the Stocks app's settings
This will make it much easier to add more settings.
This commit is contained in:
parent
71c2ccbf66
commit
34cfb6082a
@ -37,6 +37,12 @@ class StocksAppState extends State<StocksApp> {
|
|||||||
final Map<String, Stock> _stocks = <String, Stock>{};
|
final Map<String, Stock> _stocks = <String, Stock>{};
|
||||||
final List<String> _symbols = <String>[];
|
final List<String> _symbols = <String>[];
|
||||||
|
|
||||||
|
StockConfiguration _configuration = new StockConfiguration(
|
||||||
|
stockMode: StockMode.optimistic,
|
||||||
|
backupMode: BackupMode.enabled,
|
||||||
|
showGrid: false
|
||||||
|
);
|
||||||
|
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
new StockDataFetcher((StockData data) {
|
new StockDataFetcher((StockData data) {
|
||||||
@ -46,27 +52,14 @@ class StocksAppState extends State<StocksApp> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
StockMode _optimismSetting = StockMode.optimistic;
|
void configurationUpdater(StockConfiguration value) {
|
||||||
BackupMode _backupSetting = BackupMode.disabled;
|
|
||||||
bool _showGridSetting = false;
|
|
||||||
void modeUpdater(StockMode optimism) {
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_optimismSetting = optimism;
|
_configuration = value;
|
||||||
});
|
|
||||||
}
|
|
||||||
void settingsUpdater({ StockMode optimism, BackupMode backup, bool showGrid }) {
|
|
||||||
setState(() {
|
|
||||||
if (optimism != null)
|
|
||||||
_optimismSetting = optimism;
|
|
||||||
if (backup != null)
|
|
||||||
_backupSetting = backup;
|
|
||||||
if (showGrid != null)
|
|
||||||
_showGridSetting = showGrid;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ThemeData get theme {
|
ThemeData get theme {
|
||||||
switch (_optimismSetting) {
|
switch (_configuration.stockMode) {
|
||||||
case StockMode.optimistic:
|
case StockMode.optimistic:
|
||||||
return new ThemeData(
|
return new ThemeData(
|
||||||
brightness: ThemeBrightness.light,
|
brightness: ThemeBrightness.light,
|
||||||
@ -108,10 +101,10 @@ class StocksAppState extends State<StocksApp> {
|
|||||||
return new MaterialApp(
|
return new MaterialApp(
|
||||||
title: 'Stocks',
|
title: 'Stocks',
|
||||||
theme: theme,
|
theme: theme,
|
||||||
debugShowMaterialGrid: _showGridSetting,
|
debugShowMaterialGrid: _configuration.showGrid,
|
||||||
routes: <String, RouteBuilder>{
|
routes: <String, RouteBuilder>{
|
||||||
'/': (RouteArguments args) => new StockHome(_stocks, _symbols, _optimismSetting, modeUpdater),
|
'/': (RouteArguments args) => new StockHome(_stocks, _symbols, _configuration, configurationUpdater),
|
||||||
'/settings': (RouteArguments args) => new StockSettings(_optimismSetting, _backupSetting, _showGridSetting, settingsUpdater)
|
'/settings': (RouteArguments args) => new StockSettings(_configuration, configurationUpdater)
|
||||||
},
|
},
|
||||||
onGenerateRoute: _getRoute,
|
onGenerateRoute: _getRoute,
|
||||||
onLocaleChanged: _onLocaleChanged
|
onLocaleChanged: _onLocaleChanged
|
||||||
|
@ -9,12 +9,12 @@ typedef void ModeUpdater(StockMode mode);
|
|||||||
enum StockHomeTab { market, portfolio }
|
enum StockHomeTab { market, portfolio }
|
||||||
|
|
||||||
class StockHome extends StatefulComponent {
|
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 Map<String, Stock> stocks;
|
||||||
final List<String> symbols;
|
final List<String> symbols;
|
||||||
final StockMode stockMode;
|
final StockConfiguration configuration;
|
||||||
final ModeUpdater modeUpdater;
|
final ValueChanged<StockConfiguration> updater;
|
||||||
|
|
||||||
StockHomeState createState() => new StockHomeState();
|
StockHomeState createState() => new StockHomeState();
|
||||||
}
|
}
|
||||||
@ -25,10 +25,6 @@ class StockHomeState extends State<StockHome> {
|
|||||||
bool _isSearching = false;
|
bool _isSearching = false;
|
||||||
String _searchQuery;
|
String _searchQuery;
|
||||||
|
|
||||||
void initState() {
|
|
||||||
super.initState();
|
|
||||||
}
|
|
||||||
|
|
||||||
void _handleSearchBegin() {
|
void _handleSearchBegin() {
|
||||||
ModalRoute.of(context).addLocalHistoryEntry(new LocalHistoryEntry(
|
ModalRoute.of(context).addLocalHistoryEntry(new LocalHistoryEntry(
|
||||||
onRemove: () {
|
onRemove: () {
|
||||||
@ -61,8 +57,8 @@ class StockHomeState extends State<StockHome> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _handleStockModeChange(StockMode value) {
|
void _handleStockModeChange(StockMode value) {
|
||||||
if (config.modeUpdater != null)
|
if (config.updater != null)
|
||||||
config.modeUpdater(value);
|
config.updater(config.configuration.copyWith(stockMode: value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _handleMenuShow() {
|
void _handleMenuShow() {
|
||||||
@ -120,7 +116,7 @@ class StockHomeState extends State<StockHome> {
|
|||||||
onPressed: () => _handleStockModeChange(StockMode.optimistic),
|
onPressed: () => _handleStockModeChange(StockMode.optimistic),
|
||||||
child: new Row(<Widget>[
|
child: new Row(<Widget>[
|
||||||
new Flexible(child: new Text('Optimistic')),
|
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(
|
new DrawerItem(
|
||||||
@ -128,7 +124,7 @@ class StockHomeState extends State<StockHome> {
|
|||||||
onPressed: () => _handleStockModeChange(StockMode.pessimistic),
|
onPressed: () => _handleStockModeChange(StockMode.pessimistic),
|
||||||
child: new Row(<Widget>[
|
child: new Row(<Widget>[
|
||||||
new Flexible(child: new Text('Pessimistic')),
|
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(),
|
new DrawerDivider(),
|
||||||
|
@ -4,19 +4,11 @@
|
|||||||
|
|
||||||
part of stocks;
|
part of stocks;
|
||||||
|
|
||||||
typedef void SettingsUpdater({
|
|
||||||
StockMode optimism,
|
|
||||||
BackupMode backup,
|
|
||||||
bool showGrid
|
|
||||||
});
|
|
||||||
|
|
||||||
class StockSettings extends StatefulComponent {
|
class StockSettings extends StatefulComponent {
|
||||||
const StockSettings(this.optimism, this.backup, this.showGrid, this.updater);
|
const StockSettings(this.configuration, this.updater);
|
||||||
|
|
||||||
final StockMode optimism;
|
final StockConfiguration configuration;
|
||||||
final BackupMode backup;
|
final ValueChanged<StockConfiguration> updater;
|
||||||
final SettingsUpdater updater;
|
|
||||||
final bool showGrid;
|
|
||||||
|
|
||||||
StockSettingsState createState() => new StockSettingsState();
|
StockSettingsState createState() => new StockSettingsState();
|
||||||
}
|
}
|
||||||
@ -24,19 +16,19 @@ class StockSettings extends StatefulComponent {
|
|||||||
class StockSettingsState extends State<StockSettings> {
|
class StockSettingsState extends State<StockSettings> {
|
||||||
void _handleOptimismChanged(bool value) {
|
void _handleOptimismChanged(bool value) {
|
||||||
value ??= false;
|
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) {
|
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) {
|
void _handleShowGridChanged(bool value) {
|
||||||
sendUpdates(config.optimism, config.backup, value);
|
sendUpdates(config.configuration.copyWith(showGrid: value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _confirmOptimismChange() {
|
void _confirmOptimismChange() {
|
||||||
switch (config.optimism) {
|
switch (config.configuration.stockMode) {
|
||||||
case StockMode.optimistic:
|
case StockMode.optimistic:
|
||||||
_handleOptimismChanged(false);
|
_handleOptimismChanged(false);
|
||||||
break;
|
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)
|
if (config.updater != null)
|
||||||
config.updater(
|
config.updater(value);
|
||||||
optimism: optimism,
|
|
||||||
backup: backup,
|
|
||||||
showGrid: showGrid
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget buildToolBar(BuildContext context) {
|
Widget buildToolBar(BuildContext context) {
|
||||||
@ -82,8 +70,6 @@ class StockSettingsState extends State<StockSettings> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Widget buildSettingsPane(BuildContext context) {
|
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>[
|
List<Widget> rows = <Widget>[
|
||||||
new DrawerItem(
|
new DrawerItem(
|
||||||
icon: 'action/thumb_up',
|
icon: 'action/thumb_up',
|
||||||
@ -91,18 +77,18 @@ class StockSettingsState extends State<StockSettings> {
|
|||||||
child: new Row(<Widget>[
|
child: new Row(<Widget>[
|
||||||
new Flexible(child: new Text('Everything is awesome')),
|
new Flexible(child: new Text('Everything is awesome')),
|
||||||
new Checkbox(
|
new Checkbox(
|
||||||
value: config.optimism == StockMode.optimistic,
|
value: config.configuration.stockMode == StockMode.optimistic,
|
||||||
onChanged: (bool value) => _confirmOptimismChange()
|
onChanged: (bool value) => _confirmOptimismChange()
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
),
|
),
|
||||||
new DrawerItem(
|
new DrawerItem(
|
||||||
icon: 'action/backup',
|
icon: 'action/backup',
|
||||||
onPressed: () { _handleBackupChanged(!(config.backup == BackupMode.enabled)); },
|
onPressed: () { _handleBackupChanged(!(config.configuration.backupMode == BackupMode.enabled)); },
|
||||||
child: new Row(<Widget>[
|
child: new Row(<Widget>[
|
||||||
new Flexible(child: new Text('Back up stock list to the cloud')),
|
new Flexible(child: new Text('Back up stock list to the cloud')),
|
||||||
new Switch(
|
new Switch(
|
||||||
value: config.backup == BackupMode.enabled,
|
value: config.configuration.backupMode == BackupMode.enabled,
|
||||||
onChanged: _handleBackupChanged
|
onChanged: _handleBackupChanged
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
@ -113,11 +99,11 @@ class StockSettingsState extends State<StockSettings> {
|
|||||||
rows.add(
|
rows.add(
|
||||||
new DrawerItem(
|
new DrawerItem(
|
||||||
icon: 'editor/border_clear',
|
icon: 'editor/border_clear',
|
||||||
onPressed: () { _handleShowGridChanged(!config.showGrid); },
|
onPressed: () { _handleShowGridChanged(!config.configuration.showGrid); },
|
||||||
child: new Row(<Widget>[
|
child: new Row(<Widget>[
|
||||||
new Flexible(child: new Text('Show material grid (for debugging)')),
|
new Flexible(child: new Text('Show material grid (for debugging)')),
|
||||||
new Switch(
|
new Switch(
|
||||||
value: config.showGrid,
|
value: config.configuration.showGrid,
|
||||||
onChanged: _handleShowGridChanged
|
onChanged: _handleShowGridChanged
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
@ -6,3 +6,31 @@ part of stocks;
|
|||||||
|
|
||||||
enum StockMode { optimistic, pessimistic }
|
enum StockMode { optimistic, pessimistic }
|
||||||
enum BackupMode { enabled, disabled }
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user