initial commit
This commit is contained in:
73
kordophone2/Models.swift
Normal file
73
kordophone2/Models.swift
Normal file
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// Models.swift
|
||||
// kordophone2
|
||||
//
|
||||
// Created by James Magahern on 8/24/25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import XPC
|
||||
|
||||
enum Display
|
||||
{
|
||||
struct Conversation: Identifiable
|
||||
{
|
||||
let id: String
|
||||
let name: String?
|
||||
let participants: [String]
|
||||
let messagePreview: String
|
||||
|
||||
var displayName: String {
|
||||
if let name, name.count > 0 { return name }
|
||||
else { return participants.joined(separator: ", ") }
|
||||
}
|
||||
|
||||
init(from c: Serialized.Conversation) {
|
||||
self.id = c.guid
|
||||
self.name = c.displayName
|
||||
self.participants = c.participants
|
||||
self.messagePreview = c.lastMessagePreview ?? ""
|
||||
}
|
||||
|
||||
init(id: String = UUID().uuidString, name: String? = nil, participants: [String], messagePreview: String) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.participants = participants
|
||||
self.messagePreview = messagePreview
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Serialized
|
||||
{
|
||||
struct Conversation: Decodable
|
||||
{
|
||||
let guid: String
|
||||
let displayName: String?
|
||||
let participants: [String]
|
||||
let lastMessagePreview: String?
|
||||
let unreadCount: Int
|
||||
let date: Date
|
||||
|
||||
init?(xpc dict: xpc_object_t)
|
||||
{
|
||||
guard let g: String = dict["guid"] else { return nil }
|
||||
|
||||
let dn: String? = dict["display_name"]
|
||||
let lmp: String? = dict["last_message_preview"]
|
||||
|
||||
let names: [String] = dict["participants"] ?? []
|
||||
|
||||
let unread: Int = dict["unread_count"] ?? 0
|
||||
|
||||
let dt: Date = dict["date"] ?? Date(timeIntervalSince1970: 0)
|
||||
|
||||
self.guid = g
|
||||
self.displayName = dn
|
||||
self.participants = names
|
||||
self.lastMessagePreview = lmp
|
||||
self.unreadCount = unread
|
||||
self.date = dt
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user