Skip to content

Commit

Permalink
todo => todoState
Browse files Browse the repository at this point in the history
  • Loading branch information
hyjfine committed Mar 13, 2019
1 parent 4b137c2 commit bc68e2b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 34 deletions.
7 changes: 4 additions & 3 deletions lib/screens/todo_list/list_adapter/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:fish_redux/fish_redux.dart';
import 'package:flutter_app_redux/models/todo.dart';
import 'package:flutter_app_redux/screens/todo_list/state.dart';
import 'package:flutter_app_redux/screens/todo_list/todo_component/component.dart';
import 'package:flutter_app_redux/screens/todo_list/todo_component/state.dart';

class TodoListAdapter extends DynamicFlowAdapter<TodoListState> {
TodoListAdapter()
Expand All @@ -18,7 +19,7 @@ class _TodoListConnector implements Connector<TodoListState, List<ItemBean>> {
List<ItemBean> get(TodoListState state) {
if (state.todoList?.isNotEmpty == true) {
return state.todoList
.map<ItemBean>((Todo data) => ItemBean('todo', data))
.map<ItemBean>((TodoState data) => ItemBean('todo', data))
.toList(growable: true);
} else {
return <ItemBean>[];
Expand All @@ -28,10 +29,10 @@ class _TodoListConnector implements Connector<TodoListState, List<ItemBean>> {
@override
void set(TodoListState state, List<ItemBean> todoList) {
if (todoList?.isNotEmpty == true) {
state.todoList = List<Todo>.from(
state.todoList = List<TodoState>.from(
todoList.map<Todo>((ItemBean bean) => bean.data).toList());
} else {
state.todoList = <Todo>[];
state.todoList = <TodoState>[];
}
}
}
6 changes: 4 additions & 2 deletions lib/screens/todo_list/list_adapter/reducer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:fish_redux/fish_redux.dart';
import 'package:flutter_app_redux/models/todo.dart';
import 'package:flutter_app_redux/screens/todo_list/action.dart';
import 'package:flutter_app_redux/screens/todo_list/list_adapter/action.dart';
import 'package:flutter_app_redux/screens/todo_list/todo_component/state.dart'
as a;
import 'package:flutter_app_redux/screens/todo_list/state.dart';

Reducer<TodoListState> buildReducer() {
Expand All @@ -14,14 +16,14 @@ Reducer<TodoListState> buildReducer() {
TodoListState _add(TodoListState state, Action action) {
final Todo todo = action.payload;
final TodoListState newState = state.clone();
newState.todoList.add(todo);
newState.todoList.add(a.initState(todo));

return newState;
}

TodoListState _delete(TodoListState state, Action action) {
final String key = action.payload;
final TodoListState newState = state.clone();
newState.todoList.removeWhere((todo) => todo.id == key);
newState.todoList.removeWhere((todoState) => todoState.todo.id == key);
return newState;
}
12 changes: 10 additions & 2 deletions lib/screens/todo_list/reducer.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter_app_redux/models/todo.dart';
import 'package:flutter_app_redux/screens/todo_list/action.dart';
import 'package:flutter_app_redux/screens/todo_list/state.dart';
import 'package:flutter_app_redux/screens/todo_list/todo_component/state.dart'
as todoComponent;

Reducer<TodoListState> buildReducer() {
return asReducer<TodoListState>(<Object, Reducer<TodoListState>>{
Expand Down Expand Up @@ -29,7 +32,12 @@ TodoListState _failure(TodoListState state, Action action) {
TodoListState _success(TodoListState state, Action action) {
final TodoListState newState = state.clone();
newState.isLoading = false;
newState.todoList = action.payload.data ?? newState.todoList;
List<todoComponent.TodoState> todoStateList = [];
List<Todo> todoList = action.payload.data;
todoList.forEach((todo) {
todoStateList.add(todoComponent.initState(todo));
});
newState.todoList = todoStateList;
return newState;
}

Expand Down Expand Up @@ -58,7 +66,7 @@ TodoListState _deleteSuccess(TodoListState state, Action action) {
final TodoListState newState = state.clone();
newState.isLoading = false;
var list = newState.todoList;
list.removeWhere((item) => item.id == action.payload.id);
list.removeWhere((item) => item.todo.id == action.payload.id);
newState.todoList = list;
return newState;
}
4 changes: 2 additions & 2 deletions lib/screens/todo_list/state.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter_app_redux/models/todo.dart';
import 'package:flutter_app_redux/screens/todo_list/todo_component/state.dart';

class TodoListState implements Cloneable<TodoListState> {
bool isLoading;
bool isModify;
List<Todo> todoList;
List<TodoState> todoList;

@override
TodoListState clone() {
Expand Down
6 changes: 3 additions & 3 deletions lib/screens/todo_list/todo_component/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class TodoState implements Cloneable<TodoState> {
}
}

TodoState initState(String id) {
print("------initState id $id");
TodoState initState(Todo todo) {
print("------initState id $todo");
final TodoState state = TodoState();
state.isLoading = false;
state.todo = Todo();
state.todo = todo;
return state;
}
23 changes: 1 addition & 22 deletions lib/screens/todo_list/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,13 @@ import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_redux/constants/keys.dart';
import 'package:flutter_app_redux/models/todo.dart';
import 'package:flutter_app_redux/screens/todo_detail/page.dart';
import 'package:flutter_app_redux/screens/todo_list/action.dart';
import 'package:flutter_app_redux/screens/todo_list/state.dart';
import 'package:flutter_app_redux/widgets/loading.dart';

Widget buildView(
TodoListState state, Dispatch dispatch, ViewService viewService) {
List<Todo> todoList = state.todoList;
// ListView _buildListView() {
// return ListView.builder(
// key: UniqueKey(),
// itemCount: todoList.length,
// itemBuilder: (BuildContext context, int index) {
// final todo = todoList[index];
//
// return TodoItem(
// todo: todo,
// onDismissed: (direction) =>
// dispatch(TodoListActionCreator.delete(todo.id)),
// onTap: () => Navigator.push(
// context,
// MaterialPageRoute(
// builder: (_) => TodoDetailPage(todo.id).buildPage(null))),
// onCheckboxChanged: (complete) {},
// );
// },
// );
// }


Widget _buildBody() {
final ListAdapter adapter = viewService.buildAdapter();
Expand Down

0 comments on commit bc68e2b

Please sign in to comment.