Commit 55218c81 authored by Brian Egan's avatar Brian Egan

Update built_redux example with more split out folder structure

parent 12240c1f
library actions;
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_value/built_value.dart';
import 'package:built_redux/built_redux.dart';
import 'package:built_value/serializer.dart';
......
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:built_redux_sample/widgets/extra_actions_button.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart';
import 'package:built_redux_sample/selectors/selectors.dart';
class ExtraActionSelector
extends StoreConnector<AppState, AppStateBuilder, AppActions, bool> {
......@@ -10,7 +11,7 @@ class ExtraActionSelector
@override
bool connect(AppState state) {
return state.allComplete;
return allCompleteSelector(todosSelector(state));
}
@override
......
import 'package:built_redux_sample/containers/typedefs.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart';
......
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:built_redux_sample/widgets/add_edit_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart';
......
import 'package:built_redux_sample/containers/typedefs.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart';
......
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:built_redux_sample/widgets/add_edit_screen.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart';
......
library filter_selector;
import 'package:built_redux_sample/containers/typedefs.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart';
......
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:built_redux_sample/selectors/selectors.dart';
import 'package:built_redux_sample/widgets/todo_list.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart';
......@@ -27,6 +28,9 @@ class FilteredTodos
@override
List<Todo> connect(AppState state) {
return state.filteredTodos(state.activeFilter);
return filteredTodosSelector(
todosSelector(state),
activeFilterSelector(state),
);
}
}
library stats;
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:built_redux_sample/selectors/selectors.dart';
import 'package:built_redux_sample/widgets/stats_counter.dart';
import 'package:built_value/built_value.dart';
import 'package:flutter/src/widgets/framework.dart';
......@@ -9,8 +10,7 @@ import 'package:flutter_built_redux/flutter_built_redux.dart';
part 'stats.g.dart';
abstract class StatsProps
implements Built<StatsProps, StatsPropsBuilder> {
abstract class StatsProps implements Built<StatsProps, StatsPropsBuilder> {
int get numCompleted;
int get numActive;
......@@ -20,15 +20,15 @@ abstract class StatsProps
factory StatsProps([updates(StatsPropsBuilder b)]) = _$StatsProps;
}
class Stats extends StoreConnector<AppState, AppStateBuilder, AppActions,
StatsProps> {
class Stats
extends StoreConnector<AppState, AppStateBuilder, AppActions, StatsProps> {
Stats({Key key}) : super(key: key);
@override
StatsProps connect(AppState state) {
return new StatsProps((b) => b
..numCompleted = state.numCompleted
..numActive = state.numActive);
..numCompleted = numCompletedSelector(todosSelector(state))
..numActive = numActiveSelector(todosSelector(state)));
}
@override
......
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_architecture_samples/flutter_architecture_samples.dart';
......
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:built_redux_sample/widgets/details_screen.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
......
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/data_model/serializers.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/models/serializers.dart';
import 'package:path_provider/path_provider.dart';
/// Loads and saves a List of Todos using a text file stored on the device.
......
......@@ -2,7 +2,7 @@ import 'dart:async';
import 'dart:core';
import 'package:built_redux_sample/data/file_storage.dart';
import 'package:built_redux_sample/data/web_service.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
/// A class that glues together our local file storage and a remote web service.
class TodosService {
......
import 'dart:async';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
/// A class that is meant to represent a Web Service you would call to fetch
/// and persist Todos to and from the cloud.
......
library models;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:flutter_architecture_samples/uuid.dart';
part 'models.g.dart';
class AppTab extends EnumClass {
static Serializer<AppTab> get serializer => _$appTabSerializer;
static const AppTab todos = _$todos;
static const AppTab stats = _$stats;
const AppTab._(String name) : super(name);
static BuiltSet<AppTab> get values => _$appTabValues;
static AppTab valueOf(String name) => _$appTabValueOf(name);
static AppTab fromIndex(int index) {
switch (index) {
case 1:
return AppTab.stats;
default:
return AppTab.todos;
}
}
static int toIndex(AppTab tab) {
switch (tab) {
case AppTab.stats:
return 1;
default:
return 0;
}
}
}
class ExtraAction extends EnumClass {
static Serializer<ExtraAction> get serializer => _$extraActionSerializer;
static const ExtraAction toggleAllComplete = _$toggleAllComplete;
static const ExtraAction clearCompleted = _$clearCompleted;
const ExtraAction._(String name) : super(name);
static BuiltSet<ExtraAction> get values => _$extraActionValues;
static ExtraAction valueOf(String name) => _$extraActionValueOf(name);
}
class VisibilityFilter extends EnumClass {
static Serializer<VisibilityFilter> get serializer =>
_$visibilityFilterSerializer;
static const VisibilityFilter all = _$all;
static const VisibilityFilter active = _$active;
static const VisibilityFilter completed = _$completed;
const VisibilityFilter._(String name) : super(name);
static BuiltSet<VisibilityFilter> get values => _$visibilityFilterValues;
static VisibilityFilter valueOf(String name) =>
_$visibilityFilterValueOf(name);
}
abstract class AppState implements Built<AppState, AppStateBuilder> {
static Serializer<AppState> get serializer => _$appStateSerializer;
bool get isLoading;
BuiltList<Todo> get todos;
AppTab get activeTab;
VisibilityFilter get activeFilter;
AppState._();
factory AppState([updates(AppStateBuilder b)]) => new _$AppState((b) => b
..isLoading = false
..todos = new ListBuilder<Todo>([])
..activeTab = AppTab.todos
..activeFilter = VisibilityFilter.all
..update(updates));
factory AppState.loading() => new AppState((b) => b..isLoading = true);
factory AppState.fromTodos(List<Todo> todos) =>
new AppState((b) => b..todos = new ListBuilder<Todo>(todos));
bool get allComplete => todos.every((todo) => todo.complete);
List<Todo> filteredTodos(VisibilityFilter activeFilter) =>
todos.where((todo) {
if (activeFilter == VisibilityFilter.all) {
return true;
} else if (activeFilter == VisibilityFilter.active) {
return !todo.complete;
} else if (activeFilter == VisibilityFilter.completed) {
return todo.complete;
}
}).toList();
bool get hasCompletedTodos => todos.any((todo) => todo.complete);
int get numActive =>
todos.fold(0, (sum, todo) => !todo.complete ? ++sum : sum);
int get numCompleted =>
todos.fold(0, (sum, todo) => todo.complete ? ++sum : sum);
}
abstract class Todo implements Built<Todo, TodoBuilder> {
static Serializer<Todo> get serializer => _$todoSerializer;
bool get complete;
String get id;
String get note;
String get task;
Todo._();
factory Todo(String task) {
return new _$Todo._(
task: task,
complete: false,
note: '',
id: new Uuid().generateV4(),
);
}
factory Todo.builder([updates(TodoBuilder b)]) {
final builder = new TodoBuilder()
..id = new Uuid().generateV4()
..complete = false
..note = ''
..update(updates);
return builder.build();
}
}
......@@ -3,11 +3,11 @@ library built_redux_sample;
import 'package:built_redux/built_redux.dart';
import 'package:built_redux_sample/containers/add_todo.dart';
import 'package:built_redux_sample/data/todos_service.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/localization.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/redux/reducers.dart';
import 'package:built_redux_sample/storeTodosMiddleware.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:built_redux_sample/reducers/reducers.dart';
import 'package:built_redux_sample/middleware/store_todos_middleware.dart';
import 'package:built_redux_sample/widgets/home_screen.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart';
import 'package:flutter/material.dart';
......
import 'package:built_redux/built_redux.dart';
import 'package:built_redux_sample/data/todos_service.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
Middleware<AppState, AppStateBuilder, AppActions> createStoreTodosMiddleware(
TodosService service) =>
......@@ -21,10 +21,12 @@ MiddlewareHandler<AppState, AppStateBuilder, AppActions, Null> createFetchTodos(
TodosService service) {
return (MiddlewareApi<AppState, AppStateBuilder, AppActions> api,
ActionHandler next, Action<Null> action) {
service
.loadTodos()
.then(api.actions.loadTodosSuccess)
.catchError(api.actions.loadTodosFailure);
if (api.state.isLoading) {
service
.loadTodos()
.then(api.actions.loadTodosSuccess)
.catchError(api.actions.loadTodosFailure);
}
next(action);
};
......
library app_state;
import 'package:built_collection/built_collection.dart';
import 'package:built_redux_sample/models/app_tab.dart';
import 'package:built_redux_sample/models/todo.dart';
import 'package:built_redux_sample/models/visibility_filter.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'app_state.g.dart';
abstract class AppState implements Built<AppState, AppStateBuilder> {
static Serializer<AppState> get serializer => _$appStateSerializer;
bool get isLoading;
BuiltList<Todo> get todos;
AppTab get activeTab;
VisibilityFilter get activeFilter;
AppState._();
factory AppState([updates(AppStateBuilder b)]) => new _$AppState((b) => b
..isLoading = false
..todos = new ListBuilder<Todo>([])
..activeTab = AppTab.todos
..activeFilter = VisibilityFilter.all
..update(updates));
factory AppState.loading() => new AppState((b) => b..isLoading = true);
factory AppState.fromTodos(List<Todo> todos) =>
new AppState((b) => b..todos = new ListBuilder<Todo>(todos));
}
\ No newline at end of file
library app_tab;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'app_tab.g.dart';
class AppTab extends EnumClass {
static Serializer<AppTab> get serializer => _$appTabSerializer;
static const AppTab todos = _$todos;
static const AppTab stats = _$stats;
const AppTab._(String name) : super(name);
static BuiltSet<AppTab> get values => _$appTabValues;
static AppTab valueOf(String name) => _$appTabValueOf(name);
static AppTab fromIndex(int index) {
switch (index) {
case 1:
return AppTab.stats;
default:
return AppTab.todos;
}
}
static int toIndex(AppTab tab) {
switch (tab) {
case AppTab.stats:
return 1;
default:
return 0;
}
}
}
\ No newline at end of file
library extra_actions;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'extra_actions.g.dart';
class ExtraAction extends EnumClass {
static Serializer<ExtraAction> get serializer => _$extraActionSerializer;
static const ExtraAction toggleAllComplete = _$toggleAllComplete;
static const ExtraAction clearCompleted = _$clearCompleted;
const ExtraAction._(String name) : super(name);
static BuiltSet<ExtraAction> get values => _$extraActionValues;
static ExtraAction valueOf(String name) => _$extraActionValueOf(name);
}
\ No newline at end of file
library models;
export 'app_state.dart';
export 'app_tab.dart';
export 'extra_actions.dart';
export 'todo.dart';
export 'visibility_filter.dart';
library serializers;
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_value/serializer.dart';
import 'package:built_collection/built_collection.dart';
......
library todo;
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:flutter_architecture_samples/uuid.dart';
part 'todo.g.dart';
abstract class Todo implements Built<Todo, TodoBuilder> {
static Serializer<Todo> get serializer => _$todoSerializer;
bool get complete;
String get id;
String get note;
String get task;
Todo._();
factory Todo(String task) {
return new _$Todo._(
task: task,
complete: false,
note: '',
id: new Uuid().generateV4(),
);
}
factory Todo.builder([updates(TodoBuilder b)]) {
final builder = new TodoBuilder()
..id = new Uuid().generateV4()
..complete = false
..note = ''
..update(updates);
return builder.build();
}
}
library visibility_filter;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'visibility_filter.g.dart';
class VisibilityFilter extends EnumClass {
static Serializer<VisibilityFilter> get serializer =>
_$visibilityFilterSerializer;
static const VisibilityFilter all = _$all;
static const VisibilityFilter active = _$active;
static const VisibilityFilter completed = _$completed;
const VisibilityFilter._(String name) : super(name);
static BuiltSet<VisibilityFilter> get values => _$visibilityFilterValues;
static VisibilityFilter valueOf(String name) =>
_$visibilityFilterValueOf(name);
}
import 'package:built_collection/built_collection.dart';
import 'package:built_redux/built_redux.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/redux/actions.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/actions/actions.dart';
import 'package:built_redux_sample/selectors/selectors.dart';
var reducerBuilder = new ReducerBuilder<AppState, AppStateBuilder>()
..add(AppActionsNames.addTodoAction, addTodo)
......@@ -25,7 +26,7 @@ deleteTodo(AppState state, Action<String> action, AppStateBuilder builder) =>
..where((todo) => todo.id != action.payload);
toggleAll(AppState state, Action<Null> action, AppStateBuilder builder) {
final allComplete = state.allComplete;
final allComplete = allCompleteSelector(todosSelector(state));
return builder.todos = state.todos.toBuilder()
..map((todo) => (todo.toBuilder()..complete = !allComplete).build());
......
import 'package:built_redux_sample/models/models.dart';
import 'package:quiver/core.dart';
List<Todo> todosSelector(AppState state) => state.todos.toList();
VisibilityFilter activeFilterSelector(AppState state) => state.activeFilter;
AppTab activeTabSelector(AppState state) => state.activeTab;
bool isLoadingSelector(AppState state) => state.isLoading;
bool allCompleteSelector(List<Todo> todos) =>
todos.every((todo) => todo.complete);
int numActiveSelector(List<Todo> todos) =>
todos.fold(0, (sum, todo) => !todo.complete ? ++sum : sum);
int numCompletedSelector(List<Todo> todos) =>
todos.fold(0, (sum, todo) => todo.complete ? ++sum : sum);
List<Todo> filteredTodosSelector(
List<Todo> todos,
VisibilityFilter activeFilter,
) {
return todos.where((todo) {
if (activeFilter == VisibilityFilter.all) {
return true;
} else if (activeFilter == VisibilityFilter.active) {
return !todo.complete;
} else if (activeFilter == VisibilityFilter.completed) {
return todo.complete;
}
}).toList();
}
Optional<Todo> todoSelector(List<Todo> todos, String id) {
try {
return new Optional.of(todos.firstWhere((todo) => todo.id == id));
} catch (e) {
return new Optional.absent();
}
}
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_architecture_samples/flutter_architecture_samples.dart';
......
import 'package:built_redux_sample/containers/edit_todo.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_architecture_samples/flutter_architecture_samples.dart';
......
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:flutter/material.dart';
import 'package:flutter_architecture_samples/flutter_architecture_samples.dart';
......
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:flutter/material.dart';
import 'package:flutter_architecture_samples/flutter_architecture_samples.dart';
......
......@@ -4,7 +4,7 @@ import 'package:built_redux_sample/containers/filter_selector.dart';
import 'package:built_redux_sample/containers/filtered_todos.dart';
import 'package:built_redux_sample/containers/stats.dart';
import 'package:built_redux_sample/containers/tab_selector.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/localization.dart';
import 'package:built_redux_sample/widgets/filter_button.dart';
import 'package:flutter/material.dart';
......
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
......
import 'package:built_redux_sample/containers/app_loading.dart';
import 'package:built_redux_sample/containers/todo_details.dart';
import 'package:built_redux_sample/data_model/models.dart';
import 'package:built_redux_sample/models/models.dart';
import 'package:built_redux_sample/widgets/todo_item.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
......
......@@ -6,6 +6,7 @@ dependencies:
sdk: flutter
flutter_built_redux: 0.3.0
built_redux: ^7.0.0
quiver: '>=0.21.0 <0.26.0'
built_value: ^4.3.2
built_collection: ^1.3.0
flutter_architecture_samples:
......@@ -18,9 +19,8 @@ dev_dependencies:
build_runner: ^0.5.0
built_value_generator: ^4.3.2
source_gen: ^0.7.0
quiver: '>=0.21.0 <0.26.0'
test: any
mockito: 2.2.0