Inject network monitor and time monitor correctly

pull/2064/head
lihenggui 2 years ago
parent 2ec61cf6ac
commit a8aaeee69f

@ -61,7 +61,7 @@ internal val appModules = module {
*commonModule().toTypedArray(),
AnalyticsModule().module,
*databaseModule().toTypedArray(),
*dataModule().toTypedArray(),
dataModule,
dataStoreModule(),
*networkModule().toTypedArray(),
)

@ -16,13 +16,14 @@
package com.google.samples.apps.nowinandroid.core.di
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import org.koin.core.qualifier.named
import org.koin.dsl.module
val coroutineDispatcherModule = module {
single(named("IoDispatcher")) { Dispatchers.IO }
single(named("DefaultDispatcher")) { Dispatchers.Default }
single(named("MainDispatcher")) { Dispatchers.Main }
single<CoroutineDispatcher> { Dispatchers.IO }
single<CoroutineDispatcher> (named("DefaultDispatcher")) { Dispatchers.Default }
single<CoroutineDispatcher> (named("MainDispatcher")) { Dispatchers.Main }
}

@ -32,8 +32,8 @@ import org.koin.core.qualifier.named
import org.koin.dsl.module
val testDataModule = module {
single<TopicsRepository> { FakeTopicsRepository(get(named("IoDispatcher")), get()) }
single<NewsRepository> { FakeNewsRepository(get(named("IoDispatcher")), get()) }
single<TopicsRepository> { FakeTopicsRepository(get(), get()) }
single<NewsRepository> { FakeNewsRepository(get(), get()) }
single<UserDataRepository> { FakeUserDataRepository(get()) }
single<RecentSearchRepository> { FakeRecentSearchRepository() }
single<SearchContentsRepository> { FakeSearchContentsRepository() }

@ -16,18 +16,18 @@
package com.google.samples.apps.nowinandroid.core.data.di
import android.content.Context
import com.google.samples.apps.nowinandroid.core.data.util.ConnectivityManagerNetworkMonitor
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
import kotlinx.coroutines.CoroutineDispatcher
import org.koin.core.annotation.Singleton
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor
import com.google.samples.apps.nowinandroid.core.data.util.ConnectivityManagerNetworkMonitor
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneBroadcastMonitor
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.bind
import org.koin.dsl.module
@Singleton
actual class NetworkMonitorProvider(
private val context: Context,
private val dispatcher: CoroutineDispatcher,
) {
actual fun provideNetworkMonitor(): NetworkMonitor {
return ConnectivityManagerNetworkMonitor(context, dispatcher)
}
internal actual val networkMonitorModule = module {
singleOf(::ConnectivityManagerNetworkMonitor) bind NetworkMonitor::class
}
internal actual val timeZoneMonitorModule = module {
singleOf(::TimeZoneBroadcastMonitor) bind TimeZoneMonitor::class
}

@ -1,34 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.data.di
import android.content.Context
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneBroadcastMonitor
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import org.koin.core.annotation.Singleton
@Singleton
actual class TimeZoneMonitorProvider(
private val context: Context,
private val coroutineScope: CoroutineScope,
private val dispatcher: CoroutineDispatcher,
) {
actual fun provideTimeZoneMonitor(): TimeZoneMonitor =
TimeZoneBroadcastMonitor(context, coroutineScope, dispatcher)
}

@ -28,13 +28,10 @@ import com.google.samples.apps.nowinandroid.core.data.repository.SearchContentsR
import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepository
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
import com.google.samples.apps.nowinandroid.core.data.repository.UserNewsResourceRepository
import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module
import org.koin.core.module.Module
import org.koin.core.module.dsl.singleOf
import org.koin.core.qualifier.named
import org.koin.dsl.bind
import org.koin.dsl.module
import org.koin.ksp.generated.module
internal val repositoryModule = module {
singleOf(::OfflineFirstTopicsRepository) bind TopicsRepository::class
@ -42,38 +39,16 @@ internal val repositoryModule = module {
singleOf(::OfflineFirstUserDataRepository) bind UserDataRepository::class
singleOf(::DefaultRecentSearchRepository) bind RecentSearchRepository::class
singleOf(::CompositeUserNewsResourceRepository) bind UserNewsResourceRepository::class
single {
DefaultSearchContentsRepository(
get(),
get(),
get(),
get(),
get(named("IoDispatcher")),
)
} bind SearchContentsRepository::class
singleOf(::DefaultSearchContentsRepository) bind SearchContentsRepository::class
}
internal val networkMonitorModule = module {
single {
val networkMonitorProvider: NetworkMonitorProvider = get()
networkMonitorProvider.provideNetworkMonitor()
}
}
internal expect val networkMonitorModule: Module
internal expect val timeZoneMonitorModule: Module
internal val timeZoneMonitorProviderModule = module {
single {
val timeZoneMonitorProvider: TimeZoneMonitorProvider = get()
timeZoneMonitorProvider.provideTimeZoneMonitor()
}
val dataModule: Module get() = module {
includes(
repositoryModule,
networkMonitorModule,
timeZoneMonitorModule,
)
}
fun dataModule() = listOf(
DataModule().module,
repositoryModule,
networkMonitorModule,
timeZoneMonitorProviderModule,
)
@Module
@ComponentScan
class DataModule

@ -1,23 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.data.di
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
expect class NetworkMonitorProvider {
fun provideNetworkMonitor(): NetworkMonitor
}

@ -1,23 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.data.di
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor
expect class TimeZoneMonitorProvider {
fun provideTimeZoneMonitor(): TimeZoneMonitor
}

@ -16,16 +16,27 @@
package com.google.samples.apps.nowinandroid.core.data.di
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.datetime.TimeZone
import org.koin.core.annotation.Singleton
import org.koin.dsl.module
@Singleton
actual class TimeZoneMonitorProvider {
actual fun provideTimeZoneMonitor(): TimeZoneMonitor = object : TimeZoneMonitor {
override val currentTimeZone: Flow<TimeZone>
get() = flowOf(TimeZone.UTC)
internal actual val networkMonitorModule = module {
single<NetworkMonitor> {
object : NetworkMonitor {
override val isOnline: Flow<Boolean>
get() = flowOf(true)
}
}
}
internal actual val timeZoneMonitorModule = module {
single<TimeZoneMonitor> {
object : TimeZoneMonitor {
override val currentTimeZone: Flow<TimeZone>
get() = flowOf(TimeZone.UTC)
}
}
}

@ -1,30 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.data.di
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import org.koin.core.annotation.Singleton
@Singleton
actual class NetworkMonitorProvider {
actual fun provideNetworkMonitor(): NetworkMonitor = object : NetworkMonitor {
override val isOnline: Flow<Boolean>
get() = flowOf(true)
}
}

@ -16,16 +16,27 @@
package com.google.samples.apps.nowinandroid.core.data.di
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.datetime.TimeZone
import org.koin.core.annotation.Singleton
import org.koin.dsl.module
@Singleton
actual class TimeZoneMonitorProvider {
actual fun provideTimeZoneMonitor(): TimeZoneMonitor = object : TimeZoneMonitor {
override val currentTimeZone: Flow<TimeZone>
get() = flowOf(TimeZone.UTC)
internal actual val networkMonitorModule = module {
single<NetworkMonitor> {
object : NetworkMonitor {
override val isOnline: Flow<Boolean>
get() = flowOf(true)
}
}
}
internal actual val timeZoneMonitorModule = module {
single<TimeZoneMonitor> {
object : TimeZoneMonitor {
override val currentTimeZone: Flow<TimeZone>
get() = flowOf(TimeZone.UTC)
}
}
}

@ -1,30 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.data.di
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import org.koin.core.annotation.Singleton
@Singleton
actual class NetworkMonitorProvider {
actual fun provideNetworkMonitor(): NetworkMonitor = object : NetworkMonitor {
override val isOnline: Flow<Boolean>
get() = flowOf(true)
}
}

@ -44,18 +44,18 @@ internal val driverModule = module {
internal val daoModule = module {
single { (driver: SqlDriver) -> NiaDatabase(driver) }
factory { (database: NiaDatabase) -> TopicDao(database, get(named("IoDispatcher"))) }
factory { (database: NiaDatabase) -> TopicDao(database, get()) }
factory { (database: NiaDatabase) -> NewsResourceDao(database, get(named("IoDispatcher"))) }
factory { (database: NiaDatabase) -> NewsResourceDao(database, get()) }
factory { (database: NiaDatabase) -> TopicFtsDao(database, get(named("IoDispatcher"))) }
factory { (database: NiaDatabase) -> TopicFtsDao(database, get()) }
factory { (database: NiaDatabase) -> NewsResourceFtsDao(database, get(named("IoDispatcher"))) }
factory { (database: NiaDatabase) -> NewsResourceFtsDao(database, get()) }
factory { (database: NiaDatabase) ->
RecentSearchQueryDao(
database,
get(named("IoDispatcher")),
get(),
)
}
}

@ -26,7 +26,7 @@ fun dataStoreModule() = module {
single {
NiaPreferencesDataSource(
settings = get(),
dispatcher = get(named("IoDispatcher")),
dispatcher = get(),
)
}
}

Loading…
Cancel
Save