another.im-ios/Monal/another.im/XMPP/MonalWrapperModels.swift
2024-11-28 16:50:59 +01:00

111 lines
2.7 KiB
Swift

import Foundation
import monalxmpp
import SwiftUI
// MARK: - Account
enum AccountsAvailability {
case noAccounts
case allDisabled
case someEnabled
}
struct Account: Identifiable {
let id: Int
let local: String
let domain: String
let resource: String
let isEnabled: Bool
var jid: String {
"\(local)@\(domain)"
}
init?(_ dict: NSDictionary) {
guard let id = dict.value(forKey: "account_id") as? Int else { return nil }
guard let local = dict.value(forKey: "username") as? String else { return nil }
guard let domain = dict.value(forKey: "domain") as? String else { return nil }
guard let resource = dict.value(forKey: "resource") as? String else { return nil }
guard let isEnabled = dict.value(forKey: "enabled") as? Bool else { return nil }
self.id = id
self.local = local
self.domain = domain
self.resource = resource
self.isEnabled = isEnabled
}
}
extension Account: UniversalInputSelectionElement {
var icon: Image? { nil }
var text: String? { jid }
}
// MARK: - Contact
struct Contact: Identifiable {
let ownerId: Int
let contactJid: String
let name: String?
var id: String { contactJid }
init?(_ obj: MLContact) {
ownerId = obj.accountID.intValue
contactJid = obj.contactJid
name = obj.nickName.isEmpty ? nil : obj.nickName
}
}
// MARK: - Chat
struct Chat: Identifiable {
let accountId: Int
let participantJid: String
let participantName: String?
var id: String {
"\(accountId)_\(participantJid)"
}
var name: String {
if let participantName, !participantName.isEmpty {
return participantName
} else {
return participantJid
}
}
init?(_ obj: MLContact) {
guard let accId = obj.accountID as? Int else { return nil }
accountId = accId
participantJid = obj.contactJid
participantName = obj.nickName
}
}
// MARK: Message
struct Message: Identifiable {
let accountId: Int
let participantJid: String
let dbId: Int
let stanzaId: String
let timestamp: Date
let body: String
let isInbound: Bool
let encrypted: Bool
var id: String {
"\(accountId)|\(dbId)"
}
init?(_ obj: MLMessage) {
guard let accId = obj.accountID as? Int, let dbId = obj.messageDBId as? Int else { return nil }
accountId = accId
participantJid = obj.participantJid
self.dbId = dbId
stanzaId = obj.stanzaId
timestamp = obj.timestamp
body = obj.messageText
isInbound = obj.inbound
encrypted = obj.encrypted
}
}