Skip to content

Commit

Permalink
Sort out with Spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali committed Sep 23, 2024
1 parent e290e4e commit 22cada7
Show file tree
Hide file tree
Showing 185 changed files with 2,783 additions and 2,147 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*.{kt,kts}]
ktlint_function_naming_ignore_when_annotated_with=Composable, Test
30 changes: 30 additions & 0 deletions .github/workflows/spotless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Spotless Check

on: [push, pull_request]

jobs:
spotlessCheck:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'adopt'

- name: Cache Gradle dependencies
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle
- name: Run Spotless Check
run: ./gradlew spotlessCheck
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ dependencies {
implementation Deps.hilt
ksp Deps.hilt_compiler
implementation Deps.gson
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/sample/tmdb/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.sample.tmdb
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class Application : android.app.Application()
class Application : android.app.Application()
43 changes: 26 additions & 17 deletions app/src/main/java/com/sample/tmdb/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.graphics.toArgb
import androidx.core.view.WindowCompat
import com.sample.tmdb.common.ui.theme.AlphaNavigationBar

import com.sample.tmdb.common.ui.theme.AlphaNearOpaque
import com.sample.tmdb.common.ui.theme.TmdbPagingComposeTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -37,29 +35,40 @@ class MainActivity : ComponentActivity() {

@Composable
private fun ChangeSystemBarsTheme(lightTheme: Boolean) {
val statusBarColor = MaterialTheme.colors.background.copy(alpha = AlphaNearOpaque).toArgb()
val statusBarColor =
MaterialTheme.colors.background
.copy(alpha = AlphaNearOpaque)
.toArgb()
val navigationBarColor =
MaterialTheme.colors.background.copy(alpha = AlphaNavigationBar).toArgb()
MaterialTheme.colors.background
.copy(alpha = AlphaNavigationBar)
.toArgb()
LaunchedEffect(lightTheme) {
if (lightTheme) {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.light(
statusBarColor, statusBarColor,
),
navigationBarStyle = SystemBarStyle.light(
navigationBarColor, navigationBarColor,
),
statusBarStyle =
SystemBarStyle.light(
statusBarColor,
statusBarColor,
),
navigationBarStyle =
SystemBarStyle.light(
navigationBarColor,
navigationBarColor,
),
)
} else {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.dark(
statusBarColor
),
navigationBarStyle = SystemBarStyle.dark(
navigationBarColor,
),
statusBarStyle =
SystemBarStyle.dark(
statusBarColor,
),
navigationBarStyle =
SystemBarStyle.dark(
navigationBarColor,
),
)
}
}
}
}
}
105 changes: 61 additions & 44 deletions app/src/main/java/com/sample/tmdb/ui/TMDbApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,19 @@ fun TMDbApp() {
navigateToRoute = appState::navigateToBottomBarRoute,
)
}
}
},
) { innerPaddingModifier ->
val newPadding = PaddingValues(
start = innerPaddingModifier.calculateStartPadding(LocalLayoutDirection.current),
end = innerPaddingModifier.calculateEndPadding(LocalLayoutDirection.current),
top = innerPaddingModifier.calculateTopPadding(),
bottom = 0.dp,
)
val newPadding =
PaddingValues(
start = innerPaddingModifier.calculateStartPadding(LocalLayoutDirection.current),
end = innerPaddingModifier.calculateEndPadding(LocalLayoutDirection.current),
top = innerPaddingModifier.calculateTopPadding(),
bottom = 0.dp,
)
NavHost(
navController = appState.navController,
startDestination = MainDestinations.HOME_ROUTE,
modifier = Modifier.padding(newPadding)
modifier = Modifier.padding(newPadding),
) {
navigationScreens(appState.navController)
detailScreens(appState.navController)
Expand All @@ -112,16 +113,16 @@ fun TMDbApp() {
private fun TMDbBottomBar(
tabs: Array<HomeSections>,
currentRoute: String,
navigateToRoute: (String) -> Unit
navigateToRoute: (String) -> Unit,
) {
val currentSection = tabs.first { it.route == currentRoute }

Box(
Modifier.navigationBarsPadding()
Modifier.navigationBarsPadding(),
) {
BottomNavigation(
backgroundColor = MaterialTheme.colors.background.copy(alpha = AlphaNavigationBar),
elevation = TMDb_0_dp
elevation = TMDb_0_dp,
) {
tabs.forEach { section ->
val selected = section == currentSection
Expand All @@ -132,13 +133,13 @@ private fun TMDbBottomBar(
icon = {
Icon(
imageVector = if (selected) section.selectedIcon else section.unselectedIcon,
contentDescription = stringResource(id = section.title)
contentDescription = stringResource(id = section.title),
)
},
selected = selected,
unselectedContentColor = MaterialTheme.colors.onBackground.copy(alpha = ContentAlpha.disabled),
selectedContentColor = MaterialTheme.colors.onBackground,
onClick = { navigateToRoute(section.route) }
onClick = { navigateToRoute(section.route) },
)
}
}
Expand All @@ -148,7 +149,7 @@ private fun TMDbBottomBar(
private fun NavGraphBuilder.navigationScreens(navController: NavController) {
navigation(
route = MainDestinations.HOME_ROUTE,
startDestination = HomeSections.MOVIE_SECTION.route
startDestination = HomeSections.MOVIE_SECTION.route,
) {
composable(route = HomeSections.MOVIE_SECTION.route) {
MovieFeedScreen(navController = navController)
Expand All @@ -168,15 +169,19 @@ private fun NavGraphBuilder.navigationScreens(navController: NavController) {
private fun NavGraphBuilder.detailScreens(navController: NavController) {
composable(
route = "${MainDestinations.TMDB_MOVIE_DETAIL_ROUTE}/{${MainDestinations.TMDB_ID_KEY}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_ID_KEY) { type = NavType.IntType })
arguments =
listOf(
navArgument(MainDestinations.TMDB_ID_KEY) { type = NavType.IntType },
),
) {
MovieDetailScreen(navController)
}
composable(
route = "${MainDestinations.TMDB_TV_SHOW_DETAIL_ROUTE}/{${MainDestinations.TMDB_ID_KEY}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_ID_KEY) { type = NavType.IntType })
arguments =
listOf(
navArgument(MainDestinations.TMDB_ID_KEY) { type = NavType.IntType },
),
) {
TVShowDetailScreen(navController)
}
Expand All @@ -203,8 +208,10 @@ private fun NavGraphBuilder.moviePagingScreens(navController: NavController) {
}
composable(
route = "${MainDestinations.TMDB_SIMILAR_MOVIES_ROUTE}/{${MainDestinations.TMDB_SIMILAR_ID}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_SIMILAR_ID) { type = NavType.IntType })
arguments =
listOf(
navArgument(MainDestinations.TMDB_SIMILAR_ID) { type = NavType.IntType },
),
) {
SimilarMovieScreen(navController = navController)
}
Expand All @@ -231,8 +238,10 @@ private fun NavGraphBuilder.tvShowPagingScreens(navController: NavController) {
}
composable(
route = "${MainDestinations.TMDB_SIMILAR_TV_SHOW_ROUTE}/{${MainDestinations.TMDB_SIMILAR_ID}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_SIMILAR_ID) { type = NavType.IntType })
arguments =
listOf(
navArgument(MainDestinations.TMDB_SIMILAR_ID) { type = NavType.IntType },
),
) {
SimilarTVShowScreen(navController = navController)
}
Expand All @@ -250,39 +259,45 @@ private fun NavGraphBuilder.searchScreens(navController: NavController) {
private fun NavGraphBuilder.creditScreens(navController: NavController) {
composable(
route = "${MainDestinations.TMDB_CAST_ROUTE}/{${MainDestinations.TMDB_CREDIT_KEY}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_CREDIT_KEY) { type = NavType.StringType })
arguments =
listOf(
navArgument(MainDestinations.TMDB_CREDIT_KEY) { type = NavType.StringType },
),
) { from ->
CreditScreen(
R.string.cast,
navController,
gson.fromJson<List<Cast>>(
from.arguments?.getString(MainDestinations.TMDB_CREDIT_KEY),
object : TypeToken<List<Cast>>() {}.type
)
object : TypeToken<List<Cast>>() {}.type,
),
)
}
composable(
route = "${MainDestinations.TMDB_CREW_ROUTE}/{${MainDestinations.TMDB_CREDIT_KEY}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_CREDIT_KEY) { type = NavType.StringType })
arguments =
listOf(
navArgument(MainDestinations.TMDB_CREDIT_KEY) { type = NavType.StringType },
),
) { from ->
CreditScreen(
R.string.crew,
navController,
gson.fromJson<List<Crew>>(
from.arguments?.getString(MainDestinations.TMDB_CREDIT_KEY),
object : TypeToken<List<Crew>>() {}.type
)
object : TypeToken<List<Crew>>() {}.type,
),
)
}
}

private fun NavGraphBuilder.personScreen(navController: NavController) {
composable(
route = "${MainDestinations.TMDB_PERSON_ROUTE}/{${MainDestinations.TMDB_PERSON_KEY}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_PERSON_KEY) { type = NavType.StringType })
arguments =
listOf(
navArgument(MainDestinations.TMDB_PERSON_KEY) { type = NavType.StringType },
),
) {
PersonScreen({ navController.navigateUp() })
}
Expand All @@ -291,17 +306,19 @@ private fun NavGraphBuilder.personScreen(navController: NavController) {
private fun NavGraphBuilder.imagesScreen() {
composable(
route = "${MainDestinations.TMDB_IMAGES_ROUTE}/{${MainDestinations.TMDB_IMAGES_KEY}}/{${MainDestinations.TMDB_IMAGE_PAGE}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_IMAGES_KEY) { type = NavType.StringType },
navArgument(MainDestinations.TMDB_IMAGE_PAGE) { type = NavType.IntType }
)
arguments =
listOf(
navArgument(MainDestinations.TMDB_IMAGES_KEY) { type = NavType.StringType },
navArgument(MainDestinations.TMDB_IMAGE_PAGE) { type = NavType.IntType },
),
) { from ->
ImagesScreen(
images = gson.fromJson(
from.arguments?.getString(MainDestinations.TMDB_IMAGES_KEY),
object : TypeToken<List<TMDbImage>>() {}.type
),
initialPage = from.arguments?.getInt(MainDestinations.TMDB_IMAGE_PAGE)!!
images =
gson.fromJson(
from.arguments?.getString(MainDestinations.TMDB_IMAGES_KEY),
object : TypeToken<List<TMDbImage>>() {}.type,
),
initialPage = from.arguments?.getInt(MainDestinations.TMDB_IMAGE_PAGE)!!,
)
}
}
Expand All @@ -310,12 +327,12 @@ enum class HomeSections(
val route: String,
@StringRes val title: Int,
val unselectedIcon: ImageVector,
val selectedIcon: ImageVector
val selectedIcon: ImageVector,
) {
MOVIE_SECTION("Movie", R.string.movie, Icons.Outlined.Movie, Icons.Filled.Movie),
TV_SHOW_SECTION("TVShow", R.string.tv_show, Icons.Outlined.Tv, Icons.Filled.Tv),
BOOKMARK_SECTION("Bookmark", R.string.favorite, Icons.Outlined.Favorite, Icons.Filled.Favorite),
SETTING_SECTION("Setting", R.string.setting, Icons.Outlined.Settings, Icons.Filled.Settings)
SETTING_SECTION("Setting", R.string.setting, Icons.Outlined.Settings, Icons.Filled.Settings),
}

private val gson = Gson()
private val gson = Gson()
19 changes: 9 additions & 10 deletions app/src/main/java/com/sample/tmdb/ui/TMDbAppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController


/**
* Remembers and creates an instance of [TMDbAppState]
*/
@Composable
fun rememberTMDbAppState(
navController: NavHostController = rememberNavController(),
) =
fun rememberTMDbAppState(navController: NavHostController = rememberNavController()) =
remember(navController) {
TMDbAppState(navController)
}
Expand All @@ -25,7 +22,6 @@ fun rememberTMDbAppState(
class TMDbAppState(
val navController: NavHostController,
) {

// ----------------------------------------------------------
// BottomBar state source of truth
// ----------------------------------------------------------
Expand All @@ -36,8 +32,12 @@ class TMDbAppState(
// Reading this attribute will cause recompositions when the bottom bar needs shown, or not.
// Not all routes need to show the bottom bar.
val shouldShowBottomBar: Boolean
@Composable get() = navController
.currentBackStackEntryAsState().value?.destination?.route in bottomBarRoutes
@Composable get() =
navController
.currentBackStackEntryAsState()
.value
?.destination
?.route in bottomBarRoutes

// ----------------------------------------------------------
// Navigation state source of truth
Expand Down Expand Up @@ -69,6 +69,5 @@ private val NavGraph.startDestination: NavDestination?
*
* https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigation/navigation-ui/src/main/java/androidx/navigation/ui/NavigationUI.kt
*/
private tailrec fun findStartDestination(graph: NavDestination): NavDestination {
return if (graph is NavGraph) findStartDestination(graph.startDestination!!) else graph
}
private tailrec fun findStartDestination(graph: NavDestination): NavDestination =
if (graph is NavGraph) findStartDestination(graph.startDestination!!) else graph
Loading

0 comments on commit 22cada7

Please sign in to comment.