Skip to content

Commit 594a189

Browse files
committed
feat/#84: Datastore 업데이트
1 parent beaf2da commit 594a189

File tree

1 file changed

+127
-1
lines changed

1 file changed

+127
-1
lines changed

app/src/main/java/com/paw/key/core/util/PreferenceDataStore.kt

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ private val TOTAL_DISTANCE_KEY = floatPreferencesKey("total_distance")
1818
private val TOTAL_TIME_KEY = longPreferencesKey("total_time")
1919
private val TOTAL_STEPS_KEY = intPreferencesKey("total_steps")
2020

21+
private val LOGIN_EMAIL_KEY = stringPreferencesKey("login_email")
22+
private val LOGIN_PASSWORD_KEY = stringPreferencesKey("login_password")
23+
private val USER_ID_KEY = intPreferencesKey("user_id")
24+
private val USER_NAME_KEY = stringPreferencesKey("user_name")
25+
private val PET_ID_KEY = intPreferencesKey("pet_id")
26+
private val PET_NAME_KEY = stringPreferencesKey("pet_name")
27+
2128
private fun List<LatLng>.toPreferenceString(): String =
2229
joinToString(";") { "${it.latitude},${it.longitude}" }
2330

@@ -72,7 +79,126 @@ object PreferenceDataStore {
7279

7380
suspend fun clearWalkSummary(context: Context) {
7481
context.summaryStore.edit { preferences ->
75-
preferences.clear() // 모든 데이터 삭제
82+
preferences.remove(POINTS_KEY)
83+
preferences.remove(TOTAL_DISTANCE_KEY)
84+
preferences.remove(TOTAL_TIME_KEY)
85+
preferences.remove(TOTAL_STEPS_KEY)
86+
}
87+
}
88+
89+
suspend fun saveLoginInfo(
90+
context: Context,
91+
email: String,
92+
password: String
93+
) {
94+
context.summaryStore.edit { preferences ->
95+
preferences[LOGIN_EMAIL_KEY] = email
96+
preferences[LOGIN_PASSWORD_KEY] = password
97+
}
98+
}
99+
100+
fun getLoginEmail(context: Context): Flow<String> {
101+
return context.summaryStore.data.map { preferences ->
102+
preferences[LOGIN_EMAIL_KEY] ?: ""
103+
}
104+
}
105+
106+
fun getLoginPassword(context: Context): Flow<String> {
107+
return context.summaryStore.data.map { preferences ->
108+
preferences[LOGIN_PASSWORD_KEY] ?: ""
109+
}
110+
}
111+
112+
data class LoginInfo(
113+
val email: String,
114+
val password: String
115+
)
116+
117+
fun getLoginInfo(context: Context): Flow<LoginInfo> {
118+
return context.summaryStore.data.map { preferences ->
119+
LoginInfo(
120+
email = preferences[LOGIN_EMAIL_KEY] ?: "",
121+
password = preferences[LOGIN_PASSWORD_KEY] ?: ""
122+
)
123+
}
124+
}
125+
126+
suspend fun clearLoginInfo(context: Context) {
127+
context.summaryStore.edit { preferences ->
128+
preferences.remove(LOGIN_EMAIL_KEY)
129+
preferences.remove(LOGIN_PASSWORD_KEY)
130+
}
131+
}
132+
133+
suspend fun saveUserInfo(
134+
context: Context,
135+
userId: Int,
136+
userName: String,
137+
petId: Int,
138+
petName: String
139+
) {
140+
context.summaryStore.edit { preferences ->
141+
preferences[USER_ID_KEY] = userId
142+
preferences[USER_NAME_KEY] = userName
143+
preferences[PET_ID_KEY] = petId
144+
preferences[PET_NAME_KEY] = petName
145+
}
146+
}
147+
148+
fun getUserId(context: Context): Flow<Int> {
149+
return context.summaryStore.data.map { preferences ->
150+
preferences[USER_ID_KEY] ?: 0
151+
}
152+
}
153+
154+
fun getUserName(context: Context): Flow<String> {
155+
return context.summaryStore.data.map { preferences ->
156+
preferences[USER_NAME_KEY] ?: ""
157+
}
158+
}
159+
160+
fun getPetId(context: Context): Flow<Int> {
161+
return context.summaryStore.data.map { preferences ->
162+
preferences[PET_ID_KEY] ?: 0
163+
}
164+
}
165+
166+
fun getPetName(context: Context): Flow<String> {
167+
return context.summaryStore.data.map { preferences ->
168+
preferences[PET_NAME_KEY] ?: ""
169+
}
170+
}
171+
172+
data class UserInfo(
173+
val userId: Int,
174+
val userName: String,
175+
val petId: Int,
176+
val petName: String
177+
)
178+
179+
fun getUserInfo(context: Context): Flow<UserInfo> {
180+
return context.summaryStore.data.map { preferences ->
181+
UserInfo(
182+
userId = preferences[USER_ID_KEY] ?: 0,
183+
userName = preferences[USER_NAME_KEY] ?: "",
184+
petId = preferences[PET_ID_KEY] ?: 0,
185+
petName = preferences[PET_NAME_KEY] ?: ""
186+
)
187+
}
188+
}
189+
190+
suspend fun clearUserInfo(context: Context) {
191+
context.summaryStore.edit { preferences ->
192+
preferences.remove(USER_ID_KEY)
193+
preferences.remove(USER_NAME_KEY)
194+
preferences.remove(PET_ID_KEY)
195+
preferences.remove(PET_NAME_KEY)
196+
}
197+
}
198+
199+
suspend fun clearAllData(context: Context) {
200+
context.summaryStore.edit { preferences ->
201+
preferences.clear()
76202
}
77203
}
78204
}

0 commit comments

Comments
 (0)