Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use raamcosta/compose-destinations for navigation and remove fragments #15

Merged
merged 5 commits into from
Sep 11, 2022
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion app/src/main/java/com/jacob/wakatimeapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.jacob.wakatimeapp

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.jacob.wakatimeapp.core.ui.theme.WakaTimeAppTheme
import com.jacob.wakatimeapp.navigation.ApplicationNavigator
import com.jacob.wakatimeapp.navigation.NavGraphs
import com.ramcosta.composedestinations.DestinationsNavHost
import com.ramcosta.composedestinations.navigation.dependency
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -11,6 +17,15 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)

installSplashScreen()
setContentView(R.layout.activity_main)
setContent {
WakaTimeAppTheme {
DestinationsNavHost(
navGraph = NavGraphs.root,
dependenciesContainerBuilder = {
dependency(ApplicationNavigator(navController))
}
)
}
}
}
}
29 changes: 0 additions & 29 deletions app/src/main/java/com/jacob/wakatimeapp/NavigationModule.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.jacob.wakatimeapp.navigation

import androidx.compose.runtime.Composable
import com.jacob.wakatimeapp.home.ui.HomePageContent
import com.jacob.wakatimeapp.home.ui.HomePageNavigator
import com.jacob.wakatimeapp.login.ui.LoginPageContent
import com.jacob.wakatimeapp.login.ui.LoginPageNavigator
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph

@RootNavGraph(start = true)
@Destination
@Composable
fun LoginPage(loginPageNavigator: LoginPageNavigator) =
LoginPageContent(loginPageNavigator = loginPageNavigator)

@Composable
@Destination
fun HomePage(homePageNavigator: HomePageNavigator) =
HomePageContent(homePageNavigator = homePageNavigator)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.jacob.wakatimeapp.navigation

import androidx.navigation.NavController
import com.jacob.wakatimeapp.home.ui.HomePageNavigator
import com.jacob.wakatimeapp.login.ui.LoginPageNavigator
import com.jacob.wakatimeapp.navigation.destinations.HomePageDestination
import com.ramcosta.composedestinations.navigation.navigate

class ApplicationNavigator(private val navController: NavController) :
HomePageNavigator by HomePageNavigatorImpl(navController),
LoginPageNavigator by LoginPageNavigatorImpl(navController)

class LoginPageNavigatorImpl(private val navController: NavController) : LoginPageNavigator {
override fun toHomePage() = navController.navigate(HomePageDestination)
}

class HomePageNavigatorImpl(private val navController: NavController) : HomePageNavigator {
override fun toDetailsPage() = navController.navigate(HomePageDestination)
}
15 changes: 0 additions & 15 deletions app/src/main/res/layout/activity_main.xml

This file was deleted.

26 changes: 0 additions & 26 deletions app/src/main/res/navigation/nav_graph.xml

This file was deleted.

4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ buildscript {
}
}

plugins {
id("com.google.devtools.ksp") version ("1.7.10-1.0.6") apply false
}

tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ buildscript {

dependencies {
implementation("org.jetbrains.kotlin:kotlin-serialization:1.7.10")
implementation("com.google.devtools.ksp:symbol-processing-api:1.7.10-1.0.6")
}
24 changes: 21 additions & 3 deletions buildSrc/src/main/kotlin/wakatimeapp.android.application.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id("kotlin-android")
id("org.jetbrains.kotlin.kapt")
id("dagger.hilt.android.plugin")
id("com.google.devtools.ksp")
}

android {
Expand Down Expand Up @@ -38,6 +39,18 @@ android {
}

kotlinOptions { jvmTarget = "11" }

buildFeatures { compose = true }
composeOptions { kotlinCompilerExtensionVersion = "1.3.1" }

// Compose Destination config
applicationVariants.all { variant ->
kotlin.sourceSets {
getByName(variant.name) {
kotlin.srcDir("build/generated/ksp/${variant.name}/kotlin")
}
}
}
}

dependencies {
Expand All @@ -46,10 +59,15 @@ dependencies {
implementation("androidx.appcompat:appcompat:1.5.0")
implementation("androidx.legacy:legacy-support-v4:1.0.0")

// Compose
implementation("androidx.compose.ui:ui:1.2.1")
implementation("androidx.compose.material:material:1.2.1")
implementation("androidx.compose.ui:ui-tooling-preview:1.2.1")
implementation("androidx.activity:activity-compose:1.5.1")

// Navigation
implementation("androidx.navigation:navigation-fragment-ktx:2.5.1")
implementation("androidx.navigation:navigation-ui-ktx:2.5.1")
implementation("androidx.navigation:navigation-testing:2.5.1")
implementation("io.github.raamcosta.compose-destinations:core:1.6.19-beta")
ksp("io.github.raamcosta.compose-destinations:ksp:1.6.19-beta")

implementation("androidx.hilt:hilt-navigation-fragment:1.0.0")

Expand Down
10 changes: 2 additions & 8 deletions buildSrc/src/main/kotlin/wakatimeapp.android.feature.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3")

// implementation("com.google.android.material:material:1.6.0")

implementation("androidx.core:core-ktx:1.8.0")
implementation("androidx.appcompat:appcompat:1.5.0")

Expand All @@ -59,16 +57,12 @@ dependencies {
implementation("androidx.compose.ui:ui-tooling-preview:1.2.1")
implementation("androidx.activity:activity-compose:1.5.1")

// Navigation
implementation("androidx.navigation:navigation-ui-ktx:2.5.1")
implementation("androidx.navigation:navigation-fragment-ktx:2.5.1")

implementation("androidx.hilt:hilt-navigation-fragment:1.0.0")

// Hilt
implementation("com.google.dagger:hilt-android:2.43.1")
kapt("com.google.dagger:hilt-android-compiler:2.43.1")

implementation("androidx.hilt:hilt-navigation-compose:1.0.0")

// Retrofit
implementation("com.squareup.retrofit2:retrofit:2.9.0")

Expand Down
64 changes: 16 additions & 48 deletions home/src/main/java/com/jacob/wakatimeapp/home/ui/HomePage.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.jacob.wakatimeapp.home.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
Expand All @@ -14,60 +11,30 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.unit.dp
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import androidx.hilt.navigation.compose.hiltViewModel
import com.jacob.wakatimeapp.core.common.observeInLifecycle
import com.jacob.wakatimeapp.core.ui.R.drawable
import com.jacob.wakatimeapp.core.ui.TimeSpentCard
import com.jacob.wakatimeapp.core.ui.TimeSpentCardParameters
import com.jacob.wakatimeapp.core.ui.theme.Gradients
import com.jacob.wakatimeapp.core.ui.theme.WakaTimeAppTheme
import com.jacob.wakatimeapp.home.ui.components.*
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject
import timber.log.Timber

@ExperimentalCoroutinesApi
@AndroidEntryPoint
class HomePage : Fragment() {
private val viewModel by viewModels<HomePageViewModel>()

@Inject
lateinit var homePageNavigations: HomePageNavigations

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
) = ComposeView(requireContext()).apply {
setContent {
WakaTimeAppTheme {
HomePageContent(
HomePageParameters(
viewModel = viewModel,
navController = findNavController()
),
homePageNavigations = homePageNavigations
)
}
}
}
}

@ExperimentalCoroutinesApi
@Composable
private fun HomePageContent(parameters: HomePageParameters, homePageNavigations: HomePageNavigations) {
fun HomePageContent(
viewModel: HomePageViewModel = hiltViewModel(),
homePageNavigator: HomePageNavigator,
) {
Timber.e("Loaded")
val scaffoldState = rememberScaffoldState()
val snackBarCoroutineScope = rememberCoroutineScope()
val viewState by parameters.viewModel.homePageState.collectAsState()
val viewState by viewModel.homePageState.collectAsState()

parameters.viewModel.errors
viewModel.errors
.onEach {
snackBarCoroutineScope.launch {
scaffoldState.snackbarHostState.showSnackbar(
Expand All @@ -88,9 +55,9 @@ private fun HomePageContent(parameters: HomePageParameters, homePageNavigations:
is HomePageViewState.Loaded -> HomePageLoaded(
HomePageLoadedParameters(
homePageViewState = viewState as HomePageViewState.Loaded,
navController = parameters.navController
navigator = homePageNavigator
),
homePageNavigations = homePageNavigations,
homePageNavigator = homePageNavigator,
)

is HomePageViewState.Error -> HomePageError(HomePageErrorParameters(viewState as HomePageViewState.Error))
Expand All @@ -99,7 +66,10 @@ private fun HomePageContent(parameters: HomePageParameters, homePageNavigations:
}

@Composable
private fun HomePageLoaded(parameters: HomePageLoadedParameters, homePageNavigations: HomePageNavigations) {
private fun HomePageLoaded(
parameters: HomePageLoadedParameters,
homePageNavigator: HomePageNavigator,
) {
val scrollState = rememberScrollState()
Column(
modifier = Modifier
Expand All @@ -117,9 +87,7 @@ private fun HomePageLoaded(parameters: HomePageLoadedParameters, homePageNavigat
iconId = drawable.ic_time,
mainText = "Total Time Spent Today",
time = parameters.homePageViewState.contentData.todaysStats.timeSpent,
onClick = {
parameters.navController.navigate(homePageNavigations.toDetailsPage())
}
onClick = homePageNavigator::toDetailsPage
)
)
Spacer(modifier = Modifier.height(25.dp))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.jacob.wakatimeapp.home.ui

interface HomePageNavigator {
fun toDetailsPage()
}
Loading