mirror of
https://github.com/LiveFastEatTrashRaccoon/RaccoonForLemmy.git
synced 2025-02-02 03:47:09 +01:00
feat: receive content from share menu (#717)
This commit is contained in:
parent
6bff8a99cf
commit
241e04b980
@ -26,6 +26,11 @@
|
||||
<action android:name="dev.zwander.lemmyredirect.intent.action.OPEN_FEDI_LINK"/>
|
||||
<category android:name="android.intent.category.DEFAULT"/>
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEND" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:mimeType="text/plain" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity-alias
|
||||
android:name=".MainActivityAlias1"
|
||||
|
@ -2,21 +2,21 @@ package com.github.diegoberaldin.raccoonforlemmy.android
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Patterns
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.github.diegoberaldin.raccoonforlemmy.MainView
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.ComposeEvent
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.TabNavigationSection
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.di.getDrawerCoordinator
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.di.getNavigationCoordinator
|
||||
import com.github.diegoberaldin.raccoonforlemmy.feature.home.ui.HomeTab
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
private const val DEEP_LINK_DELAY = 500L
|
||||
|
||||
@ -71,17 +71,22 @@ class MainActivity : ComponentActivity() {
|
||||
)
|
||||
}
|
||||
|
||||
intent?.data?.toString()?.also {
|
||||
handleDeeplink(it)
|
||||
}
|
||||
handleIntent(intent)
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
super.onNewIntent(intent)
|
||||
intent?.data?.toString()?.also {
|
||||
runBlocking {
|
||||
delay(DEEP_LINK_DELAY)
|
||||
handleDeeplink(it)
|
||||
handleIntent(intent)
|
||||
}
|
||||
|
||||
private fun handleIntent(intent: Intent?) = intent?.apply {
|
||||
when (action) {
|
||||
Intent.ACTION_SEND -> intent.getStringExtra(Intent.EXTRA_TEXT)?.let { content ->
|
||||
handleCreatePost(content)
|
||||
}
|
||||
|
||||
else -> data.toString().takeUnless { it.isEmpty() }?.also { url ->
|
||||
handleDeeplink(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -90,4 +95,15 @@ class MainActivity : ComponentActivity() {
|
||||
val navigationCoordinator = getNavigationCoordinator()
|
||||
navigationCoordinator.submitDeeplink(url)
|
||||
}
|
||||
|
||||
private fun handleCreatePost(content: String) {
|
||||
val looksLikeAnUrl = Patterns.WEB_URL.matcher(content).matches()
|
||||
val event = if (looksLikeAnUrl) {
|
||||
ComposeEvent.WithUrl(content)
|
||||
} else {
|
||||
ComposeEvent.WithText(content)
|
||||
}
|
||||
val navigationCoordinator = getNavigationCoordinator()
|
||||
navigationCoordinator.submitComposeEvent(event)
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ internal class DefaultNavigationCoordinator : NavigationCoordinator {
|
||||
override val currentSection = MutableStateFlow<TabNavigationSection?>(null)
|
||||
override val onDoubleTabSelection = MutableSharedFlow<TabNavigationSection>()
|
||||
override val deepLinkUrl = MutableSharedFlow<String>()
|
||||
override val composeEvents = MutableSharedFlow<ComposeEvent>()
|
||||
override val inboxUnread = MutableStateFlow(0)
|
||||
override val canPop = MutableStateFlow(false)
|
||||
override val exitMessageVisible = MutableStateFlow(false)
|
||||
@ -102,6 +103,16 @@ internal class DefaultNavigationCoordinator : NavigationCoordinator {
|
||||
}
|
||||
}
|
||||
|
||||
override fun submitComposeEvent(event: ComposeEvent) {
|
||||
scope.launch {
|
||||
delay(DEEP_LINK_DELAY)
|
||||
runCatching {
|
||||
ensureActive()
|
||||
composeEvents.emit(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun setCanGoBackCallback(value: (() -> Boolean)?) {
|
||||
canGoBackCallback = value
|
||||
}
|
||||
|
@ -18,6 +18,11 @@ sealed interface TabNavigationSection {
|
||||
data object Settings : TabNavigationSection
|
||||
}
|
||||
|
||||
sealed interface ComposeEvent {
|
||||
data class WithUrl(val url: String) : ComposeEvent
|
||||
data class WithText(val text: String) : ComposeEvent
|
||||
}
|
||||
|
||||
sealed interface SideMenuEvents {
|
||||
data class Open(val screen: Screen) : SideMenuEvents
|
||||
data object Close : SideMenuEvents
|
||||
@ -30,12 +35,14 @@ interface NavigationCoordinator {
|
||||
val onDoubleTabSelection: Flow<TabNavigationSection>
|
||||
val inboxUnread: StateFlow<Int>
|
||||
val deepLinkUrl: Flow<String?>
|
||||
val composeEvents: Flow<ComposeEvent?>
|
||||
val canPop: StateFlow<Boolean>
|
||||
val exitMessageVisible: StateFlow<Boolean>
|
||||
val sideMenuEvents: Flow<SideMenuEvents>
|
||||
|
||||
fun setCurrentSection(section: TabNavigationSection)
|
||||
fun submitDeeplink(url: String)
|
||||
fun submitComposeEvent(event: ComposeEvent)
|
||||
fun setRootNavigator(value: Navigator?)
|
||||
fun setCanGoBackCallback(value: (() -> Boolean)?)
|
||||
fun getCanGoBackCallback(): (() -> Boolean)?
|
||||
|
@ -50,6 +50,7 @@ import com.github.diegoberaldin.raccoonforlemmy.core.commonui.lemmyui.getPostFro
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.commonui.lemmyui.getUserFromUrl
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.l10n.ProvideXmlStrings
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.l10n.di.getL10nManager
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.ComposeEvent
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.DrawerEvent
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.SideMenuEvents
|
||||
import com.github.diegoberaldin.raccoonforlemmy.core.navigation.di.getDrawerCoordinator
|
||||
@ -195,7 +196,21 @@ fun App(onLoadingFinished: () -> Unit = {}) {
|
||||
else -> Unit
|
||||
}
|
||||
}.launchIn(this)
|
||||
navigationCoordinator.composeEvents.debounce(750).onEach { event ->
|
||||
when (event) {
|
||||
is ComposeEvent.WithText -> detailOpener.openCreatePost(
|
||||
initialText = event.text,
|
||||
forceCommunitySelection = true,
|
||||
)
|
||||
|
||||
is ComposeEvent.WithUrl -> detailOpener.openCreatePost(
|
||||
initialUrl = event.url,
|
||||
forceCommunitySelection = true,
|
||||
)
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
}.launchIn(this)
|
||||
navigationCoordinator.sideMenuEvents.onEach { evt ->
|
||||
when (evt) {
|
||||
is SideMenuEvents.Open -> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user