session-android/src/org/thoughtcrime/securesms/loki/FriendRequestView.kt

136 lines
6.5 KiB
Kotlin
Raw Normal View History

2019-06-21 02:21:48 +00:00
package org.thoughtcrime.securesms.loki
import android.content.Context
2019-06-21 04:18:38 +00:00
import android.os.Build
2019-06-21 02:21:48 +00:00
import android.util.AttributeSet
import android.view.View
2019-06-21 04:18:38 +00:00
import android.widget.Button
2019-06-21 02:21:48 +00:00
import android.widget.LinearLayout
import android.widget.TextView
2019-06-21 04:18:38 +00:00
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.database.DatabaseFactory
2019-06-27 06:03:05 +00:00
import org.thoughtcrime.securesms.database.model.MessageRecord
2019-06-24 04:10:09 +00:00
import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus
2019-06-21 02:21:48 +00:00
class FriendRequestView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
private var isUISetUp = false
2019-06-27 06:03:05 +00:00
private var message: MessageRecord? = null
2019-06-21 04:18:38 +00:00
var delegate: FriendRequestViewDelegate? = null
2019-06-21 02:21:48 +00:00
// region Components
2019-06-21 04:18:38 +00:00
private val topSpacer by lazy {
val result = View(context)
result.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, toPx(12, resources))
2019-06-21 04:18:38 +00:00
result
}
2019-06-21 02:21:48 +00:00
private val label by lazy {
val result = TextView(context)
2019-07-22 03:08:56 +00:00
result.setTextColor(resources.getColorWithID(R.color.white, context.theme))
2019-06-21 02:21:48 +00:00
result.textAlignment = TextView.TEXT_ALIGNMENT_CENTER
result
}
2019-06-21 04:18:38 +00:00
private val buttonLinearLayout by lazy {
val result = LinearLayout(context)
result.orientation = HORIZONTAL
result
}
2019-06-21 02:21:48 +00:00
// endregion
// region Initialization
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context) : this(context, null)
// endregion
// region Updating
2019-06-27 06:03:05 +00:00
fun update(message: MessageRecord) {
2019-06-24 23:28:37 +00:00
this.message = message
setUpUIIfNeeded()
updateUI()
}
2019-06-21 02:21:48 +00:00
private fun setUpUIIfNeeded() {
if (isUISetUp) { return }
isUISetUp = true
2019-06-21 02:21:48 +00:00
orientation = VERTICAL
addView(topSpacer)
addView(label)
2019-06-27 06:03:05 +00:00
if (!message!!.isOutgoing) {
fun button(): Button {
val result = Button(context)
result.setBackgroundColor(resources.getColorWithID(R.color.transparent, context.theme))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
result.elevation = 0f
result.stateListAnimator = null
}
val layoutParams = LayoutParams(0, toPx(50, resources))
layoutParams.weight = 1f
result.layoutParams = layoutParams
return result
2019-06-21 04:18:38 +00:00
}
val acceptButton = button()
acceptButton.text = resources.getString(R.string.view_friend_request_accept_button_title)
acceptButton.setTextColor(resources.getColorWithID(R.color.signal_primary, context.theme))
2019-06-21 04:18:38 +00:00
acceptButton.setOnClickListener { accept() }
buttonLinearLayout.addView(acceptButton)
val rejectButton = button()
rejectButton.text = resources.getString(R.string.view_friend_request_reject_button_title)
2019-06-21 04:18:38 +00:00
rejectButton.setTextColor(resources.getColorWithID(R.color.red, context.theme))
rejectButton.setOnClickListener { reject() }
buttonLinearLayout.addView(rejectButton)
buttonLinearLayout.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, toPx(50, resources))
2019-06-21 04:18:38 +00:00
addView(buttonLinearLayout)
}
2019-06-21 02:21:48 +00:00
}
private fun updateUI() {
val database = DatabaseFactory.getLokiMessageFriendRequestDatabase(context)
2019-06-27 06:03:05 +00:00
val contactID = DatabaseFactory.getThreadDatabase(context).getRecipientForThreadId(message!!.threadId)!!.address.toString()
2019-07-19 05:23:45 +00:00
val contactDisplayName = DatabaseFactory.getLokiUserDisplayNameDatabase(context).getDisplayName(contactID) ?: contactID
2019-06-27 06:03:05 +00:00
if (!message!!.isOutgoing) {
val friendRequestStatus = database.getFriendRequestStatus(message!!.id)
2019-06-28 02:32:23 +00:00
visibility = if (friendRequestStatus == LokiMessageFriendRequestStatus.NONE) View.GONE else View.VISIBLE
buttonLinearLayout.visibility = if (friendRequestStatus != LokiMessageFriendRequestStatus.REQUEST_PENDING) View.GONE else View.VISIBLE
val formatID = when (friendRequestStatus) {
2019-06-28 02:32:23 +00:00
LokiMessageFriendRequestStatus.NONE, LokiMessageFriendRequestStatus.REQUEST_SENDING_OR_FAILED -> return
LokiMessageFriendRequestStatus.REQUEST_PENDING -> R.string.view_friend_request_incoming_pending_message
LokiMessageFriendRequestStatus.REQUEST_ACCEPTED -> R.string.view_friend_request_incoming_accepted_message
LokiMessageFriendRequestStatus.REQUEST_REJECTED -> R.string.view_friend_request_incoming_declined_message
LokiMessageFriendRequestStatus.REQUEST_EXPIRED -> R.string.view_friend_request_incoming_expired_message
}
2019-07-19 05:23:45 +00:00
label.text = resources.getString(formatID, contactDisplayName)
} else {
2019-06-27 06:03:05 +00:00
val friendRequestStatus = database.getFriendRequestStatus(message!!.id)
2019-06-28 02:32:23 +00:00
visibility = if (friendRequestStatus == LokiMessageFriendRequestStatus.NONE) View.GONE else View.VISIBLE
buttonLinearLayout.visibility = View.GONE
val formatID = when (friendRequestStatus) {
2019-06-28 02:32:23 +00:00
LokiMessageFriendRequestStatus.NONE -> return
LokiMessageFriendRequestStatus.REQUEST_SENDING_OR_FAILED -> null
2019-06-28 02:32:23 +00:00
LokiMessageFriendRequestStatus.REQUEST_PENDING, LokiMessageFriendRequestStatus.REQUEST_REJECTED -> R.string.view_friend_request_outgoing_pending_message
LokiMessageFriendRequestStatus.REQUEST_ACCEPTED -> R.string.view_friend_request_outgoing_accepted_message
LokiMessageFriendRequestStatus.REQUEST_EXPIRED -> R.string.view_friend_request_outgoing_expired_message
2019-06-21 04:18:38 +00:00
}
if (formatID != null) {
2019-07-19 05:23:45 +00:00
label.text = resources.getString(formatID, contactDisplayName)
2019-06-21 04:18:38 +00:00
}
label.visibility = if (formatID != null) View.VISIBLE else View.GONE
topSpacer.visibility = label.visibility
2019-06-21 04:18:38 +00:00
}
}
// endregion
// region Interaction
private fun accept() {
val database = DatabaseFactory.getLokiMessageFriendRequestDatabase(context)
2019-06-27 06:03:05 +00:00
database.setFriendRequestStatus(message!!.id, LokiMessageFriendRequestStatus.REQUEST_ACCEPTED)
delegate?.acceptFriendRequest(message!!)
2019-06-21 04:18:38 +00:00
}
private fun reject() {
val database = DatabaseFactory.getLokiMessageFriendRequestDatabase(context)
2019-06-27 06:03:05 +00:00
database.setFriendRequestStatus(message!!.id, LokiMessageFriendRequestStatus.REQUEST_REJECTED)
delegate?.rejectFriendRequest(message!!)
2019-06-21 02:21:48 +00:00
}
// endregion
}