session-android/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/components/MentionCandidateSelectionVi...

87 lines
3.6 KiB
Kotlin
Raw Normal View History

2021-07-09 03:14:21 +02:00
package org.thoughtcrime.securesms.conversation.v2.components
2019-10-10 04:53:02 +02:00
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
2021-05-18 01:50:16 +02:00
import org.session.libsession.messaging.mentions.Mention
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.util.toPx
2019-10-10 04:53:02 +02:00
2019-10-15 04:51:18 +02:00
class MentionCandidateSelectionView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : ListView(context, attrs, defStyleAttr) {
2019-10-11 07:37:28 +02:00
private var mentionCandidates = listOf<Mention>()
set(newValue) { field = newValue; mentionCandidateSelectionViewAdapter.mentionCandidates = newValue }
2020-01-16 05:15:08 +01:00
var glide: GlideRequests? = null
set(newValue) { field = newValue; mentionCandidateSelectionViewAdapter.glide = newValue }
2021-05-21 07:02:34 +02:00
var openGroupServer: String? = null
set(newValue) { field = newValue; mentionCandidateSelectionViewAdapter.openGroupServer = openGroupServer }
var openGroupRoom: String? = null
set(newValue) { field = newValue; mentionCandidateSelectionViewAdapter.openGroupRoom = openGroupRoom }
2019-10-11 07:37:28 +02:00
var onMentionCandidateSelected: ((Mention) -> Unit)? = null
2019-10-10 04:53:02 +02:00
2019-10-11 07:37:28 +02:00
private val mentionCandidateSelectionViewAdapter by lazy { Adapter(context) }
2019-10-10 04:53:02 +02:00
private class Adapter(private val context: Context) : BaseAdapter() {
2019-10-11 07:37:28 +02:00
var mentionCandidates = listOf<Mention>()
2019-10-10 04:53:02 +02:00
set(newValue) { field = newValue; notifyDataSetChanged() }
2020-01-16 05:15:08 +01:00
var glide: GlideRequests? = null
2021-05-21 07:02:34 +02:00
var openGroupServer: String? = null
var openGroupRoom: String? = null
2019-10-10 04:53:02 +02:00
override fun getCount(): Int {
2019-10-11 07:37:28 +02:00
return mentionCandidates.count()
2019-10-10 04:53:02 +02:00
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
2019-10-11 07:37:28 +02:00
override fun getItem(position: Int): Mention {
return mentionCandidates[position]
2019-10-10 04:53:02 +02:00
}
override fun getView(position: Int, cellToBeReused: View?, parent: ViewGroup): View {
2020-01-16 05:15:08 +01:00
val cell = cellToBeReused as MentionCandidateView? ?: MentionCandidateView.inflate(LayoutInflater.from(context), parent)
2019-10-11 07:37:28 +02:00
val mentionCandidate = getItem(position)
2020-01-16 05:15:08 +01:00
cell.glide = glide
2019-10-11 07:37:28 +02:00
cell.mentionCandidate = mentionCandidate
2021-05-21 07:02:34 +02:00
cell.openGroupServer = openGroupServer
cell.openGroupRoom = openGroupRoom
2019-10-10 04:53:02 +02:00
return cell
}
}
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context) : this(context, null)
init {
2020-01-16 05:15:08 +01:00
clipToOutline = true
2019-10-11 07:37:28 +02:00
adapter = mentionCandidateSelectionViewAdapter
mentionCandidateSelectionViewAdapter.mentionCandidates = mentionCandidates
2019-10-10 06:30:34 +02:00
setOnItemClickListener { _, _, position, _ ->
2019-10-11 07:37:28 +02:00
onMentionCandidateSelected?.invoke(mentionCandidates[position])
2019-10-10 06:30:34 +02:00
}
2019-10-10 04:53:02 +02:00
}
2019-10-10 05:44:08 +02:00
2019-10-11 07:37:28 +02:00
fun show(mentionCandidates: List<Mention>, threadID: Long) {
val openGroup = DatabaseComponent.get(context).lokiThreadDatabase().getOpenGroupChat(threadID)
2021-05-21 07:02:34 +02:00
if (openGroup != null) {
openGroupServer = openGroup.server
openGroupRoom = openGroup.room
2019-10-15 04:39:17 +02:00
}
2019-10-11 07:37:28 +02:00
this.mentionCandidates = mentionCandidates
2019-10-10 05:44:08 +02:00
val layoutParams = this.layoutParams as ViewGroup.LayoutParams
2020-01-16 05:15:08 +01:00
layoutParams.height = toPx(Math.min(mentionCandidates.count(), 4) * 44, resources)
2019-10-10 05:44:08 +02:00
this.layoutParams = layoutParams
}
fun hide() {
val layoutParams = this.layoutParams as ViewGroup.LayoutParams
layoutParams.height = 0
this.layoutParams = layoutParams
}
2019-10-10 04:53:02 +02:00
}