chore: improve deep link management

This commit is contained in:
Diego Beraldin 2023-10-10 22:22:00 +02:00
parent 5ec2466e3b
commit eb51d38341
2 changed files with 57 additions and 20 deletions

View File

@ -14,11 +14,40 @@ fun handleUrl(
uriHandler: UriHandler,
navigator: Navigator? = null,
) {
val community = extractCommunity(url)
val user = extractUser(url)
val matches = Regex("https?://(?<instance>.*?)(?<pathAndQuery>/.*)").findAll(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 {
community != null -> {
navigator?.push(
community != null && navigator != null -> {
navigator.push(
CommunityDetailScreen(
community = community,
otherInstance = community.host
@ -26,8 +55,8 @@ fun handleUrl(
)
}
user != null -> {
navigator?.push(
user != null && navigator != null -> {
navigator.push(
UserDetailScreen(
user = user,
otherInstance = user.host
@ -39,9 +68,11 @@ fun handleUrl(
uriHandler.openUri(url)
}
else -> {
navigator?.push(WebViewScreen(url))
navigator != null -> {
navigator.push(WebViewScreen(url))
}
else -> Unit
}
}

View File

@ -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.settings.ui.SettingsTab
import com.github.diegoberaldin.raccoonforlemmy.ui.navigation.TabNavigationItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext
import kotlin.math.roundToInt
internal class MainScreen : Screen {
@ -100,20 +102,9 @@ internal class MainScreen : Screen {
navigationCoordinator.apply {
setBottomBarScrollConnection(scrollConnection)
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)
LaunchedEffect(drawerCoordinator) {
drawerCoordinator.toggleEvents.onEach { evt ->
@ -144,7 +135,8 @@ internal class MainScreen : Screen {
}
ModalNavigationDrawer(
drawerState = drawerState,
drawerContent = {
drawerContent =
{
ModalDrawerSheet {
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)
}
}
}