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

Feature/#33 coroutine dispatchers di #52

Merged
merged 6 commits into from
Dec 12, 2023
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
1 change: 1 addition & 0 deletions app-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ android {
dependencies {
implementation(projects.presentation)

implementation(projects.core.android)
implementation(projects.core.model)
implementation(projects.core.common)
implementation(projects.core.network)
Expand Down
1 change: 1 addition & 0 deletions core/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
9 changes: 9 additions & 0 deletions core/android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.suwiki.android.library)
alias(libs.plugins.suwiki.android.hilt)
}

android {
namespace = "com.suwiki.core.android"
}
Empty file added core/android/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions core/android/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
4 changes: 4 additions & 0 deletions core/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
10 changes: 10 additions & 0 deletions core/android/src/main/java/com/suwiki/core/android/Dispatcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.suwiki.core.android

import javax.inject.Qualifier

@Qualifier
annotation class Dispatcher(val suwikiDispatcher: SuwikiDispatchers)

enum class SuwikiDispatchers {
IO,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.suwiki.core.android

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

@Module
@InstallIn(SingletonComponent::class)
object DispatchersModule {
@Provides
@Dispatcher(SuwikiDispatchers.IO)
fun providesIODispatcher(): CoroutineDispatcher = Dispatchers.IO
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton

@Module
Expand All @@ -33,7 +30,6 @@ object DataStoreModule {
): DataStore<UserPreference> =
DataStoreFactory.create(
serializer = userPreferenceSerializer,
scope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
) {
context.dataStoreFile("user_preference.pb")
}
Expand All @@ -47,7 +43,6 @@ object DataStoreModule {
corruptionHandler = ReplaceFileCorruptionHandler(
produceNewData = { emptyPreferences() },
),
scope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
produceFile = { applicationContext.preferencesDataStoreFile("suwiki-preference") },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import tech.thdev.useful.encrypted.data.store.preferences.security.generateUsefulSecurity
import javax.inject.Singleton

Expand All @@ -33,7 +30,6 @@ object DataStoreModule {
corruptionHandler = ReplaceFileCorruptionHandler(
produceNewData = { emptyPreferences() },
),
scope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
produceFile = { applicationContext.preferencesDataStoreFile("security-preference") },
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import com.suwiki.core.model.openmajor.OpenMajor
import com.suwiki.data.openmajor.datasource.LocalOpenMajorDataSource
import com.suwiki.data.openmajor.datasource.RemoteOpenMajorDataSource
import com.suwiki.domain.openmajor.repository.OpenMajorRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

class OpenMajorRepositoryImpl @Inject constructor(
Expand Down Expand Up @@ -36,7 +34,7 @@ class OpenMajorRepositoryImpl @Inject constructor(
setLocalOpenMajorVersion(remoteVersion)
}
}
}.flowOn(Dispatchers.IO)
}

override suspend fun getBookmarkedOpenMajorList(): List<String> {
return remoteOpenMajorDataSource.getBookmarkedMajorList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import com.suwiki.core.model.timetable.OpenLecture
import com.suwiki.data.timetable.datasource.LocalOpenLectureDatasource
import com.suwiki.data.timetable.datasource.RemoteOpenLectureDataSource
import com.suwiki.domain.timetable.repository.OpenLectureRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject

class OpenLectureRepositoryImpl @Inject constructor(
Expand All @@ -34,5 +32,5 @@ class OpenLectureRepositoryImpl @Inject constructor(
setOpenLectureListVersion(remoteVersion)
}
}
}.flowOn(Dispatchers.IO)
}
}
4 changes: 3 additions & 1 deletion local/openmajor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ android {

dependencies {
implementation(projects.core.model)
implementation(projects.data.openmajor)
implementation(projects.core.android)
implementation(projects.core.database)

implementation(projects.data.openmajor)

ksp(libs.room.compiler)
implementation(libs.room.runtime)
implementation(libs.room.ktx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.floatPreferencesKey
import com.suwiki.core.android.Dispatcher
import com.suwiki.core.android.SuwikiDispatchers
import com.suwiki.core.database.database.OpenMajorDatabase
import com.suwiki.core.database.di.NormalDataStore
import com.suwiki.core.model.openmajor.OpenMajor
import com.suwiki.data.openmajor.datasource.LocalOpenMajorDataSource
import com.suwiki.local.openmajor.converter.toEntity
import com.suwiki.local.openmajor.converter.toModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import javax.inject.Inject

class LocalOpenMajorDataSourceImpl @Inject constructor(
@NormalDataStore private val dataStore: DataStore<Preferences>,
@Dispatcher(SuwikiDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
private val db: OpenMajorDatabase,
) : LocalOpenMajorDataSource {

Expand All @@ -33,15 +38,15 @@ class LocalOpenMajorDataSourceImpl @Inject constructor(
dataStore.edit { it[OPEN_MAJOR_VERSION] = version }
}

override suspend fun getLocalOpenMajorList(): List<OpenMajor> {
return db.openMajorDao().getAll().map { it.toModel() }
override suspend fun getLocalOpenMajorList(): List<OpenMajor> = withContext(ioDispatcher) {
db.openMajorDao().getAll().map { it.toModel() }
}

override suspend fun saveAllOpenMajors(majors: List<OpenMajor>) {
override suspend fun saveAllOpenMajors(majors: List<OpenMajor>) = withContext(ioDispatcher) {
db.openMajorDao().insertAll(majors.map { it.toEntity() })
}

override suspend fun deleteAllOpenMajors() {
override suspend fun deleteAllOpenMajors() = withContext(ioDispatcher) {
db.openMajorDao().deleteAll()
}
}
4 changes: 3 additions & 1 deletion local/timetable/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ android {

dependencies {
implementation(projects.core.model)
implementation(projects.data.timetable)
implementation(projects.core.android)
implementation(projects.core.database)

implementation(projects.data.timetable)

ksp(libs.room.compiler)
implementation(libs.room.runtime)
implementation(libs.room.ktx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.longPreferencesKey
import com.suwiki.core.android.Dispatcher
import com.suwiki.core.android.SuwikiDispatchers
import com.suwiki.core.database.database.OpenLectureDatabase
import com.suwiki.core.database.di.NormalDataStore
import com.suwiki.core.model.timetable.OpenLecture
import com.suwiki.data.timetable.datasource.LocalOpenLectureDatasource
import com.suwiki.local.timetable.converter.toEntity
import com.suwiki.local.timetable.converter.toModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import javax.inject.Inject

class LocalOpenLectureDatasourceImpl @Inject constructor(
@NormalDataStore private val dataStore: DataStore<Preferences>,
@Dispatcher(SuwikiDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
private val openLectureDatabase: OpenLectureDatabase,
) : LocalOpenLectureDatasource {

Expand All @@ -34,15 +39,15 @@ class LocalOpenLectureDatasourceImpl @Inject constructor(
return data.map { it[LOCAL_OPEN_LECTURE_VERSION] ?: 0L }
}

override suspend fun getOpenLectureList(): List<OpenLecture> {
return openLectureDatabase.openLectureDao().getAll().map { it.toModel() }
override suspend fun getOpenLectureList(): List<OpenLecture> = withContext(ioDispatcher) {
openLectureDatabase.openLectureDao().getAll().map { it.toModel() }
}

override suspend fun insertOpenLecture(data: OpenLecture) {
override suspend fun insertOpenLecture(data: OpenLecture) = withContext(ioDispatcher) {
openLectureDatabase.openLectureDao().insert(data.toEntity())
}

override suspend fun deleteAllOpenLecture() {
override suspend fun deleteAllOpenLecture() = withContext(ioDispatcher) {
openLectureDatabase.openLectureDao().deleteAll()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.longPreferencesKey
import com.suwiki.core.android.Dispatcher
import com.suwiki.core.android.SuwikiDispatchers
import com.suwiki.core.database.database.TimetableDatabase
import com.suwiki.core.database.di.NormalDataStore
import com.suwiki.core.model.timetable.Timetable
import com.suwiki.data.timetable.datasource.LocalTimetableDataSource
import com.suwiki.local.timetable.converter.toEntity
import com.suwiki.local.timetable.converter.toModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import javax.inject.Inject

class LocalTimetableDatasourceImpl @Inject constructor(
@NormalDataStore private val dataStore: DataStore<Preferences>,
@Dispatcher(SuwikiDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
private val timetableDatabase: TimetableDatabase,
) : LocalTimetableDataSource {

Expand All @@ -26,23 +31,23 @@ class LocalTimetableDatasourceImpl @Inject constructor(
private val data: Flow<Preferences>
get() = dataStore.data

override suspend fun getAllTimetable(): List<Timetable> {
return timetableDatabase.timetableDao().getAll().map { it.toModel() }
override suspend fun getAllTimetable(): List<Timetable> = withContext(ioDispatcher) {
timetableDatabase.timetableDao().getAll().map { it.toModel() }
}

override suspend fun getTimetable(createTime: Long): Timetable {
return timetableDatabase.timetableDao().get(createTime).toModel()
override suspend fun getTimetable(createTime: Long): Timetable = withContext(ioDispatcher) {
timetableDatabase.timetableDao().get(createTime).toModel()
}

override suspend fun deleteAllTimetable() {
override suspend fun deleteAllTimetable() = withContext(ioDispatcher) {
timetableDatabase.timetableDao().deleteAll()
}

override suspend fun deleteTimetable(data: Timetable) {
override suspend fun deleteTimetable(data: Timetable) = withContext(ioDispatcher) {
timetableDatabase.timetableDao().delete(data.toEntity())
}

override suspend fun updateTimetable(data: Timetable) {
override suspend fun updateTimetable(data: Timetable) = withContext(ioDispatcher) {
timetableDatabase.timetableDao().update(data.toEntity())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.mangbaam.presentation.ui.theme.UswtimetableTheme
import com.suwiki.domain.user.usecase.LoginUseCase
import com.suwiki.domain.openmajor.usecase.GetOpenMajorListUseCase
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import timber.log.Timber
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

@Inject
lateinit var useCase1: LoginUseCase
lateinit var useCase1: GetOpenMajorListUseCase

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -43,19 +44,10 @@ class MainActivity : ComponentActivity() {
fun Greeting(
name: String,
modifier: Modifier = Modifier,
useCase1: LoginUseCase,
useCase1: GetOpenMajorListUseCase,
) {
LaunchedEffect(key1 = Unit) {
useCase1(
loginId = "pos1070",
password = "1q2w3e4r!",
)
.onSuccess {
Timber.tag("Retrofit2").d("$it")
}
.onFailure {
Timber.tag("Retrofit2").d("$it")
}
useCase1().collect { Timber.d("$it") }
}

Text(
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rootProject.name = "uswtimetable"
include(":presentation")
include(":app-compose")

include(":core:android")
include(":core:model")
include(":core:common")
include(":core:network")
Expand Down