Skip to content

Commit

Permalink
[chore] Remote Local 레포지토리 통합 및 데이터 소스 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
fbghgus123 committed Jul 6, 2024
1 parent 9d4dd96 commit f99abfd
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.mashup.dorabangs.domain.repository
package com.mashup.dorabangs.data.datasource.local.api

import kotlinx.coroutines.flow.Flow

interface UserDataStoreRepository {
interface UserLocalDataSource {

suspend fun setUserAccessToken(accessToken: String)
fun getUserAccessToken(): Flow<String>

suspend fun setIsFirstEntry(isFirst: Boolean)

fun getUserAccessToken(): Flow<String>

fun getIsFirstEntry(): Flow<Boolean>
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
package com.mashup.dorabangs.data.datasource.local
package com.mashup.dorabangs.data.datasource.local.impl

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.stringPreferencesKey
import com.mashup.dorabangs.data.datasource.local.api.UserLocalDataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import java.io.IOException
import javax.inject.Inject

class UserPreferenceDataSource @Inject constructor(
class UserLocalDataSourceImpl @Inject constructor(
private val dataStore: DataStore<Preferences>,
) {
) : UserLocalDataSource {

suspend fun setUserAccessToken(accessToken: String) {
override suspend fun setUserAccessToken(accessToken: String) {
setDataStore(stringPreferencesKey(ACCESS_TOKEN), accessToken)
}

suspend fun setIsFirstEntry(isFirst: Boolean) {
override suspend fun setIsFirstEntry(isFirst: Boolean) {
setDataStore(booleanPreferencesKey(FIRST_ENTRY), isFirst)
}

fun getUserAccessToken(): Flow<String> {
override fun getUserAccessToken(): Flow<String> {
return getDataStore(stringPreferencesKey(ACCESS_TOKEN), "")
}

fun getIsFirstEntry(): Flow<Boolean> {
override fun getIsFirstEntry(): Flow<Boolean> {
return getDataStore(booleanPreferencesKey(FIRST_ENTRY), false)
}

Expand All @@ -51,6 +52,7 @@ class UserPreferenceDataSource @Inject constructor(
preferences[key] ?: defaultValue
}
}

companion object {
private const val ACCESS_TOKEN = "ACCESS_TOKEN"
private const val FIRST_ENTRY = "FIRST_ENTRY"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mashup.dorabangs.data.datasource.remote.api

import com.mashup.dorabangs.data.model.DeviceTokenDataModel

interface UserRemoteDataSource {

suspend fun registerUser(deviceToken: DeviceTokenDataModel): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mashup.dorabangs.data.datasource.remote.impl

import com.mashup.dorabangs.data.datasource.remote.api.UserRemoteDataSource
import com.mashup.dorabangs.data.model.DeviceTokenDataModel
import com.mashup.dorabangs.data.network.service.UserService
import javax.inject.Inject

class UserRemoteDataSourceImpl @Inject constructor(
private val userService: UserService
) : UserRemoteDataSource {

override suspend fun registerUser(deviceToken: DeviceTokenDataModel): String =
userService.registerUser(deviceToken)
?.data
?.accessToken ?: ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mashup.dorabangs.data.di

import com.mashup.dorabangs.data.datasource.local.api.UserLocalDataSource
import com.mashup.dorabangs.data.datasource.local.impl.UserLocalDataSourceImpl
import com.mashup.dorabangs.data.datasource.remote.api.UserRemoteDataSource
import com.mashup.dorabangs.data.datasource.remote.impl.UserRemoteDataSourceImpl
import dagger.Binds
import dagger.Module
import dagger.Reusable
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
abstract class DataSourceModule {

@Binds
@Reusable
abstract fun bindUserLocalDataSource(
dataSource: UserLocalDataSourceImpl,
): UserLocalDataSource

@Binds
@Reusable
abstract fun bindUserRemoteDataSource(
dataSource: UserRemoteDataSourceImpl,
): UserRemoteDataSource
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.mashup.dorabangs.data.BuildConfig
import com.mashup.dorabangs.domain.usecase.user.GetUserAccessTokenUseCase
import com.mashup.dorabangs.data.datasource.local.api.UserLocalDataSource
import dagger.Module
import dagger.Provides
import dagger.Reusable
Expand Down Expand Up @@ -40,13 +40,13 @@ object NetworkModule {
fun providesDorabangsOkHttpClient(
httpLoggingInterceptor: HttpLoggingInterceptor,
flipperOkhttpInterceptor: FlipperOkhttpInterceptor,
getUserAccessTokenUseCase: GetUserAccessTokenUseCase,
userLocalDataSource: UserLocalDataSource,
): OkHttpClient =
OkHttpClient.Builder().apply {
addInterceptor(
Interceptor { chain ->
val token = runBlocking {
runCatching { getUserAccessTokenUseCase() }.getOrDefault("")
runCatching { userLocalDataSource.getUserAccessToken() }.getOrDefault("")
}
val request = chain.request().newBuilder()
.addHeader(AUTHORIZATION, "Bearer $token")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package com.mashup.dorabangs.data.di

import com.mashup.dorabangs.data.repository.UserDataStoreRepositoryImpl
import com.mashup.dorabangs.data.repository.UserRemoteRepositoryImpl
import com.mashup.dorabangs.domain.repository.UserDataStoreRepository
import com.mashup.dorabangs.domain.repository.UserRemoteRepository
import com.mashup.dorabangs.data.repository.UserRepositoryImpl
import com.mashup.dorabangs.domain.repository.UserRepository
import dagger.Binds
import dagger.Module
import dagger.Reusable
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
@Binds
@Reusable
abstract fun bindUserDataStoreRepository(
userDataStoreRepositoryImpl: UserDataStoreRepositoryImpl,
): UserDataStoreRepository

@Binds
@Reusable
abstract fun bindUserRemoteRepository(
userRemoteRepositoryImpl: UserRemoteRepositoryImpl,
): UserRemoteRepository
abstract fun bindUserRepository(
repositoryImpl: UserRepositoryImpl,
): UserRepository
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package com.mashup.dorabangs.data.network.service

import com.mashup.dorabangs.data.model.DeviceTokenDataModel
import com.mashup.dorabangs.data.model.UserAccessTokenResponseModel
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.POST

interface UserService {

@POST("users")
suspend fun registerUser(@Body deviceToken: DeviceTokenDataModel): Response<UserAccessTokenResponseModel>
suspend fun registerUser(@Body deviceToken: DeviceTokenDataModel): UserAccessTokenResponseModel
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mashup.dorabangs.data.repository

import com.mashup.dorabangs.data.datasource.local.api.UserLocalDataSource
import com.mashup.dorabangs.data.datasource.remote.api.UserRemoteDataSource
import com.mashup.dorabangs.data.model.asData
import com.mashup.dorabangs.domain.model.DeviceToken
import com.mashup.dorabangs.domain.repository.UserRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class UserRepositoryImpl @Inject constructor(
private val userLocalDataSource: UserLocalDataSource,
private val userRemoteDataSource: UserRemoteDataSource,
) : UserRepository {
override suspend fun setUserAccessToken(accessToken: String) {
userLocalDataSource.setUserAccessToken(accessToken = accessToken)
}

override fun getUserAccessToken(): Flow<String> {
return userLocalDataSource.getUserAccessToken()
}

override suspend fun setIsFirstEntry(isFirst: Boolean) {
userLocalDataSource.setIsFirstEntry(isFirst = isFirst)
}

override fun getIsFirstEntry(): Flow<Boolean> {
return userLocalDataSource.getIsFirstEntry()
}

override suspend fun registerDeviceToken(deviceToken: DeviceToken): String {
return userRemoteDataSource.registerUser(deviceToken.asData())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mashup.dorabangs.domain.repository

import com.mashup.dorabangs.domain.model.DeviceToken
import kotlinx.coroutines.flow.Flow

interface UserRepository {
suspend fun setUserAccessToken(accessToken: String)
fun getUserAccessToken(): Flow<String>
suspend fun setIsFirstEntry(isFirst: Boolean)
fun getIsFirstEntry(): Flow<Boolean>
suspend fun registerDeviceToken(deviceToken: DeviceToken): String
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.mashup.dorabangs.domain.usecase.user

import com.mashup.dorabangs.domain.repository.UserDataStoreRepository
import com.mashup.dorabangs.domain.repository.UserRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetIsFirstEntryUseCase @Inject constructor(
private val userDataStoreRepository: UserDataStoreRepository,
private val userRepository: UserRepository,
) {
operator fun invoke(): Flow<Boolean> {
return userDataStoreRepository.getIsFirstEntry()
return userRepository.getIsFirstEntry()
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.mashup.dorabangs.domain.usecase.user

import com.mashup.dorabangs.domain.repository.UserDataStoreRepository
import com.mashup.dorabangs.domain.repository.UserRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetUserAccessTokenUseCase @Inject constructor(
private val userDataStoreRepository: UserDataStoreRepository,
private val userRepository: UserRepository,
) {
operator fun invoke(): Flow<String> {
return userDataStoreRepository.getUserAccessToken()
return userRepository.getUserAccessToken()
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.mashup.dorabangs.domain.usecase.user

import com.mashup.dorabangs.domain.model.DeviceToken
import com.mashup.dorabangs.domain.repository.UserRemoteRepository
import com.mashup.dorabangs.domain.repository.UserRepository
import javax.inject.Inject

class RegisterUserUseCase @Inject constructor(
private val userRemoteRepository: UserRemoteRepository,
private val userRepository: UserRepository,
) {

suspend operator fun invoke(deviceToken: DeviceToken): String {
return userRemoteRepository.registerUser(deviceToken)
return userRepository.registerDeviceToken(deviceToken)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.mashup.dorabangs.domain.usecase.user

import com.mashup.dorabangs.domain.repository.UserDataStoreRepository
import com.mashup.dorabangs.domain.repository.UserRepository
import javax.inject.Inject

class SetIsFirstEntryUseCase @Inject constructor(
private val userDataStoreRepository: UserDataStoreRepository,
private val userRepository: UserRepository,
) {
suspend operator fun invoke(isFirst: Boolean) {
userDataStoreRepository.setIsFirstEntry(isFirst = isFirst)
userRepository.setIsFirstEntry(isFirst = isFirst)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.mashup.dorabangs.domain.usecase.user

import com.mashup.dorabangs.domain.repository.UserDataStoreRepository
import com.mashup.dorabangs.domain.repository.UserRepository
import javax.inject.Inject

class SetUserAccessTokenUseCase @Inject constructor(
private val userDataStoreRepository: UserDataStoreRepository,
private val userRepository: UserRepository,
) {
suspend operator fun invoke(accessToken: String) {
userDataStoreRepository.setUserAccessToken(accessToken = accessToken)
userRepository.setUserAccessToken(accessToken = accessToken)
}
}

0 comments on commit f99abfd

Please sign in to comment.