Move packages to com.moez.QKSMS

This commit is contained in:
Moez Bhatti
2018-06-20 14:29:07 -04:00
parent d8141737b3
commit a5f518042e
268 changed files with 1101 additions and 1116 deletions
@@ -0,0 +1,140 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.feature.qkreply
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.os.Build
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.Menu
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.moez.QKSMS.common.base.QkThemedActivity
import com.moez.QKSMS.common.util.extensions.autoScrollToStart
import com.moez.QKSMS.common.util.extensions.resolveThemeColor
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.common.util.extensions.setVisible
import com.moez.QKSMS.feature.compose.MessagesAdapter
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import dagger.android.AndroidInjection
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.qkreply_activity.*
import javax.inject.Inject
class QkReplyActivity : QkThemedActivity(), QkReplyView {
@Inject lateinit var adapter: MessagesAdapter
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
override val menuItemIntent: Subject<Int> = PublishSubject.create()
override val textChangedIntent by lazy { message.textChanges() }
override val changeSimIntent by lazy { sim.clicks() }
override val sendIntent by lazy { send.clicks() }
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory)[QkReplyViewModel::class.java] }
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
super.onCreate(savedInstanceState)
setFinishOnTouchOutside(prefs.qkreplyTapDismiss.get())
setContentView(R.layout.qkreply_activity)
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
viewModel.bindView(this)
theme
.autoDisposable(scope())
.subscribe { theme ->
send.setBackgroundTint(theme.theme)
send.setTint(theme.textPrimary)
}
toolbar.clipToOutline = true
adapter.autoScrollToStart(messages)
messages.layoutManager = LinearLayoutManager(this).apply { stackFromEnd = true }
messages.adapter = adapter
// These theme attributes don't apply themselves on API 21
if (Build.VERSION.SDK_INT <= 22) {
toolbar.setBackgroundTint(resolveThemeColor(R.attr.colorPrimary))
background.setBackgroundTint(resolveThemeColor(R.attr.composeBackground))
composeBackground.setBackgroundTint(resolveThemeColor(R.attr.composeBackground))
composeBackgroundGradient.setBackgroundTint(resolveThemeColor(R.attr.composeBackground))
}
}
override fun render(state: QkReplyState) {
if (state.hasError) {
finish()
}
threadId.onNext(state.data?.first?.id ?: 0)
title = state.title
toolbar.menu.findItem(R.id.expand)?.isVisible = !state.expanded
toolbar.menu.findItem(R.id.collapse)?.isVisible = state.expanded
adapter.data = state.data
counter.text = state.remaining
counter.setVisible(counter.text.isNotBlank())
sim.setVisible(state.subscription != null)
sim.contentDescription = getString(R.string.compose_sim_cd, state.subscription?.displayName)
simIndex.text = "${state.subscription?.simSlotIndex?.plus(1)}"
send.isEnabled = state.canSend
send.imageAlpha = if (state.canSend) 255 else 128
}
override fun setDraft(draft: String) {
message.setText(draft)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.qkreply, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
menuItemIntent.onNext(item.itemId)
return true
}
override fun getActivityThemeRes(night: Boolean, black: Boolean) = when {
night && black -> R.style.AppThemeBlackDialog
night && !black -> R.style.AppThemeDarkDialog
else -> R.style.AppThemeLightDialog
}
}
@@ -0,0 +1,22 @@
package com.moez.QKSMS.feature.qkreply
import android.arch.lifecycle.ViewModel
import com.moez.QKSMS.injection.ViewModelKey
import dagger.Module
import dagger.Provides
import dagger.multibindings.IntoMap
import javax.inject.Named
@Module
class QkReplyActivityModule {
@Provides
@Named("threadId")
fun provideThreadId(activity: QkReplyActivity): Long = activity.intent.extras?.getLong("threadId") ?: 0L
@Provides
@IntoMap
@ViewModelKey(QkReplyViewModel::class)
fun provideQkReplyViewModel(viewModel: QkReplyViewModel): ViewModel = viewModel
}
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.feature.qkreply
import com.moez.QKSMS.compat.SubscriptionInfoCompat
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Message
import io.realm.RealmResults
data class QkReplyState(
val hasError: Boolean = false,
val title: String = "",
val expanded: Boolean = false,
val data: Pair<Conversation, RealmResults<Message>>? = null,
val remaining: String = "",
val subscription: SubscriptionInfoCompat? = null,
val canSend: Boolean = false
)
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.feature.qkreply
import com.moez.QKSMS.common.base.QkView
import io.reactivex.Observable
interface QkReplyView : QkView<QkReplyState> {
val menuItemIntent: Observable<Int>
val textChangedIntent: Observable<CharSequence>
val changeSimIntent: Observable<*>
val sendIntent: Observable<Unit>
fun setDraft(draft: String)
fun finish()
}
@@ -0,0 +1,229 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.feature.qkreply
import android.telephony.SmsMessage
import com.moez.QKSMS.R
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkViewModel
import com.moez.QKSMS.compat.SubscriptionManagerCompat
import com.moez.QKSMS.extensions.asObservable
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.interactor.MarkRead
import com.moez.QKSMS.interactor.SendMessage
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Message
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import com.moez.QKSMS.util.ActiveSubscriptionObservable
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import io.realm.RealmResults
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Named
class QkReplyViewModel @Inject constructor(
@Named("threadId") threadId: Long,
private val conversationRepo: ConversationRepository,
private val markRead: MarkRead,
private val messageRepo: MessageRepository,
private val navigator: Navigator,
private val sendMessage: SendMessage,
private val subscriptionManager: SubscriptionManagerCompat
) : QkViewModel<QkReplyView, QkReplyState>(QkReplyState()) {
private val conversation by lazy {
conversationRepo.getConversationAsync(threadId)
.asObservable<Conversation>()
.filter { it.isLoaded }
.filter { it.isValid }
.distinctUntilChanged()
}
private val messages: Subject<RealmResults<Message>> =
BehaviorSubject.createDefault(messageRepo.getUnreadMessages(threadId))
init {
disposables += markRead
disposables += sendMessage
// When the set of messages changes, update the state
// If we're ever showing an empty set of messages, then it's time to shut down to activity
disposables += Observables
.combineLatest(messages, conversation) { messages, conversation ->
newState { copy(data = Pair(conversation, messages)) }
messages
}
.switchMap { messages -> messages.asObservable() }
.filter { it.isLoaded }
.filter { it.isValid }
.filter { it.isEmpty() }
.subscribe { newState { copy(hasError = true) } }
disposables += conversation
.map { conversation -> conversation.getTitle() }
.distinctUntilChanged()
.subscribe { title -> newState { copy(title = title) } }
val latestSubId = messages
.map { messages -> messages.lastOrNull()?.subId ?: -1 }
.distinctUntilChanged()
val subscriptions = ActiveSubscriptionObservable(subscriptionManager)
disposables += Observables.combineLatest(latestSubId, subscriptions) { subId, subs ->
val sub = if (subs.size > 1) subs.firstOrNull { it.subscriptionId == subId } ?: subs[0] else null
newState { copy(subscription = sub) }
}.subscribe()
}
override fun bindView(view: QkReplyView) {
super.bindView(view)
conversation
.map { conversation -> conversation.draft }
.distinctUntilChanged()
.autoDisposable(view.scope())
.subscribe { draft -> view.setDraft(draft) }
// Mark read
view.menuItemIntent
.filter { id -> id == R.id.read }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> conversation.id }
.autoDisposable(view.scope())
.subscribe { threadId ->
markRead.execute(listOf(threadId)) { newState { copy(hasError = true) } }
}
// Call
view.menuItemIntent
.filter { id -> id == R.id.call }
.withLatestFrom(conversation, { _, conversation -> conversation })
.mapNotNull { conversation -> conversation.recipients.first()?.address }
.doOnNext { address -> navigator.makePhoneCall(address) }
.autoDisposable(view.scope())
.subscribe { newState { copy(hasError = true) } }
// Show all messages
view.menuItemIntent
.filter { id -> id == R.id.expand }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> messageRepo.getMessages(conversation.id) }
.doOnNext(messages::onNext)
.autoDisposable(view.scope())
.subscribe { newState { copy(expanded = true) } }
// Show unread messages only
view.menuItemIntent
.filter { id -> id == R.id.collapse }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> messageRepo.getUnreadMessages(conversation.id) }
.doOnNext(messages::onNext)
.autoDisposable(view.scope())
.subscribe { newState { copy(expanded = false) } }
// View conversation
view.menuItemIntent
.filter { id -> id == R.id.view }
.withLatestFrom(conversation, { _, conversation -> conversation })
.map { conversation -> conversation.id }
.doOnNext { threadId -> navigator.showConversation(threadId) }
.autoDisposable(view.scope())
.subscribe { newState { copy(hasError = true) } }
// 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 { 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 { 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 ->
conversationRepo.saveDraft(threadId, draft)
})
.autoDisposable(view.scope())
.subscribe()
// Toggle to the next sim slot
view.changeSimIntent
.withLatestFrom(state) { _, state ->
val subs = subscriptionManager.activeSubscriptionInfoList
val subIndex = subs.indexOfFirst { it.subscriptionId == state.subscription?.subscriptionId }
val subscription = when {
subIndex == -1 -> null
subIndex < subs.size - 1 -> subs[subIndex + 1]
else -> subs[0]
}
newState { copy(subscription = subscription) }
}
.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(state, conversation, { body, state, conversation ->
val subId = state.subscription?.subscriptionId ?: -1
val threadId = conversation.id
val addresses = conversation.recipients.map { it.address }
sendMessage.execute(SendMessage.Params(subId, threadId, addresses, body))
view.setDraft("")
threadId
})
.doOnNext { threadId ->
markRead.execute(listOf(threadId)) {
newState { copy(hasError = true) }
}
}
.autoDisposable(view.scope())
.subscribe()
}
}