|
| 1 | +package com.amrdeveloper.codeviewlibrary.plugin; |
| 2 | + |
| 3 | +import android.text.Editable; |
| 4 | +import android.text.Selection; |
| 5 | +import android.text.TextUtils; |
| 6 | +import android.text.TextWatcher; |
| 7 | +import android.text.style.UnderlineSpan; |
| 8 | +import android.widget.TextView; |
| 9 | + |
| 10 | +import java.util.LinkedList; |
| 11 | + |
| 12 | +public class UndoRedoManager { |
| 13 | + |
| 14 | + private final TextView textView; |
| 15 | + private final EditHistory editHistory; |
| 16 | + private final TextChangeWatcher textChangeWatcher; |
| 17 | + |
| 18 | + private boolean isUndoOrRedo = false; |
| 19 | + |
| 20 | + public UndoRedoManager(TextView textView) { |
| 21 | + this.textView = textView; |
| 22 | + editHistory = new EditHistory(); |
| 23 | + textChangeWatcher = new TextChangeWatcher(); |
| 24 | + } |
| 25 | + |
| 26 | + public void undo() { |
| 27 | + EditNode edit = editHistory.getPrevious(); |
| 28 | + if (edit == null) return; |
| 29 | + |
| 30 | + Editable text = textView.getEditableText(); |
| 31 | + int start = edit.start; |
| 32 | + int end = start + (edit.after != null ? edit.after.length() : 0); |
| 33 | + |
| 34 | + isUndoOrRedo = true; |
| 35 | + text.replace(start, end, edit.before); |
| 36 | + isUndoOrRedo = false; |
| 37 | + |
| 38 | + UnderlineSpan[] underlineSpans = text.getSpans(0, text.length(), UnderlineSpan.class); |
| 39 | + for (Object span : underlineSpans) text.removeSpan(span); |
| 40 | + |
| 41 | + Selection.setSelection(text, edit.before == null ? start : (start + edit.before.length())); |
| 42 | + } |
| 43 | + |
| 44 | + public void redo() { |
| 45 | + EditNode edit = editHistory.getNext(); |
| 46 | + if (edit == null) return; |
| 47 | + |
| 48 | + Editable text = textView.getEditableText(); |
| 49 | + int start = edit.start; |
| 50 | + int end = start + (edit.before != null ? edit.before.length() : 0); |
| 51 | + |
| 52 | + isUndoOrRedo = true; |
| 53 | + text.replace(start, end, edit.after); |
| 54 | + isUndoOrRedo = false; |
| 55 | + |
| 56 | + UnderlineSpan[] underlineSpans = text.getSpans(0, text.length(), UnderlineSpan.class); |
| 57 | + for (Object span : underlineSpans) text.removeSpan(span); |
| 58 | + |
| 59 | + Selection.setSelection(text, edit.after == null ? start : (start + edit.after.length())); |
| 60 | + } |
| 61 | + |
| 62 | + public void connect() { |
| 63 | + textView.addTextChangedListener(textChangeWatcher); |
| 64 | + } |
| 65 | + |
| 66 | + public void disconnect() { |
| 67 | + textView.removeTextChangedListener(textChangeWatcher); |
| 68 | + } |
| 69 | + |
| 70 | + public void setMaxHistorySize(int maxSize) { |
| 71 | + editHistory.setMaxHistorySize(maxSize); |
| 72 | + } |
| 73 | + |
| 74 | + public void clearHistory() { |
| 75 | + editHistory.clear(); |
| 76 | + } |
| 77 | + |
| 78 | + public boolean canUndo() { |
| 79 | + return editHistory.position > 0; |
| 80 | + } |
| 81 | + |
| 82 | + public boolean canRedo() { |
| 83 | + return editHistory.position < editHistory.historyList.size(); |
| 84 | + } |
| 85 | + |
| 86 | + private static final class EditHistory { |
| 87 | + |
| 88 | + private int position = 0; |
| 89 | + private int maxHistorySize = -1; |
| 90 | + |
| 91 | + private final LinkedList<EditNode> historyList = new LinkedList<>(); |
| 92 | + |
| 93 | + private void clear() { |
| 94 | + position = 0; |
| 95 | + historyList.clear(); |
| 96 | + } |
| 97 | + |
| 98 | + private void add(EditNode item) { |
| 99 | + while (historyList.size() > position) historyList.removeLast(); |
| 100 | + historyList.add(item); |
| 101 | + position++; |
| 102 | + if (maxHistorySize >= 0) trimHistory(); |
| 103 | + } |
| 104 | + |
| 105 | + private void setMaxHistorySize(int maxHistorySize) { |
| 106 | + this.maxHistorySize = maxHistorySize; |
| 107 | + if (this.maxHistorySize >= 0) trimHistory(); |
| 108 | + } |
| 109 | + |
| 110 | + private void trimHistory() { |
| 111 | + while (historyList.size() > maxHistorySize) { |
| 112 | + historyList.removeFirst(); |
| 113 | + position--; |
| 114 | + } |
| 115 | + |
| 116 | + if (position < 0) position = 0; |
| 117 | + } |
| 118 | + |
| 119 | + private EditNode getCurrent() { |
| 120 | + if (position == 0) return null; |
| 121 | + return historyList.get(position - 1); |
| 122 | + } |
| 123 | + |
| 124 | + private EditNode getPrevious() { |
| 125 | + if (position == 0) return null; |
| 126 | + position--; |
| 127 | + return historyList.get(position); |
| 128 | + } |
| 129 | + |
| 130 | + private EditNode getNext() { |
| 131 | + if (position >= historyList.size()) return null; |
| 132 | + EditNode item = historyList.get(position); |
| 133 | + position++; |
| 134 | + return item; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static final class EditNode { |
| 139 | + |
| 140 | + private int start; |
| 141 | + private CharSequence before; |
| 142 | + private CharSequence after; |
| 143 | + |
| 144 | + public EditNode(int start, CharSequence before, CharSequence after) { |
| 145 | + this.start = start; |
| 146 | + this.before = before; |
| 147 | + this.after = after; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private enum ActionType { |
| 152 | + INSERT, DELETE, PASTE, NOT_DEF; |
| 153 | + } |
| 154 | + |
| 155 | + private final class TextChangeWatcher implements TextWatcher { |
| 156 | + |
| 157 | + private CharSequence beforeChange; |
| 158 | + private CharSequence afterChange; |
| 159 | + private ActionType lastActionType = ActionType.NOT_DEF; |
| 160 | + |
| 161 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 162 | + if (isUndoOrRedo) return; |
| 163 | + beforeChange = s.subSequence(start, start + count); |
| 164 | + } |
| 165 | + |
| 166 | + public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 167 | + if (isUndoOrRedo) return; |
| 168 | + afterChange = s.subSequence(start, start + count); |
| 169 | + makeBatch(start); |
| 170 | + } |
| 171 | + |
| 172 | + private void makeBatch(int start) { |
| 173 | + ActionType action = getActionType(); |
| 174 | + EditNode currentNode = editHistory.getCurrent(); |
| 175 | + if (lastActionType != action || ActionType.PASTE == action || currentNode == null) { |
| 176 | + editHistory.add(new EditNode(start, beforeChange, afterChange)); |
| 177 | + } else { |
| 178 | + if (action == ActionType.DELETE) { |
| 179 | + currentNode.start = start; |
| 180 | + currentNode.before = TextUtils.concat(beforeChange, currentNode.before); |
| 181 | + } else { |
| 182 | + currentNode.after = TextUtils.concat(currentNode.after, afterChange); |
| 183 | + } |
| 184 | + } |
| 185 | + lastActionType = action; |
| 186 | + } |
| 187 | + |
| 188 | + private ActionType getActionType() { |
| 189 | + if (!TextUtils.isEmpty(beforeChange) && TextUtils.isEmpty(afterChange)) { |
| 190 | + return ActionType.DELETE; |
| 191 | + } else if (TextUtils.isEmpty(beforeChange) && !TextUtils.isEmpty(afterChange)) { |
| 192 | + return ActionType.INSERT; |
| 193 | + } else { |
| 194 | + return ActionType.PASTE; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + public void afterTextChanged(Editable s) { |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments