-
Notifications
You must be signed in to change notification settings - Fork 0
[Refactor] SharedPreference에서 datastore로 마이그레이션 #396
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
df8bc40
delete: MySharedPreferences 제거 후 AccountDataStore와 TokenStore로 마이그레이션
HI-JIN2 9e622f5
chore: package
HI-JIN2 42087b5
chore: 안쓰는 거 삭제 alarm
HI-JIN2 6b3abca
chore: 리베이스 실수
HI-JIN2 2606d7e
fix: GetUserCollegeDepartmentUseCase에서 non-nullable 상태로 항상 반환
HI-JIN2 c97a405
chore: 리베이스 오류 수정
HI-JIN2 a4dcc87
chore: 리베이스 전 MySharedPreference 활용 로직 제거
HI-JIN2 26ddc0a
chore: rebase
HI-JIN2 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
149 changes: 0 additions & 149 deletions
149
app/src/main/java/com/eatssu/android/data/MySharedPreferences.kt
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
app/src/main/java/com/eatssu/android/data/dto/request/ChangePwRequest.kt
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/eatssu/android/data/local/AccountDataStore.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package com.eatssu.android.data.local | ||
|
|
||
| import android.content.Context | ||
| import androidx.datastore.preferences.core.edit | ||
| import androidx.datastore.preferences.core.intPreferencesKey | ||
| import androidx.datastore.preferences.core.stringPreferencesKey | ||
| import androidx.datastore.preferences.preferencesDataStore | ||
| import com.eatssu.android.domain.model.College | ||
| import com.eatssu.android.domain.model.Department | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.map | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| private val Context.accountDataStore by preferencesDataStore(name = "account") | ||
|
|
||
| @Singleton | ||
| class AccountDataStore @Inject constructor( | ||
| @ApplicationContext private val context: Context | ||
| ) { | ||
| companion object Keys { | ||
| val EMAIL = stringPreferencesKey("MY_EMAIL") | ||
| val NAME = stringPreferencesKey("MY_NAME") | ||
| val COLLEGE_ID = intPreferencesKey("MY_COLLEGE_ID") | ||
| val COLLEGE_NAME = stringPreferencesKey("MY_COLLEGE") | ||
| val DEPT_ID = intPreferencesKey("MY_DEPARTMENT_ID") | ||
| val DEPT_NAME = stringPreferencesKey("MY_DEPARTMENT") | ||
| } | ||
|
|
||
| val email: Flow<String> = context.accountDataStore.data.map { it[EMAIL].orEmpty() } | ||
| suspend fun setEmail(v: String) = context.accountDataStore.edit { it[EMAIL] = v } | ||
|
|
||
| val name: Flow<String> = context.accountDataStore.data.map { it[NAME].orEmpty() } | ||
| suspend fun setName(v: String) = context.accountDataStore.edit { it[NAME] = v } | ||
|
|
||
| val college: Flow<College?> = context.accountDataStore.data.map { | ||
| val id = it[COLLEGE_ID] ?: -1 | ||
| val name = it[COLLEGE_NAME] | ||
| if (id >= 0 && !name.isNullOrBlank()) College(id, name) else null | ||
| } | ||
|
|
||
| suspend fun setCollege(v: College) = context.accountDataStore.edit { | ||
| it[COLLEGE_ID] = v.collegeId | ||
| it[COLLEGE_NAME] = v.collegeName | ||
| } | ||
|
|
||
| val department: Flow<Department?> = context.accountDataStore.data.map { | ||
| val id = it[DEPT_ID] ?: -1 | ||
| val name = it[DEPT_NAME] | ||
| if (id >= 0 && !name.isNullOrBlank()) Department(id, name) else null | ||
| } | ||
|
|
||
| suspend fun setDepartment(v: Department) = context.accountDataStore.edit { | ||
| it[DEPT_ID] = v.departmentId | ||
| it[DEPT_NAME] = v.departmentName | ||
| } | ||
|
|
||
| suspend fun clear() = context.accountDataStore.edit { it.clear() } | ||
| } |
17 changes: 11 additions & 6 deletions
17
.../data/repository/PreferencesRepository.kt → ...su/android/data/local/SettingDataStore.kt
This file contains hidden or 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,31 +1,36 @@ | ||
| // PreferencesRepository.kt | ||
| package com.eatssu.android.data.repository | ||
| package com.eatssu.android.data.local | ||
|
|
||
| import android.content.Context | ||
| 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.preferencesDataStore | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.map | ||
| import javax.inject.Inject | ||
|
|
||
| class PreferencesRepository(private val context: Context) { | ||
| private val Context.settingDataStore: DataStore<Preferences> by preferencesDataStore(name = "settings") | ||
|
|
||
| private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings") | ||
| class SettingDataStore @Inject constructor( | ||
| @ApplicationContext private val context: Context | ||
| ) { | ||
|
|
||
| companion object { | ||
| private val DAILY_NOTIFICATION_KEY = booleanPreferencesKey("daily_notification") | ||
| } | ||
|
|
||
| val dailyNotificationStatus: Flow<Boolean> = context.dataStore.data | ||
| val dailyNotificationStatus: Flow<Boolean> = context.settingDataStore.data | ||
| .map { preferences -> | ||
| preferences[DAILY_NOTIFICATION_KEY] ?: false // Default value is false | ||
| } | ||
|
|
||
| suspend fun setDailyNotificationStatus(status: Boolean) { | ||
| context.dataStore.edit { preferences -> | ||
| context.settingDataStore.edit { preferences -> | ||
| preferences[DAILY_NOTIFICATION_KEY] = status | ||
| } | ||
| } | ||
|
|
||
| suspend fun clear() = context.settingDataStore.edit { it.clear() } | ||
| } |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/eatssu/android/data/local/TokenStore.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.eatssu.android.data.local | ||
|
|
||
| import android.content.SharedPreferences | ||
| import androidx.core.content.edit | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class TokenStore @Inject constructor( | ||
| private val securePrefs: SharedPreferences | ||
| ) { | ||
| private companion object { | ||
| const val KEY_ACCESS = "ACCESS_TOKEN" | ||
| const val KEY_REFRESH = "REFRESH_TOKEN" | ||
| } | ||
|
|
||
| var accessToken: String | ||
| get() = securePrefs.getString(KEY_ACCESS, "").orEmpty() | ||
| set(v) = securePrefs.edit { putString(KEY_ACCESS, v) } | ||
|
|
||
| var refreshToken: String | ||
| get() = securePrefs.getString(KEY_REFRESH, "").orEmpty() | ||
| set(v) = securePrefs.edit { putString(KEY_REFRESH, v) } | ||
|
|
||
| fun clear() = securePrefs.edit { remove(KEY_ACCESS); remove(KEY_REFRESH) } | ||
| } | ||
This file contains hidden or 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,4 +1,4 @@ | ||
| package com.eatssu.android.data.repository | ||
| package com.eatssu.android.data.local | ||
|
|
||
| import android.content.Context | ||
| import androidx.datastore.core.DataStore | ||
|
|
@@ -7,16 +7,18 @@ import androidx.datastore.preferences.core.edit | |
| import androidx.datastore.preferences.core.stringPreferencesKey | ||
| import androidx.datastore.preferences.preferencesDataStore | ||
| import com.eatssu.common.enums.Restaurant | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.flow.first | ||
| import timber.log.Timber | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| private val Context.widgetPrefsDataStore: DataStore<Preferences> by preferencesDataStore(name = "widget_prefs") | ||
|
Member
Author
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. 위젯용 data store을 건들이지는 않아서 기존 바탕화면에 적재한 위젯에 영향은 없을듯합니다! |
||
|
|
||
| @Singleton | ||
| class WidgetPreferencesRepository @Inject constructor( | ||
| private val context: Context, | ||
| class WidgetDataStore @Inject constructor( | ||
| @ApplicationContext private val context: Context, | ||
| ) { | ||
| private val Context.widgetPrefsDataStore: DataStore<Preferences> by preferencesDataStore(name = "widget_prefs") | ||
| private fun fileKeyRestaurantKey(fileKey: String) = | ||
| stringPreferencesKey("widget_restaurant_by_fileKey_$fileKey") | ||
|
|
||
|
|
||
2 changes: 1 addition & 1 deletion
2
...data/dto/request/ChangeNicknameRequest.kt → ...mote/dto/request/ChangeNicknameRequest.kt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...ata/dto/request/CheckValidTokenRequest.kt → ...ote/dto/request/CheckValidTokenRequest.kt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...data/dto/request/LoginWithKakaoRequest.kt → ...mote/dto/request/LoginWithKakaoRequest.kt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...d/data/dto/request/ModifyReviewRequest.kt → ...remote/dto/request/ModifyReviewRequest.kt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...android/data/dto/request/ReportRequest.kt → .../data/remote/dto/request/ReportRequest.kt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...data/dto/request/UserDepartmentRequest.kt → ...mote/dto/request/UserDepartmentRequest.kt
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...id/data/dto/request/WriteReviewRequest.kt → .../remote/dto/request/WriteReviewRequest.kt
This file contains hidden or 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
Oops, something went wrong.
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.
로그인, 세션 관련 처리할 때 token이 null인지가 아니라 empty인지를 확인하는 부분 볼 때마다 ""인지 null인지 사람이 개발하다 헷갈려서 미래에 문제가 생길 수도 있겠다 생각했는데, 이번 기회에 바꾸면 좋을 것 같아요. 어떻게 생각하시나요?
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.
nullorempty를 검사하면 되지 않을까요??
제훈님 말씀은
""을 저장하지 않게 하자그리고null 아니면 토큰만 반환하도록 하자인가용?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.
nullOrEmpty를 검사하면 되긴 하지만 ""라는 빈 공백 값이 가능한 상황은 최대한 줄이면 좋다고 생각했어요! 말씀해주신대로 Token 아니면 null을 반환해 ""를 쓰지 말자가 맞습니다!