Show character remaining counter when compsing message

This commit is contained in:
Moez Bhatti
2018-03-12 15:25:38 -07:00
parent 154389b5f1
commit 6d6b3fbf77
4 changed files with 58 additions and 16 deletions
@@ -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)
+35 -16
View File
@@ -66,7 +66,8 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/compose_bar_background"
android:gravity="center_vertical">
android:gravity="bottom"
tools:backgroundTint="@color/backgroundLight">
<LinearLayout
android:id="@+id/messageBackground"
@@ -126,24 +127,42 @@
</LinearLayout>
<FrameLayout
android:id="@+id/send"
android:layout_width="44dp"
android:layout_height="44dp"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:background="@drawable/circle"
android:elevation="4dp"
tools:backgroundTint="@color/tools_theme">
android:orientation="vertical">
<ImageView
android:id="@+id/sendIcon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:src="@drawable/ic_send_black_24dp"
tools:tint="@color/textPrimaryDark" />
<presentation.common.widget.QkTextView
android:id="@+id/counter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:gravity="center"
android:textSize="12sp"
app:textColor="secondary"
tools:text="108 / 2" />
</FrameLayout>
<FrameLayout
android:id="@+id/send"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginBottom="8dp"
android:background="@drawable/circle"
android:elevation="4dp"
tools:backgroundTint="@color/tools_theme">
<ImageView
android:id="@+id/sendIcon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:src="@drawable/ic_send_black_24dp"
tools:tint="@color/textPrimaryDark" />
</FrameLayout>
</LinearLayout>
</LinearLayout>