B4J Library [B4X] [class] UndoManager

Discussion in 'B4J Libraries & Classes' started by Erel, Nov 13, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User


    The UndoManager class implements a simple undo / redo system.
    It is compatible with B4J, B4A and B4i.

    It maintains an internal stack of states. You can use any custom type you like to represent each state as long as it is supported by B4XSerializator.

    Using this class is simple. You just need to call AddState whenever there is a new state. UndoManager will compare it to the current top state and will discard duplicate states. This allows us to use a timer to add the states:
    Type UndoData (Text As String, clr As Int)

    Sub Timer1_Tick
    'nothing will happen if the state is the same as the previous one
    End Sub

    Sub GetState As UndoData
    Dim ud As UndoData
       ud.Text = TextArea1.Text
       ud.clr = fx.Colors.To32Bit(ColorPicker1.SelectedColor)
    Return ud
    End Sub
    In the above code we track the text and selected color.

    All that is left to do is to call undo or redo and set back the states:
    Sub btnUndo_Action
    Dim ud As UndoData = manager.Undo
    End Sub

    Sub SetState (ud As UndoData)
       TextArea1.Text = ud.Text
       ColorPicker1.SelectedColor = fx.Colors.From32Bit(ud.clr)
    End Sub
    The class module is in the attached example. Note that the example requires B4J v4.70+ (https://www.b4x.com/android/forum/threads/72913/#content).
    If you are using it with B4i then you need to use iRandomAccessFile v1.51+.

    Attached Files:

    Johan Hormaza, BPak, itgirl and 15 others like this.
  2. LucaMs

    LucaMs Expert Licensed User

    I still have to try it but it seems to be a great and very useful [multi-platform] class!!!

    Thank you, Erel.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice