zhujie
2025-04-03 fe04012057d024770e0180543483d393281a542f
app/src/main/java/com/example/firstapp/utils/PreferencesManager.kt
对比新文件
@@ -0,0 +1,58 @@
package com.example.firstapp.utils
import android.content.Context
import android.content.SharedPreferences
object PreferencesManager {
    private const val PREF_NAME = "app_preferences"
    private const val KEY_TOKEN = "user_token"
    private const val KEY_PHONE = "user_phone"
    private const val KEY_FIRST_INSTALL = "first_install"
    private const val LAST_LOGIN_PHONE = "last_login_phone"
    private lateinit var preferences: SharedPreferences
    fun init(context: Context) {
        preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
    }
    fun saveToken(token: String) {
        preferences.edit().putString(KEY_TOKEN, token).apply()
    }
    fun getToken(): String? {
        return preferences.getString(KEY_TOKEN, null)
    }
    fun savePhone(phone: String) {
        preferences.edit().putString(KEY_PHONE, phone).apply()
    }
    fun getPhone(): String? {
        return preferences.getString(KEY_PHONE, null)
    }
    fun clearUserData() {
        preferences.edit().apply {
            remove(KEY_TOKEN)
            remove(KEY_PHONE)
            apply()
        }
    }
    fun isFirstInstall(): Boolean {
        return preferences.getBoolean(KEY_FIRST_INSTALL, true)
    }
    fun setFirstInstall(isFirst: Boolean) {
        preferences.edit().putBoolean(KEY_FIRST_INSTALL, isFirst).apply()
    }
    fun saveLastLoginPhone(phone: String) {
        preferences.edit().putString(LAST_LOGIN_PHONE, phone).apply()
    }
    fun getLastLoginPhone(): String {
        return preferences.getString(LAST_LOGIN_PHONE, "") ?: ""
    }
}