Unlock scheduled tab, show pending scheduled messages

This commit is contained in:
Moez Bhatti
2018-06-26 22:36:28 -04:00
parent 272f4bceb9
commit 61c8ca17e8
9 changed files with 163 additions and 10 deletions
@@ -30,6 +30,7 @@ import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import io.realm.Realm
import io.realm.RealmResults
import javax.inject.Inject
import javax.inject.Singleton
@@ -51,6 +52,13 @@ class ContactRepositoryImpl @Inject constructor(val context: Context) : ContactR
.map { id -> Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id) }
}
override fun getContacts(): RealmResults<Contact> {
val realm = Realm.getDefaultInstance()
return realm.where(Contact::class.java)
.sort("name")
.findAll()
}
override fun getUnmanagedContacts(): Flowable<List<Contact>> {
val realm = Realm.getDefaultInstance()
return realm.where(Contact::class.java)
@@ -22,11 +22,14 @@ import android.net.Uri
import com.moez.QKSMS.model.Contact
import io.reactivex.Flowable
import io.reactivex.Single
import io.realm.RealmResults
interface ContactRepository {
fun findContactUri(address: String): Single<Uri>
fun getContacts(): RealmResults<Contact>
fun getUnmanagedContacts(): Flowable<List<Contact>>
}
@@ -18,6 +18,12 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
* based on the data
*/
var emptyView: View? = null
set(value) {
if (field === value) return
field = value
value?.setVisible(data?.isLoaded == true && data?.isEmpty() == true)
}
private val emptyListener: (OrderedRealmCollection<T>) -> Unit = { data ->
emptyView?.setVisible(data.isLoaded && data.isEmpty())
@@ -49,6 +49,7 @@ import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.common.util.extensions.setVisible
import com.moez.QKSMS.feature.conversations.ConversationItemTouchCallback
import com.moez.QKSMS.feature.conversations.ConversationsAdapter
import com.moez.QKSMS.feature.scheduled.ScheduledMessageAdapter
import com.moez.QKSMS.repository.SyncRepository
import com.uber.autodispose.kotlin.autoDisposable
import dagger.android.AndroidInjection
@@ -63,6 +64,7 @@ class MainActivity : QkThemedActivity(), MainView {
@Inject lateinit var navigator: Navigator
@Inject lateinit var conversationsAdapter: ConversationsAdapter
@Inject lateinit var scheduledMessageAdapter: ScheduledMessageAdapter
@Inject lateinit var searchAdapter: SearchAdapter
@Inject lateinit var itemTouchCallback: ConversationItemTouchCallback
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
@@ -122,8 +124,6 @@ class MainActivity : QkThemedActivity(), MainView {
// Don't allow clicks to pass through the drawer layout
drawer.clicks().subscribe()
scheduled.isEnabled = false
// Set the theme color tint to the recyclerView, progressbar, and FAB
colors.themeObservable()
.doOnNext { recyclerView.scrapViews() }
@@ -160,7 +160,6 @@ class MainActivity : QkThemedActivity(), MainView {
toggle.drawerArrowDrawable.color = resolveThemeColor(android.R.attr.textColorSecondary)
conversationsAdapter.autoScrollToStart(recyclerView)
conversationsAdapter.emptyView = empty
}
override fun render(state: MainState) {
@@ -194,6 +193,8 @@ class MainActivity : QkThemedActivity(), MainView {
rateLayout.setVisible(state.showRating)
compose.setVisible(state.page is Inbox || state.page is Archived)
conversationsAdapter.emptyView = empty.takeIf { state.page is Inbox || state.page is Archived }
scheduledMessageAdapter.emptyView = empty.takeIf { state.page is Archived }
when (state.page) {
is Inbox -> {
@@ -227,7 +228,8 @@ class MainActivity : QkThemedActivity(), MainView {
is Scheduled -> {
setTitle(R.string.title_scheduled)
recyclerView.adapter = null
if (recyclerView.adapter !== scheduledMessageAdapter) recyclerView.adapter = scheduledMessageAdapter
scheduledMessageAdapter.updateData(state.page.data)
itemTouchHelper.attachToRecyclerView(null)
empty.setText(R.string.scheduled_empty_text)
}
@@ -18,7 +18,9 @@
*/
package com.moez.QKSMS.feature.main
import com.moez.QKSMS.feature.scheduled.ScheduledMessageAdapter
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.ScheduledMessage
import com.moez.QKSMS.model.SearchResult
import com.moez.QKSMS.repository.SyncRepository
import io.realm.RealmResults
@@ -55,4 +57,4 @@ data class Archived(
val selected: Int = 0) : MainPage()
data class Scheduled(
val data: Any? = null) : MainPage()
val data: RealmResults<ScheduledMessage>) : MainPage()
@@ -36,7 +36,7 @@ import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.manager.RatingManager
import com.moez.QKSMS.model.SyncLog
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import com.moez.QKSMS.repository.ScheduledMessageRepository
import com.moez.QKSMS.repository.SyncRepository
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.Observable
@@ -50,7 +50,6 @@ import java.util.concurrent.TimeUnit
import javax.inject.Inject
class MainViewModel @Inject constructor(
private val messageRepo: MessageRepository,
private val markAllSeen: MarkAllSeen,
private val conversationRepo: ConversationRepository,
private val deleteConversations: DeleteConversations,
@@ -63,6 +62,7 @@ class MainViewModel @Inject constructor(
private val navigator: Navigator,
private val permissionManager: PermissionManager,
private val ratingManager: RatingManager,
private val scheduledMessageRepo: ScheduledMessageRepository,
private val syncMessages: SyncMessages,
private val syncRepository: SyncRepository
) : QkViewModel<MainView, MainState>(MainState(page = Inbox(data = conversationRepo.getConversations()))) {
@@ -190,7 +190,7 @@ class MainViewModel @Inject constructor(
when (it) {
DrawerItem.INBOX -> newState { copy(page = Inbox(data = conversationRepo.getConversations())) }
DrawerItem.ARCHIVED -> newState { copy(page = Archived(data = conversationRepo.getConversations(true))) }
DrawerItem.SCHEDULED -> newState { copy(page = Scheduled()) }
DrawerItem.SCHEDULED -> newState { copy(page = Scheduled(data = scheduledMessageRepo.getScheduledMessages())) }
else -> {
} // Do nothing
}
@@ -0,0 +1,54 @@
package com.moez.QKSMS.feature.scheduled
import android.telephony.PhoneNumberUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkRealmAdapter
import com.moez.QKSMS.common.base.QkViewHolder
import com.moez.QKSMS.common.util.DateFormatter
import com.moez.QKSMS.model.Contact
import com.moez.QKSMS.model.PhoneNumber
import com.moez.QKSMS.model.Recipient
import com.moez.QKSMS.model.ScheduledMessage
import com.moez.QKSMS.repository.ContactRepository
import io.realm.RealmList
import kotlinx.android.synthetic.main.scheduled_message_list_item.view.*
import javax.inject.Inject
class ScheduledMessageAdapter @Inject constructor(
private val contactRepo: ContactRepository,
private val dateFormatter: DateFormatter
) : QkRealmAdapter<ScheduledMessage>() {
private val contacts by lazy { contactRepo.getContacts() }
private val contactMap: HashMap<String, Contact> = hashMapOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
return QkViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.scheduled_message_list_item, parent, false))
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val message = getItem(position)!!
val view = holder.itemView
message.recipients.forEach { address ->
if (!contactMap.containsKey(address)) {
contactMap[address] = contacts
.firstOrNull { it.numbers.any { number -> PhoneNumberUtils.compare(address, number.address) } }
?: Contact(numbers = RealmList(PhoneNumber(address)))
}
}
// GroupAvatarView only accepts recipients, so map the phone numbers to recipients
view.avatars.contacts = message.recipients.map { address -> Recipient(address = address) }
view.recipients.text = message.recipients.joinToString(",") { address ->
contactMap[address]?.name?.takeIf { it.isNotBlank() } ?: address
}
view.date.text = dateFormatter.getScheduledTimestamp(message.date)
view.body.text = message.body
}
}
@@ -60,8 +60,7 @@
<LinearLayout
android:id="@+id/scheduled"
style="@style/DrawerRow"
android:alpha="0.5">
style="@style/DrawerRow">
<ImageView
android:id="@+id/scheduledIcon"
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ 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/>.
-->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="12dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="12dp">
<com.moez.QKSMS.common.widget.GroupAvatarView
android:id="@+id/avatars"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="top"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/recipients"
style="@style/TextPrimary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@id/avatars"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
app:textSize="secondary"
tools:text="@tools:sample/full_names" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/date"
style="@style/TextSecondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
app:layout_constraintStart_toStartOf="@id/recipients"
app:layout_constraintTop_toBottomOf="@id/recipients"
tools:text="Oct 11" />
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/body"
style="@style/TextPrimary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/recipients"
app:layout_constraintTop_toBottomOf="@id/date"
tools:text="@tools:sample/lorem/random" />
</androidx.constraintlayout.widget.ConstraintLayout>