2019-06-21 12:21:48 +10:00
|
|
|
package org.thoughtcrime.securesms.loki
|
|
|
|
|
|
|
|
import android.content.Context
|
2019-06-21 14:18:38 +10:00
|
|
|
import android.os.Build
|
2019-06-21 12:21:48 +10:00
|
|
|
import android.util.AttributeSet
|
2019-08-07 12:00:12 +10:00
|
|
|
import android.view.Gravity
|
2019-06-21 12:21:48 +10:00
|
|
|
import android.view.View
|
2019-06-21 14:18:38 +10:00
|
|
|
import android.widget.Button
|
2019-06-21 12:21:48 +10:00
|
|
|
import android.widget.LinearLayout
|
2019-08-07 12:00:12 +10:00
|
|
|
import android.widget.ProgressBar
|
2019-06-21 12:21:48 +10:00
|
|
|
import android.widget.TextView
|
2019-08-07 12:00:12 +10:00
|
|
|
import network.loki.messenger.R
|
2019-06-24 11:22:53 +10:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2019-08-07 12:13:14 +10:00
|
|
|
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord
|
2019-06-27 16:03:05 +10:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord
|
2019-07-24 15:05:39 +10:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord
|
2019-06-24 14:10:09 +10:00
|
|
|
import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus
|
2019-06-21 12:21:48 +10:00
|
|
|
|
|
|
|
class FriendRequestView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
|
2019-06-24 11:22:53 +10:00
|
|
|
private var isUISetUp = false
|
2019-06-27 16:03:05 +10:00
|
|
|
private var message: MessageRecord? = null
|
2019-06-21 14:18:38 +10:00
|
|
|
var delegate: FriendRequestViewDelegate? = null
|
|
|
|
|
2019-06-21 12:21:48 +10:00
|
|
|
// region Components
|
2019-06-21 14:18:38 +10:00
|
|
|
private val topSpacer by lazy {
|
|
|
|
val result = View(context)
|
2019-06-24 11:22:53 +10:00
|
|
|
result.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, toPx(12, resources))
|
2019-06-21 14:18:38 +10:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-06-21 12:21:48 +10:00
|
|
|
private val label by lazy {
|
|
|
|
val result = TextView(context)
|
2019-07-22 13:08:56 +10:00
|
|
|
result.setTextColor(resources.getColorWithID(R.color.white, context.theme))
|
2019-06-21 12:21:48 +10:00
|
|
|
result.textAlignment = TextView.TEXT_ALIGNMENT_CENTER
|
|
|
|
result
|
|
|
|
}
|
2019-06-21 14:18:38 +10:00
|
|
|
|
|
|
|
private val buttonLinearLayout by lazy {
|
|
|
|
val result = LinearLayout(context)
|
|
|
|
result.orientation = HORIZONTAL
|
|
|
|
result
|
|
|
|
}
|
2019-08-07 12:00:12 +10:00
|
|
|
|
|
|
|
private val loaderContainer by lazy {
|
|
|
|
val result = LinearLayout(context)
|
|
|
|
val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, toPx(50, resources))
|
|
|
|
result.layoutParams = layoutParams
|
|
|
|
result.gravity = Gravity.CENTER
|
|
|
|
result
|
|
|
|
}
|
2019-06-21 12:21:48 +10:00
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Initialization
|
|
|
|
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
|
|
|
|
constructor(context: Context) : this(context, null)
|
2019-06-24 11:22:53 +10:00
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Updating
|
2019-06-27 16:03:05 +10:00
|
|
|
fun update(message: MessageRecord) {
|
2019-06-25 09:28:37 +10:00
|
|
|
this.message = message
|
2019-06-24 11:22:53 +10:00
|
|
|
setUpUIIfNeeded()
|
|
|
|
updateUI()
|
|
|
|
}
|
2019-06-21 12:21:48 +10:00
|
|
|
|
2019-06-24 11:22:53 +10:00
|
|
|
private fun setUpUIIfNeeded() {
|
|
|
|
if (isUISetUp) { return }
|
|
|
|
isUISetUp = true
|
2019-06-21 12:21:48 +10:00
|
|
|
orientation = VERTICAL
|
|
|
|
addView(topSpacer)
|
|
|
|
addView(label)
|
2019-06-27 16:03:05 +10:00
|
|
|
if (!message!!.isOutgoing) {
|
2019-08-07 12:00:12 +10:00
|
|
|
val loader = ProgressBar(context)
|
|
|
|
loader.isIndeterminate = true
|
|
|
|
val color = resources.getColorWithID(R.color.white, context.theme)
|
|
|
|
loader.indeterminateDrawable.setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN)
|
|
|
|
val loaderLayoutParams = LayoutParams(LayoutParams.MATCH_PARENT, toPx(24, resources))
|
|
|
|
loader.layoutParams = loaderLayoutParams
|
|
|
|
loaderContainer.addView(loader)
|
|
|
|
addView(loaderContainer)
|
2019-06-24 11:22:53 +10:00
|
|
|
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
|
|
|
|
}
|
2019-08-07 12:00:12 +10:00
|
|
|
val buttonLayoutParams = LayoutParams(0, toPx(50, resources))
|
|
|
|
buttonLayoutParams.weight = 1f
|
|
|
|
result.layoutParams = buttonLayoutParams
|
2019-06-24 11:22:53 +10:00
|
|
|
return result
|
2019-06-21 14:18:38 +10:00
|
|
|
}
|
2019-06-24 11:22:53 +10: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 14:18:38 +10:00
|
|
|
acceptButton.setOnClickListener { accept() }
|
|
|
|
buttonLinearLayout.addView(acceptButton)
|
2019-06-24 11:22:53 +10:00
|
|
|
val rejectButton = button()
|
|
|
|
rejectButton.text = resources.getString(R.string.view_friend_request_reject_button_title)
|
2019-06-21 14:18:38 +10:00
|
|
|
rejectButton.setTextColor(resources.getColorWithID(R.color.red, context.theme))
|
|
|
|
rejectButton.setOnClickListener { reject() }
|
|
|
|
buttonLinearLayout.addView(rejectButton)
|
2019-06-24 11:22:53 +10:00
|
|
|
buttonLinearLayout.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, toPx(50, resources))
|
2019-06-21 14:18:38 +10:00
|
|
|
addView(buttonLinearLayout)
|
2019-06-24 11:22:53 +10:00
|
|
|
}
|
2019-06-21 12:21:48 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateUI() {
|
2019-08-07 12:13:14 +10:00
|
|
|
val message = message
|
2019-08-09 12:00:01 +10:00
|
|
|
val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context)
|
2019-06-27 16:03:05 +10:00
|
|
|
val contactID = DatabaseFactory.getThreadDatabase(context).getRecipientForThreadId(message!!.threadId)!!.address.toString()
|
2019-08-09 12:00:01 +10:00
|
|
|
val contactDisplayName = DatabaseFactory.getLokiUserDatabase(context).getDisplayName(contactID) ?: contactID
|
2019-08-07 12:13:14 +10:00
|
|
|
if (message is MediaMmsMessageRecord && message.quote != null) { visibility = View.GONE; return }
|
2019-07-24 15:05:39 +10:00
|
|
|
val isTextMessage = message is SmsMessageRecord
|
|
|
|
if (!isTextMessage) return
|
2019-08-09 12:00:01 +10:00
|
|
|
val friendRequestStatus = lokiMessageDatabase.getFriendRequestStatus(message.id)
|
2019-08-07 12:13:14 +10:00
|
|
|
if (!message.isOutgoing) {
|
2019-06-28 12:32:23 +10:00
|
|
|
visibility = if (friendRequestStatus == LokiMessageFriendRequestStatus.NONE) View.GONE else View.VISIBLE
|
2019-06-24 11:22:53 +10:00
|
|
|
buttonLinearLayout.visibility = if (friendRequestStatus != LokiMessageFriendRequestStatus.REQUEST_PENDING) View.GONE else View.VISIBLE
|
2019-08-07 12:00:12 +10:00
|
|
|
loaderContainer.visibility = if (friendRequestStatus == LokiMessageFriendRequestStatus.REQUEST_SENDING) View.VISIBLE else View.GONE
|
2019-06-24 11:22:53 +10:00
|
|
|
val formatID = when (friendRequestStatus) {
|
2019-08-07 12:00:12 +10:00
|
|
|
LokiMessageFriendRequestStatus.NONE, LokiMessageFriendRequestStatus.REQUEST_SENDING, LokiMessageFriendRequestStatus.REQUEST_FAILED -> return
|
2019-06-24 11:22:53 +10:00
|
|
|
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 15:23:45 +10:00
|
|
|
label.text = resources.getString(formatID, contactDisplayName)
|
2019-06-24 11:22:53 +10:00
|
|
|
} else {
|
2019-06-28 12:32:23 +10:00
|
|
|
visibility = if (friendRequestStatus == LokiMessageFriendRequestStatus.NONE) View.GONE else View.VISIBLE
|
2019-06-24 11:22:53 +10:00
|
|
|
buttonLinearLayout.visibility = View.GONE
|
2019-08-07 12:00:12 +10:00
|
|
|
loaderContainer.visibility = View.GONE
|
2019-06-24 11:22:53 +10:00
|
|
|
val formatID = when (friendRequestStatus) {
|
2019-06-28 12:32:23 +10:00
|
|
|
LokiMessageFriendRequestStatus.NONE -> return
|
2019-08-07 12:00:12 +10:00
|
|
|
LokiMessageFriendRequestStatus.REQUEST_SENDING, LokiMessageFriendRequestStatus.REQUEST_FAILED -> null
|
2019-06-28 12:32:23 +10:00
|
|
|
LokiMessageFriendRequestStatus.REQUEST_PENDING, LokiMessageFriendRequestStatus.REQUEST_REJECTED -> R.string.view_friend_request_outgoing_pending_message
|
2019-06-24 11:22:53 +10:00
|
|
|
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 14:18:38 +10:00
|
|
|
}
|
2019-06-24 11:22:53 +10:00
|
|
|
if (formatID != null) {
|
2019-07-19 15:23:45 +10:00
|
|
|
label.text = resources.getString(formatID, contactDisplayName)
|
2019-06-21 14:18:38 +10:00
|
|
|
}
|
2019-06-24 11:22:53 +10:00
|
|
|
label.visibility = if (formatID != null) View.VISIBLE else View.GONE
|
|
|
|
topSpacer.visibility = label.visibility
|
2019-06-21 14:18:38 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Interaction
|
|
|
|
private fun accept() {
|
2019-08-09 12:00:01 +10:00
|
|
|
val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context)
|
|
|
|
lokiMessageDatabase.setFriendRequestStatus(message!!.id, LokiMessageFriendRequestStatus.REQUEST_SENDING)
|
2019-08-07 12:00:12 +10:00
|
|
|
updateUI()
|
2019-06-27 16:03:05 +10:00
|
|
|
delegate?.acceptFriendRequest(message!!)
|
2019-06-21 14:18:38 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun reject() {
|
2019-08-09 12:00:01 +10:00
|
|
|
val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context)
|
|
|
|
lokiMessageDatabase.setFriendRequestStatus(message!!.id, LokiMessageFriendRequestStatus.REQUEST_REJECTED)
|
2019-08-07 12:00:12 +10:00
|
|
|
updateUI()
|
2019-06-27 16:03:05 +10:00
|
|
|
delegate?.rejectFriendRequest(message!!)
|
2019-06-21 12:21:48 +10:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
}
|