//
//  WP_PhotoAlbum.swift
//  WallpaperHD_Live
//
//  Created by 忆海16 on 2024/7/25.
//

import UIKit

class WP_PhotoAlbum: NSObject,Codable {
    var img:String?
    
    init(img: String?) {
            self.img = img
        }
}
// 偏好设置管理器
class PreferencesManager {
    static let shared = PreferencesManager()
    let defaults = UserDefaults.standard
    
    // 保存模型对象数组
    func saveRakModels(_ models: [WP_PhotoAlbum]) {
        do {
            let encoder = JSONEncoder()
            let encodedData = try encoder.encode(models)
            defaults.set(encodedData, forKey: "rakModels")
        } catch {
            print("Error encoding rakModels: \(error)")
        }
    }
    
    // 添加新的模型对象
    func addRakModel(_ model: WP_PhotoAlbum) {
        var existingModels = retrieveRakModels() ?? [] // 检索之前保存的数据，如果没有数据则使用空数组
        existingModels.append(model) // 将新的数据模型添加到数组中
        saveRakModels(existingModels) // 保存数组到偏好设置中
    }
    
    // 检索模型对象数组
    func retrieveRakModels() -> [WP_PhotoAlbum]? {
        if let savedData = defaults.data(forKey: "rakModels") {
            do {
                let decoder = JSONDecoder()
                let loadedModels = try decoder.decode([WP_PhotoAlbum].self, from: savedData)
                return loadedModels
            } catch {
                print("Error decoding rakModels: \(error)")
                return nil
            }
        }
        return nil
    }
}
