session-android/src/org/thoughtcrime/securesms/loki/MentionCandidateSelectionVi...

80 lines
3.3 KiB
Kotlin
Raw Normal View History

2019-10-10 04:53:02 +02:00
package org.thoughtcrime.securesms.loki
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
2019-10-10 05:44:08 +02:00
import org.thoughtcrime.securesms.database.DatabaseFactory
2019-10-11 07:37:28 +02:00
import org.whispersystems.signalservice.loki.messaging.Mention
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 }
2019-10-15 04:39:17 +02:00
var publicChatServer: String? = null
set(newValue) { field = newValue; mentionCandidateSelectionViewAdapter.publicChatServer = publicChatServer }
2019-10-15 04:39:17 +02:00
var publicChatChannel: Long? = null
set(newValue) { field = newValue; mentionCandidateSelectionViewAdapter.publicChatChannel = publicChatChannel }
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() }
2019-10-15 04:39:17 +02:00
var publicChatServer: String? = null
var publicChatChannel: Long? = 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 {
2019-10-11 07:37:28 +02:00
val cell = cellToBeReused as MentionCandidateSelectionViewCell? ?: MentionCandidateSelectionViewCell.inflate(LayoutInflater.from(context), parent)
val mentionCandidate = getItem(position)
cell.mentionCandidate = mentionCandidate
2019-10-15 04:39:17 +02:00
cell.publicChatServer = publicChatServer
cell.publicChatChannel = publicChatChannel
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 {
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) {
2019-10-15 04:39:17 +02:00
val publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
if (publicChat != null) {
publicChatServer = publicChat.server
publicChatChannel = publicChat.channel
}
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
2019-10-11 07:37:28 +02:00
layoutParams.height = toPx(6 + Math.min(mentionCandidates.count(), 4) * 52, 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
}