adding a DateTime thread view type

This commit is contained in:
tibbi 2020-04-03 23:07:34 +02:00
parent 9a47de4a30
commit 59c5d16f62
7 changed files with 52 additions and 25 deletions

View File

@ -6,6 +6,7 @@ import com.simplemobiletools.smsmessenger.adapters.ThreadAdapter
import com.simplemobiletools.smsmessenger.extensions.getMessages import com.simplemobiletools.smsmessenger.extensions.getMessages
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME
import com.simplemobiletools.smsmessenger.models.ThreadItem
import kotlinx.android.synthetic.main.activity_thread.* import kotlinx.android.synthetic.main.activity_thread.*
class ThreadActivity : SimpleActivity() { class ThreadActivity : SimpleActivity() {
@ -18,8 +19,9 @@ class ThreadActivity : SimpleActivity() {
val threadID = intent.getIntExtra(THREAD_ID, 0) val threadID = intent.getIntExtra(THREAD_ID, 0)
val messages = getMessages(threadID) val messages = getMessages(threadID)
messages.sortBy { it.id } messages.sortBy { it.id }
val items = messages.toMutableList() as ArrayList<ThreadItem>
ThreadAdapter(this, messages, thread_messages_list, thread_messages_fastscroller) { ThreadAdapter(this, items, thread_messages_list, thread_messages_fastscroller) {
}.apply { }.apply {
thread_messages_list.adapter = this thread_messages_list.adapter = this

View File

@ -11,13 +11,16 @@ import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.smsmessenger.R import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.SimpleActivity import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.helpers.RECEIVED_MESSAGE import com.simplemobiletools.smsmessenger.helpers.THREAD_DATE_TIME
import com.simplemobiletools.smsmessenger.helpers.SENT_MESSAGE import com.simplemobiletools.smsmessenger.helpers.THREAD_RECEIVED_MESSAGE
import com.simplemobiletools.smsmessenger.helpers.THREAD_SENT_MESSAGE
import com.simplemobiletools.smsmessenger.models.Message import com.simplemobiletools.smsmessenger.models.Message
import com.simplemobiletools.smsmessenger.models.ThreadDateTime
import com.simplemobiletools.smsmessenger.models.ThreadItem
import kotlinx.android.synthetic.main.item_received_message.view.* import kotlinx.android.synthetic.main.item_received_message.view.*
class ThreadAdapter( class ThreadAdapter(
activity: SimpleActivity, var messages: ArrayList<Message>, activity: SimpleActivity, var messages: ArrayList<ThreadItem>,
recyclerView: MyRecyclerView, recyclerView: MyRecyclerView,
fastScroller: FastScroller, fastScroller: FastScroller,
itemClick: (Any) -> Unit itemClick: (Any) -> Unit
@ -43,29 +46,33 @@ class ThreadAdapter(
override fun getSelectableItemCount() = messages.size override fun getSelectableItemCount() = messages.size
override fun getIsItemSelectable(position: Int) = true override fun getIsItemSelectable(position: Int) = !isThreadDateTime(position)
override fun getItemSelectionKey(position: Int) = messages.getOrNull(position)?.id override fun getItemSelectionKey(position: Int) = (messages.getOrNull(position) as? Message)?.id
override fun getItemKeyPosition(key: Int) = messages.indexOfFirst { it.id == key } override fun getItemKeyPosition(key: Int) = messages.indexOfFirst { (it as? Message)?.id == key }
override fun onActionModeCreated() {} override fun onActionModeCreated() {}
override fun onActionModeDestroyed() {} override fun onActionModeDestroyed() {}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layout = if (viewType == RECEIVED_MESSAGE) { val layout = when (viewType) {
R.layout.item_received_message THREAD_DATE_TIME -> R.layout.item_thread_date_time
} else { THREAD_RECEIVED_MESSAGE -> R.layout.item_received_message
R.layout.item_sent_message else -> R.layout.item_sent_message
} }
return createViewHolder(layout, parent) return createViewHolder(layout, parent)
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val message = messages[position] val item = messages[position]
holder.bindView(message, true, true) { itemView, layoutPosition -> holder.bindView(item, true, true) { itemView, layoutPosition ->
setupView(itemView, message) if (item is ThreadDateTime) {
setupDateTime(itemView, item)
} else {
setupView(itemView, item as Message)
}
} }
bindViewHolder(holder) bindViewHolder(holder)
} }
@ -73,16 +80,15 @@ class ThreadAdapter(
override fun getItemCount() = messages.size override fun getItemCount() = messages.size
override fun getItemViewType(position: Int): Int { override fun getItemViewType(position: Int): Int {
return if (messages[position].isReceivedMessage()) { val item = messages[position]
RECEIVED_MESSAGE return when {
} else { item is ThreadDateTime -> THREAD_DATE_TIME
SENT_MESSAGE (messages[position] as? Message)?.isReceivedMessage() == true -> THREAD_RECEIVED_MESSAGE
else -> THREAD_SENT_MESSAGE
} }
} }
private fun getItemWithKey(key: Int): Message? = messages.firstOrNull { it.id == key } private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime
private fun getSelectedItems() = messages.filter { selectedKeys.contains(it.id) } as ArrayList<Message>
private fun setupView(view: View, message: Message) { private fun setupView(view: View, message: Message) {
view.apply { view.apply {
@ -96,4 +102,8 @@ class ThreadAdapter(
} }
} }
} }
private fun setupDateTime(view: View, dateTime: ThreadDateTime) {
}
} }

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.smsmessenger.helpers
const val THREAD_ID = "thread_id" const val THREAD_ID = "thread_id"
const val THREAD_NAME = "thread_name" const val THREAD_NAME = "thread_name"
// message types for the thread list view // view types for the thread list view
const val RECEIVED_MESSAGE = 1 const val THREAD_DATE_TIME = 1
const val SENT_MESSAGE = 2 const val THREAD_RECEIVED_MESSAGE = 2
const val THREAD_SENT_MESSAGE = 3

View File

@ -5,6 +5,6 @@ import android.provider.Telephony
data class Message( data class Message(
val id: Int, val subject: String, val body: String, val type: Int, val address: String, val date: Int, val read: Boolean, val id: Int, val subject: String, val body: String, val type: Int, val address: String, val date: Int, val read: Boolean,
val thread: Int val thread: Int
) { ) : ThreadItem() {
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX
} }

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.smsmessenger.models
open class ThreadDateTime(val date: Int)

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.smsmessenger.models
open class ThreadItem

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/thread_date_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/normal_text_size"
tools:text="13 March, 13:30" />