wangrong
2024-12-10 62a7ff352d506398c6ac8824b2d3fe7c08dee8fb
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import android.content.Context
import android.os.Parcel
import com.shsening.autouploadvideos.AppConfig
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.io.Serializable
import android.os.Parcelable
import com.shsening.autouploadvideos.utils.Utils
 
data class SessionProfile(
    var id: String,
    var name: String,
    var type: Int,
    var lastUploadTime: String,
    val profiles: MutableList<Profile> = mutableListOf()
) : Parcelable, Serializable { // 逗号分隔接口
    // Parcelable 的实现
    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(id)
        parcel.writeString(name)
        parcel.writeInt(type)
        parcel.writeString(lastUploadTime)
        parcel.writeTypedList(profiles) // 对于列表,使用 writeTypedList
    }
    override fun describeContents(): Int {
        return 0
    }
    // Parcelable 的 Companion Object
    companion object CREATOR : Parcelable.Creator<SessionProfile> {
        override fun createFromParcel(parcel: Parcel): SessionProfile {
            val id = parcel.readString().orEmpty()
            val name = parcel.readString().orEmpty()
            val type = parcel.readInt()
            val lastUploadTime = parcel.readString().orEmpty()
            val profiles = mutableListOf<Profile>().apply {
                parcel.readTypedList(this, Profile.CREATOR)
            }
            return SessionProfile(id, name, type, lastUploadTime, profiles)
        }
 
        override fun newArray(size: Int): Array<SessionProfile?> {
            return arrayOfNulls(size)
        }
    }
    // 添加或更新 Profile 的方法
    fun addOrUpdateProfile(profile: Profile) {
        val existingProfile = profiles.find { it.name == profile.name }
        if (existingProfile != null) {
            existingProfile.status = profile.status
        } else {
            profiles.add(profile)
        }
    }
    fun getValidProfileNames(): List<String> {
        return profiles.filter { it.status }.map { it.name }
    }
}
 
data class Profile(
    var name: String,
    var status: Boolean
) : Serializable,Parcelable {
 
    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeByte(if (status) 1 else 0)
    }
    override fun describeContents(): Int {
        return 0
    }
    companion object CREATOR : Parcelable.Creator<Profile> {
        override fun createFromParcel(parcel: Parcel): Profile {
            val name = parcel.readString().orEmpty()
            val status = parcel.readByte() != 0.toByte()
            return Profile(name, status)
        }
        override fun newArray(size: Int): Array<Profile?> {
            return arrayOfNulls(size)
        }
    }
}
 
/**
 * val profileManager = SessionProfileManager(context)
 * profileManager.addProfile(1, "douyin", true)
 * val profiles = profileManager.getProfile(1)
 * profileManager.removeProfile(1)
 * val allProfiles = profileManager.getAllProfiles()
 */
 
class SessionProfileManager(private val context: Context) {
    companion object {
        private const val FILE_NAME = "session_profiles.json"
    }
 
    private val sessionProfiles = mutableListOf<SessionProfile>()
 
    init {
        ensureFileExists() // 确保文件存在
        loadProfiles()
    }
    // 确保文件存在
    private fun ensureFileExists() {
        val file = File(context.filesDir, FILE_NAME)
        if (!file.exists()) {
            file.writeText("[]") // 创建空 JSON 数组作为初始内容
        }
    }
 
    // 加载文件内容
    private fun loadProfiles() {
        val file = File(context.filesDir, FILE_NAME)
        val jsonString = file.readText()
        val jsonArray = JSONArray(jsonString)
        sessionProfiles.clear()
        for (i in 0 until jsonArray.length()) {
            val jsonObj = jsonArray.getJSONObject(i)
            sessionProfiles.add(jsonToSessionProfile(jsonObj))
        }
    }
 
    // 保存到文件
    private fun saveProfiles() {
        val jsonArray = JSONArray()
        sessionProfiles.forEach { profile ->
            jsonArray.put(sessionProfileToJson(profile))
        }
        val file = File(context.filesDir, FILE_NAME)
        file.writeText(jsonArray.toString())
    }
 
    fun saveToFile() {
        saveProfiles()
    }
 
    // Convert JSON to SessionProfile
    private fun jsonToSessionProfile(json: JSONObject): SessionProfile {
        val id = json.getString("id")
        val name = json.getString("name")
        val type = json.getInt("type")
        val lastUploadTime = json.getString("lastUploadTime")
        val profilesJsonArray = json.getJSONArray("profiles")
        val profiles = mutableListOf<Profile>()
        for (i in 0 until profilesJsonArray.length()) {
            val profileJson = profilesJsonArray.getJSONObject(i)
            profiles.add(Profile(profileJson.getString("name"), profileJson.getBoolean("status")))
        }
        return SessionProfile(id, name, type, lastUploadTime, profiles)
    }
 
    // Convert SessionProfile to JSON
    private fun sessionProfileToJson(profile: SessionProfile): JSONObject {
        val json = JSONObject()
        json.put("id", profile.id)
        json.put("name", profile.name)
        json.put("type", profile.type)
        json.put("lastUploadTime", profile.lastUploadTime)
        val profilesJsonArray = JSONArray()
        profile.profiles.forEach {
            val profileJson = JSONObject()
            profileJson.put("name", it.name)
            profileJson.put("status", it.status)
            profilesJsonArray.put(profileJson)
        }
        json.put("profiles", profilesJsonArray)
        return json
    }
 
    // Get all session profiles
    fun getAllProfiles(): List<SessionProfile> {
        if (sessionProfiles.isEmpty()) {
            loadProfiles()
        }
        return sessionProfiles.toList() // 将内部的 profiles 返回为不可变列表
    }
    // Add a new session profile
    fun checkProfileNameExist(name: String): Boolean {
        return sessionProfiles.any { it.name == name }
    }
 
    // Remove a session profile
    fun removeProfile(id: String) {
        sessionProfiles.removeAll { it.id == id }
        saveProfiles()
    }
 
    // 3. 添加或更新 SessionProfile
    fun addOrUpdateSessionProfile(sessionProfile: SessionProfile) {
        val existingProfile = sessionProfiles.find { it.id == sessionProfile.id }
        if (existingProfile != null) {
            val index = sessionProfiles.indexOf(existingProfile)
            sessionProfiles[index] = sessionProfile
        } else {
            // 添加新的 Profile
            sessionProfiles.add(sessionProfile)
        }
    }
}