From 230598d47b68c65e57533242c324b003bb765fd9 Mon Sep 17 00:00:00 2001 From: youjin09222 Date: Fri, 3 May 2024 23:29:14 +0900 Subject: [PATCH] =?UTF-8?q?feat/#11:=20UserPreference=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sopt/now/compose/data/UserPreference.kt | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/src/main/java/com/sopt/now/compose/data/UserPreference.kt diff --git a/app/src/main/java/com/sopt/now/compose/data/UserPreference.kt b/app/src/main/java/com/sopt/now/compose/data/UserPreference.kt new file mode 100644 index 0000000..4a3f5f3 --- /dev/null +++ b/app/src/main/java/com/sopt/now/compose/data/UserPreference.kt @@ -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 + } + } + } +} \ No newline at end of file