Skip to content

Commit

Permalink
0.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
zjuwjf committed May 26, 2019
1 parent 612ef0a commit fd59f60
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 88 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@
- add effectMiddleware
- add protected attribute method, more friendly to OOP
- remove debug_report

## [0.1.9]
- add mixed-store's batch notification feature
12 changes: 6 additions & 6 deletions example/lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'package:flutter/material.dart';

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

//create global page helper
Page<T, dynamic> createGlobalPage<T extends GlobalBaseState<T> >(
Page<T, dynamic> connectExtraStore<T extends GlobalBaseState<T>>(
Page<T, dynamic> page) {
return page
..connectExtraStore(GlobalStore.store, (T pagestate, GlobalState appState) {
Expand All @@ -20,8 +20,8 @@ Widget createApp() {
final AbstractRoutes routes = HybridRoutes(routes: <AbstractRoutes>[
PageRoutes(
pages: <String, Page<Object, dynamic>>{
'todo_list': createGlobalPage(ToDoListPage()),
'todo_edit': createGlobalPage(TodoEditPage())
'todo_list': connectExtraStore(ToDoListPage()),
'todo_edit': connectExtraStore(TodoEditPage())
},
),
]);
Expand Down
9 changes: 2 additions & 7 deletions example/lib/global_store/action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ import 'dart:ui';

import 'package:fish_redux/fish_redux.dart';

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

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

static Action onchangeThemeColor(Color color){
static Action onchangeThemeColor(Color color) {
return Action(GlobalAction.changeThemeColor, payload: color);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:fish_redux/fish_redux.dart';
import 'package:sample/global_store/state.dart';
import 'state.dart';

/// Definition of Cloneable
mixin GlobalBaseState<T extends Cloneable<T>> implements Cloneable<T> {
T lessClone(GlobalState state);
}
}
16 changes: 0 additions & 16 deletions example/lib/global_store/global_store.dart

This file was deleted.

13 changes: 2 additions & 11 deletions example/lib/global_store/reducer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@ 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;
}
GlobalState _onchangeThemeColor(GlobalState state, Action action) =>
state.clone()..themeColor = action.payload;
4 changes: 0 additions & 4 deletions example/lib/global_store/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ class GlobalState implements Cloneable<GlobalState> {
return GlobalState();
}
}

GlobalState initGlobalState(Map<String, dynamic> args) {
return GlobalState();
}
10 changes: 10 additions & 0 deletions example/lib/global_store/store.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:fish_redux/fish_redux.dart';
import 'reducer.dart';
import 'state.dart';

class GlobalStore {
static Store<GlobalState> _globalStore;

static Store<GlobalState> get store =>
_globalStore ??= createStore<GlobalState>(GlobalState(), buildReducer());
}
16 changes: 10 additions & 6 deletions example/lib/todo_edit_page/action.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import 'package:fish_redux/fish_redux.dart';

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

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

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

static Action changeTheme(){
return Action(ToDoEditAction.changeTheme);
static Action onChangeTheme() {
return const Action(ToDoEditAction.onChangeTheme);
}
}
25 changes: 12 additions & 13 deletions example/lib/todo_edit_page/effect.dart
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
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 '../global_store/action.dart';
import '../global_store/store.dart';
import '../todo_list_page/todo_component/component.dart';
import 'action.dart';
import 'state.dart';

Effect<TodoEditState> buildEffect() {
return combineEffects(<Object, Effect<TodoEditState>>{
Lifecycle.initState: _init,
ToDoEditAction.done: _onDone,
ToDoEditAction.changeTheme : _onchangeTheme,
ToDoEditAction.onDone: _onDone,
ToDoEditAction.onChangeTheme: _onChangeTheme,
});
}

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

ctx.state.descEditController.addListener(() {
ctx.dispatch(
ToDoEditActionCreator.update(null, ctx.state.descEditController.text, null));
ctx.dispatch(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) {
void _onChangeTheme(Action action, Context<TodoEditState> ctx) {
ctx.state.themeIdx++;
if(ctx.state.themeIdx >= ctx.state.themeColorSlots.length)
{
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]));
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());
}
9 changes: 4 additions & 5 deletions example/lib/todo_edit_page/reducer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import 'action.dart';
import 'state.dart';

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

TodoEditState _update(TodoEditState state, Action action) {
final Map<String, String> update = action.payload ?? <String, String>{};
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();
final String strThemeIdx = update['themeidx'] ?? newState.themeIdx.toString();
newState.themeIdx = int.tryParse(strThemeIdx);
return newState;
}
}
17 changes: 9 additions & 8 deletions example/lib/todo_edit_page/state.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:fish_redux/fish_redux.dart';
// import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:sample/global_store/global_base_state.dart';
import 'package:sample/global_store/state.dart';
import '../global_store/base_state.dart';
import '../global_store/state.dart';
import '../todo_list_page/todo_component/component.dart';

class TodoEditState with GlobalBaseState<TodoEditState> {
Expand All @@ -23,16 +23,17 @@ class TodoEditState with GlobalBaseState<TodoEditState> {
return TodoEditState()
..nameEditController = nameEditController
..descEditController = descEditController
.. focusNodeName = focusNodeName
.. focusNodeDesc = focusNodeDesc
..focusNodeName = focusNodeName
..focusNodeDesc = focusNodeDesc
..toDo = toDo
..themeIdx = themeIdx
..themeColorSlots = themeColorSlots;
}

@override
TodoEditState lessClone(GlobalState state)
{
return (state.themeColor == themeColor) ? this : clone()..themeColor = state.themeColor;
TodoEditState lessClone(GlobalState state) {
return (state.themeColor == themeColor) ? this : clone()
..themeColor = state.themeColor;
}
}

Expand Down
4 changes: 1 addition & 3 deletions example/lib/todo_list_page/page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:fish_redux/fish_redux.dart';
import 'package:sample/global_store/global_store.dart';
import 'package:sample/global_store/state.dart';

import 'effect.dart';
import 'list_adapter/adapter.dart';
Expand All @@ -26,4 +24,4 @@ class ToDoListPage extends Page<PageState, Map<String, dynamic>> {
logMiddleware(tag: 'ToDoListPage'),
],
);
}
}
12 changes: 6 additions & 6 deletions example/lib/todo_list_page/state.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'dart:ui';

import 'package:fish_redux/fish_redux.dart';
import 'package:sample/global_store/global_base_state.dart';
import 'package:sample/global_store/state.dart';
import '../global_store/base_state.dart';
import '../global_store/state.dart';
import 'report_component/component.dart';
import 'todo_component/component.dart';

class PageState with GlobalBaseState<PageState>{
class PageState with GlobalBaseState<PageState> {
List<ToDoState> toDos;
Color themeColor;

Expand All @@ -16,9 +16,9 @@ class PageState with GlobalBaseState<PageState>{
}

@override
PageState lessClone(GlobalState state)
{
return (state.themeColor == themeColor) ? this : clone()..themeColor = state.themeColor;
PageState lessClone(GlobalState state) {
return (state.themeColor == themeColor) ? this : clone()
..themeColor = state.themeColor;
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: fish_redux
description: Fish Redux is an assembled flutter application framework based on Redux state management.
version: 0.1.8
version: 0.1.9
author: Alibaba Xianyu Team <tino.wjf@alibaba-inc.com>
homepage: https://github.com/alibaba/fish-redux

Expand Down

0 comments on commit fd59f60

Please sign in to comment.