-
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
Refactor: Preferences Datastore 적용 #15
Open
Eonji-sw
wants to merge
4
commits into
develop
Choose a base branch
from
refactor/apply-prefDatastore
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,7 @@ import com.sopt.now.data.dto.UserDto | |
|
||
interface SharedPreferenceDataSource { | ||
var checkLogin: Boolean | ||
fun saveUserInfo(userDto: UserDto?) | ||
fun getUserInfo(): UserDto | ||
|
||
fun clear() | ||
suspend fun saveUserInfo(userDto: UserDto?) | ||
suspend fun getUserInfo(): UserDto | ||
suspend fun clear() | ||
Comment on lines
+7
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. suspend를 사용해도 되고 타입을 Flow타입으로 감싸주어도 될것 같습니다! |
||
} |
68 changes: 54 additions & 14 deletions
68
data/src/main/java/com/sopt/now/data/datasourceimpl/SharedPreferenceDataSourceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,75 @@ | ||
package com.sopt.now.data.datasourceimpl | ||
|
||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.emptyPreferences | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import com.sopt.now.data.datasource.SharedPreferenceDataSource | ||
import com.sopt.now.data.dto.UserDto | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import java.io.IOException | ||
import javax.inject.Inject | ||
|
||
class SharedPreferenceDataSourceImpl @Inject constructor( | ||
private val sharedPreferences: SharedPreferences | ||
private val dataStore: DataStore<Preferences> | ||
) : SharedPreferenceDataSource { | ||
override var checkLogin: Boolean | ||
get() = sharedPreferences.getBoolean(CHECK_LOGIN, false) | ||
set(value) = sharedPreferences.edit { putBoolean(CHECK_LOGIN, value) } | ||
get() = runBlocking { | ||
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. runBlocking을 사용할때 유의해야할 점은 뭘까요 ? |
||
dataStore.data | ||
.catch { exception -> | ||
if (exception is IOException) { | ||
emit(emptyPreferences()) | ||
} else { | ||
throw exception | ||
} | ||
} | ||
.map { preference -> | ||
preference[stringPreferencesKey(CHECK_LOGIN)]?.toBoolean() ?: false | ||
}.first() | ||
} | ||
set(value) { | ||
runBlocking { | ||
dataStore.edit { preferences -> | ||
preferences[stringPreferencesKey(CHECK_LOGIN)] = | ||
value.toString() | ||
} | ||
} | ||
} | ||
|
||
override fun saveUserInfo(userDto: UserDto?) { | ||
override suspend fun saveUserInfo(userDto: UserDto?) { | ||
val json = Json.encodeToString(userDto) | ||
sharedPreferences.edit { putString(USER_INFO, json) } | ||
dataStore.edit { preferences -> | ||
preferences[stringPreferencesKey(USER_INFO)] = json | ||
} | ||
} | ||
|
||
override fun getUserInfo(): UserDto { | ||
val json = sharedPreferences.getString(USER_INFO, "") | ||
if (json.isNullOrBlank()) return UserDto("", "", "", "") | ||
override suspend fun getUserInfo(): UserDto { | ||
val json = dataStore.data | ||
.catch { exception -> | ||
if (exception is IOException) { | ||
emit(emptyPreferences()) | ||
} else { | ||
throw exception | ||
} | ||
} | ||
.map { preference -> | ||
preference[stringPreferencesKey(USER_INFO)] ?: "" | ||
}.first() | ||
|
||
if (json.isBlank()) return UserDto("", "", "", "") | ||
return Json.decodeFromString(json) | ||
} | ||
|
||
override fun clear() { | ||
sharedPreferences.edit { | ||
remove(USER_INFO) | ||
remove(CHECK_LOGIN) | ||
override suspend fun clear() { | ||
dataStore.edit { preferences -> | ||
preferences.remove(stringPreferencesKey(USER_INFO)) | ||
preferences.remove(stringPreferencesKey(CHECK_LOGIN)) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
이거 모듈 data모듈로 이동해 주세요!
지금은 app모듈, data모듈 둘다 라이브러리에 의존해야하는데 data모듈에서 필요할때만 사용해도 될것 같아요