Skip to content

Commit

Permalink
add : sharedPreferences 분리 #2
Browse files Browse the repository at this point in the history
  • Loading branch information
SeonHwan-Kim committed Apr 26, 2023
1 parent b8e1887 commit c945074
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".SoptApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/org/android/go/sopt/SoptApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.android.go.sopt

import android.app.Application
import org.android.go.sopt.util.UserSharedPreferences

class SoptApplication : Application() {
companion object{
lateinit var prefs: UserSharedPreferences
}

override fun onCreate() {
prefs = UserSharedPreferences(applicationContext)
super.onCreate()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.android.go.sopt.util

import android.content.Context
import android.content.SharedPreferences
import org.android.go.sopt.data.User

class UserSharedPreferences(context: Context) {
private val prefs: SharedPreferences = context.getSharedPreferences(KEY_PREFS, 0)

fun getString(key: String, defValue: String?): String {
return prefs.getString(key, defValue).toString()
}

fun setString(key: String, value: String?) {
prefs.edit().putString(key, value).apply()
}

fun getBoolean(key: String, defValue: Boolean): Boolean {
return prefs.getBoolean(key, defValue)
}

fun setBoolean(key: String, value: Boolean) {
prefs.edit().putBoolean(key, value).apply()
}

fun saveUserInformation(
isLogin: Boolean,
user: User
) {
setBoolean(KEY_ISLOGIN, isLogin)
setString(KEY_ID, user.id)
setString(KEY_PASSWORD, user.password)
setString(KEY_NAME, user.name)
setString(KEY_SPECIALTY, user.specialty)
}

fun deleteUserInformation() = prefs.edit().clear().apply()

companion object {
private const val KEY_PREFS = "userInfo"
private const val KEY_ISLOGIN = "isLogin"
private const val KEY_ID = "id"
private const val KEY_PASSWORD = "password"
private const val KEY_NAME = "name"
private const val KEY_SPECIALTY = "specialty"
}
}

0 comments on commit c945074

Please sign in to comment.