CNLiveSaveSearchDB.swift 2.96 KB
//
//  CNLiveFileManager.swift
//  metaCode
//
//  Created by open on 2023/11/13.
//

import Foundation

class CNLiveSaveSearchDB: NSObject {
    
    static let shared = CNLiveSaveSearchDB.init()
    
    open func save(object: Any, key: String) {
        var temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
        if (temp_dic == nil) {
            temp_dic = NSMutableDictionary.init()
        }
        temp_dic?.setObject(object, forKey: key as NSCopying)
        temp_dic?.write(toFile: userFileDir+"storage.plist", atomically: true)
    }
    
    open func take(key: String) -> Any {
        let temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
        if (temp_dic == nil) {
            return NSMutableDictionary()
        }
        return temp_dic?.object(forKey: key) as Any
    }
    
    open func takeAll() -> NSDictionary {
        let temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
        if (temp_dic == nil) {
            return NSMutableDictionary()
        }
        return temp_dic ?? NSDictionary()
    }
    
    open func resetAllSave() {
        let temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
        if temp_dic != nil {
            temp_dic?.removeAllObjects()
            temp_dic?.write(toFile: userFileDir+"storage.plist", atomically: true)
        }
    }
    
    open func delete(key: String) {
        let temp_dic = NSMutableDictionary.init(contentsOfFile: userFileDir+"storage.plist")
        if temp_dic != nil {
            temp_dic?.removeObject(forKey: key)
            temp_dic?.write(toFile: userFileDir+"storage.plist", atomically: true)
        }
    }
        
    private lazy var userFileDir: String = {
        let userFileDir = NSString()
        var path: String = ""
        path = "\(fileDir)/userId_\(UserInfoManager.shared.userInfo.uid)/"
        let fileManager = FileManager.default
        var isDir: ObjCBool = false
        let exist: Bool = fileManager.fileExists(atPath: path, isDirectory: &isDir)
        if !(isDir.boolValue && exist) {
            do {
                try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
            } catch {
                debugPrint("文件目录创建失败")
                return ""
            }
        }
        return path as String
    }()
    
    private lazy var fileDir: String = {
        let path = "\(NSHomeDirectory())/Documents/metaCode/saveSearch"
        let fileManager = FileManager.default
        var isDir: ObjCBool = false
        let exist: Bool = fileManager.fileExists(atPath: path, isDirectory: &isDir)
        if !(isDir.boolValue && exist) {
            do {
                try fileManager.createDirectory(atPath: path, withIntermediateDirectories: true)
            } catch {
                debugPrint("文件目录创建失败")
                return ""
            }
        }
        return path as String
    }()
    
}