Further build out friend request view

This commit is contained in:
Niels Andriesse 2019-06-21 14:18:38 +10:00
parent 7c501980fa
commit f6542b9834
3 changed files with 126 additions and 3 deletions

View File

@ -1,19 +1,50 @@
package org.thoughtcrime.securesms.loki
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.sms.IncomingTextMessage
import org.thoughtcrime.securesms.sms.OutgoingTextMessage
class FriendRequestView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
var message: Any? = null
set(newValue) {
field = newValue
kind = if (message is IncomingTextMessage) Kind.Incoming else Kind.Outgoing
}
var kind: Kind? = null
var delegate: FriendRequestViewDelegate? = null
// region Types
enum class Kind { Incoming, Outgoing }
// endregion
// region Components
private val topSpacer by lazy {
val result = View(context)
result.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, 12)
result
}
private val label by lazy {
val result = TextView(context)
result.setTextColor(resources.getColorWithID(R.color.core_grey_90, context.theme))
// TODO: Typeface
result.textAlignment = TextView.TEXT_ALIGNMENT_CENTER
result
}
private val buttonLinearLayout by lazy {
val result = LinearLayout(context)
result.orientation = HORIZONTAL
result.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, 50)
result
}
// endregion
// region Initialization
@ -22,17 +53,80 @@ class FriendRequestView(context: Context, attrs: AttributeSet?, defStyleAttr: In
init {
orientation = VERTICAL
val topSpacer = View(context)
topSpacer.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, 12)
addView(topSpacer)
addView(label)
// if (kind == Kind.Incoming) {
//
val buttonLayoutParams = LayoutParams(0, 50)
buttonLayoutParams.weight = 1f
// Accept button
val acceptButton = Button(context)
acceptButton.text = "Accept"
acceptButton.setTextColor(resources.getColorWithID(R.color.signal_primary, context.theme))
acceptButton.setBackgroundColor(resources.getColorWithID(R.color.transparent, context.theme))
// TODO: Typeface
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
acceptButton.elevation = 0f
acceptButton.stateListAnimator = null
}
acceptButton.setOnClickListener { accept() }
acceptButton.layoutParams = buttonLayoutParams
buttonLinearLayout.addView(acceptButton)
// Reject button
val rejectButton = Button(context)
rejectButton.text = "Reject"
rejectButton.setTextColor(resources.getColorWithID(R.color.red, context.theme))
rejectButton.setBackgroundColor(resources.getColorWithID(R.color.transparent, context.theme))
// TODO: Typeface
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
rejectButton.elevation = 0f
rejectButton.stateListAnimator = null
}
rejectButton.setOnClickListener { reject() }
rejectButton.layoutParams = buttonLayoutParams
buttonLinearLayout.addView(rejectButton)
//
addView(buttonLinearLayout)
// }
updateUI()
// TODO: Observe friend request status changes
}
// endregion
// region Updating
private fun updateUI() {
label.text = "You've sent a friend request"
when (kind) {
Kind.Incoming -> {
val message = this.message as IncomingTextMessage
// buttonLinearLayout.visibility = View.GONE // TODO: Base on friend request status
val text = { // TODO: Base on friend request status
"You've received a friend request"
}()
label.text = text
}
Kind.Outgoing -> {
val message = this.message as OutgoingTextMessage
// buttonLinearLayout.visibility = View.GONE
val text = {
"You've sent a friend request"
}()
label.text = text
}
}
}
// endregion
// region Interaction
private fun accept() {
val message = this.message as IncomingTextMessage
// TODO: Update message friend request status
delegate?.acceptFriendRequest(message)
}
private fun reject() {
val message = this.message as IncomingTextMessage
// TODO: Update message friend request status
delegate?.rejectFriendRequest(message)
}
// endregion
}

View File

@ -0,0 +1,16 @@
package org.thoughtcrime.securesms.loki
import org.thoughtcrime.securesms.sms.IncomingTextMessage
interface FriendRequestViewDelegate {
/**
* Implementations of this method should update the thread's friend request status
* and send a friend request accepted message.
*/
fun acceptFriendRequest(friendRequest: IncomingTextMessage)
/**
* Implementations of this method should update the thread's friend request status
* and remove the pre keys associated with the contact.
*/
fun rejectFriendRequest(friendRequest: IncomingTextMessage)
}

View File

@ -0,0 +1,13 @@
package org.thoughtcrime.securesms.loki
import android.content.res.Resources
import android.os.Build
import android.support.annotation.ColorRes
fun Resources.getColorWithID(@ColorRes id: Int, theme: Resources.Theme?): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getColor(id, theme)
} else {
@Suppress("DEPRECATION") getColor(id)
}
}