1
zhujie
2025-04-15 879ec1ae04b37eb7bf9357903d10acc860d84d5b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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 const val PREF_LAST_CHECK_TIME_PREFIX = "last_check_time_"
    private const val KEY_INVITE = "user_invite"
 
    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, "") ?: ""
    }
 
    fun getLastCheckTime(categoryId: Int): Long {
        return preferences.getLong(PREF_LAST_CHECK_TIME_PREFIX + categoryId, 0)
    }
 
    fun setLastCheckTime(categoryId: Int, time: Long) {
        preferences.edit().putLong(PREF_LAST_CHECK_TIME_PREFIX + categoryId, time).apply()
    }
 
    fun getInviteCode(): String {
        return preferences.getString(KEY_INVITE, "") ?: ""
    }
 
    fun setInviteCode(invite: String) {
        preferences.edit().putString(KEY_INVITE, invite).apply()
    }