-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #557 from ubergeek42/volume-btn-roles-new
Volume buttons: implement Weechat-like input history rather than sent history
- Loading branch information
Showing
11 changed files
with
177 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 101 additions & 44 deletions
145
app/src/main/java/com/ubergeek42/WeechatAndroid/utils/History.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,111 @@ | ||
package com.ubergeek42.WeechatAndroid.utils | ||
|
||
import android.os.Bundle | ||
import android.text.Editable | ||
import android.os.Parcel | ||
import android.widget.EditText | ||
import androidx.core.os.bundleOf | ||
import com.ubergeek42.WeechatAndroid.service.P | ||
|
||
class History { | ||
private var index = -1 | ||
private var userInput: Editable? = null | ||
private var selectionStart: Int? = null | ||
private var selectionEnd: Int? = null | ||
|
||
fun navigateOffset(editText: EditText, offset: Int) { | ||
if (index == -1) { | ||
userInput = editText.text | ||
selectionStart = editText.selectionStart | ||
selectionEnd = editText.selectionEnd | ||
} | ||
val newIndex = index + offset | ||
messageAt(newIndex)?.let { | ||
editText.text = it | ||
editText.post { | ||
if (newIndex == -1) { | ||
// Restore user selection. | ||
editText.setSelection(selectionStart ?: 0, selectionEnd ?: editText.length()) | ||
} else { | ||
// Go to end for sent messages. | ||
editText.setSelection(editText.length()) | ||
} | ||
import androidx.core.text.getSpans | ||
import com.ubergeek42.WeechatAndroid.R | ||
import com.ubergeek42.WeechatAndroid.upload.ShareSpan | ||
|
||
class History : java.io.Serializable { | ||
companion object { | ||
// Sentinel index representing the most recent end of the message history. | ||
private const val END = -1 | ||
|
||
private const val MAX_MESSAGE_LENGTH = 2000 | ||
private const val MAX_HISTORY_LENGTH = 40 | ||
} | ||
|
||
class Message : java.io.Serializable { | ||
lateinit var content: String | ||
var selectionStart: Int = 0 | ||
var selectionEnd: Int = 0 | ||
|
||
constructor(editText: EditText) { | ||
updateFromEdit(editText) | ||
} | ||
|
||
constructor(source: Parcel) { | ||
content = source.readString() ?: "" | ||
selectionStart = source.readInt() | ||
selectionStart = source.readInt() | ||
} | ||
|
||
|
||
fun updateFromEdit(editText: EditText) { | ||
content = Utils.cut(editText.text.toString(), MAX_MESSAGE_LENGTH) | ||
// Input length might be shorter because of the text ellipsis from above. | ||
selectionStart = minOf(content.length, editText.selectionStart) | ||
selectionEnd = minOf(content.length, editText.selectionEnd) | ||
} | ||
|
||
fun applyToEdit(editText: EditText) { | ||
editText.setText(content) | ||
editText.setSelection(selectionStart, selectionEnd) | ||
} | ||
} | ||
|
||
private var messages: MutableList<Message> = mutableListOf() | ||
private var index = END | ||
|
||
enum class Direction { Older, Newer } | ||
|
||
// Save message iff it is not empty and not equal to the most recent message. | ||
// Will remove the oldest message if the message size goes above the maximum history size. | ||
private fun maybeSaveMessage(editText: EditText): Boolean { | ||
val message = Message(editText) | ||
val content = message.content | ||
if (content.trim().isNotEmpty()) { | ||
val last = messages.getOrNull(0)?.content | ||
if (last?.equalsIgnoringUselessSpans(content) != true) { | ||
messages.add(0, message) | ||
if (messages.size > MAX_HISTORY_LENGTH) messages.removeLast() | ||
return true | ||
} | ||
index = newIndex | ||
} | ||
return false | ||
} | ||
|
||
fun save(): Bundle = bundleOf( | ||
Pair("index", index), | ||
Pair("userInput", userInput), | ||
Pair("selStart", selectionStart), | ||
Pair("selEnd", selectionEnd), | ||
) | ||
|
||
fun restore(bundle: Bundle) { | ||
index = bundle.getInt("index", -1) | ||
userInput = bundle.get("userInput") as Editable? | ||
selectionStart = bundle.getInt("selStart", -1).let { if (it == -1) null else it } | ||
selectionEnd = bundle.getInt("selEnd", -1).let { if (it == -1) null else it } | ||
private fun shareSpanCount(editText: EditText): Int = editText.text.getSpans<ShareSpan>().size | ||
|
||
fun navigate(editText: EditText, direction: Direction) { | ||
val spc = shareSpanCount(editText) | ||
if (spc > 0) { | ||
// The editText contains non-uploaded ShareSpans that would be lost by navigating away. | ||
// Bail out early to prevent data loss. | ||
Toaster.ErrorToast.show( | ||
editText.context.resources.getQuantityText( | ||
R.plurals.error__history_with_sharespans, | ||
spc | ||
).toString() | ||
) | ||
return | ||
} | ||
var newIndex = when (direction) { | ||
Direction.Older -> index + 1 | ||
Direction.Newer -> index - 1 | ||
} | ||
if (index == END) { | ||
// Not in history yet. Push current edit to history. | ||
val wasInserted = maybeSaveMessage(editText) | ||
// Special case when saving from end and going older: since we've just inserted, we need | ||
// to skip over the message that was just saved. | ||
if (wasInserted && direction == Direction.Older) newIndex += 1 | ||
} else { | ||
// In history already. Save changes before navigating away. | ||
messages[index].updateFromEdit(editText) | ||
} | ||
if (newIndex < END) newIndex = END | ||
if (newIndex >= messages.size) newIndex = messages.size - 1 | ||
if (newIndex == END) { | ||
if (editText.text.isNotEmpty()) editText.setText("") | ||
} else if (newIndex != index) { | ||
messages[newIndex].applyToEdit(editText) | ||
} | ||
index = newIndex | ||
} | ||
|
||
private fun messageAt(index: Int): Editable? = | ||
if (index == -1) userInput else P.sentMessages.getOrNull(P.sentMessages.size - 1 - index) | ||
?.let { Editable.Factory.getInstance().newEditable(it) } | ||
fun reset(editText: EditText) { | ||
maybeSaveMessage(editText) | ||
index = END | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.