|
@@ -7,7 +7,7 @@
|
|
|
import ObjectMapper
|
|
|
|
|
|
func kHandleTSHistory(){
|
|
|
- for model in TSAIRintoneHistory.listModelArray {
|
|
|
+ for model in TSAIRintoneHistory.shared.listModels {
|
|
|
if model.modelType != .example {
|
|
|
switch model.actionStatus {
|
|
|
case .pending,.running:
|
|
@@ -17,7 +17,7 @@ func kHandleTSHistory(){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- for model in TSPosterHistory.listModelArray {
|
|
|
+ for model in TSPosterHistory.shared.listModels {
|
|
|
if model.modelType != .example {
|
|
|
switch model.actionStatus {
|
|
|
case .pending,.running:
|
|
@@ -27,7 +27,7 @@ func kHandleTSHistory(){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- for model in TSPhotoHistory.listModelArray {
|
|
|
+ for model in TSPhotoHistory.shared.listModels {
|
|
|
if model.modelType != .example {
|
|
|
switch model.actionStatus {
|
|
|
case .pending,.running:
|
|
@@ -36,73 +36,130 @@ func kHandleTSHistory(){
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
-//海报历史记录
|
|
|
-class TSPosterHistory{
|
|
|
- @UserDefault(key: "kPosterTextPicHistoryListString", defaultValue: "")
|
|
|
- static private var historyString: String
|
|
|
- static var listModelArray: [TSActionInfoModel] = {
|
|
|
+
|
|
|
+// MARK: - 基础历史记录类
|
|
|
+class TSBaseHistoryManager<ModelType: TSBaseModel> {
|
|
|
+ // 子类必须重写的属性
|
|
|
+ var historyKey: String { fatalError("必须重写 historyKey") }
|
|
|
+ var exampleDataKey: String { fatalError("必须重写 exampleDataKey") }
|
|
|
+ var exampleModels: [ModelType] { fatalError("必须重写 exampleModels") }
|
|
|
+
|
|
|
+ func findModelID(modelID: Int)->Int?{
|
|
|
+ fatalError("必须重写 findModelID")
|
|
|
+ }
|
|
|
+
|
|
|
+ func saveModelAfterProcess(){
|
|
|
|
|
|
- if UserDefaults.standard.string(forKey: "insertPosterExampleData") == nil {
|
|
|
- insertExampleData()
|
|
|
- UserDefaults.standard.set("1", forKey: "insertPosterExampleData")
|
|
|
- UserDefaults.standard.synchronize()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 存储属性
|
|
|
+ private var _historyString: String {
|
|
|
+ get { UserDefaults.standard.string(forKey: historyKey) ?? "" }
|
|
|
+ set { UserDefaults.standard.set(newValue, forKey: historyKey) }
|
|
|
+ }
|
|
|
+
|
|
|
+ private var _listModels: [ModelType]?
|
|
|
+ var listModels: [ModelType] {
|
|
|
+ get {
|
|
|
+ if _listModels == nil { loadModels() }
|
|
|
+ return _listModels ?? []
|
|
|
}
|
|
|
-
|
|
|
- if let listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: historyString){
|
|
|
- return listModelArray
|
|
|
+ set {
|
|
|
+ _listModels = newValue
|
|
|
}
|
|
|
- return []
|
|
|
- }()
|
|
|
+ }
|
|
|
|
|
|
- static func saveModel(model:TSActionInfoModel){
|
|
|
- self.listModelArray.insert(model, at: 0)
|
|
|
- self.saveHistoryString()
|
|
|
+ // MARK: - 公共方法
|
|
|
+ func saveModel(model: ModelType, at index: Int = 0) {
|
|
|
+ listModels.insert(model, at: index)
|
|
|
+ saveHistory()
|
|
|
+ saveModelAfterProcess()
|
|
|
}
|
|
|
|
|
|
- static func removeModel(model:TSActionInfoModel){
|
|
|
- self.listModelArray.removeAll { $0 === model }
|
|
|
- self.saveHistoryString()
|
|
|
+ func removeModel(model:ModelType) {
|
|
|
+ self.listModels.removeAll { $0 === model }
|
|
|
+ saveHistory()
|
|
|
}
|
|
|
|
|
|
+ func removeModel(index: Int) {
|
|
|
+ guard index >= 0 && index < listModels.count else { return }
|
|
|
+ listModels.remove(at: index)
|
|
|
+ saveHistory()
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- static func replaceModel(oldID: Int, newModel: TSActionInfoModel) {
|
|
|
- if let index = self.listModelArray.firstIndex(where: {$0.id == oldID}) {
|
|
|
- self.listModelArray[index] = newModel
|
|
|
- dePrint("TSPosterHistory.listModelArray Model replaced at index \(index)")
|
|
|
+ func replaceModel(oldID: Int, newModel: ModelType){
|
|
|
+ if let index = findModelID(modelID: oldID) {
|
|
|
+ listModels[index] = newModel
|
|
|
+ dePrint("\(Self.self).listModels Model replaced at index \(index)")
|
|
|
} else {
|
|
|
- self.listModelArray.insert(newModel, at: 0)
|
|
|
- dePrint("TSPosterHistory.listModel ArrayModel not found")
|
|
|
+ listModels.insert(newModel, at: 0)
|
|
|
+ dePrint("\(Self.self).listModels Model not found")
|
|
|
}
|
|
|
- dePrint("TSPosterHistory.listModelArray.count=\(TSAIRintoneHistory.listModelArray.count)")
|
|
|
- self.saveHistoryString()
|
|
|
+ dePrint("\(Self.self).listModels.count=\(listModels.count)")
|
|
|
+ saveHistory()
|
|
|
}
|
|
|
|
|
|
- static func removeIndex(index:Int){
|
|
|
- self.listModelArray.remove(at: index)
|
|
|
- self.saveHistoryString()
|
|
|
+ func dePrintAllModel() {
|
|
|
+ dePrint("=======================结果查询开始======================")
|
|
|
+ dePrint("\(Self.self).listModels.count=\(listModels.count)")
|
|
|
+ for model in listModels {
|
|
|
+ dePrint(model.toJSON())
|
|
|
+ }
|
|
|
+ dePrint("=======================结果查询结束======================")
|
|
|
}
|
|
|
|
|
|
- static func saveHistoryString(){
|
|
|
- if let jsonString = listModelArray.toJSONString() {
|
|
|
- historyString = jsonString
|
|
|
+ // MARK: - 私有方法
|
|
|
+ private func saveHistory() {
|
|
|
+ if let jsonString = listModels.toJSONString() {
|
|
|
+ _historyString = jsonString
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static func insertExampleData(){
|
|
|
- let array = [
|
|
|
- self.createExampleModel(imageName: "poster_example_0"),
|
|
|
- self.createExampleModel(imageName: "poster_example_1"),
|
|
|
- self.createExampleModel(imageName: "poster_example_2")
|
|
|
- ]
|
|
|
- if let jsonString = array.toJSONString() {
|
|
|
- self.historyString = jsonString
|
|
|
+ private func loadModels() {
|
|
|
+ if exampleModels.count > 0 {
|
|
|
+ // 第一次运行时插入示例数据
|
|
|
+ if UserDefaults.standard.string(forKey: exampleDataKey) == nil {
|
|
|
+ insertExampleData()
|
|
|
+ UserDefaults.standard.set("1", forKey: exampleDataKey)
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ // 从历史记录加载模型
|
|
|
+ if let models = Mapper<ModelType>().mapArray(JSONString: _historyString) {
|
|
|
+ _listModels = models
|
|
|
+ } else {
|
|
|
+ _listModels = []
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private func insertExampleData() {
|
|
|
+ if let jsonString = exampleModels.toJSONString() {
|
|
|
+ _historyString = jsonString
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// MARK: - 海报历史记录
|
|
|
+final class TSPosterHistory: TSBaseHistoryManager<TSActionInfoModel> {
|
|
|
+ static let shared = TSPosterHistory()
|
|
|
+ override var historyKey: String { "kPosterTextPicHistoryListString" }
|
|
|
+ override var exampleDataKey: String { "insertPosterExampleData" }
|
|
|
+
|
|
|
+ override func findModelID(modelID: Int) -> Int? {
|
|
|
+ return listModels.firstIndex(where: {$0.id == modelID})
|
|
|
+ }
|
|
|
+
|
|
|
+ override var exampleModels: [TSActionInfoModel] {
|
|
|
+ [
|
|
|
+ createExampleModel(imageName: "poster_example_0"),
|
|
|
+ createExampleModel(imageName: "poster_example_1"),
|
|
|
+ createExampleModel(imageName: "poster_example_2")
|
|
|
+ ]
|
|
|
}
|
|
|
|
|
|
- private static func createExampleModel(imageName:String)->TSActionInfoModel{
|
|
|
+ private func createExampleModel(imageName: String) -> TSActionInfoModel {
|
|
|
let model = TSActionInfoModel()
|
|
|
model.modelType = .example
|
|
|
model.request.prompt = "Example"
|
|
@@ -113,68 +170,25 @@ class TSPosterHistory{
|
|
|
return model
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-//头像历史记录
|
|
|
-class TSPhotoHistory{
|
|
|
- @UserDefault(key: "kPhotoTextPicHistoryListString", defaultValue: "")
|
|
|
- static private var historyString: String
|
|
|
- static var listModelArray: [TSActionInfoModel] = {
|
|
|
- if UserDefaults.standard.string(forKey: "insertPhotoExampleData") == nil {
|
|
|
- insertExampleData()
|
|
|
- UserDefaults.standard.set("1", forKey: "insertPhotoExampleData")
|
|
|
- UserDefaults.standard.synchronize()
|
|
|
- }
|
|
|
- if let listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: historyString){
|
|
|
- return listModelArray
|
|
|
- }
|
|
|
- return []
|
|
|
- }()
|
|
|
-
|
|
|
- static func saveModel(model:TSActionInfoModel){
|
|
|
- self.listModelArray.insert(model, at: 0)
|
|
|
- self.saveHistoryString()
|
|
|
- }
|
|
|
+// MARK: - 头像历史记录
|
|
|
+final class TSPhotoHistory: TSBaseHistoryManager<TSActionInfoModel> {
|
|
|
+ static let shared = TSPhotoHistory()
|
|
|
+ override var historyKey: String { "kPhotoTextPicHistoryListString" }
|
|
|
+ override var exampleDataKey: String { "insertPhotoExampleData" }
|
|
|
|
|
|
- static func removeModel(model:TSActionInfoModel){
|
|
|
- self.listModelArray.removeAll { $0 === model }
|
|
|
- self.saveHistoryString()
|
|
|
+ override func findModelID(modelID: Int) -> Int? {
|
|
|
+ return listModels.firstIndex(where: {$0.id == modelID})
|
|
|
}
|
|
|
|
|
|
- static func removeIndex(index:Int){
|
|
|
- self.listModelArray.remove(at: index)
|
|
|
- self.saveHistoryString()
|
|
|
- }
|
|
|
-
|
|
|
- static func replaceModel(oldID: Int, newModel: TSActionInfoModel) {
|
|
|
- if let index = self.listModelArray.firstIndex(where: {$0.id == oldID}) {
|
|
|
- self.listModelArray[index] = newModel
|
|
|
- dePrint("TSPhotoHistory.listModelArray Model replaced at index \(index)")
|
|
|
- } else {
|
|
|
- self.listModelArray.insert(newModel, at: 0)
|
|
|
- dePrint("TSPhotoHistory.listModel ArrayModel not found")
|
|
|
- }
|
|
|
- dePrint("TSPhotoHistory.listModelArray.count=\(TSAIRintoneHistory.listModelArray.count)")
|
|
|
- self.saveHistoryString()
|
|
|
- }
|
|
|
-
|
|
|
- static func saveHistoryString(){
|
|
|
- if let jsonString = listModelArray.toJSONString() {
|
|
|
- self.historyString = jsonString
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static func insertExampleData(){
|
|
|
- let array = [
|
|
|
- self.createExampleModel(imageName: "photo_example_0"),
|
|
|
- self.createExampleModel(imageName: "photo_example_1"),
|
|
|
- self.createExampleModel(imageName: "photo_example_2")
|
|
|
+ override var exampleModels: [TSActionInfoModel] {
|
|
|
+ [
|
|
|
+ createExampleModel(imageName: "photo_example_0"),
|
|
|
+ createExampleModel(imageName: "photo_example_1"),
|
|
|
+ createExampleModel(imageName: "photo_example_2")
|
|
|
]
|
|
|
- if let jsonString = array.toJSONString() {
|
|
|
- self.historyString = jsonString
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
- private static func createExampleModel(imageName:String)->TSActionInfoModel{
|
|
|
+ private func createExampleModel(imageName: String) -> TSActionInfoModel {
|
|
|
let model = TSActionInfoModel()
|
|
|
model.modelType = .example
|
|
|
model.request.prompt = "Example"
|
|
@@ -185,83 +199,23 @@ class TSPhotoHistory{
|
|
|
return model
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-//AI铃声历史记录
|
|
|
-class TSAIRintoneHistory{
|
|
|
- @UserDefault(key: "kRintoneTextMusicHistoryListString", defaultValue: "")
|
|
|
- static private var historyString: String
|
|
|
- static var listModelArray: [TSActionInfoModel] = {
|
|
|
-
|
|
|
- if UserDefaults.standard.string(forKey: "insertRintoneExampleData") == nil {
|
|
|
- insertExampleData()
|
|
|
- UserDefaults.standard.set("1", forKey: "insertRintoneExampleData")
|
|
|
- UserDefaults.standard.synchronize()
|
|
|
- }
|
|
|
-
|
|
|
- if let listModelArray = Mapper<TSActionInfoModel>().mapArray(JSONString: historyString){
|
|
|
- return listModelArray
|
|
|
- }
|
|
|
- return []
|
|
|
- }()
|
|
|
-
|
|
|
- static func saveModel(model:TSActionInfoModel){
|
|
|
- //若存在,则不更新替换
|
|
|
- if let index = self.listModelArray.firstIndex(where: { $0.id == model.id }) {
|
|
|
- self.listModelArray[index] = model
|
|
|
- }else{
|
|
|
- self.listModelArray.insert(model, at: 0)
|
|
|
- }
|
|
|
- self.saveHistoryString()
|
|
|
- }
|
|
|
-
|
|
|
- static func removeModel(model:TSActionInfoModel){
|
|
|
- self.listModelArray.removeAll { $0.id == model.id }
|
|
|
- self.saveHistoryString()
|
|
|
- }
|
|
|
-
|
|
|
- static func replaceModel(oldID: Int, newModel: TSActionInfoModel) {
|
|
|
- if let index = self.listModelArray.firstIndex(where: {$0.id == oldID}) {
|
|
|
- self.listModelArray[index] = newModel
|
|
|
- dePrint("TSAIRintoneHistory.listModelArray Model replaced at index \(index)")
|
|
|
- } else {
|
|
|
- self.listModelArray.insert(newModel, at: 0)
|
|
|
- dePrint("TSAIRintoneHistory.listModel ArrayModel not found")
|
|
|
- }
|
|
|
- dePrint("TSAIRintoneHistory.listModelArray.count=\(TSAIRintoneHistory.listModelArray.count)")
|
|
|
- self.saveHistoryString()
|
|
|
- }
|
|
|
-
|
|
|
- static func removeIndex(index:Int){
|
|
|
- self.listModelArray.remove(at: index)
|
|
|
- self.saveHistoryString()
|
|
|
- }
|
|
|
+// MARK: - AI铃声历史记录
|
|
|
+final class TSAIRintoneHistory: TSBaseHistoryManager<TSActionInfoModel> {
|
|
|
+ static let shared = TSAIRintoneHistory()
|
|
|
+ override var historyKey: String { "kRintoneTextMusicHistoryListString" }
|
|
|
+ override var exampleDataKey: String { "insertRintoneExampleData" }
|
|
|
|
|
|
- static func saveHistoryString(){
|
|
|
- if let jsonString = listModelArray.toJSONString() {
|
|
|
- self.historyString = jsonString
|
|
|
- }
|
|
|
+ override func findModelID(modelID: Int) -> Int? {
|
|
|
+ return listModels.firstIndex(where: {$0.id == modelID})
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- static func dePrintAllModel(){
|
|
|
- dePrint("=======================结果查询开始======================")
|
|
|
- dePrint("TSAIRintoneHistory.listModelArray.count=\(TSAIRintoneHistory.listModelArray.count)")
|
|
|
- for model in TSAIRintoneHistory.listModelArray {
|
|
|
- dePrint(model.toJSON())
|
|
|
- }
|
|
|
- dePrint("=======================结果查询结束======================")
|
|
|
- }
|
|
|
-
|
|
|
- private static func insertExampleData(){
|
|
|
- let array = [
|
|
|
- self.createExampleModel(),
|
|
|
+ override var exampleModels: [TSActionInfoModel] {
|
|
|
+ [
|
|
|
+ createExampleModel()
|
|
|
]
|
|
|
- if let jsonString = array.toJSONString() {
|
|
|
- historyString = jsonString
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
- private static func createExampleModel()->TSActionInfoModel{
|
|
|
+ private func createExampleModel()->TSActionInfoModel{
|
|
|
let model = TSActionInfoModel()
|
|
|
model.modelType = .example
|
|
|
model.request.duration = 30
|
|
@@ -273,45 +227,24 @@ class TSAIRintoneHistory{
|
|
|
return model
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-//我的下载铃声历史记录
|
|
|
-class TSMineRintoneHistory{
|
|
|
- @UserDefault(key: "kMineRintoneHistoryListString", defaultValue: "")
|
|
|
- static private var historyString: String
|
|
|
- static var listModelArray: [TSRingModel] = {
|
|
|
-
|
|
|
- if let listModelArray = Mapper<TSRingModel>().mapArray(JSONString: historyString){
|
|
|
- return listModelArray
|
|
|
- }
|
|
|
- return []
|
|
|
- }()
|
|
|
+// MARK: - 我的下载铃声历史记录
|
|
|
+final class TSMineRintoneHistory: TSBaseHistoryManager<TSRingModel> {
|
|
|
+ static let shared = TSMineRintoneHistory()
|
|
|
+ override var historyKey: String { "kMineRintoneHistoryListString" }
|
|
|
+ override var exampleDataKey: String { "insertMineRintoneExampleData" }
|
|
|
|
|
|
- static func saveModel(model:TSRingModel){
|
|
|
- if let _ = self.listModelArray.first(where: { $0.audioUrl == model.audioUrl }) {
|
|
|
- return
|
|
|
- }
|
|
|
- self.listModelArray.insert(model, at: 0)
|
|
|
- self.saveHistoryString()
|
|
|
- isHaveNew = true
|
|
|
- }
|
|
|
+ var isHaveNew:Bool = false
|
|
|
|
|
|
- static func removeModel(model:TSRingModel){
|
|
|
- listModelArray.removeAll { $0 === model }
|
|
|
- self.saveHistoryString()
|
|
|
+ override func findModelID(modelID: Int) -> Int? {
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
- static func removeIndex(index:Int){
|
|
|
- listModelArray.remove(at: index)
|
|
|
- self.saveHistoryString()
|
|
|
+ override var exampleModels: [TSRingModel] {
|
|
|
+ []
|
|
|
}
|
|
|
|
|
|
- static func saveHistoryString(){
|
|
|
- if let jsonString = listModelArray.toJSONString() {
|
|
|
- historyString = jsonString
|
|
|
- }
|
|
|
+ override func saveModelAfterProcess() {
|
|
|
+ isHaveNew = true
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- static var isHaveNew:Bool = false
|
|
|
}
|
|
|
+
|