The main idea is to not handle those user inputs immediately, but instead of capture the user’s “intent” to make a change, and implement some structure for processing those changes one-by-one to actually handle the change. When you write your UI in this way, it becomes much easier to just store those Intents or the resulting State, and then you can more easily navigate through that history of States.
This general idea of capturing the “Intent” and processing those intents elsewhere is generally known as the MVI (or UDF) pattern. There are several libraries that build this model out for you, such that you only need to define your Intents and how those get processed. In the Web world, Redux is one of the most popular options for this programming model, and there are several options available in Kotlin, including a library I maintain called
Ballast.
Ballast doesn’t currently have anything out-of-the-box for an undo/redo functionality, but the core functionality is available to implement such functionality pretty easily, and there’s even an
open issue for this. This would be a very welcome contribution to this library!