No Description

RecentGroupInfo.swift 3.1KB

    // // LocalStorage.swift // PaiAi // // Created by mac on 2016/10/28. // Copyright © 2016年 FFIB. All rights reserved. // import UIKit extension GroupItem: ArrayUserDefaultCodable { init(value: [String: AnyObject]) { self.init(JSON: value)! } func toDictionary() -> [String: AnyObject] { return toJSON() as [String : AnyObject] } } extension UserDefaults { struct GroupInfo: ArrayUserDefaultable { typealias Item = GroupItem enum ArrayDefaultKey: String { case recentGroup } } } struct RecentGroupInfo { static var share: RecentGroupInfo { return RecentGroupInfo() } private var infos: [GroupItem] private var lock: NSLock var count: Int { return infos.count } init() { infos = UserDefaults.GroupInfo.array(forKey: .recentGroup) lock = NSLock() } subscript(index: Int) -> GroupItem { return infos[index] } mutating func add(_ item: GroupItem) { lock.lock() if infos.count == 3 { infos.removeFirst() } infos.append(item) lock.unlock() } func values() -> [GroupItem] { return infos.reversed() } func save() { UserDefaults.GroupInfo.set(infos, forKey: .recentGroup) } } //func addGroupInfoToRecent(_ groupInfo: GroupModel) { // let group = NSKeyedArchiver.archivedData(withRootObject: groupInfo.toJSON()) // let md5Id = groupInfo.group_id.digestString(algorithm: .md5) // let data: [String: Data] = [md5Id: group] // if var array = UserDefaults.Group.array(forKey: .lastVisitGroup) as? [[String: Data]] { // var arr = [[String: Data]]() // for i in 0..<array.count { // if (Array(array[i].keys) as [String])[0] != md5Id { // arr.append(array[i]) // } // } // if arr.count == 3 { // arr.removeFirst() // } // arr.append(data) // UserDefaults.Group.set(arr, forKey: .lastVisitGroup) // } else { // UserDefaults.Group.set([data], forKey: .lastVisitGroup) // } //} //func searchGroupInfoRecent(index: Int) -> GroupModel { // if let locale = UserDefaults.Group.array(forKey: .lastVisitGroup) as? [[String: Data]] { // guard let dict = NSKeyedUnarchiver.unarchiveObject(with: (Array(locale[index].values) as [Data])[0]) as? [String: Any] else { // return GroupModel() // } // return GroupModel(map: Map(mappingType: .fromJSON, JSON: dict)) // } // return GroupModel() //} //func removeGroupInfoRecent(groupId: String) { // let md5Id = groupId.digestString(algorithm: .md5) // if let array = UserDefaults.Group.array(forKey: .lastVisitGroup) as? [[String: Data]] { // let newArr = array.filter({ (element) -> Bool in // return !element.keys.contains(md5Id) // }) // UserDefaults.Group.set(newArr, forKey: .lastVisitGroup) // } //} //func returnGroupInfoCount() -> Int { // if let locale = UserDefaults.Group.array(forKey: .lastVisitGroup) as? [[String: Data]] { // return locale.count // } // return 0 //}