// Path: sample-android/src/main/java/com/example/sample_android/action/Add.java
// @AutoValue
// public abstract class Add implements Action {
//    
//     public static Add create(String text) {
//         return new AutoValue_Add(text);
//     }
// 
//     public abstract String text();
// }
// 
// Path: sample-android/src/main/java/com/example/sample_android/action/Edit.java
// @AutoValue
// public abstract class Edit implements UpdateItem {
//     
//     public static Edit create(int id, String text) {
//         return new AutoValue_Edit(id, text);
//     }
//     
//     public abstract int id();
//     
//     public abstract String text();
// }
// 
// Path: sample-android/src/main/java/com/example/sample_android/state/TodoItem.java
// @AutoValue
// public abstract class TodoItem {
//     
//     public static TodoItem create(int id, String text, boolean done) {
//         return new AutoValue_TodoItem(id, text, done);
//     }
//     
//     public abstract int id();
// 
//     public abstract String text();
//    
//     public abstract boolean done();
// }
// 
// Path: sample-android/src/main/java/com/example/sample_android/store/MainStore.java
// public class MainStore extends SimpleStore<TodoList> {
// 
//     private final Dispatcher<Action, Action> dispatcher;
//     private final Dispatcher<Thunk<Action, Action>, Void> thunkDispatcher;
//     private final ReplayMiddleware<TodoList, Action, Action> replayMiddleware;
//     private final MonitorMiddleware<TodoList, Action, Action> monitorMiddleware;
// 
//     public MainStore(Context context) {
//         super(TodoList.initial());
//         Reducer<Action, TodoList> reducer = TodoListReducers.reducer();
//         replayMiddleware = new ReplayMiddleware<>(this, reducer);
//         monitorMiddleware = new MonitorMiddleware<>(this, new MonitorMiddleware.Config("10.0.2.2", 8000));
//         dispatcher = Dispatcher.forStore(this, reducer)
//                 .chain(new LogMiddleware<Action, Action>("ACTION"),
//                         replayMiddleware,
//                         monitorMiddleware,
//                         new PersistenceMiddleware<Action, Action>(this, new Datastore(context)));
//         thunkDispatcher = new ThunkDispatcher<>(dispatcher)
//                 .chain(new LogMiddleware<Thunk<Action, Action>, Void>("THUNK_ACTION"));
//     }
// 
//     public Action dispatch(Action action) {
//         return dispatcher.dispatch(action);
//     }
// 
//     public void dispatch(Thunk<Action, Action> thunk) {
//         thunkDispatcher.dispatch(thunk);
//     }
// 
//     public ReplayMiddleware<TodoList, Action, Action> getReplayMiddleware() {
//         return replayMiddleware;
//     }
// }


import android.app.Dialog;
import androidx.lifecycle.ViewModelProviders;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.appcompat.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.example.sample_android.action.Add;
import com.example.sample_android.action.Edit;
import com.example.sample_android.state.TodoItem;
import com.example.sample_android.store.MainStore;
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        store = ViewModelProviders.of(getActivity()).get(TodoViewModel.class).getStore();
        id = getArguments().getInt("id", -1);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.item_dialog, null);
        final TextView textView = (TextView) view.findViewById(R.id.text);
        String text = null;
        for (TodoItem todoItem : store.getState().items()) {
            if (todoItem.id() == id) {
                text = todoItem.text();
                break;
            }
        }
        textView.setText(text);

        return new AlertDialog.Builder(getContext(), getTheme())
                .setTitle(id >= 0 ? "Edit" : "New")
                .setView(view)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String text = TextUtils.isEmpty(textView.getText()) ? "Item" : textView.getText().toString();
                        if (id >= 0) {
                            store.dispatch(Edit.create(id, text));
                        } else {
// Path: sample-android/src/main/java/com/example/sample_android/action/Add.java
// @AutoValue
// public abstract class Add implements Action {
//    
//     public static Add create(String text) {
//         return new AutoValue_Add(text);
//     }
// 
//     public abstract String text();
// }
// 
// Path: sample-android/src/main/java/com/example/sample_android/action/Edit.java
// @AutoValue
// public abstract class Edit implements UpdateItem {
//     
//     public static Edit create(int id, String text) {
//         return new AutoValue_Edit(id, text);
//     }
//     
//     public abstract int id();
//     
//     public abstract String text();
// }
// 
// Path: sample-android/src/main/java/com/example/sample_android/state/TodoItem.java
// @AutoValue
// public abstract class TodoItem {
//     
//     public static TodoItem create(int id, String text, boolean done) {
//         return new AutoValue_TodoItem(id, text, done);
//     }
//     
//     public abstract int id();
// 
//     public abstract String text();
//    
//     public abstract boolean done();
// }
// 
// Path: sample-android/src/main/java/com/example/sample_android/store/MainStore.java
// public class MainStore extends SimpleStore<TodoList> {
// 
//     private final Dispatcher<Action, Action> dispatcher;
//     private final Dispatcher<Thunk<Action, Action>, Void> thunkDispatcher;
//     private final ReplayMiddleware<TodoList, Action, Action> replayMiddleware;
//     private final MonitorMiddleware<TodoList, Action, Action> monitorMiddleware;
// 
//     public MainStore(Context context) {
//         super(TodoList.initial());
//         Reducer<Action, TodoList> reducer = TodoListReducers.reducer();
//         replayMiddleware = new ReplayMiddleware<>(this, reducer);
//         monitorMiddleware = new MonitorMiddleware<>(this, new MonitorMiddleware.Config("10.0.2.2", 8000));
//         dispatcher = Dispatcher.forStore(this, reducer)
//                 .chain(new LogMiddleware<Action, Action>("ACTION"),
//                         replayMiddleware,
//                         monitorMiddleware,
//                         new PersistenceMiddleware<Action, Action>(this, new Datastore(context)));
//         thunkDispatcher = new ThunkDispatcher<>(dispatcher)
//                 .chain(new LogMiddleware<Thunk<Action, Action>, Void>("THUNK_ACTION"));
//     }
// 
//     public Action dispatch(Action action) {
//         return dispatcher.dispatch(action);
//     }
// 
//     public void dispatch(Thunk<Action, Action> thunk) {
//         thunkDispatcher.dispatch(thunk);
//     }
// 
//     public ReplayMiddleware<TodoList, Action, Action> getReplayMiddleware() {
//         return replayMiddleware;
//     }
// }


// Path: sample-android/src/main/java/com/example/sample_android/TodoItemDialogFragment.java
import android.app.Dialog;
import androidx.lifecycle.ViewModelProviders;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.appcompat.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.example.sample_android.action.Add;
import com.example.sample_android.action.Edit;
import com.example.sample_android.state.TodoItem;
import com.example.sample_android.store.MainStore;

    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        store = ViewModelProviders.of(getActivity()).get(TodoViewModel.class).getStore();
        id = getArguments().getInt("id", -1);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.item_dialog, null);
        final TextView textView = (TextView) view.findViewById(R.id.text);
        String text = null;
        for (TodoItem todoItem : store.getState().items()) {
            if (todoItem.id() == id) {
                text = todoItem.text();
                break;
            }
        }
        textView.setText(text);

        return new AlertDialog.Builder(getContext(), getTheme())
                .setTitle(id >= 0 ? "Edit" : "New")
                .setView(view)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String text = TextUtils.isEmpty(textView.getText()) ? "Item" : textView.getText().toString();
                        if (id >= 0) {
                            store.dispatch(Edit.create(id, text));
                        } else {
                            store.dispatch(Add.create(text));