Parse URLs out of text when sharing to Android
This commit is contained in:
@ -39,12 +39,11 @@ abstract class SaveSheetActivityBase: AppCompatActivity() {
|
||||
Intent.ACTION_SEND -> {
|
||||
if (intent.type?.startsWith("text/plain") == true) {
|
||||
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
|
||||
Log.d(ContentValues.TAG, "Extracted text: $extractedText")
|
||||
extractedText = it
|
||||
viewModel.saveURL(it)
|
||||
Log.d(ContentValues.TAG, "Extracted text: $extractedText")
|
||||
}
|
||||
}
|
||||
|
||||
if (intent.type?.startsWith("text/html") == true) {
|
||||
intent.getStringExtra(Intent.EXTRA_HTML_TEXT)?.let {
|
||||
extractedText = it
|
||||
@ -155,7 +154,9 @@ abstract class SaveSheetActivityBase: AppCompatActivity() {
|
||||
viewModel: SaveViewModel,
|
||||
modalBottomSheetState: ModalBottomSheetState
|
||||
) {
|
||||
Box(modifier = Modifier.height(300.dp).background(Color.White)) {
|
||||
Box(modifier = Modifier
|
||||
.height(300.dp)
|
||||
.background(Color.White)) {
|
||||
SaveContent(viewModel, modalBottomSheetState, modifier = Modifier.fillMaxSize())
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.util.*
|
||||
import java.util.regex.Pattern
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@ -36,6 +37,11 @@ class SaveViewModel @Inject constructor(
|
||||
datastoreRepo.getString(DatastoreKeys.omnivoreAuthToken)
|
||||
}
|
||||
|
||||
fun cleanUrl(url: String): String? {
|
||||
return Regex("https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)")
|
||||
.findAll(url).map { it.value }.first()
|
||||
}
|
||||
|
||||
fun saveURL(url: String) {
|
||||
viewModelScope.launch {
|
||||
isLoading = true
|
||||
@ -54,6 +60,9 @@ class SaveViewModel @Inject constructor(
|
||||
.addHttpHeader("Authorization", value = authToken)
|
||||
.build()
|
||||
|
||||
// Attempt to parse the URL out of the text, if that fails send the text
|
||||
val cleanedUrl = cleanUrl(url) ?: url
|
||||
|
||||
try {
|
||||
clientRequestID = UUID.randomUUID().toString()
|
||||
|
||||
@ -62,7 +71,7 @@ class SaveViewModel @Inject constructor(
|
||||
SaveUrlInput(
|
||||
clientRequestId = clientRequestID!!,
|
||||
source = "android",
|
||||
url = url
|
||||
url = cleanedUrl
|
||||
)
|
||||
)
|
||||
).execute()
|
||||
|
||||
Reference in New Issue
Block a user