Skip to content

Commit

Permalink
feat/#8: UserDatasource, UserRepository 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kangyein9892 committed Nov 14, 2024
1 parent 27f4359 commit 2b4fe9a
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 4 deletions.
1 change: 1 addition & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
implementation(libs.material)

implementation(libs.hilt)
implementation(project(":domain"))
ksp(libs.hilt.compiler)
implementation(libs.kotlinx.serialization.json)

Expand Down
30 changes: 30 additions & 0 deletions data/src/main/java/org/sopt/and/data/datasource/UserDataSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.sopt.and.data.datasource

import android.content.SharedPreferences
import jakarta.inject.Inject
import org.sopt.and.data.di.UserSharedPreference

internal class UserDataSource @Inject constructor(
@UserSharedPreference private val userSharedPreference: SharedPreferences
) {

var id: String
get() = userSharedPreference.getString(ID, "").toString()
set(value) = userSharedPreference.edit().putString(ID, value).apply()

var password: String
get() = userSharedPreference.getString(PASSWORD, "").toString()
set(value) = userSharedPreference.edit().putString(PASSWORD, value).apply()

fun clearIdPassword() {
id = DEFAULT_STRING
password = DEFAULT_STRING
userSharedPreference.edit().clear().apply()
}

companion object {
private const val ID = "id"
private const val PASSWORD = "password"
private const val DEFAULT_STRING = ""
}
}
21 changes: 21 additions & 0 deletions data/src/main/java/org/sopt/and/data/di/DataSourceModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.sopt.and.data.di

import android.content.SharedPreferences
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.and.data.datasource.UserDataSource
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
internal object DataSourceModule {

@Singleton
@Provides
fun provideUserDataSource(
@UserSharedPreference userSharedPreference: SharedPreferences
): UserDataSource = UserDataSource(userSharedPreference)

}
16 changes: 16 additions & 0 deletions data/src/main/java/org/sopt/and/data/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.sopt.and.data.di

import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.and.data.repository.UserRepositoryImpl
import org.sopt.and.domain.repository.UserRepository

@Module
@InstallIn(SingletonComponent::class)
internal interface RepositoryModule {

@Binds
fun bindUserRepository(userRepositoryImpl: UserRepositoryImpl): UserRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sopt.and.data.repository

import jakarta.inject.Inject
import org.sopt.and.data.datasource.UserDataSource
import org.sopt.and.domain.repository.UserRepository

internal class UserRepositoryImpl @Inject constructor(
private val userDataSource: UserDataSource
): UserRepository {
override fun saveUser(id: String, password: String) {
userDataSource.id = id
userDataSource.password = password
}

override fun clearIdPassword() {
userDataSource.clearIdPassword()
}

override fun getId(): String {
return userDataSource.id
}

}
4 changes: 0 additions & 4 deletions domain/src/main/java/org/sopt/and/domain/MyClass.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.sopt.and.domain.repository

interface UserRepository {
fun saveUser(id: String, password: String)
fun clearIdPassword()
fun getId(): String
}

0 comments on commit 2b4fe9a

Please sign in to comment.