pushdeer/ios/PushDeer-iOS/PushDeer/Model/MessageModel.swift
hext c3d0cd13f5 新增功能: 桌面小部件, 展示最近消息, 支持收到推送自动刷新;
新增功能: 1分钟内删除两条消息, 会提示是否一键清空全部消息.
2022-04-19 00:35:56 +08:00

101 lines
3.5 KiB
Swift

//
// MessageModel.swift
// PushDeer
//
// Created by HEXT on 2022/1/15.
//
import Foundation
import CoreData
extension MessageModel {
convenience init(id: Int64, uid: String, text: String, desp: String, type: String, pushkey_name: String, created_at: String, context: NSManagedObjectContext = PersistenceController.shared.container.viewContext) {
self.init(context: context)
self.id = id
self.uid = uid
self.text = text
self.desp = desp
self.type = type
self.pushkey_name = pushkey_name
self.created_at = created_at
}
convenience init(messageItem: MessageItem, context: NSManagedObjectContext = PersistenceController.shared.container.viewContext) {
self.init(
id: Int64(messageItem.id),
uid: messageItem.uid,
text: messageItem.text,
desp: messageItem.desp,
type: messageItem.type,
pushkey_name: messageItem.pushkey_name,
created_at: messageItem.created_at,
context: context)
}
var createdDateStr: String {
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
let createdDate = dateFormatter.date(from: self.created_at ?? "") ?? Date()
let timeInterval = -createdDate.timeIntervalSinceNow
let minute = Int(floor(timeInterval / 60))
if minute == 0 {
return "刚刚"
} else if minute <= 30 {
return "\(minute)分钟前"
} else if Calendar.current.isDateInToday(createdDate) {
dateFormatter.dateFormat = "HH:mm:ss"
} else {
dateFormatter.dateFormat = "yyyy/MM/dd HH:mm:ss"
}
return dateFormatter.string(from: createdDate)
}
static let _viewContext = PersistenceController.shared.container.viewContext
static let _fetchRequest = MessageModel.fetchRequest()
///
static func deleteAll() throws -> Void {
// let fetchRequest: NSFetchRequest<NSFetchRequestResult> = MessageModel.fetchRequest()
// let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
// try _viewContext.execute(deleteRequest)
let fetchRequest = MessageModel.fetchRequest()
let models = try _viewContext.fetch(fetchRequest)
models.forEach { model in
_viewContext.delete(model)
}
try _viewContext.save()
}
///
static func saveAndUpdate(messageItems: [MessageItem]) throws -> Void {
try messageItems.forEach(saveAndUpdate)
}
///
static func saveAndUpdate(messageItem: MessageItem) throws -> Void {
_fetchRequest.predicate = NSPredicate(format: "id = \(messageItem.id)")
let models = try _viewContext.fetch(_fetchRequest)
if models.isEmpty {
// , context
_ = MessageModel(messageItem: messageItem, context: _viewContext)
} else {
// , ,
models.enumerated().forEach { element in
let messageModel = element.element
let index = element.offset
if index == 0 {
messageModel.id = Int64(messageItem.id);
messageModel.uid = messageItem.uid;
messageModel.text = messageItem.text;
messageModel.desp = messageItem.desp;
messageModel.type = messageItem.type;
messageModel.pushkey_name = messageItem.pushkey_name;
messageModel.created_at = messageItem.created_at;
} else {
_viewContext.delete(messageModel)
}
}
}
// context
try _viewContext.save()
}
}