marcglasberg / async_redux

Flutter Package: A Redux version tailored for Flutter, which is easy to learn, to use, to test, and has no boilerplate. Allows for both sync and async reducers.
Other
230 stars 41 forks source link

Idea: UndoableAction #76

Closed SunlightBro closed 3 years ago

SunlightBro commented 4 years ago

I had this thought of maybe having an undo feature in async_redux:

Image a class like this in this package,

abstract class UndoableAction<St> extends ReduxAction<St> {}

you could use in your app like this (for example in a delete action):

class DeleteTodoAction extends UndoableAction<AppState>{
  final int index;
  DeleteTodoAction(this.index);

  @override
  AppState reduce() => state.copyWith(todos: state.todos..removeAt(index));
}

That would after the Action is finished, display a Dialog similar to the UserExceptionDialog with an Undo-Button, that when pressed reverts to the previous state. Screenshot_20200619-164315_2

I know this could be solved outside of async_redux, but I think this is not something only I come across and it would be awesome to have something like as an easy to use implementation for this Undo feature. Let me know what you think.

marcglasberg commented 4 years ago

The store has a single, immutable state. So if you save a copy of the state and then reinstate it (replace the current state with the old one) you have your undo. So I'd say AsyncRedux (and Redux in general) already has this feature.

The problem is that this will replace all of the saved state, not only the deleted item. This could delete some other important state changes that happened between the delete and the undo.

So, for this to work you have to replace only the part of the state that contains the deleted item, and you must make sure the rest of the state remains consistent with the undeletion of the item.

So, I don't see a way to do this automatically or how AsyncRedux could help with that.