Skip to content

Commit

Permalink
refactor/#33: Dispatcher 하드 코딩 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
jinukeu committed Dec 12, 2023
1 parent 34699e7 commit 8fd5111
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 22 deletions.
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
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 @@ -34,5 +34,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
2 changes: 1 addition & 1 deletion 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 Expand Up @@ -60,4 +61,3 @@ include(":domain:lectureevaluation:my")
include(":domain:lectureevaluation:editor")
include(":domain:timetable")
include(":domain:notice")
include(":core:android")

0 comments on commit 8fd5111

Please sign in to comment.