adding a DateTime thread view type
This commit is contained in:
parent
9a47de4a30
commit
59c5d16f62
|
@ -6,6 +6,7 @@ import com.simplemobiletools.smsmessenger.adapters.ThreadAdapter
|
|||
import com.simplemobiletools.smsmessenger.extensions.getMessages
|
||||
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
|
||||
import com.simplemobiletools.smsmessenger.helpers.THREAD_NAME
|
||||
import com.simplemobiletools.smsmessenger.models.ThreadItem
|
||||
import kotlinx.android.synthetic.main.activity_thread.*
|
||||
|
||||
class ThreadActivity : SimpleActivity() {
|
||||
|
@ -18,8 +19,9 @@ class ThreadActivity : SimpleActivity() {
|
|||
val threadID = intent.getIntExtra(THREAD_ID, 0)
|
||||
val messages = getMessages(threadID)
|
||||
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 {
|
||||
thread_messages_list.adapter = this
|
||||
|
|
|
@ -11,13 +11,16 @@ import com.simplemobiletools.commons.views.FastScroller
|
|||
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||
import com.simplemobiletools.smsmessenger.R
|
||||
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
||||
import com.simplemobiletools.smsmessenger.helpers.RECEIVED_MESSAGE
|
||||
import com.simplemobiletools.smsmessenger.helpers.SENT_MESSAGE
|
||||
import com.simplemobiletools.smsmessenger.helpers.THREAD_DATE_TIME
|
||||
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.ThreadDateTime
|
||||
import com.simplemobiletools.smsmessenger.models.ThreadItem
|
||||
import kotlinx.android.synthetic.main.item_received_message.view.*
|
||||
|
||||
class ThreadAdapter(
|
||||
activity: SimpleActivity, var messages: ArrayList<Message>,
|
||||
activity: SimpleActivity, var messages: ArrayList<ThreadItem>,
|
||||
recyclerView: MyRecyclerView,
|
||||
fastScroller: FastScroller,
|
||||
itemClick: (Any) -> Unit
|
||||
|
@ -43,29 +46,33 @@ class ThreadAdapter(
|
|||
|
||||
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 onActionModeDestroyed() {}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val layout = if (viewType == RECEIVED_MESSAGE) {
|
||||
R.layout.item_received_message
|
||||
} else {
|
||||
R.layout.item_sent_message
|
||||
val layout = when (viewType) {
|
||||
THREAD_DATE_TIME -> R.layout.item_thread_date_time
|
||||
THREAD_RECEIVED_MESSAGE -> R.layout.item_received_message
|
||||
else -> R.layout.item_sent_message
|
||||
}
|
||||
return createViewHolder(layout, parent)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val message = messages[position]
|
||||
holder.bindView(message, true, true) { itemView, layoutPosition ->
|
||||
setupView(itemView, message)
|
||||
val item = messages[position]
|
||||
holder.bindView(item, true, true) { itemView, layoutPosition ->
|
||||
if (item is ThreadDateTime) {
|
||||
setupDateTime(itemView, item)
|
||||
} else {
|
||||
setupView(itemView, item as Message)
|
||||
}
|
||||
}
|
||||
bindViewHolder(holder)
|
||||
}
|
||||
|
@ -73,16 +80,15 @@ class ThreadAdapter(
|
|||
override fun getItemCount() = messages.size
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return if (messages[position].isReceivedMessage()) {
|
||||
RECEIVED_MESSAGE
|
||||
} else {
|
||||
SENT_MESSAGE
|
||||
val item = messages[position]
|
||||
return when {
|
||||
item is ThreadDateTime -> THREAD_DATE_TIME
|
||||
(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 getSelectedItems() = messages.filter { selectedKeys.contains(it.id) } as ArrayList<Message>
|
||||
private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime
|
||||
|
||||
private fun setupView(view: View, message: Message) {
|
||||
view.apply {
|
||||
|
@ -96,4 +102,8 @@ class ThreadAdapter(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupDateTime(view: View, dateTime: ThreadDateTime) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.simplemobiletools.smsmessenger.helpers
|
|||
const val THREAD_ID = "thread_id"
|
||||
const val THREAD_NAME = "thread_name"
|
||||
|
||||
// message types for the thread list view
|
||||
const val RECEIVED_MESSAGE = 1
|
||||
const val SENT_MESSAGE = 2
|
||||
// view types for the thread list view
|
||||
const val THREAD_DATE_TIME = 1
|
||||
const val THREAD_RECEIVED_MESSAGE = 2
|
||||
const val THREAD_SENT_MESSAGE = 3
|
||||
|
|
|
@ -5,6 +5,6 @@ import android.provider.Telephony
|
|||
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 thread: Int
|
||||
) {
|
||||
) : ThreadItem() {
|
||||
fun isReceivedMessage() = type == Telephony.Sms.MESSAGE_TYPE_INBOX
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package com.simplemobiletools.smsmessenger.models
|
||||
|
||||
open class ThreadDateTime(val date: Int)
|
|
@ -0,0 +1,3 @@
|
|||
package com.simplemobiletools.smsmessenger.models
|
||||
|
||||
open class ThreadItem
|
|
@ -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" />
|
Loading…
Reference in New Issue