Skip to content

Commit

Permalink
add app store example
Browse files Browse the repository at this point in the history
  • Loading branch information
coldfox committed May 22, 2019
1 parent fc05eb8 commit 82af468
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 10 deletions.
15 changes: 13 additions & 2 deletions example/lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import 'package:flutter/material.dart';

import 'package:fish_redux/fish_redux.dart';
import 'package:sample/global_store/state.dart';
import 'global_store/global_store.dart';
import 'todo_edit_page/page.dart';
import 'todo_list_page/page.dart';

//create global page helper
Page<T, dynamic> createGlobalPage<T extends Cloneable<T>>(
Page<T, dynamic> page) {
return page
..connectExtraStore(GlobalStore.store, (T pagestate, GlobalState appState) {
return pagestate.clone();
});
}

Widget createApp() {
final AbstractRoutes routes = HybridRoutes(routes: <AbstractRoutes>[
PageRoutes(
pages: <String, Page<Object, dynamic>>{
'todo_list': ToDoListPage(),
'todo_edit': TodoEditPage(),
'todo_list': createGlobalPage(ToDoListPage()),
'todo_edit': createGlobalPage(TodoEditPage())
},
),
]);
Expand Down
16 changes: 16 additions & 0 deletions example/lib/global_store/action.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'dart:ui';

import 'package:fish_redux/fish_redux.dart';

//TODO replace with your own action
enum GlobalAction { action, changeThemeColor}

class GlobalActionCreator {
static Action onAction() {
return const Action(GlobalAction.action);
}

static Action onchangeThemeColor(Color color){
return Action(GlobalAction.changeThemeColor, payload: color);
}
}
16 changes: 16 additions & 0 deletions example/lib/global_store/global_store.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:fish_redux/fish_redux.dart';
import 'package:sample/global_store/reducer.dart';
import 'package:sample/global_store/state.dart';

class GlobalStore{
static Store<GlobalState> _globalStore;
static GlobalState get state => store.getState();

static Store<GlobalState> get store{
if(null == _globalStore){
_globalStore = createStore<GlobalState>(initState(null), buildReducer());
}
return _globalStore;
}

}
26 changes: 26 additions & 0 deletions example/lib/global_store/reducer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:ui';

import 'package:fish_redux/fish_redux.dart';

import 'action.dart';
import 'state.dart';

Reducer<GlobalState> buildReducer() {
return asReducer(
<Object, Reducer<GlobalState>>{
GlobalAction.action: _onAction,
GlobalAction.changeThemeColor: _onchangeThemeColor,
},
);
}

GlobalState _onAction(GlobalState state, Action action) {
final GlobalState newState = state.clone();
return newState;
}

GlobalState _onchangeThemeColor(GlobalState state, Action action) {
final GlobalState newState = state.clone();
newState.themeColor = action.payload as Color;
return newState;
}
15 changes: 15 additions & 0 deletions example/lib/global_store/state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'dart:ui';

import 'package:fish_redux/fish_redux.dart';

class GlobalState implements Cloneable<GlobalState> {
Color themeColor;
@override
GlobalState clone() {
return GlobalState();
}
}

GlobalState initState(Map<String, dynamic> args) {
return GlobalState();
}
10 changes: 7 additions & 3 deletions example/lib/todo_edit_page/action.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import 'package:fish_redux/fish_redux.dart';

enum ToDoEditAction { update, done }
enum ToDoEditAction { update, done, changeTheme }

class ToDoEditActionCreator {
static Action update(String name, String desc) {
static Action update(String name, String desc, String themeidx) {
return Action(
ToDoEditAction.update,
payload: <String, String>{'name': name, 'desc': desc},
payload: <String, String>{'name': name, 'desc': desc, 'themeidx' : themeidx},
);
}

static Action done() {
return const Action(ToDoEditAction.done);
}

static Action changeTheme(){
return Action(ToDoEditAction.changeTheme);
}
}
19 changes: 17 additions & 2 deletions example/lib/todo_edit_page/effect.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:sample/global_store/action.dart';
import 'package:sample/global_store/global_store.dart';

import '../todo_list_page/todo_component/component.dart';
import 'action.dart';
Expand All @@ -9,21 +11,34 @@ Effect<TodoEditState> buildEffect() {
return combineEffects(<Object, Effect<TodoEditState>>{
Lifecycle.initState: _init,
ToDoEditAction.done: _onDone,
ToDoEditAction.changeTheme : _onchangeTheme,
});
}

void _init(Action action, Context<TodoEditState> ctx) {
ctx.state.nameEditController.addListener(() {
ctx.dispatch(
ToDoEditActionCreator.update(ctx.state.nameEditController.text, null));
ToDoEditActionCreator.update(ctx.state.nameEditController.text, null, null));
});

ctx.state.descEditController.addListener(() {
ctx.dispatch(
ToDoEditActionCreator.update(null, ctx.state.descEditController.text));
ToDoEditActionCreator.update(null, ctx.state.descEditController.text, null));
});
}

void _onDone(Action action, Context<TodoEditState> ctx) {
Navigator.of(ctx.context).pop<ToDoState>(ctx.state.toDo);
}

void _onchangeTheme(Action action, Context<TodoEditState> ctx) {
ctx.state.themeIdx++;
if(ctx.state.themeIdx >= ctx.state.themeColorSlots.length)
{
ctx.state.themeIdx = 0;
}
//change global data
GlobalStore.store.dispatch(GlobalActionCreator.onchangeThemeColor(ctx.state.themeColorSlots[ctx.state.themeIdx]));
//notify todo edit page update data
ToDoEditActionCreator.update(null, null, ctx.state.themeIdx.toString());
}
6 changes: 4 additions & 2 deletions example/lib/todo_edit_page/reducer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'state.dart';

Reducer<TodoEditState> buildReducer() {
return asReducer<TodoEditState>(<Object, Reducer<TodoEditState>>{
ToDoEditAction.update: _update,
ToDoEditAction.update: _update
});
}

Expand All @@ -14,5 +14,7 @@ TodoEditState _update(TodoEditState state, Action action) {
final TodoEditState newState = state.clone();
newState.toDo.title = update['name'] ?? newState.toDo.title;
newState.toDo.desc = update['desc'] ?? newState.toDo.desc;
String strThemeIdx = update['themeidx'] ?? newState.themeIdx.toString();
newState.themeIdx = int.tryParse(strThemeIdx);
return newState;
}
}
9 changes: 8 additions & 1 deletion example/lib/todo_edit_page/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ class TodoEditState implements Cloneable<TodoEditState> {
FocusNode focusNodeName;
FocusNode focusNodeDesc;

int themeIdx;
List<Color> themeColorSlots;

@override
TodoEditState clone() {
return TodoEditState()
..nameEditController = nameEditController
..descEditController = descEditController
.. focusNodeName = focusNodeName
.. focusNodeDesc = focusNodeDesc
..toDo = toDo;
..toDo = toDo
..themeIdx = themeIdx
..themeColorSlots = themeColorSlots;
}
}

Expand All @@ -29,5 +34,7 @@ TodoEditState initState(ToDoState arg) {
state.descEditController = TextEditingController(text: arg?.desc);
state.focusNodeName = FocusNode();
state.focusNodeDesc = FocusNode();
state.themeIdx = 0;
state.themeColorSlots = [Colors.green, Colors.red, Colors.black, Colors.blue];
return state;
}
9 changes: 9 additions & 0 deletions example/lib/todo_edit_page/view.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:sample/global_store/global_store.dart';

import 'action.dart';
import 'state.dart';
Expand All @@ -8,6 +9,7 @@ Widget buildView(
TodoEditState state, Dispatch dispatch, ViewService viewService) {
return Scaffold(
appBar: AppBar(
backgroundColor:GlobalStore.state.themeColor,
title: const Text('Todo'),
),
body: Container(
Expand Down Expand Up @@ -40,6 +42,13 @@ Widget buildView(
],
),
),
new RaisedButton(
padding: new EdgeInsets.only(left: 20.0, top: 10.0, right: 20.0, bottom: 10.0),
color: Colors.blue,
child: new Text("Change theme", style: new TextStyle(fontSize: 18), overflow:TextOverflow.ellipsis),
onPressed: () {
dispatch(ToDoEditActionCreator.changeTheme());
}),
Expanded(
child: Container(
margin: const EdgeInsets.only(top: 32.0),
Expand Down
2 changes: 2 additions & 0 deletions example/lib/todo_list_page/view.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:sample/global_store/global_store.dart';

import 'action.dart';
import 'state.dart';
Expand All @@ -8,6 +9,7 @@ Widget buildView(PageState state, Dispatch dispatch, ViewService viewService) {
final ListAdapter adapter = viewService.buildAdapter();
return Scaffold(
appBar: AppBar(
backgroundColor:GlobalStore.state.themeColor,
title: const Text('ToDoList'),
),
body: Container(
Expand Down

0 comments on commit 82af468

Please sign in to comment.