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)
|
}
|
}
|
}
|