From f6542b983469d2939d67e9df29437d919b00ec9a Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 21 Jun 2019 14:18:38 +1000 Subject: [PATCH] Further build out friend request view --- .../securesms/loki/FriendRequestView.kt | 100 +++++++++++++++++- .../loki/FriendRequestViewDelegate.kt | 16 +++ .../securesms/loki/GeneralUtilities.kt | 13 +++ 3 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/loki/FriendRequestViewDelegate.kt create mode 100644 src/org/thoughtcrime/securesms/loki/GeneralUtilities.kt diff --git a/src/org/thoughtcrime/securesms/loki/FriendRequestView.kt b/src/org/thoughtcrime/securesms/loki/FriendRequestView.kt index 6d1d9edb00..b5510941b5 100644 --- a/src/org/thoughtcrime/securesms/loki/FriendRequestView.kt +++ b/src/org/thoughtcrime/securesms/loki/FriendRequestView.kt @@ -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 } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/FriendRequestViewDelegate.kt b/src/org/thoughtcrime/securesms/loki/FriendRequestViewDelegate.kt new file mode 100644 index 0000000000..1fe8168733 --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/FriendRequestViewDelegate.kt @@ -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) +} \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/GeneralUtilities.kt b/src/org/thoughtcrime/securesms/loki/GeneralUtilities.kt new file mode 100644 index 0000000000..b3347351fc --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/GeneralUtilities.kt @@ -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) + } +} \ No newline at end of file