session-android/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt

72 lines
3.1 KiB
Kotlin
Raw Normal View History

2021-06-15 01:42:18 +02:00
package org.thoughtcrime.securesms.conversation.v2.input_bar
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
2021-06-15 06:05:32 +02:00
import android.widget.RelativeLayout
2021-06-16 02:39:24 +02:00
import androidx.core.view.isVisible
2021-06-15 05:01:05 +02:00
import kotlinx.android.synthetic.main.view_input_bar.view.*
2021-06-15 01:42:18 +02:00
import network.loki.messenger.R
2021-06-15 08:14:35 +02:00
import org.thoughtcrime.securesms.loki.utilities.toDp
2021-06-16 02:39:24 +02:00
import org.thoughtcrime.securesms.loki.utilities.toPx
import kotlin.math.max
2021-06-15 01:42:18 +02:00
2021-06-16 06:50:41 +02:00
class InputBar : RelativeLayout, InputBarEditTextDelegate {
2021-06-16 01:51:50 +02:00
var delegate: InputBarDelegate? = null
2021-06-15 01:42:18 +02:00
2021-06-15 06:05:32 +02:00
private val attachmentsButton by lazy { InputBarButton(context, R.drawable.ic_plus_24) }
private val microphoneButton by lazy { InputBarButton(context, R.drawable.ic_microphone) }
2021-06-16 02:39:24 +02:00
private val sendButton by lazy { InputBarButton(context, R.drawable.ic_arrow_up, true) }
2021-06-15 06:05:32 +02:00
2021-06-15 01:42:18 +02:00
// region Lifecycle
2021-06-16 06:50:41 +02:00
constructor(context: Context) : super(context) { initialize() }
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { initialize() }
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { initialize() }
2021-06-15 01:42:18 +02:00
2021-06-16 06:50:41 +02:00
private fun initialize() {
2021-06-15 01:42:18 +02:00
LayoutInflater.from(context).inflate(R.layout.view_input_bar, this)
2021-06-16 06:50:41 +02:00
// Attachments button
2021-06-15 06:05:32 +02:00
attachmentsButtonContainer.addView(attachmentsButton)
attachmentsButton.layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
2021-06-16 06:50:41 +02:00
// Microphone button
2021-06-16 02:39:24 +02:00
microphoneOrSendButtonContainer.addView(microphoneButton)
2021-06-15 06:05:32 +02:00
microphoneButton.layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
2021-06-16 06:50:41 +02:00
microphoneButton.onLongPress = {
showVoiceMessageUI()
}
// Send button
2021-06-16 02:39:24 +02:00
microphoneOrSendButtonContainer.addView(sendButton)
sendButton.layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
sendButton.isVisible = false
2021-06-16 06:50:41 +02:00
// Edit text
2021-06-15 08:14:35 +02:00
inputBarEditText.imeOptions = inputBarEditText.imeOptions or 16777216 // Always use incognito keyboard
inputBarEditText.delegate = this
}
// endregion
2021-06-16 02:39:24 +02:00
// region Updating
override fun inputBarEditTextContentChanged(text: CharSequence) {
sendButton.isVisible = text.isNotEmpty()
microphoneButton.isVisible = text.isEmpty()
}
2021-06-15 08:14:35 +02:00
override fun inputBarEditTextHeightChanged(newValue: Int) {
2021-06-16 01:51:50 +02:00
val vMargin = toDp(4, resources)
val layoutParams = inputBarLinearLayout.layoutParams as LayoutParams
2021-06-16 02:39:24 +02:00
val newHeight = max(newValue + 2 * vMargin, toPx(56, resources))
2021-06-16 01:51:50 +02:00
layoutParams.height = newHeight
inputBarLinearLayout.layoutParams = layoutParams
delegate?.inputBarHeightChanged(newHeight)
2021-06-15 01:42:18 +02:00
}
2021-06-16 06:50:41 +02:00
private fun showVoiceMessageUI() {
delegate?.showVoiceMessageUI()
}
2021-06-15 01:42:18 +02:00
// endregion
2021-06-16 01:51:50 +02:00
}
interface InputBarDelegate {
fun inputBarHeightChanged(newValue: Int)
2021-06-16 06:50:41 +02:00
fun showVoiceMessageUI()
2021-06-15 01:42:18 +02:00
}