mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-16 19:18:52 +01:00
Add ability to send messages via qkreply
This commit is contained in:
@@ -26,12 +26,15 @@ import android.view.MenuItem
|
||||
import android.view.ViewGroup
|
||||
import android.view.Window
|
||||
import android.view.WindowManager
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.jakewharton.rxbinding2.widget.textChanges
|
||||
import com.moez.QKSMS.R
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.base.QkThemedActivity
|
||||
import common.util.extensions.autoScrollToStart
|
||||
import common.util.extensions.setBackgroundTint
|
||||
import common.util.extensions.setVisible
|
||||
import feature.compose.MessagesAdapter
|
||||
import injection.appComponent
|
||||
import io.reactivex.rxkotlin.Observables
|
||||
@@ -44,6 +47,8 @@ class QkReplyActivity : QkThemedActivity<QkReplyViewModel>(), QkReplyView {
|
||||
|
||||
override val viewModelClass = QkReplyViewModel::class
|
||||
override val menuItemIntent: Subject<Int> = PublishSubject.create()
|
||||
override val textChangedIntent by lazy { message.textChanges() }
|
||||
override val sendIntent by lazy { send.clicks() }
|
||||
|
||||
@Inject lateinit var adapter: MessagesAdapter
|
||||
|
||||
@@ -108,6 +113,15 @@ class QkReplyActivity : QkThemedActivity<QkReplyViewModel>(), QkReplyView {
|
||||
toolbar.menu.findItem(R.id.collapse)?.isVisible = state.expanded
|
||||
|
||||
adapter.data = state.data
|
||||
|
||||
counter.text = state.remaining
|
||||
counter.setVisible(counter.text.isNotBlank())
|
||||
|
||||
send.isEnabled = state.canSend
|
||||
}
|
||||
|
||||
override fun setDraft(draft: String) {
|
||||
message.setText(draft)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
|
||||
@@ -25,5 +25,7 @@ import model.Message
|
||||
data class QkReplyState(
|
||||
val title: String = "",
|
||||
val expanded: Boolean = false,
|
||||
val data: Pair<Conversation, RealmResults<Message>>? = null
|
||||
val data: Pair<Conversation, RealmResults<Message>>? = null,
|
||||
val remaining: String = "",
|
||||
val canSend: Boolean = false
|
||||
)
|
||||
@@ -18,12 +18,15 @@
|
||||
*/
|
||||
package feature.qkreply
|
||||
|
||||
import android.view.MenuItem
|
||||
import common.base.QkView
|
||||
import io.reactivex.Observable
|
||||
|
||||
interface QkReplyView : QkView<QkReplyState> {
|
||||
|
||||
val menuItemIntent: Observable<Int>
|
||||
val textChangedIntent: Observable<CharSequence>
|
||||
val sendIntent: Observable<Unit>
|
||||
|
||||
fun setDraft(draft: String)
|
||||
|
||||
}
|
||||
@@ -19,23 +19,28 @@
|
||||
package feature.qkreply
|
||||
|
||||
import android.content.Intent
|
||||
import android.telephony.SmsMessage
|
||||
import com.moez.QKSMS.R
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.base.QkViewModel
|
||||
import injection.appComponent
|
||||
import interactor.MarkRead
|
||||
import interactor.SendMessage
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import model.Conversation
|
||||
import repository.MessageRepository
|
||||
import util.extensions.asObservable
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(QkReplyState()) {
|
||||
|
||||
@Inject lateinit var markRead: MarkRead
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var sendMessage: SendMessage
|
||||
|
||||
private val conversation by lazy {
|
||||
messageRepo.getConversationAsync(intent.getLongExtra("threadId", -1))
|
||||
@@ -49,6 +54,7 @@ class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(
|
||||
appComponent.inject(this)
|
||||
|
||||
disposables += markRead
|
||||
disposables += sendMessage
|
||||
|
||||
disposables += conversation
|
||||
.doOnNext { conversation -> newState { it.copy(title = conversation.getTitle()) } }
|
||||
@@ -83,6 +89,55 @@ class QkReplyViewModel(intent: Intent) : QkViewModel<QkReplyView, QkReplyState>(
|
||||
.map { conversation -> Pair(conversation, messageRepo.getUnreadMessages(conversation.id)) }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { data -> newState { it.copy(expanded = false, data = data) } }
|
||||
|
||||
// Enable the send button when there is text input into the new message body or there's
|
||||
// an attachment, disable otherwise
|
||||
view.textChangedIntent
|
||||
.map { text -> text.isNotBlank() }
|
||||
.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)
|
||||
.map { draft -> draft.toString() }
|
||||
.observeOn(Schedulers.io())
|
||||
.withLatestFrom(conversation.map { it.id }, { draft, threadId ->
|
||||
messageRepo.saveDraft(threadId, draft)
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
// Send a message when the send button is clicked, and disable editing mode if it's enabled
|
||||
view.sendIntent
|
||||
.withLatestFrom(view.textChangedIntent, { _, body -> body })
|
||||
.map { body -> body.toString() }
|
||||
.withLatestFrom(conversation, { body, conversation ->
|
||||
val threadId = conversation.id
|
||||
val addresses = conversation.recipients.map { it.address }
|
||||
sendMessage.execute(SendMessage.Params(threadId, addresses, body, listOf()))
|
||||
view.setDraft("")
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user