Open or copy URL WIP

This commit is contained in:
Al Lansley 2024-08-12 16:42:15 +10:00
parent ea84aa1478
commit 802cf19598
3 changed files with 58 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
@ -10,6 +11,7 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.Button
import android.widget.LinearLayout
import android.widget.LinearLayout.VERTICAL
import android.widget.ScrollView
import android.widget.Space
import android.widget.TextView
import androidx.annotation.AttrRes
@ -21,6 +23,7 @@ import androidx.core.text.HtmlCompat
import androidx.core.view.updateMargins
import androidx.fragment.app.Fragment
import network.loki.messenger.R
import org.thoughtcrime.securesms.conversation.v2.Util.writeTextToClipboard
import org.thoughtcrime.securesms.util.toPx
@DslMarker
@ -66,6 +69,26 @@ class SessionDialogBuilder(val context: Context) {
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
.apply { updateMargins(dp40, 0, dp40, 0) }
}
}
fun textWithMaxOfFiveLines(text: CharSequence?, @StyleRes style: Int = 0) {
ScrollView(context, null, 0).apply {
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
// fix this so it adds and shows a scrollbar if it exceeds 5 lines
here!
text(text, style) {
maxLines = 5
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
.apply { updateMargins(dp40, 0, dp40, 0) }
}.apply { refreshDrawableState() }
}
}
private fun text(text: CharSequence?, @StyleRes style: Int, modify: TextView.() -> Unit) {
@ -127,6 +150,9 @@ class SessionDialogBuilder(val context: Context) {
fun okButton(listener: (() -> Unit) = {}) = button(android.R.string.ok) { listener() }
fun cancelButton(listener: (() -> Unit) = {}) = button(android.R.string.cancel, R.string.AccessibilityId_cancel) { listener() }
fun copyUrlButton(listener: (() -> Unit) = {}) = button(android.R.string.copyUrl, R.string.AccessibilityId_copy) { listener() }
fun button(
@StringRes text: Int,
@StringRes contentDescriptionRes: Int = text,
@ -149,18 +175,29 @@ class SessionDialogBuilder(val context: Context) {
fun Context.showSessionDialog(build: SessionDialogBuilder.() -> Unit): AlertDialog =
SessionDialogBuilder(this).apply { build() }.show()
fun Context.showOpenUrlDialog(build: SessionDialogBuilder.() -> Unit): AlertDialog =
private fun Context.showOpenUrlDialogInner(build: SessionDialogBuilder.() -> Unit): AlertDialog =
SessionDialogBuilder(this).apply {
title(R.string.urlOpen)
text(R.string.urlOpenBrowser)
build()
}.show()
fun Context.showOpenUrlDialog(url: String): AlertDialog =
showOpenUrlDialog {
okButton { openUrl(url) }
cancelButton()
fun Context.showOpenUrlDialogPublicFacing(url: String): AlertDialog {
return SessionDialogBuilder(this).apply {
title(R.string.urlOpen)
val txt = getString(R.string.urlOpenBrowser) + "\n\n" + "https://github.com/oxen-io/session-android/theworldslongrsturlisverylongindeedandshouldgoover5linesbecausethatswhatIneedtotestbecausemaryhadalittlelamb" //url
textWithMaxOfFiveLines(txt)
dangerButton(R.string.open) { openUrl(url) }
copyUrlButton { writeTextToClipboard(context, url) }
}.show()
}
// showOpenUrlDialogInner {
// okButton { openUrl(url) }
// cancelButton()
// }
fun Context.openUrl(url: String) = Intent(Intent.ACTION_VIEW, Uri.parse(url)).let(::startActivity)

View File

@ -16,8 +16,13 @@ import org.thoughtcrime.securesms.conversation.v2.ModalUrlBottomSheet
import org.thoughtcrime.securesms.conversation.v2.utilities.MessageBubbleUtilities
import org.thoughtcrime.securesms.database.model.MmsMessageRecord
import com.bumptech.glide.RequestManager
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.SessionDialogBuilder
import org.thoughtcrime.securesms.mms.ImageSlide
import org.thoughtcrime.securesms.showOpenUrlDialogPublicFacing
class LinkPreviewView : LinearLayout {
private val binding: ViewLinkPreviewBinding by lazy { ViewLinkPreviewBinding.bind(this) }
private val cornerMask by lazy { CornerMask(this) }
@ -85,9 +90,16 @@ class LinkPreviewView : LinearLayout {
}
fun openURL() {
val url = this.url ?: return
val url = this.url ?: return Log.w("LinkPreviewView", "Cannot open a null URL")
val activity = context as AppCompatActivity
ModalUrlBottomSheet(url).show(activity.supportFragmentManager, "Open URL Dialog")
// OLDE!
//ModalUrlBottomSheet(url).show(activity.supportFragmentManager, "Open URL Dialog")
activity.showOpenUrlDialogPublicFacing(url)
}
// endregion
}

View File

@ -27,6 +27,7 @@
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/small_font_size"
android:layout_margin="@dimen/large_spacing"
android:maxLines="5"
android:textAlignment="center" />
<TextView