common: change UrlLauncher to be backed by UriHandler

This commit is contained in:
Harsh Shandilya 2021-10-28 21:59:02 +05:30
parent d285afed0b
commit 67893552e7
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
5 changed files with 13 additions and 16 deletions

View file

@ -70,7 +70,7 @@ fun LobstersApp(
val postActions = remember { val postActions = remember {
object : PostActions { object : PostActions {
override fun viewPost(postUrl: String, commentsUrl: String) { override fun viewPost(postUrl: String, commentsUrl: String) {
urlLauncher.launch(postUrl.ifEmpty { commentsUrl }) urlLauncher.openUri(postUrl.ifEmpty { commentsUrl })
} }
override fun viewComments(postId: String) { override fun viewComments(postId: String) {
@ -78,7 +78,7 @@ fun LobstersApp(
} }
override fun viewCommentsPage(commentsUrl: String) { override fun viewCommentsPage(commentsUrl: String) {
urlLauncher.launch(commentsUrl) urlLauncher.openUri(commentsUrl)
} }
override fun toggleSave(post: SavedPost) { override fun toggleSave(post: SavedPost) {

View file

@ -3,15 +3,16 @@ package dev.msfjarvis.claw.common.urllauncher
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.ui.platform.UriHandler
actual class UrlLauncher(private val context: Context) { class UrlLauncher(private val context: Context) : UriHandler {
actual fun launch(url: String) { override fun openUri(uri: String) {
val customTabsIntent = val customTabsIntent =
CustomTabsIntent.Builder() CustomTabsIntent.Builder()
.setShareState(CustomTabsIntent.SHARE_STATE_ON) .setShareState(CustomTabsIntent.SHARE_STATE_ON)
.setShowTitle(true) .setShowTitle(true)
.setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK) .setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK)
.build() .build()
customTabsIntent.launchUrl(context, Uri.parse(url)) customTabsIntent.launchUrl(context, Uri.parse(uri))
} }
} }

View file

@ -1,5 +0,0 @@
package dev.msfjarvis.claw.common.urllauncher
expect class UrlLauncher {
fun launch(url: String)
}

View file

@ -1,14 +1,15 @@
package dev.msfjarvis.claw.common.urllauncher package dev.msfjarvis.claw.common.urllauncher
import androidx.compose.ui.platform.UriHandler
import java.awt.Desktop import java.awt.Desktop
import java.net.URI import java.net.URI
actual class UrlLauncher { class UrlLauncher : UriHandler {
actual fun launch(url: String) { override fun openUri(uri: String) {
if (Desktop.isDesktopSupported()) { if (Desktop.isDesktopSupported()) {
val desktop = Desktop.getDesktop() val desktop = Desktop.getDesktop()
if (desktop.isSupported(Desktop.Action.BROWSE)) { if (desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(URI(url)) desktop.browse(URI(uri))
} }
} }
} }

View file

@ -37,15 +37,15 @@ fun main() = application {
val postActions = remember { val postActions = remember {
object : PostActions { object : PostActions {
override fun viewPost(postUrl: String, commentsUrl: String) { override fun viewPost(postUrl: String, commentsUrl: String) {
urlLauncher.launch(postUrl.ifEmpty { commentsUrl }) urlLauncher.openUri(postUrl.ifEmpty { commentsUrl })
} }
override fun viewComments(postId: String) { override fun viewComments(postId: String) {
urlLauncher.launch("${LobstersApi.BASE_URL}/s/${postId}") urlLauncher.openUri("${LobstersApi.BASE_URL}/s/${postId}")
} }
override fun viewCommentsPage(commentsUrl: String) { override fun viewCommentsPage(commentsUrl: String) {
urlLauncher.launch(commentsUrl) urlLauncher.openUri(commentsUrl)
} }
override fun toggleSave(post: SavedPost) {} override fun toggleSave(post: SavedPost) {}