Skip to content

Commit

Permalink
feat/#11: UserPreference 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 3, 2024
1 parent 6ab7a9e commit 230598d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions app/src/main/java/com/sopt/now/compose/data/UserPreference.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.sopt.now.compose.data

import android.content.Context

class UserPreference(context: Context) {
private val sharedPreferences = context.getSharedPreferences("userData", Context.MODE_PRIVATE)

// 사용자 아이디 저장
fun saveUserId(userId: String) {
with(sharedPreferences.edit()){
putString("userId", userId)
apply()
}
}

// 사용자 데이터 가져오기
fun getUserId(): String? {
with(sharedPreferences){
val userId = getString("userId", null)
return userId
}
}

// 사용자 데이터 저장
fun saveUserData(userData: UserData) {
with(sharedPreferences.edit()){
putString("userId", userData.userId)
putString("userName", userData.userName)
putString("userPhone", userData.userPhone)
apply()
}
}

// 사용자 데이터 가져오기
fun getUserData(): UserData? {
with(sharedPreferences){
val userId = getString("userId", null)
val userName = getString("userName", null)
val userPhone = getString("userPhone", null)

return if (userId != null && userName != null && userPhone != null) {
UserData(userId, userName, userPhone)
} else {
null
}
}
}
}

0 comments on commit 230598d

Please sign in to comment.