Skip to content

Commit

Permalink
Merge pull request #170 from muedsa/migrating-tv-foundation
Browse files Browse the repository at this point in the history
Compose for tv beta migration
  • Loading branch information
muedsa authored Sep 9, 2024
2 parents 3fd8420 + be9dcbf commit f8c24b4
Show file tree
Hide file tree
Showing 61 changed files with 979 additions and 831 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,7 @@ lint/tmp/
*.keystore

# Google Services (e.g. APIs or Firebase)
google-services.json
google-services.json

# Kotlin
.kotlin/
30 changes: 7 additions & 23 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import org.jetbrains.kotlin.incremental.createDirectory

import java.io.FileInputStream
import java.util.Properties

Expand All @@ -10,7 +10,8 @@ plugins {
alias(libs.plugins.hiltAndroid)
alias(libs.plugins.gmsGoogleService)
alias(libs.plugins.firebaseCrashlytics)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.composeCompiler)
alias(libs.plugins.androidRoom)
}

val keystorePropertiesFile: File = rootProject.file("keystore.properties")
Expand All @@ -19,22 +20,6 @@ if (keystorePropertiesFile.exists() && keystorePropertiesFile.canRead()) {
keystoreProperties.load(FileInputStream(keystorePropertiesFile))
}

val schemasDir: File = project.file("schemas")
if (!schemasDir.exists()) {
schemasDir.createDirectory()
}

class RoomSchemaArgProvider(
@get:InputDirectory
@get:PathSensitive(PathSensitivity.RELATIVE)
val schemaDir: File
) : CommandLineArgumentProvider {

override fun asArguments(): Iterable<String> {
return listOf("room.schemaLocation=${schemaDir.path}")
}
}

android {
namespace = "com.muedsa.agetv"
compileSdk = 34
Expand Down Expand Up @@ -111,9 +96,9 @@ android {
}
}

// testOptions {
// unitTests.isReturnDefaultValues = true
// }
room {
schemaDirectory("$projectDir/schemas")
}
}

dependencies {
Expand All @@ -139,7 +124,6 @@ dependencies {
debugImplementation(libs.ui.tooling)
debugImplementation(libs.ui.test.manifest)

implementation(libs.tv.foundation)
implementation(libs.tv.material)

implementation(libs.navigation.compose)
Expand Down Expand Up @@ -183,5 +167,5 @@ dependencies {
}

ksp {
arg(RoomSchemaArgProvider(File(projectDir, "schemas")))
arg("room.generateKotlin", "true")
}
13 changes: 5 additions & 8 deletions app/src/main/kotlin/com/muedsa/agetv/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,28 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.navigation.compose.rememberNavController
import androidx.tv.material3.ExperimentalTvMaterial3Api
import com.muedsa.agetv.model.LazyType
import com.muedsa.agetv.ui.navigation.AppNavigation
import com.muedsa.agetv.viewmodel.HomePageViewModel
import com.muedsa.agetv.screens.AppNavigation
import com.muedsa.agetv.screens.home.main.MainScreenViewModel
import com.muedsa.compose.tv.theme.TvTheme
import com.muedsa.compose.tv.widget.Scaffold
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val homePageViewModel: HomePageViewModel by viewModels()
private val mainScreenViewModel: MainScreenViewModel by viewModels()

@OptIn(ExperimentalTvMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
splashScreen.setKeepOnScreenCondition {
homePageViewModel.homeDataSF.value.type == LazyType.LOADING
mainScreenViewModel.homeDataSF.value.type == LazyType.LOADING
}
setContent {
TvTheme {
Scaffold {
AppNavigation(navController = rememberNavController())
AppNavigation()
}
}
}
Expand Down
38 changes: 13 additions & 25 deletions app/src/main/kotlin/com/muedsa/agetv/PlaybackActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,22 @@ package com.muedsa.agetv
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.tv.material3.ExperimentalTvMaterial3Api
import androidx.tv.material3.MaterialTheme
import androidx.tv.material3.NonInteractiveSurfaceDefaults
import androidx.tv.material3.Surface
import com.muedsa.agetv.ui.features.playback.PlaybackScreen
import androidx.tv.material3.SurfaceDefaults
import com.muedsa.agetv.screens.playback.PlaybackScreen
import com.muedsa.compose.tv.theme.TvTheme
import com.muedsa.compose.tv.useLocalToastMsgBoxController
import com.muedsa.compose.tv.widget.AppBackHandler
import com.muedsa.compose.tv.widget.FillTextScreen
import com.muedsa.compose.tv.widget.LocalErrorMsgBoxState
import com.muedsa.compose.tv.widget.Scaffold
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PlaybackActivity : ComponentActivity() {

@OptIn(ExperimentalTvMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -36,7 +30,7 @@ class PlaybackActivity : ComponentActivity() {
TvTheme {
Scaffold(
holdBack = false,
colors = NonInteractiveSurfaceDefaults.colors(
colors = SurfaceDefaults.colors(
containerColor = Color.Black,
contentColor = MaterialTheme.colorScheme.onBackground
)
Expand All @@ -47,26 +41,20 @@ class PlaybackActivity : ComponentActivity() {
val backListeners = remember {
mutableStateListOf<() -> Unit>()
}
val errorMsgBoxState = LocalErrorMsgBoxState.current
val toastController = useLocalToastMsgBoxController()
AppBackHandler {
backListeners.forEach {
it()
}
errorMsgBoxState.error("再次点击返回键退出")
}
Surface(
modifier = Modifier.fillMaxSize(),
shape = RectangleShape,

) {
PlaybackScreen(
aid = aid,
episodeTitle = episodeTitle,
mediaUrl = mediaUrl,
danEpisodeId = episodeId,
backListeners = backListeners
)
toastController.warning("再次点击返回键退出")
}
PlaybackScreen(
aid = aid,
episodeTitle = episodeTitle,
mediaUrl = mediaUrl,
danEpisodeId = episodeId,
backListeners = backListeners
)
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions app/src/main/kotlin/com/muedsa/agetv/Upscayl.kt

This file was deleted.

98 changes: 98 additions & 0 deletions app/src/main/kotlin/com/muedsa/agetv/screens/AppNavigation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.muedsa.agetv.screens

import androidx.compose.runtime.Composable
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.dialog
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.muedsa.agetv.screens.detail.AnimeDetailScreen
import com.muedsa.agetv.screens.home.HomeNavScreen
import com.muedsa.agetv.screens.setting.AppSettingScreen
import com.muedsa.compose.tv.LocalNavHostControllerProvider
import com.muedsa.compose.tv.LocalRightSideDrawerControllerProvider
import com.muedsa.compose.tv.widget.FullWidthDialogProperties
import com.muedsa.compose.tv.widget.RightSideDrawerWithNavController
import com.muedsa.compose.tv.widget.RightSideDrawerWithNavDrawerContent

@Composable
fun AppNavigation(navController: NavHostController = rememberNavController()) {

val viewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
}

val drawerController =
RightSideDrawerWithNavController(navController, NavigationItems.RightSideDrawer.path)

LocalNavHostControllerProvider(navController) {
LocalRightSideDrawerControllerProvider(drawerController) {
NavHost(
navController = navController,
startDestination = buildRoute(NavigationItems.Home, listOf("0"))
) {

composable(
route = NavigationItems.Home.path,
arguments = listOf(navArgument("tabIndex") {
type = NavType.IntType
})
) {
HomeNavScreen(
tabIndex = checkNotNull(it.arguments?.getInt("tabIndex")),
mainScreenViewModel = hiltViewModel(viewModelStoreOwner),
)
}

composable(
route = NavigationItems.Detail.path,
arguments = listOf(navArgument("animeId") {
type = NavType.StringType
})
) {
AnimeDetailScreen()
}

dialog(
route = NavigationItems.Setting.path,
dialogProperties = FullWidthDialogProperties()
) {
AppSettingScreen()
}

dialog(
route = NavigationItems.RightSideDrawer.path,
dialogProperties = FullWidthDialogProperties()
) {
RightSideDrawerWithNavDrawerContent(controller = drawerController)
}
}
}
}
}

fun buildRoute(
navItem: NavigationItems,
pathParams: List<String>?
): String {
var route = navItem.path
if (!navItem.pathParams.isNullOrEmpty()) {
checkNotNull(pathParams)
check(pathParams.size == navItem.pathParams.size)
for (i in 0 until navItem.pathParams.size) {
route = route.replace(navItem.pathParams[i], pathParams[i])
}
}
return route
}

fun NavHostController.navigate(
navItem: NavigationItems,
pathParams: List<String>?
) {
navigate(buildRoute(navItem, pathParams))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.muedsa.agetv.ui.navigation
package com.muedsa.agetv.screens

sealed class NavigationItems(
val path: String,
Expand All @@ -10,7 +10,5 @@ sealed class NavigationItems(

data object Setting : NavigationItems("setting")

data object NotFound : NavigationItems("not_found")

data object RightSideDrawer : NavigationItems("right_side_drawer")
}
Loading

0 comments on commit f8c24b4

Please sign in to comment.