generated from DO-SOPT-ANDROID/do-sopt-android-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from DO-SOPT-ANDROID/feat/week3
PR : 실습 3주차 필수과제
- Loading branch information
Showing
19 changed files
with
385 additions
and
95 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
app/src/main/java/org/sopt/dosopttemplate/db/local/PreferenceManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.sopt.dosopttemplate.db.local | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlinx.coroutines.withContext | ||
import org.sopt.dosopttemplate.R | ||
import org.sopt.dosopttemplate.model.User | ||
|
||
class PreferenceManager(val context: Context) { | ||
init { | ||
getInstance(context) | ||
} | ||
fun getId(): String = sharedPreferencesInstance.getString(ID, "").orEmpty() | ||
fun getPassword(): String = sharedPreferencesInstance.getString(PWD, "").orEmpty() | ||
fun getNickname(): String = sharedPreferencesInstance.getString(NICKNAME, "").orEmpty() | ||
fun getMBTI(): String = sharedPreferencesInstance.getString(MBTI, "").orEmpty() | ||
fun getAutoLogin(): Boolean = sharedPreferencesInstance.getBoolean(AUTO_LOGIN, false) | ||
|
||
fun setAutoLogin(auto: Boolean) { | ||
sharedPreferencesInstance.edit(commit = true) { | ||
putBoolean(AUTO_LOGIN, auto) | ||
} | ||
} | ||
|
||
fun setUser(user: User?) { | ||
sharedPreferencesInstance.edit(commit = true) { | ||
user?.let { | ||
putString(ID, it.id) | ||
putString(PWD, it.password) | ||
putString(NICKNAME, it.nickname) | ||
putString(MBTI, it.mbti.toString()) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
const val EXTRA_USER = "USER" | ||
const val ID = "ID" | ||
const val PWD = "PWD" | ||
const val NICKNAME = "NICKNAME" | ||
const val MBTI = "MBTI" | ||
const val AUTO_LOGIN = "AUTO_LOGIN" | ||
|
||
lateinit var sharedPreferencesInstance: SharedPreferences | ||
|
||
@Synchronized | ||
private fun getInstance(context: Context) { | ||
if (::sharedPreferencesInstance.isInitialized) { | ||
sharedPreferencesInstance = context.getSharedPreferences( | ||
context.getString(R.string.preference_file_key), | ||
Context.MODE_PRIVATE | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.