mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
Show character remaining counter when compsing message
This commit is contained in:
@@ -219,6 +219,9 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
||||
|
||||
if (title != state.title) title = state.title
|
||||
|
||||
counter.text = state.remaining
|
||||
counter.setVisible(counter.text.isNotBlank())
|
||||
|
||||
send.isEnabled = state.canSend
|
||||
sendIcon.isEnabled = state.canSend
|
||||
}
|
||||
|
||||
@@ -34,5 +34,6 @@ data class ComposeState(
|
||||
val archived: Boolean = false,
|
||||
val messages: Pair<Conversation, RealmResults<Message>>? = null,
|
||||
val attachments: List<Uri> = ArrayList(),
|
||||
val remaining: String = "",
|
||||
val canSend: Boolean = false
|
||||
)
|
||||
@@ -22,6 +22,7 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.telephony.PhoneNumberUtils
|
||||
import android.telephony.SmsMessage
|
||||
import android.view.KeyEvent
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import com.mlsdev.rximagepicker.RxImagePicker
|
||||
@@ -339,6 +340,24 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { canSend -> newState { it.copy(canSend = canSend) } }
|
||||
|
||||
// Show the remaining character counter when necessary
|
||||
view.textChangedIntent
|
||||
.observeOn(Schedulers.computation())
|
||||
.map { draft -> SmsMessage.calculateLength(draft, false) }
|
||||
.map { array ->
|
||||
val messages = array[0]
|
||||
val remaining = array[2]
|
||||
|
||||
when {
|
||||
messages <= 1 && remaining > 10 -> ""
|
||||
messages <= 1 && remaining <= 10 -> "$remaining"
|
||||
else -> "$remaining / $messages"
|
||||
}
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { remaining -> newState { it.copy(remaining = remaining) } }
|
||||
|
||||
// Update the draft whenever the text is changed
|
||||
view.textChangedIntent
|
||||
.debounce(100, TimeUnit.MILLISECONDS)
|
||||
|
||||
Reference in New Issue
Block a user