PPartisan / RunDo

Adds Undo/Redo functionality to editable text fields in Android.
MIT License
13 stars 4 forks source link

RunDo

Banner Image

RunDo adds Undo/Redo functionality to EditText fields in Android.

Releases

View Releases

Download

Current version is v1.0.5

JavaDoc available at ppartisan.github.io

Implementation

Gradle Dependency

jcenter() & mavenCentral()

Add the following to your module's build.gradle file:

dependencies {
    compile 'com.werdpressed.partisan:rundo:1.0.5'
}
maven

In addition to the dependency above, add:

repositories {
    maven {
        url 'https://dl.bintray.com/ppartisan/maven/'
    }
}

Usage

Recommended usage is via the following method:

RunDo.Factory.getInstance(FragmentManager fm);

To be used in the following way (as an example):

public class MyActivity extends Activity implements Rundo.TextLink, View.OnClickListener {

    private RunDo mRunDo;
    private EditText mEditText;
    private Button mButton;

    //...

    mRunDo = RunDo.Factory.getInstance(getFragmentManager());
    mButton.setOnClickListener(this);

    //...

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.undo_button:
                mRunDo.undo();
                break;
            case R.id.redo_button:
                mRunDo.redo();
                break;
        }

    }

    @Override
    public EditText getEditText() {
        return mEditText;
    }

}

See the sample app for a complete example

The getInstance() method requires a FragmentManager (whether android.app.FragmentManager or android.suppoer.v4.app.FragmentManager) argument.

RunDo implementations extend either android.app.Fragment or android.support.v4.app.Fragment.

Calling Undo/Redo

To call Undo or Redo, use the undo() and redo() methods:

mRunDo.undo();
mRunDo.redo();

For example:

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
        case R.id.undo_button:
            mRunDo.undo();
            break;
        case R.id.redo_button:
            mRunDo.redo();
            break;
    }

}

Tweaking Parameters

There are two ways to customise RunDo objects; setQueueSize(int size) and setTimerLength(long lengthInMillis).

setQueueSize() adjusts the size of the undo and redo queues to hold the specified number of entries, before entries from the opposite end of the queue begin to be removed. The default size is 10. Calling this method will clear all current entries from both queues.

setTimerLength() adjust the countdown between the user's last text entry and the period at which any altered text is saved to the undo queue. The timer is reset if further text is entered during this period. The default value is 2000 milliseconds (2 seconds).

Clearing Queues

Use [clearAllQueues()](http://ppartisan.github.io/RunDo/JavaDoc/com/werdpressed/partisan/rundo/RunDo.html#clearAllQueues()) to remove all elements from both undo and redo queues.

Callbacks

Implement RunDo.Callbacks to be notified whenever [undo()](http://ppartisan.github.io/RunDo/JavaDoc/com/werdpressed/partisan/rundo/RunDo.html#undo()) or [redo()](http://ppartisan.github.io/RunDo/JavaDoc/com/werdpressed/partisan/rundo/RunDo.html#redo()) is called:

@Override
public void undoCalled() {
}

@Override
public void redoCalled() {
}