Skip to content

Commit

Permalink
[optimize|build] Modify download screen entrance; optimize code; upda…
Browse files Browse the repository at this point in the history
…te dependencies
  • Loading branch information
SkyD666 committed Aug 14, 2024
1 parent e9cfa57 commit 692c256
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 170 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
minSdk = 24
targetSdk = 35
versionCode = 21
versionName = "2.1-alpha12"
versionName = "2.1-alpha13"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -184,8 +184,8 @@ dependencies {
implementation("com.google.android.material:material:1.12.0")
implementation("com.google.accompanist:accompanist-permissions:0.34.0")

implementation("com.google.dagger:hilt-android:2.51.1")
ksp("com.google.dagger:hilt-android-compiler:2.51.1")
implementation("com.google.dagger:hilt-android:2.52")
ksp("com.google.dagger:hilt-android-compiler:2.52")
implementation("androidx.hilt:hilt-navigation-compose:1.2.0")
implementation("androidx.profileinstaller:profileinstaller:1.3.1")

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/skyd/anivu/model/bean/MoreBean.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.skyd.anivu.model.bean

import androidx.annotation.DrawableRes
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.vector.ImageVector
import com.skyd.anivu.base.BaseBean

data class MoreBean(
val title: String,
@DrawableRes val icon: Int,
val icon: ImageVector,
val iconTint: Color,
val shape: Shape,
val shapeColor: Color,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class DownloadTorrentWorker(context: Context, parameters: WorkerParameters) :
0,
Intent(
Intent.ACTION_VIEW,
DOWNLOAD_SCREEN_DEEP_LINK.toUri(),
DOWNLOAD_SCREEN_DEEP_LINK.deepLink.toUri(),
applicationContext,
MainActivity::class.java
),
Expand Down
157 changes: 151 additions & 6 deletions app/src/main/java/com/skyd/anivu/ui/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,51 @@ import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Storage
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.util.Consumer
import androidx.navigation.NavController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import androidx.navigation.navDeepLink
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.skyd.anivu.R
import com.skyd.anivu.base.BaseComposeActivity
import com.skyd.anivu.ext.toDecodedUrl
import com.skyd.anivu.ui.local.LocalNavController
import com.skyd.anivu.ui.screen.MAIN_SCREEN_ROUTE
import com.skyd.anivu.ui.screen.MainScreen
import com.skyd.anivu.ui.screen.RequestStoragePermissionScreen
import com.skyd.anivu.ui.screen.about.ABOUT_SCREEN_ROUTE
import com.skyd.anivu.ui.screen.about.AboutScreen
import com.skyd.anivu.ui.screen.about.update.UpdateDialog
Expand All @@ -45,6 +68,7 @@ import com.skyd.anivu.ui.screen.download.DOWNLOAD_LINK_KEY
import com.skyd.anivu.ui.screen.download.DOWNLOAD_SCREEN_DEEP_LINK
import com.skyd.anivu.ui.screen.download.DOWNLOAD_SCREEN_ROUTE
import com.skyd.anivu.ui.screen.download.DownloadScreen
import com.skyd.anivu.ui.screen.download.openDownloadScreen
import com.skyd.anivu.ui.screen.filepicker.EXTENSION_NAME_KEY
import com.skyd.anivu.ui.screen.filepicker.FILE_PICKER_ID_KEY
import com.skyd.anivu.ui.screen.filepicker.FILE_PICKER_SCREEN_ROUTE
Expand Down Expand Up @@ -95,11 +119,14 @@ import com.skyd.anivu.ui.screen.settings.transmission.TRANSMISSION_SCREEN_ROUTE
import com.skyd.anivu.ui.screen.settings.transmission.TransmissionScreen
import com.skyd.anivu.ui.screen.settings.transmission.proxy.PROXY_SCREEN_ROUTE
import com.skyd.anivu.ui.screen.settings.transmission.proxy.ProxyScreen
import com.skyd.anivu.ui.local.LocalNavController
import dagger.hilt.android.AndroidEntryPoint

private val deepLinks = listOf(DOWNLOAD_SCREEN_DEEP_LINK)

@AndroidEntryPoint
class MainActivity : BaseComposeActivity() {
private var needHandleOnCreateIntent = true

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
Expand All @@ -108,7 +135,59 @@ class MainActivity : BaseComposeActivity() {
CompositionLocalProvider(
LocalNavController provides rememberNavController(),
) {
MainContent()
MainContent(onHandleIntent = { IntentHandler() })
}
}
}

@Composable
private fun IntentHandler() {
val navController = LocalNavController.current
if (needHandleOnCreateIntent) {
LaunchedEffect(Unit) {
needHandleOnCreateIntent = false
handleIntent(intent = intent, navController = navController)
}
}

DisposableEffect(navController) {
val listener = Consumer<Intent> { newIntent ->
handleIntent(intent = newIntent, navController = navController)
}
addOnNewIntentListener(listener)
onDispose { removeOnNewIntentListener(listener) }
}
}

private fun handleIntent(intent: Intent?, navController: NavController) {
intent ?: return
val data = intent.data
if (Intent.ACTION_VIEW == intent.action && data != null) {
val scheme = data.scheme
var url: String? = null
when (scheme) {
"anivu" -> {
navController.navigate(
data,
deepLinks.firstOrNull {
data.toString().startsWith(it.deepLink)
}?.navOptions
)
}

"magnet" -> url = data.toString()
"http", "https" -> {
val path = data.path
if (path != null && path.endsWith(".torrent")) {
url = data.toString()
}
}
}
if (url != null) {
openDownloadScreen(
navController = navController,
downloadLink = url,
)
}
}
}
Expand Down Expand Up @@ -186,15 +265,17 @@ private fun MainNavHost() {
composable(
route = DOWNLOAD_SCREEN_ROUTE,
arguments = listOf(navArgument(DOWNLOAD_LINK_KEY) { nullable = true }),
deepLinks = listOf(navDeepLink { uriPattern = DOWNLOAD_SCREEN_DEEP_LINK }),
deepLinks = listOf(navDeepLink { uriPattern = DOWNLOAD_SCREEN_DEEP_LINK.deepLink }),
) {
DownloadScreen(downloadLink = it.arguments?.getString(DOWNLOAD_LINK_KEY))
}
composable(
route = "$READ_SCREEN_ROUTE/{$ARTICLE_ID_KEY}",
arguments = listOf(navArgument(ARTICLE_ID_KEY) { type = NavType.StringType }),
) {
ReadScreen(articleId = it.arguments?.getString(ARTICLE_ID_KEY).orEmpty().toDecodedUrl())
ReadScreen(
articleId = it.arguments?.getString(ARTICLE_ID_KEY).orEmpty().toDecodedUrl()
)
}
composable(
route = SEARCH_SCREEN_ROUTE,
Expand Down Expand Up @@ -222,7 +303,7 @@ private fun MainNavHost() {
}

@Composable
private fun MainContent() {
private fun MainContent(onHandleIntent: @Composable () -> Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
var permissionGranted by remember {
mutableStateOf(Environment.isExternalStorageManager())
Expand All @@ -233,6 +314,7 @@ private fun MainContent() {

if (permissionGranted) {
MainNavHost()
onHandleIntent()
} else {
RequestStoragePermissionScreen(
shouldShowRationale = false,
Expand All @@ -255,6 +337,7 @@ private fun MainContent() {
)
if (storagePermissionState.allPermissionsGranted) {
MainNavHost()
onHandleIntent()
} else {
RequestStoragePermissionScreen(
shouldShowRationale = storagePermissionState.shouldShowRationale,
Expand All @@ -273,4 +356,66 @@ private fun MainContent() {
onError = { openUpdateDialog = false },
)
}
}

@Composable
fun RequestStoragePermissionScreen(
shouldShowRationale: Boolean,
onPermissionRequest: () -> Unit
) {
Scaffold { padding ->
Column(
modifier = Modifier
.padding(padding)
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(horizontal = 30.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(Modifier.height(50.dp))

Text(
text = stringResource(R.string.storage_permission_request_screen_title),
style = MaterialTheme.typography.titleLarge,
textAlign = TextAlign.Center,
)

Icon(
modifier = Modifier
.padding(30.dp)
.size(110.dp),
imageVector = Icons.Rounded.Storage,
contentDescription = null,
)

val textToShow = if (shouldShowRationale) {
// If the user has denied the permission but the rationale can be shown,
// then gently explain why the app requires this permission
stringResource(R.string.storage_permission_request_screen_rationale)
} else {
// If it's the first time the user lands on this feature, or the user
// doesn't want to be asked again for this permission, explain that the
// permission is required
stringResource(R.string.storage_permission_request_screen_first_tip)
}
Text(
text = textToShow,
style = MaterialTheme.typography.bodyMedium,
textAlign = TextAlign.Center,
)

Spacer(
modifier = Modifier
.weight(1f)
.padding(vertical = 20.dp)
)

Button(
modifier = Modifier.padding(vertical = 30.dp),
onClick = onPermissionRequest,
) {
Text(stringResource(R.string.storage_permission_request_screen_request_permission))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.skyd.anivu.ui.component.deeplink

import androidx.navigation.NavOptions

data class DeepLinkData(
val deepLink: String,
val navOptions: NavOptions? = null,
)
Loading

0 comments on commit 692c256

Please sign in to comment.