mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-03 23:02:35 +02:00
Clickable Phone and Emails
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
package com.vitorpamplona.amethyst.ui.components
|
||||||
|
|
||||||
|
import android.content.ActivityNotFoundException
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import androidx.compose.foundation.text.ClickableText
|
||||||
|
import androidx.compose.material.LocalTextStyle
|
||||||
|
import androidx.compose.material.MaterialTheme
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.platform.LocalUriHandler
|
||||||
|
import androidx.compose.ui.text.AnnotatedString
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ClickableEmail(email: String) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
|
||||||
|
ClickableText(
|
||||||
|
text = AnnotatedString("$email "),
|
||||||
|
onClick = { runCatching { context.sendMail(email) } },
|
||||||
|
style = LocalTextStyle.current.copy(color = MaterialTheme.colors.primary),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Context.sendMail(to: String, subject: String? = null) {
|
||||||
|
try {
|
||||||
|
val intent = Intent(Intent.ACTION_SEND)
|
||||||
|
intent.type = "vnd.android.cursor.item/email" // or "message/rfc822"
|
||||||
|
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(to))
|
||||||
|
if (subject != null)
|
||||||
|
intent.putExtra(Intent.EXTRA_SUBJECT, subject)
|
||||||
|
startActivity(intent)
|
||||||
|
} catch (e: ActivityNotFoundException) {
|
||||||
|
// TODO: Handle case where no email app is available
|
||||||
|
} catch (t: Throwable) {
|
||||||
|
// TODO: Handle potential other type of exceptions
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,33 @@
|
|||||||
|
package com.vitorpamplona.amethyst.ui.components
|
||||||
|
|
||||||
|
import android.content.ActivityNotFoundException
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.compose.foundation.text.ClickableText
|
||||||
|
import androidx.compose.material.LocalTextStyle
|
||||||
|
import androidx.compose.material.MaterialTheme
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.platform.LocalUriHandler
|
||||||
|
import androidx.compose.ui.text.AnnotatedString
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ClickablePhone(phone: String) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
|
||||||
|
ClickableText(
|
||||||
|
text = AnnotatedString("$phone "),
|
||||||
|
onClick = { runCatching { context.dial(phone) } },
|
||||||
|
style = LocalTextStyle.current.copy(color = MaterialTheme.colors.primary),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Context.dial(phone: String) {
|
||||||
|
try {
|
||||||
|
val intent = Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null))
|
||||||
|
startActivity(intent)
|
||||||
|
} catch (t: Throwable) {
|
||||||
|
// TODO: Handle potential exceptions
|
||||||
|
}
|
||||||
|
}
|
@@ -60,6 +60,10 @@ fun RichTextViewer(content: String, tags: List<List<String>>?, note: Note, accou
|
|||||||
} else {
|
} else {
|
||||||
UrlPreview(word, word)
|
UrlPreview(word, word)
|
||||||
}
|
}
|
||||||
|
} else if (Patterns.EMAIL_ADDRESS.matcher(word).matches()) {
|
||||||
|
ClickableEmail(word)
|
||||||
|
} else if (Patterns.PHONE.matcher(word).matches()) {
|
||||||
|
ClickablePhone(word)
|
||||||
} else if (noProtocolUrlValidator.matcher(word).matches()) {
|
} else if (noProtocolUrlValidator.matcher(word).matches()) {
|
||||||
UrlPreview("https://$word", word)
|
UrlPreview("https://$word", word)
|
||||||
} else if (tagIndex.matcher(word).matches() && tags != null) {
|
} else if (tagIndex.matcher(word).matches() && tags != null) {
|
||||||
|
Reference in New Issue
Block a user