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
34cfb608
Commit
34cfb608
authored
Jan 10, 2016
by
Ian Hickson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor the Stocks app's settings
This will make it much easier to add more settings.
parent
71c2ccbf
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
58 deletions
+61
-58
main.dart
examples/stocks/lib/main.dart
+12
-19
stock_home.dart
examples/stocks/lib/stock_home.dart
+7
-11
stock_settings.dart
examples/stocks/lib/stock_settings.dart
+14
-28
stock_types.dart
examples/stocks/lib/stock_types.dart
+28
-0
No files found.
examples/stocks/lib/main.dart
View file @
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
,
mode
Updater
),
'/settings'
:
(
RouteArguments
args
)
=>
new
StockSettings
(
_
optimismSetting
,
_backupSetting
,
_showGridSetting
,
settings
Updater
)
'/'
:
(
RouteArguments
args
)
=>
new
StockHome
(
_stocks
,
_symbols
,
_
configuration
,
configuration
Updater
),
'/settings'
:
(
RouteArguments
args
)
=>
new
StockSettings
(
_
configuration
,
configuration
Updater
)
},
onGenerateRoute:
_getRoute
,
onLocaleChanged:
_onLocaleChanged
...
...
examples/stocks/lib/stock_home.dart
View file @
34cfb608
...
...
@@ -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
.
modeU
pdater
);
const
StockHome
(
this
.
stocks
,
this
.
symbols
,
this
.
configuration
,
this
.
u
pdater
);
final
Map
<
String
,
Stock
>
stocks
;
final
List
<
String
>
symbols
;
final
Stock
Mode
stockMode
;
final
ModeUpdater
modeU
pdater
;
final
Stock
Configuration
configuration
;
final
ValueChanged
<
StockConfiguration
>
u
pdater
;
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
.
modeU
pdater
!=
null
)
config
.
modeUpdater
(
value
);
if
(
config
.
u
pdater
!=
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
(),
...
...
examples/stocks/lib/stock_settings.dart
View file @
34cfb608
...
...
@@ -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
(
Stock
Mode
optimism
,
BackupMode
backup
,
bool
showGrid
)
{
void
sendUpdates
(
Stock
Configuration
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
),
])
...
...
examples/stocks/lib/stock_types.dart
View file @
34cfb608
...
...
@@ -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
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