Use openLinksWIth preference in ItemScreen

This commit is contained in:
Shinokuni 2024-07-15 14:07:10 +02:00
parent 965fb2ea67
commit 273868d270
5 changed files with 41 additions and 4 deletions

View File

@ -58,6 +58,7 @@ dependencies {
implementation(libs.workmanager)
implementation(libs.encrypted.preferences)
implementation(libs.datastore)
implementation(libs.browser)
implementation(libs.jsoup)
implementation(libs.jodatime)

View File

@ -44,7 +44,7 @@ val composeAppModule = module {
factory { AccountScreenModel(get()) }
factory { (itemId: Int) -> ItemScreenModel(get(), itemId) }
factory { (itemId: Int) -> ItemScreenModel(get(), itemId, get()) }
factory { (accountType: Account, mode: AccountCredentialsScreenMode) ->
AccountCredentialsScreenModel(accountType, mode, get())

View File

@ -3,6 +3,8 @@ package com.readrops.app.item
import android.content.Intent
import android.net.Uri
import android.widget.RelativeLayout
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
@ -32,6 +34,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
@ -43,6 +46,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.net.toUri
import androidx.core.view.children
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import cafe.adriel.voyager.koin.getScreenModel
@ -132,8 +136,22 @@ class ItemScreen(
}
fun openUrl(url: String) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
context.startActivity(intent)
if (state.openInExternalBrowser) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
context.startActivity(intent)
} else {
CustomTabsIntent.Builder()
.setDefaultColorSchemeParams(
CustomTabColorSchemeParams
.Builder()
.setToolbarColor(accentColor.toArgb())
.build()
)
.setShareState(CustomTabsIntent.SHARE_STATE_ON)
.setUrlBarHidingEnabled(true)
.build()
.launchUrl(context, url.toUri())
}
}
Scaffold(

View File

@ -13,6 +13,7 @@ import cafe.adriel.voyager.core.model.screenModelScope
import coil.imageLoader
import coil.request.ImageRequest
import com.readrops.app.repositories.BaseRepository
import com.readrops.app.util.Preferences
import com.readrops.db.Database
import com.readrops.db.entities.Item
import com.readrops.db.entities.account.Account
@ -31,6 +32,7 @@ import java.io.FileOutputStream
class ItemScreenModel(
private val database: Database,
private val itemId: Int,
private val preferences: Preferences,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
) : StateScreenModel<ItemState>(ItemState()), KoinComponent {
@ -64,6 +66,20 @@ class ItemScreenModel(
}
}
}
screenModelScope.launch(dispatcher) {
preferences.openLinksWith.flow
.collect { value ->
mutableState.update {
it.copy(
openInExternalBrowser = when (value) {
"external_navigator" -> true
else -> false
}
)
}
}
}
}
fun shareItem(item: Item, context: Context) {
@ -158,5 +174,6 @@ data class ItemState(
val itemWithFeed: ItemWithFeed? = null,
val bottomBarState: BottomBarState = BottomBarState(),
val imageDialogUrl: String? = null,
val fileDownloadedEvent: Boolean = false
val fileDownloadedEvent: Boolean = false,
val openInExternalBrowser: Boolean = false
)

View File

@ -95,6 +95,7 @@ palette = "androidx.palette:palette-ktx:1.0.0"
workmanager = "androidx.work:work-runtime-ktx:2.9.0"
encrypted-preferences = "androidx.security:security-crypto:1.1.0-alpha06"
datastore = "androidx.datastore:datastore-preferences:1.1.1"
browser = "androidx.browser:browser:1.8.0"
# test
junit4 = "junit:junit:4.13.2"