Skip to content

Commit

Permalink
inject clock provider
Browse files Browse the repository at this point in the history
  • Loading branch information
tkhs0604 committed Sep 19, 2023
1 parent d456d3f commit 051d5b8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import io.github.droidkaigi.confsched2023.data.di.AppAndroidBuildConfig
import io.github.droidkaigi.confsched2023.model.BuildConfigProvider
import kotlinx.datetime.Clock
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
Expand All @@ -15,9 +16,19 @@ class AppModule {
@Singleton
@AppAndroidBuildConfig
fun provideBuildConfigProvider(): BuildConfigProvider = AppBuildConfigProvider()

@Provides
@Singleton
fun provideClockProvider(): ClockProvider = object : ClockProvider {
override fun clock(): Clock = Clock.System
}
}

class AppBuildConfigProvider(
override val versionName: String = BuildConfig.VERSION_NAME,
override val debugBuild: Boolean = BuildConfig.DEBUG,
) : BuildConfigProvider

interface ClockProvider {
fun clock(): Clock
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
Expand All @@ -19,12 +20,18 @@ import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.window.layout.DisplayFeature
import androidx.window.layout.WindowInfoTracker
import dagger.hilt.android.AndroidEntryPoint
import io.github.droidkaigi.confsched2023.ui.compositionlocal.LocalClock
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

@Inject
lateinit var clockProvider: ClockProvider

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
Expand Down Expand Up @@ -55,10 +62,12 @@ class MainActivity : ComponentActivity() {
setContent {
val windowSize = calculateWindowSizeClass(this)
val displayFeatures = calculateDisplayFeatures(this)
KaigiApp(
windowSize = windowSize,
displayFeatures = displayFeatures,
)
CompositionLocalProvider(LocalClock provides clockProvider.clock()) {
KaigiApp(
windowSize = windowSize,
displayFeatures = displayFeatures,
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.droidkaigi.confsched2023

import dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import javax.inject.Singleton

@TestInstallIn(
components = [SingletonComponent::class],
replaces = [AppModule::class],
)
@Module
class FakeAppModule {
@Provides
@Singleton
fun provideClockProvider(): ClockProvider = object : ClockProvider {
override fun clock(): Clock = object : Clock {
override fun now() = Instant.parse("2023-09-14T10:00:00.00Z")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.droidkaigi.confsched2023.testing.robot

import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.isRoot
Expand All @@ -26,8 +25,6 @@ import io.github.droidkaigi.confsched2023.sessions.component.TimetableUiTypeChan
import io.github.droidkaigi.confsched2023.sessions.section.TimetableTabTestTag
import io.github.droidkaigi.confsched2023.testing.RobotTestRule
import io.github.droidkaigi.confsched2023.testing.coroutines.runTestWithLogging
import io.github.droidkaigi.confsched2023.ui.compositionlocal.FakeClock
import io.github.droidkaigi.confsched2023.ui.compositionlocal.LocalClock
import kotlinx.coroutines.test.TestDispatcher
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds
Expand All @@ -53,14 +50,12 @@ class TimetableScreenRobot @Inject constructor(

fun setupTimetableScreenContent() {
composeTestRule.setContent {
CompositionLocalProvider(LocalClock provides FakeClock) {
KaigiTheme {
TimetableScreen(
onSearchClick = { },
onTimetableItemClick = { },
onBookmarkIconClick = { },
)
}
KaigiTheme {
TimetableScreen(
onSearchClick = { },
onTimetableItemClick = { },
onBookmarkIconClick = { },
)
}
}
waitUntilIdle()
Expand Down

0 comments on commit 051d5b8

Please sign in to comment.