Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Store.Builder API #8

Merged
merged 7 commits into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ private MethodSpec createConstructor() {
reducer.getStoreModel().getVariableName(),
reducer.getStoreModel().getClassName(),
reducer.getStoreModel().getBuilderName(),
StoreBuilderClassElement.ADD_REDUCER_METHOD_NAME,
StoreBuilderClassElement.REDUCER_SETTER_METHOD_NAME,
reducer.getVariableName(),
StoreBuilderClassElement.ADD_INITIAL_STATE_METHOD_NAME,
StoreBuilderClassElement.INITIAL_STATE_SETTER_METHOD_NAME,
reducer.getStoreModel().getBuilderVariableName(),
reducer.getStoreModel().getStateVariableName(),
StoreBuilderClassElement.BUILD_METHOD_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
public class StoreBuilderClassElement {
public static final String TAG = StoreBuilderClassElement.class.getSimpleName();

static final String ADD_REDUCER_METHOD_NAME = "addReducer";
static final String ADD_INITIAL_STATE_METHOD_NAME = "addInitialState";
static final String REDUCER_SETTER_METHOD_NAME = "setReducer";
static final String INITIAL_STATE_SETTER_METHOD_NAME = "setInitialState";
static final String ADD_MIDDLEWARE_METHOD_NAME = "addMiddleware";
static final String BUILD_METHOD_NAME = "build";
static final String ERROR_MESSAGE_NOT_INITIALIZED_EXCEPTION = "$N has not been initialized.";
Expand Down Expand Up @@ -80,9 +80,10 @@ private MethodSpec createBuilderConstructor() {
private MethodSpec createAddMiddlewareMethodSpec() {
return MethodSpec.methodBuilder(ADD_MIDDLEWARE_METHOD_NAME)
.addModifiers(Modifier.PUBLIC)
.addAnnotation(getOverrideAnnotation())
.returns(storeModel.getBuilder())
.addParameter(getParameterSpec(Middleware.class))
.addStatement("getMiddlewares().add(middleware)")
.addStatement("super.addMiddleware(middleware)")
.addStatement("return this")
.build();
}
Expand All @@ -91,7 +92,7 @@ private List<MethodSpec> createAddReducerMethodSpecs() {
List<MethodSpec> specs = new ArrayList<>();
for (ReducerModel reducerModel : reducerModels) {
specs.add(
MethodSpec.methodBuilder(ADD_REDUCER_METHOD_NAME)
MethodSpec.methodBuilder(REDUCER_SETTER_METHOD_NAME)
.addModifiers(Modifier.PUBLIC)
.returns(storeModel.getBuilder())
.addParameter(getParameterSpec(reducerModel.getReducer()))
Expand All @@ -100,7 +101,7 @@ private List<MethodSpec> createAddReducerMethodSpecs() {
.build()
);
specs.add(
MethodSpec.methodBuilder(ADD_INITIAL_STATE_METHOD_NAME)
MethodSpec.methodBuilder(INITIAL_STATE_SETTER_METHOD_NAME)
.addModifiers(Modifier.PUBLIC)
.returns(storeModel.getBuilder())
.addParameter(getParameterSpec(reducerModel.getState()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,18 @@ public static class Counter {
" super();",
" }",
"",
" @Override",
" public Builder addMiddleware(Middleware middleware) {",
" getMiddlewares().add(middleware);",
" super.addMiddleware(middleware);",
" return this;",
" }",
"",
" public Builder addReducer(CounterReducer counterReducer) {",
" public Builder setReducer(CounterReducer counterReducer) {",
" this.counterReducer = counterReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(Counter counter) {",
" public Builder setInitialState(Counter counter) {",
" this.counter = counter;",
" return this;",
" }",
Expand Down Expand Up @@ -148,17 +149,18 @@ public static class CounterTakesOnlyStateArgument {
" super();",
" }",
"",
" @Override",
" public Builder addMiddleware(Middleware middleware) {",
" getMiddlewares().add(middleware);",
" super.addMiddleware(middleware);",
" return this;",
" }",
"",
" public Builder addReducer(CounterReducer counterReducer) {",
" public Builder setReducer(CounterReducer counterReducer) {",
" this.counterReducer = counterReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(Counter counter) {",
" public Builder setInitialState(Counter counter) {",
" this.counter = counter;",
" return this;",
" }",
Expand Down Expand Up @@ -241,17 +243,18 @@ public static class TodoList {
" super();",
" }",
"",
" @Override",
" public Builder addMiddleware(Middleware middleware) {",
" getMiddlewares().add(middleware);",
" super.addMiddleware(middleware);",
" return this;",
" }",
"",
" public Builder addReducer(TodoListReducer todoListReducer) {",
" public Builder setReducer(TodoListReducer todoListReducer) {",
" this.todoListReducer = todoListReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(TodoList todoList) {",
" public Builder setInitialState(TodoList todoList) {",
" this.todoList = todoList;",
" return this;",
" }",
Expand Down Expand Up @@ -296,10 +299,10 @@ public static class CombinedTwoReducers {
" protected DroiduxRootStore(Builder builder) {",
" super(builder);",
" this.counterReducer = builder.counterReducer;",
" this.counterStore = new DroiduxCounterStore.Builder().addReducer(counterReducer).addInitialState(builder.counter).build();",
" this.counterStore = new DroiduxCounterStore.Builder().setReducer(counterReducer).setInitialState(builder.counter).build();",
" addStore(counterStore);",
" this.todoListReducer = builder.todoListReducer;",
" this.todoListStore = new DroiduxTodoListStore.Builder().addReducer(todoListReducer).addInitialState(builder.todoList).build();",
" this.todoListStore = new DroiduxTodoListStore.Builder().setReducer(todoListReducer).setInitialState(builder.todoList).build();",
" addStore(todoListStore);",
" }",
"",
Expand All @@ -321,27 +324,28 @@ public static class CombinedTwoReducers {
" super();",
" }",
"",
" @Override",
" public Builder addMiddleware(Middleware middleware) {",
" getMiddlewares().add(middleware);",
" super.addMiddleware(middleware);",
" return this;",
" }",
"",
" public Builder addReducer(CounterReducer counterReducer) {",
" public Builder setReducer(CounterReducer counterReducer) {",
" this.counterReducer = counterReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(Counter counter) {",
" public Builder setInitialState(Counter counter) {",
" this.counter = counter;",
" return this;",
" }",
"",
" public Builder addReducer(TodoListReducer todoListReducer) {",
" public Builder setReducer(TodoListReducer todoListReducer) {",
" this.todoListReducer = todoListReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(TodoList todoList) {",
" public Builder setInitialState(TodoList todoList) {",
" this.todoList = todoList;",
" return this;",
" }",
Expand Down
5 changes: 3 additions & 2 deletions droidux/src/main/java/info/izumin/android/droidux/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ public Builder() {
this.middlewares = new ArrayList<>();
}

protected List<Middleware> getMiddlewares() {
return middlewares;
public Builder addMiddleware(Middleware middleware) {
middlewares.add(middleware);
return this;
}

public abstract Store build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public void onCreate() {
super.onCreate();
store = new DroiduxRootStore.Builder()
.addMiddleware(new Logger())
.addReducer(new TodoListReducer())
.addInitialState(new TodoList(new ArrayList<>()))
.setReducer(new TodoListReducer())
.setInitialState(new TodoList(new ArrayList<>()))
.build();
}

Expand Down