I want to create a type of history thing in my pro...
# getting-started
z
I want to create a type of history thing in my project, where the users actions are added to a list or some other array, and then the user can go back or forward. It should behave like how you can go back and forth in a file browser. I'm unsure how to represent this in code though. Any ideas?
c
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!
👌 1
m
https://en.wikipedia.org/wiki/Command_pattern
Uses
[…]
Multi-level undo
If all user actions in a program are implemented as command objects, the program can keep a stack of the most recently executed commands. When the user wants to undo a command, the program simply pops the most recent command object and executes its undo() method.
Instead of popping commands from the list you leave them in, and move the pointer accordingly.
c
@zt You got me interested in actually trying to build out this feature, and it was as easy to make as I expected so I just released this experimental feature in Ballast version 2.2.0. You can view the documentation here, play with an example, and join us over at #ballast for help getting started using Ballast or learning how to use this new feature.
💥 1
z
I might try this out then, I'm using compose desktop