mirror of
https://github.com/LiveFastEatTrashRaccoon/RaccoonForLemmy.git
synced 2025-02-04 00:47:32 +01:00
chore: improve deep link management
This commit is contained in:
parent
5ec2466e3b
commit
eb51d38341
@ -14,11 +14,40 @@ fun handleUrl(
|
|||||||
uriHandler: UriHandler,
|
uriHandler: UriHandler,
|
||||||
navigator: Navigator? = null,
|
navigator: Navigator? = null,
|
||||||
) {
|
) {
|
||||||
val community = extractCommunity(url)
|
val matches = Regex("https?://(?<instance>.*?)(?<pathAndQuery>/.*)").findAll(url)
|
||||||
val user = extractUser(url)
|
var instance = ""
|
||||||
|
val mangledUrl = buildString {
|
||||||
|
if (matches.count() > 0) {
|
||||||
|
val match = matches.iterator().next()
|
||||||
|
val value = match.groups["pathAndQuery"]?.value.orEmpty()
|
||||||
|
instance = match.groups["instance"]?.value.orEmpty()
|
||||||
|
if (value.isNotEmpty()) {
|
||||||
|
append(value)
|
||||||
|
} else {
|
||||||
|
append(url)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
append(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val community = extractCommunity(mangledUrl)?.let {
|
||||||
|
if (it.host.isEmpty()) {
|
||||||
|
it.copy(host = instance)
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val user = extractUser(mangledUrl)?.let {
|
||||||
|
if (it.host.isEmpty()) {
|
||||||
|
it.copy(host = instance)
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
when {
|
when {
|
||||||
community != null -> {
|
community != null && navigator != null -> {
|
||||||
navigator?.push(
|
navigator.push(
|
||||||
CommunityDetailScreen(
|
CommunityDetailScreen(
|
||||||
community = community,
|
community = community,
|
||||||
otherInstance = community.host
|
otherInstance = community.host
|
||||||
@ -26,8 +55,8 @@ fun handleUrl(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
user != null -> {
|
user != null && navigator != null -> {
|
||||||
navigator?.push(
|
navigator.push(
|
||||||
UserDetailScreen(
|
UserDetailScreen(
|
||||||
user = user,
|
user = user,
|
||||||
otherInstance = user.host
|
otherInstance = user.host
|
||||||
@ -39,9 +68,11 @@ fun handleUrl(
|
|||||||
uriHandler.openUri(url)
|
uriHandler.openUri(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
navigator != null -> {
|
||||||
navigator?.push(WebViewScreen(url))
|
navigator.push(WebViewScreen(url))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else -> Unit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,10 +51,12 @@ import com.github.diegoberaldin.raccoonforlemmy.feature.search.multicommunity.de
|
|||||||
import com.github.diegoberaldin.raccoonforlemmy.feature.search.ui.SearchTab
|
import com.github.diegoberaldin.raccoonforlemmy.feature.search.ui.SearchTab
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.feature.settings.ui.SettingsTab
|
import com.github.diegoberaldin.raccoonforlemmy.feature.settings.ui.SettingsTab
|
||||||
import com.github.diegoberaldin.raccoonforlemmy.ui.navigation.TabNavigationItem
|
import com.github.diegoberaldin.raccoonforlemmy.ui.navigation.TabNavigationItem
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.drop
|
import kotlinx.coroutines.flow.drop
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
internal class MainScreen : Screen {
|
internal class MainScreen : Screen {
|
||||||
@ -100,20 +102,9 @@ internal class MainScreen : Screen {
|
|||||||
navigationCoordinator.apply {
|
navigationCoordinator.apply {
|
||||||
setBottomBarScrollConnection(scrollConnection)
|
setBottomBarScrollConnection(scrollConnection)
|
||||||
setCurrentSection(HomeTab)
|
setCurrentSection(HomeTab)
|
||||||
|
|
||||||
// handles deep link urls
|
|
||||||
deeplinkUrl.onEach {
|
|
||||||
handleUrl(
|
|
||||||
url = it,
|
|
||||||
uriHandler = uriHandler,
|
|
||||||
navigator = navigator,
|
|
||||||
openExternal = settings.openUrlsInExternalBrowser,
|
|
||||||
)
|
|
||||||
}.launchIn(this@LaunchedEffect)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
|
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
|
||||||
LaunchedEffect(drawerCoordinator) {
|
LaunchedEffect(drawerCoordinator) {
|
||||||
drawerCoordinator.toggleEvents.onEach { evt ->
|
drawerCoordinator.toggleEvents.onEach { evt ->
|
||||||
@ -144,7 +135,8 @@ internal class MainScreen : Screen {
|
|||||||
}
|
}
|
||||||
ModalNavigationDrawer(
|
ModalNavigationDrawer(
|
||||||
drawerState = drawerState,
|
drawerState = drawerState,
|
||||||
drawerContent = {
|
drawerContent =
|
||||||
|
{
|
||||||
ModalDrawerSheet {
|
ModalDrawerSheet {
|
||||||
TabNavigator(ModalDrawerContent)
|
TabNavigator(ModalDrawerContent)
|
||||||
}
|
}
|
||||||
@ -198,5 +190,19 @@ internal class MainScreen : Screen {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handles deep link URLs
|
||||||
|
LaunchedEffect(navigator, navigationCoordinator) {
|
||||||
|
navigationCoordinator.deeplinkUrl.onEach {
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
handleUrl(
|
||||||
|
url = it,
|
||||||
|
uriHandler = uriHandler,
|
||||||
|
navigator = navigator,
|
||||||
|
openExternal = settings.openUrlsInExternalBrowser,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}.launchIn(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user