-
Notifications
You must be signed in to change notification settings - Fork 0
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
[7주차/필수] XML repository 패턴 #18
base: develop-xml
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,11 @@ | ||
package com.sopt.now.data.database | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import com.sopt.now.data.model.local.FriendDao | ||
import com.sopt.now.data.model.local.FriendEntity | ||
|
||
@Database(entities = [FriendEntity::class], version = 2) | ||
@Database(entities = [FriendEntity::class], version = 1) | ||
abstract class FriendDatabase : RoomDatabase() { | ||
abstract fun friendDao(): FriendDao | ||
|
||
companion object{ | ||
@Volatile | ||
private var INSTANCE: FriendDatabase? = null | ||
|
||
fun getDatabase(context: Context): FriendDatabase { | ||
return INSTANCE ?: synchronized(this) { | ||
val instance = Room.databaseBuilder( | ||
context.applicationContext, | ||
FriendDatabase::class.java, | ||
"friend_database" | ||
).build() | ||
INSTANCE = instance | ||
instance | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.sopt.now.data.datasource | ||
|
||
import com.sopt.now.data.model.local.FriendEntity | ||
|
||
interface FriendDataSource { | ||
suspend fun insertFriend(friendEntity: FriendEntity) | ||
suspend fun getFriend(): List<FriendEntity> | ||
suspend fun deleteFriend(id:Int) | ||
suspend fun deleteAll() | ||
Comment on lines
+6
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 친구 추가랑 삭제까지 .... 와우 !! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.sopt.now.data.datasource | ||
|
||
interface SharedPrefDataSource { | ||
fun setUserId(key: String, value: Int) | ||
fun getUserId(key: String): Int | ||
fun getBoolean(key: String): Boolean | ||
fun setBoolean(key: String, value: Boolean) | ||
fun clearUserData() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.sopt.now.data.datasourceImpl | ||
|
||
import com.sopt.now.data.database.FriendDatabase | ||
import com.sopt.now.data.datasource.FriendDataSource | ||
import com.sopt.now.data.model.local.FriendEntity | ||
import javax.inject.Inject | ||
|
||
class FriendDataSourceImpl @Inject constructor( | ||
private val friendDatabase: FriendDatabase | ||
) : FriendDataSource { | ||
override suspend fun insertFriend(friendEntity: FriendEntity) { | ||
friendDatabase.friendDao().insertFriend(friendEntity) | ||
} | ||
|
||
override suspend fun getFriend(): List<FriendEntity> { | ||
return friendDatabase.friendDao().getFriend() | ||
} | ||
|
||
override suspend fun deleteFriend(id: Int) { | ||
friendDatabase.friendDao().deleteFriend(id) | ||
} | ||
|
||
override suspend fun deleteAll() { | ||
friendDatabase.friendDao().deleteAll() | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.sopt.now.data.datasourceImpl | ||
|
||
import android.content.SharedPreferences | ||
import com.sopt.now.data.datasource.SharedPrefDataSource | ||
import javax.inject.Inject | ||
|
||
class SharedPrefDataSourceImpl @Inject constructor( | ||
private val sharedPreferences: SharedPreferences | ||
) : SharedPrefDataSource { | ||
|
||
override fun setUserId(key: String, value: Int) { | ||
return sharedPreferences.edit().putInt(key, value).apply() | ||
} | ||
|
||
override fun getUserId(key: String): Int { | ||
return sharedPreferences.getInt(key, 0) | ||
} | ||
|
||
override fun getBoolean(key: String): Boolean { | ||
return sharedPreferences.getBoolean(key, false) | ||
} | ||
|
||
override fun setBoolean(key: String, value: Boolean) { | ||
sharedPreferences.edit().putBoolean(key, value).apply() | ||
} | ||
|
||
override fun clearUserData() { | ||
sharedPreferences.edit().clear().apply() | ||
} | ||
} | ||
Comment on lines
+7
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 sharedPreferences에 레포 적용해봐야겠네요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 적용해봐야겠네요 ! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.sopt.now.data.di | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import com.sopt.now.data.database.FriendDatabase | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataBaseModule { | ||
@Singleton | ||
@Provides | ||
fun provideDataBase( | ||
@ApplicationContext context: Context | ||
): FriendDatabase = Room.databaseBuilder(context, FriendDatabase::class.java, "friend.db") | ||
.createFromAsset("database/friend.db").build() | ||
|
||
@Singleton | ||
@Provides | ||
fun provideFriendDao(friendDataBase: FriendDatabase) = friendDataBase.friendDao() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.sopt.now.data.di | ||
|
||
import com.sopt.now.data.datasource.FriendDataSource | ||
import com.sopt.now.data.datasource.SharedPrefDataSource | ||
import com.sopt.now.data.datasourceImpl.FriendDataSourceImpl | ||
import com.sopt.now.data.datasourceImpl.SharedPrefDataSourceImpl | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class DataSourceModule { | ||
@Singleton | ||
@Binds | ||
abstract fun bindsFriendDataSource(friendDataSource: FriendDataSourceImpl): FriendDataSource | ||
|
||
@Singleton | ||
@Binds | ||
abstract fun bindsSharedPRefDataSource(sharedPrefDataSource: SharedPrefDataSourceImpl): SharedPrefDataSource | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.sopt.now.data.di | ||
|
||
import com.sopt.now.data.repository.FriendRepository | ||
import com.sopt.now.data.repository.FriendRepositoryImpl | ||
import com.sopt.now.data.repository.SharedPrefRepository | ||
import com.sopt.now.data.repository.SharedPrefRepositoryImpl | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class RepositoryModule { | ||
@Singleton | ||
@Binds | ||
abstract fun bindsFriendRepository( | ||
friendRepository: FriendRepositoryImpl | ||
): FriendRepository | ||
|
||
@Singleton | ||
@Binds | ||
abstract fun bindsSharedPrefRepository( | ||
sharedPrefRepository: SharedPrefRepositoryImpl | ||
): SharedPrefRepository | ||
} | ||
Comment on lines
+13
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우왕 Dagger-Hilt Module 사용 신기하네요 .... 굿굿 !! |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.sopt.now.data.repository | ||
|
||
import com.sopt.now.data.model.local.FriendEntity | ||
|
||
interface FriendRepository { | ||
suspend fun insertFriend(friendEntity: FriendEntity) | ||
suspend fun getFriend(): Result<List<FriendEntity>> | ||
suspend fun deleteFriend(id:Int) | ||
suspend fun deleteAll() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.sopt.now.data.repository | ||
|
||
import com.sopt.now.data.datasource.FriendDataSource | ||
import com.sopt.now.data.model.local.FriendEntity | ||
import javax.inject.Inject | ||
|
||
class FriendRepositoryImpl @Inject constructor( | ||
private val friendDataSource: FriendDataSource | ||
) : FriendRepository { | ||
override suspend fun insertFriend(friendEntity: FriendEntity) { | ||
friendDataSource.insertFriend(friendEntity) | ||
|
||
} | ||
|
||
override suspend fun getFriend(): Result<List<FriendEntity>> = | ||
runCatching { | ||
friendDataSource.getFriend() | ||
} | ||
|
||
|
||
override suspend fun deleteFriend(id: Int) { | ||
friendDataSource.deleteFriend(id) | ||
|
||
} | ||
|
||
override suspend fun deleteAll() { | ||
friendDataSource.deleteAll() | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.sopt.now.data.repository | ||
|
||
interface SharedPrefRepository { | ||
fun setUserId(key: String, value: Int) | ||
fun getUserId(key: String): Int | ||
fun getBoolean(key: String): Boolean | ||
fun setBoolean(key: String, value: Boolean) | ||
fun clearUserData() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.sopt.now.data.repository | ||
|
||
import com.sopt.now.data.datasource.SharedPrefDataSource | ||
import javax.inject.Inject | ||
|
||
class SharedPrefRepositoryImpl @Inject constructor( | ||
private val sharedPrefDataSource: SharedPrefDataSource | ||
) :SharedPrefRepository { | ||
override fun setUserId(key: String, value: Int) { | ||
sharedPrefDataSource.setUserId(key, value) | ||
} | ||
|
||
override fun getUserId(key: String): Int { | ||
return sharedPrefDataSource.getUserId(key) | ||
} | ||
|
||
override fun getBoolean(key: String): Boolean { | ||
return sharedPrefDataSource.getBoolean(key) | ||
} | ||
|
||
override fun setBoolean(key: String, value: Boolean) { | ||
sharedPrefDataSource.setBoolean(key, value) | ||
} | ||
|
||
override fun clearUserData() { | ||
sharedPrefDataSource.clearUserData() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리스트를 가져오니까 getFriends 라는 네이밍을 사용하는 게 더 좋을 것 같아요!