diff --git a/domain/src/main/kotlin/com/mashup/dorabangs/domain/repository/UserDataStoreRepository.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/local/api/UserLocalDataSource.kt similarity index 71% rename from domain/src/main/kotlin/com/mashup/dorabangs/domain/repository/UserDataStoreRepository.kt rename to data/src/main/kotlin/com/mashup/dorabangs/data/datasource/local/api/UserLocalDataSource.kt index e3f85a39..9dada452 100644 --- a/domain/src/main/kotlin/com/mashup/dorabangs/domain/repository/UserDataStoreRepository.kt +++ b/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/local/api/UserLocalDataSource.kt @@ -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 + suspend fun setIsFirstEntry(isFirst: Boolean) + + fun getUserAccessToken(): Flow + fun getIsFirstEntry(): Flow -} +} \ No newline at end of file diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/local/UserPreferenceDataSource.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/local/impl/UserLocalDataSourceImpl.kt similarity index 78% rename from data/src/main/kotlin/com/mashup/dorabangs/data/datasource/local/UserPreferenceDataSource.kt rename to data/src/main/kotlin/com/mashup/dorabangs/data/datasource/local/impl/UserLocalDataSourceImpl.kt index f841518e..43a6a3f5 100644 --- a/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/local/UserPreferenceDataSource.kt +++ b/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/local/impl/UserLocalDataSourceImpl.kt @@ -1,4 +1,4 @@ -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 @@ -6,29 +6,30 @@ 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, -) { +) : 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 { + override fun getUserAccessToken(): Flow { return getDataStore(stringPreferencesKey(ACCESS_TOKEN), "") } - fun getIsFirstEntry(): Flow { + override fun getIsFirstEntry(): Flow { return getDataStore(booleanPreferencesKey(FIRST_ENTRY), false) } @@ -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" diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/remote/api/UserRemoteDataSource.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/remote/api/UserRemoteDataSource.kt new file mode 100644 index 00000000..d61d56f8 --- /dev/null +++ b/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/remote/api/UserRemoteDataSource.kt @@ -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 +} \ No newline at end of file diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/remote/impl/UserRemoteDataSourceImpl.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/remote/impl/UserRemoteDataSourceImpl.kt new file mode 100644 index 00000000..d75f09fb --- /dev/null +++ b/data/src/main/kotlin/com/mashup/dorabangs/data/datasource/remote/impl/UserRemoteDataSourceImpl.kt @@ -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 ?: "" +} \ No newline at end of file diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/di/DataSourceModule.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/di/DataSourceModule.kt new file mode 100644 index 00000000..145a7555 --- /dev/null +++ b/data/src/main/kotlin/com/mashup/dorabangs/data/di/DataSourceModule.kt @@ -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 +} \ No newline at end of file diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/di/NetworkModule.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/di/NetworkModule.kt index e753a97c..e0f4979c 100644 --- a/data/src/main/kotlin/com/mashup/dorabangs/data/di/NetworkModule.kt +++ b/data/src/main/kotlin/com/mashup/dorabangs/data/di/NetworkModule.kt @@ -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 @@ -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") diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/di/RepositoryModule.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/di/RepositoryModule.kt index 220b4054..d2dc6e43 100644 --- a/data/src/main/kotlin/com/mashup/dorabangs/data/di/RepositoryModule.kt +++ b/data/src/main/kotlin/com/mashup/dorabangs/data/di/RepositoryModule.kt @@ -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 } diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/network/service/UserService.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/network/service/UserService.kt index 88e7483d..09b0101d 100644 --- a/data/src/main/kotlin/com/mashup/dorabangs/data/network/service/UserService.kt +++ b/data/src/main/kotlin/com/mashup/dorabangs/data/network/service/UserService.kt @@ -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 + suspend fun registerUser(@Body deviceToken: DeviceTokenDataModel): UserAccessTokenResponseModel } diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/repository/UserDataStoreRepositoryImpl.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/repository/UserDataStoreRepositoryImpl.kt deleted file mode 100644 index 07e4bec7..00000000 --- a/data/src/main/kotlin/com/mashup/dorabangs/data/repository/UserDataStoreRepositoryImpl.kt +++ /dev/null @@ -1,27 +0,0 @@ -package com.mashup.dorabangs.data.repository - -import com.mashup.dorabangs.data.datasource.local.UserPreferenceDataSource -import com.mashup.dorabangs.domain.repository.UserDataStoreRepository -import kotlinx.coroutines.flow.Flow -import javax.inject.Inject - -class UserDataStoreRepositoryImpl @Inject constructor( - private val dataSource: UserPreferenceDataSource, - -) : UserDataStoreRepository { - override suspend fun setUserAccessToken(accessToken: String) { - dataSource.setUserAccessToken(accessToken = accessToken) - } - - override fun getUserAccessToken(): Flow { - return dataSource.getUserAccessToken() - } - - override suspend fun setIsFirstEntry(isFirst: Boolean) { - dataSource.setIsFirstEntry(isFirst = isFirst) - } - - override fun getIsFirstEntry(): Flow { - return dataSource.getIsFirstEntry() - } -} diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/repository/UserRemoteRepositoryImpl.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/repository/UserRemoteRepositoryImpl.kt deleted file mode 100644 index 45d3cbf2..00000000 --- a/data/src/main/kotlin/com/mashup/dorabangs/data/repository/UserRemoteRepositoryImpl.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.mashup.dorabangs.data.repository - -import com.mashup.dorabangs.data.model.asData -import com.mashup.dorabangs.data.network.service.UserService -import com.mashup.dorabangs.domain.model.DeviceToken -import com.mashup.dorabangs.domain.repository.UserRemoteRepository -import javax.inject.Inject - -class UserRemoteRepositoryImpl @Inject constructor( - private val userService: UserService, -) : UserRemoteRepository { - - override suspend fun registerUser(deviceToken: DeviceToken): String = - userService.registerUser(deviceToken.asData()) - .body() - ?.data - ?.accessToken ?: "" -} diff --git a/data/src/main/kotlin/com/mashup/dorabangs/data/repository/UserRepositoryImpl.kt b/data/src/main/kotlin/com/mashup/dorabangs/data/repository/UserRepositoryImpl.kt new file mode 100644 index 00000000..e57f1d53 --- /dev/null +++ b/data/src/main/kotlin/com/mashup/dorabangs/data/repository/UserRepositoryImpl.kt @@ -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 { + return userLocalDataSource.getUserAccessToken() + } + + override suspend fun setIsFirstEntry(isFirst: Boolean) { + userLocalDataSource.setIsFirstEntry(isFirst = isFirst) + } + + override fun getIsFirstEntry(): Flow { + return userLocalDataSource.getIsFirstEntry() + } + + override suspend fun registerDeviceToken(deviceToken: DeviceToken): String { + return userRemoteDataSource.registerUser(deviceToken.asData()) + } +} diff --git a/domain/src/main/kotlin/com/mashup/dorabangs/domain/repository/UserRemoteRepository.kt b/domain/src/main/kotlin/com/mashup/dorabangs/domain/repository/UserRemoteRepository.kt deleted file mode 100644 index 9bf14a05..00000000 --- a/domain/src/main/kotlin/com/mashup/dorabangs/domain/repository/UserRemoteRepository.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.mashup.dorabangs.domain.repository - -import com.mashup.dorabangs.domain.model.DeviceToken - -interface UserRemoteRepository { - - suspend fun registerUser(deviceToken: DeviceToken): String -} diff --git a/domain/src/main/kotlin/com/mashup/dorabangs/domain/repository/UserRepository.kt b/domain/src/main/kotlin/com/mashup/dorabangs/domain/repository/UserRepository.kt new file mode 100644 index 00000000..f79729ba --- /dev/null +++ b/domain/src/main/kotlin/com/mashup/dorabangs/domain/repository/UserRepository.kt @@ -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 + suspend fun setIsFirstEntry(isFirst: Boolean) + fun getIsFirstEntry(): Flow + suspend fun registerDeviceToken(deviceToken: DeviceToken): String +} diff --git a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/GetIsFirstEntryUseCase.kt b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/GetIsFirstEntryUseCase.kt index 6fde4724..e50536ef 100644 --- a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/GetIsFirstEntryUseCase.kt +++ b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/GetIsFirstEntryUseCase.kt @@ -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 { - return userDataStoreRepository.getIsFirstEntry() + return userRepository.getIsFirstEntry() } } diff --git a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/GetUserAccessTokenUseCase.kt b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/GetUserAccessTokenUseCase.kt index 1f675b4d..850bb8e0 100644 --- a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/GetUserAccessTokenUseCase.kt +++ b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/GetUserAccessTokenUseCase.kt @@ -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 { - return userDataStoreRepository.getUserAccessToken() + return userRepository.getUserAccessToken() } } diff --git a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/RegisterUserUseCase.kt b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/RegisterUserUseCase.kt index 8e8dbab0..39323bb9 100644 --- a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/RegisterUserUseCase.kt +++ b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/RegisterUserUseCase.kt @@ -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) } } diff --git a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/SetIsFirstEntryUseCase.kt b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/SetIsFirstEntryUseCase.kt index c558f5cf..b3d64b43 100644 --- a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/SetIsFirstEntryUseCase.kt +++ b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/SetIsFirstEntryUseCase.kt @@ -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) } } diff --git a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/SetUserAccessTokenUseCase.kt b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/SetUserAccessTokenUseCase.kt index d19a9b74..e43d2198 100644 --- a/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/SetUserAccessTokenUseCase.kt +++ b/domain/src/main/kotlin/com/mashup/dorabangs/domain/usecase/user/SetUserAccessTokenUseCase.kt @@ -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) } }