Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="https" />
</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>
</application>

Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/com/paulcoding/hviewer/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.paulcoding.hviewer

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand Down Expand Up @@ -39,13 +40,20 @@ class MainActivity : ComponentActivity() {

enableEdgeToEdge()
setContent {
Content()
Content(intent)
}
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setContent {
Content(intent)
}
}
}

@Composable
fun Content() {
fun Content(intent: Intent?) {
val githubState by Github.stateFlow.collectAsState()
val repoUrl = githubState.remoteUrl

Expand All @@ -65,7 +73,7 @@ fun Content() {
HViewerTheme {
if (githubState.isLoading)
UpdateDialog()
AppEntry()
AppEntry(intent)
}
}

Expand Down
43 changes: 42 additions & 1 deletion app/src/main/java/com/paulcoding/hviewer/ui/page/AppEntry.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.paulcoding.hviewer.ui.page

import android.content.Intent
import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NamedNavArgument
import androidx.navigation.NavBackStackEntry
Expand All @@ -18,6 +22,8 @@ import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.paulcoding.hviewer.R
import com.paulcoding.hviewer.helper.makeToast
import com.paulcoding.hviewer.model.PostItem
import com.paulcoding.hviewer.model.SiteConfigs
import com.paulcoding.hviewer.model.Tag
Expand All @@ -38,13 +44,14 @@ import com.paulcoding.hviewer.ui.page.tabs.TabsPage
import com.paulcoding.hviewer.ui.page.web.WebPage

@Composable
fun AppEntry() {
fun AppEntry(intent: Intent?) {
val navController = rememberNavController()

val githubState by Github.stateFlow.collectAsState()
val siteConfigs = githubState.siteConfigs ?: SiteConfigs()
val appViewModel: AppViewModel = viewModel()
val appState by appViewModel.stateFlow.collectAsState()
val context = LocalContext.current

fun navToImages(post: PostItem) {
appViewModel.setCurrentPost(post)
Expand All @@ -60,6 +67,39 @@ fun AppEntry() {
val startDestination =
remember { if (Preferences.pin.isNotEmpty()) Route.LOCK else Route.SITES }

// handle intent
val updatedIntent by rememberUpdatedState(intent)

fun handleIntentUrl(url: String) {
val postItem = PostItem(url = url)
if (postItem.getSiteConfig(siteConfigs.toHostsMap()) != null) {
navToImages(postItem)
} else {
makeToast(context.getString(R.string.invalid_url, url))
}
}

LaunchedEffect(updatedIntent) {
updatedIntent?.apply {
when (action) {
Intent.ACTION_SEND -> {
if ("text/plain" == type) {
getStringExtra(Intent.EXTRA_TEXT)?.let {
handleIntentUrl(it)
}
}
}

Intent.ACTION_VIEW -> {
handleIntentUrl(data.toString())
}

else -> {
}
}
}
}

NavHost(navController, startDestination = startDestination) {
animatedComposable(Route.SITES) {
SitesPage(
Expand Down Expand Up @@ -124,6 +164,7 @@ fun AppEntry() {
appViewModel.setWebViewUrl(it)
navController.navigate(Route.WEBVIEW)
},
hostMap = siteConfigs.toHostsMap(),
goBack = {
navController.popBackStack()
})
Expand Down
19 changes: 13 additions & 6 deletions app/src/main/java/com/paulcoding/hviewer/ui/page/post/PostPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ package com.paulcoding.hviewer.ui.page.post
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import com.paulcoding.hviewer.model.SiteConfig
import com.paulcoding.hviewer.ui.page.AppViewModel

@Composable
fun PostPage(appViewModel: AppViewModel, navToWebView: (String) -> Unit, goBack: () -> Unit) {
fun PostPage(
appViewModel: AppViewModel,
hostMap: Map<String, SiteConfig>,
navToWebView: (String) -> Unit,
goBack: () -> Unit
) {
val appState by appViewModel.stateFlow.collectAsState()
val post = appState.post
val siteConfig = appState.site.second

ImageList(
postUrl = post.url, siteConfig = siteConfig,
goBack = goBack
)
post.getSiteConfig(hostMap)?.let {
ImageList(
postUrl = post.url, siteConfig = it,
goBack = goBack
)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
<string name="edit_local_scripts">Edit local scripts</string>
<string name="added_to_tabs">Added to tabs</string>
<string name="tabs">Tabs</string>
<string name="invalid_url">Invalid URL %1$s</string>
</resources>