Skip to content

Commit

Permalink
Fixed possible crash when opening a URL
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickunterwegs committed Jan 30, 2025
1 parent 9b024b7 commit c7e9450
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions app/src/main/java/at/techbee/jtx/ui/detail/DetailsCardUrl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ package at.techbee.jtx.ui.detail

import android.content.ActivityNotFoundException
import android.util.Log
import android.widget.Toast
import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Clear
import androidx.compose.material.icons.outlined.Link
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -30,7 +40,6 @@ import at.techbee.jtx.ui.reusable.elements.HeadlineWithIcon
import at.techbee.jtx.util.UiUtil


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DetailsCardUrl(
initialUrl: String,
Expand All @@ -42,13 +51,18 @@ fun DetailsCardUrl(
val headline = stringResource(id = R.string.url)
var url by rememberSaveable { mutableStateOf(initialUrl) }
val uriHandler = LocalUriHandler.current
val context = LocalContext.current

ElevatedCard(modifier = modifier, onClick = {
try {
if (url.isNotBlank() && !isEditMode)
uriHandler.openUri(url)
} catch (e: ActivityNotFoundException) {
Log.d("PropertyCardUrl", "Failed opening Uri $url\n$e")
Toast.makeText(context, e.message?:"", Toast.LENGTH_LONG).show()
} catch (e: IllegalArgumentException) {
Log.d("PropertyCardUrl", "Failed opening Uri $url$e")
Toast.makeText(context, e.message?:"", Toast.LENGTH_LONG).show()
}
}) {
Column(
Expand Down

0 comments on commit c7e9450

Please sign in to comment.