Skip to content
Snippets Groups Projects
Commit 931fa0fb authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Implement recently sent messages history (UI only)

parent 39b7af5d
No related branches found
No related tags found
No related merge requests found
......@@ -77,6 +77,9 @@ class ChatActivity : ServiceBoundActivity(), SharedPreferences.OnSharedPreferenc
@BindView(R.id.autocomplete_list2)
lateinit var autocompleteList2: RecyclerView
@BindView(R.id.msg_history)
lateinit var msgHistory: RecyclerView
private lateinit var drawerToggle: ActionBarDrawerToggle
private val handler = AndroidHandlerThread("Chat")
......@@ -179,6 +182,20 @@ class ChatActivity : ServiceBoundActivity(), SharedPreferences.OnSharedPreferenc
}
}
msgHistory.itemAnimator = DefaultItemAnimator()
msgHistory.layoutManager = LinearLayoutManager(this)
msgHistory.adapter = MessageHistoryAdapter(
this,
viewModel.recentlySentMessages,
handler::post,
::runOnUiThread,
{ text ->
chatline.setText(text)
chatline.setSelection(chatline.length())
historyPanel.panelState = SlidingUpPanelLayout.PanelState.COLLAPSED
}
)
database = QuasselDatabase.Creator.init(application)
setSupportActionBar(toolbar)
......@@ -336,6 +353,7 @@ class ChatActivity : ServiceBoundActivity(), SharedPreferences.OnSharedPreferenc
session.bufferSyncer?.bufferInfo(bufferId)?.also { bufferInfo ->
val output = mutableListOf<IAliasManager.Command>()
for (line in text.lineSequence()) {
viewModel.addRecentlySentMessage(line)
session.aliasManager?.processInput(
bufferInfo,
inputEditor.formattedString,
......
package de.kuschku.quasseldroid_ng.ui.chat
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.Observer
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
import de.kuschku.quasseldroid_ng.R
class MessageHistoryAdapter(
lifecycleOwner: LifecycleOwner,
liveData: LiveData<List<CharSequence>?>,
runInBackground: (() -> Unit) -> Any,
runOnUiThread: (Runnable) -> Any,
private val clickListener: ((CharSequence) -> Unit)? = null
) : RecyclerView.Adapter<MessageHistoryAdapter.MessageViewHolder>() {
var data = mutableListOf<CharSequence>()
init {
liveData.observe(lifecycleOwner, Observer { it: List<CharSequence>? ->
runInBackground {
val list = it ?: emptyList()
val old: List<CharSequence> = data
val new: List<CharSequence> = list
val result = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
old[oldItemPosition] == new[newItemPosition]
override fun getOldListSize() = old.size
override fun getNewListSize() = new.size
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
old[oldItemPosition] == new[newItemPosition]
}, true)
runOnUiThread(Runnable {
data.clear()
data.addAll(new)
result.dispatchUpdatesTo(this@MessageHistoryAdapter)
})
}
})
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = MessageViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.widget_history_message, parent, false),
clickListener = clickListener
)
override fun onBindViewHolder(holder: MessageViewHolder, position: Int) =
holder.bind(data[position])
override fun getItemCount() = data.size
class MessageViewHolder(
itemView: View,
private val clickListener: ((CharSequence) -> Unit)? = null
) : RecyclerView.ViewHolder(itemView) {
@BindView(R.id.content)
lateinit var content: TextView
var value: CharSequence? = null
init {
ButterKnife.bind(this, itemView)
itemView.setOnClickListener {
val value = value
if (value != null)
clickListener?.invoke(value)
}
}
fun bind(data: CharSequence) {
value = data
content.text = data
}
}
}
\ No newline at end of file
......@@ -43,6 +43,16 @@ class QuasselViewModel : ViewModel() {
this.bufferViewConfig.value = bufferViewConfig
}
val MAX_RECENT_MESSAGES = 20
val recentlySentMessages = MutableLiveData<List<CharSequence>>()
fun addRecentlySentMessage(message: CharSequence) {
recentlySentMessages.value =
listOf(message) +
recentlySentMessages.value.orEmpty()
.filter { it == message }
.take(MAX_RECENT_MESSAGES - 1)
}
val backend = backendWrapper.switchMap { it }
val sessionManager = backend.map { it.sessionManager() }
val session = sessionManager.switchMapRx { it.session }
......@@ -490,5 +500,6 @@ class QuasselViewModel : ViewModel() {
showHidden.postValue(false)
selectedBufferId.postValue(-1)
collapsedNetworks.value = emptySet()
recentlySentMessages.value = emptyList()
}
}
<?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/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?attr/backgroundMenuItem"
android:fontFamily="sans-serif-medium"
android:gravity="center_vertical"
android:minHeight="48dp"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:singleLine="true"
android:textColor="?attr/colorTextPrimary"
android:textSize="13sp"
tools:text="Historical Message" />
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment