Skip to content

Commit

Permalink
refactor/#11: shared preferences 상수 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 11, 2024
1 parent 9223db8 commit 3c28258
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions app/src/main/java/com/sopt/now/compose/data/UserPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@ package com.sopt.now.compose.data
import android.content.Context

import androidx.core.content.edit
import com.sopt.now.compose.data.UserPreference.UserPreferenceKeys.KEY_USER_ID
import com.sopt.now.compose.data.UserPreference.UserPreferenceKeys.KEY_USER_NAME
import com.sopt.now.compose.data.UserPreference.UserPreferenceKeys.KEY_USER_PHONE
import com.sopt.now.compose.data.UserPreference.UserPreferenceKeys.PREF_USER_DATA

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

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

// 사용자 아이디 가져오기
fun getUserId(): String? {
return sharedPreferences.getString("userId", null)
return sharedPreferences.getString(KEY_USER_ID, null)
}

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

Expand All @@ -42,4 +46,10 @@ class UserPreference(context: Context) {
}
}
}
object UserPreferenceKeys {
const val PREF_USER_DATA = "userData"
const val KEY_USER_ID = "userId"
const val KEY_USER_NAME = "userName"
const val KEY_USER_PHONE = "userPhone"
}
}

0 comments on commit 3c28258

Please sign in to comment.