diff options
Diffstat (limited to 'Hotline/Models')
| -rw-r--r-- | Hotline/Models/ChatMessage.swift | 35 | ||||
| -rw-r--r-- | Hotline/Models/FileInfo.swift | 31 | ||||
| -rw-r--r-- | Hotline/Models/Hotline.swift | 129 | ||||
| -rw-r--r-- | Hotline/Models/NewsCategory.swift | 34 | ||||
| -rw-r--r-- | Hotline/Models/User.swift | 33 |
5 files changed, 133 insertions, 129 deletions
diff --git a/Hotline/Models/ChatMessage.swift b/Hotline/Models/ChatMessage.swift new file mode 100644 index 0000000..8a57d61 --- /dev/null +++ b/Hotline/Models/ChatMessage.swift @@ -0,0 +1,35 @@ +import SwiftUI + +enum ChatMessageType { + case agreement + case status + case message + case server +} + +struct ChatMessage: Identifiable { + let id = UUID() + + let text: String + let type: ChatMessageType + let date: Date + let username: String? + + static let parser = /^\s*([^\:]+)\:\s*(.+)/ + + init(text: String, type: ChatMessageType, date: Date) { + self.type = type + self.date = date + + if + type == .message, + let match = text.firstMatch(of: ChatMessage.parser) { + self.username = String(match.1) + self.text = String(match.2) + } + else { + self.username = nil + self.text = text + } + } +} diff --git a/Hotline/Models/FileInfo.swift b/Hotline/Models/FileInfo.swift new file mode 100644 index 0000000..0e2c76d --- /dev/null +++ b/Hotline/Models/FileInfo.swift @@ -0,0 +1,31 @@ +import SwiftUI + +@Observable class FileInfo: Identifiable { + let id: UUID = UUID() + + let path: [String] + let name: String + + let type: String + let creator: String + let fileSize: UInt + + let isFolder: Bool + var children: [FileInfo]? = nil + + init(hotlineFile: HotlineFile) { + self.path = hotlineFile.path + self.name = hotlineFile.name + self.type = hotlineFile.type + self.creator = hotlineFile.creator + self.fileSize = UInt(hotlineFile.fileSize) + self.isFolder = hotlineFile.isFolder + if self.isFolder { + self.children = [] + } + } + + static func == (lhs: FileInfo, rhs: FileInfo) -> Bool { + return lhs.id == rhs.id + } +} diff --git a/Hotline/Models/Hotline.swift b/Hotline/Models/Hotline.swift index beb3a74..92366fa 100644 --- a/Hotline/Models/Hotline.swift +++ b/Hotline/Models/Hotline.swift @@ -1,134 +1,5 @@ import SwiftUI -enum NewsCategoryType { - case bundle - case category -} - -@Observable class NewsCategory: Identifiable, Hashable { - let id: UUID = UUID() - - let name: String - let count: UInt16 - let type: NewsCategoryType - - init(hotlineNewsCategory: HotlineNewsCategory) { - self.name = hotlineNewsCategory.name - self.count = hotlineNewsCategory.count - - if hotlineNewsCategory.type == 2 { - self.type = .bundle - } - else { - self.type = .category - } - } - - func hash(into hasher: inout Hasher) { - hasher.combine(self.id) - } - - static func == (lhs: NewsCategory, rhs: NewsCategory) -> Bool { - return lhs.id == rhs.id - } -} - -@Observable class FileInfo: Identifiable { - let id: UUID = UUID() - - let path: [String] - let name: String - - let type: String - let creator: String - let fileSize: UInt - - let isFolder: Bool - var children: [FileInfo]? = nil - - init(hotlineFile: HotlineFile) { - self.path = hotlineFile.path - self.name = hotlineFile.name - self.type = hotlineFile.type - self.creator = hotlineFile.creator - self.fileSize = UInt(hotlineFile.fileSize) - self.isFolder = hotlineFile.isFolder - if self.isFolder { - self.children = [] - } - } - - static func == (lhs: FileInfo, rhs: FileInfo) -> Bool { - return lhs.id == rhs.id - } -} - -enum ChatMessageType { - case agreement - case status - case message - case server -} - -struct ChatMessage: Identifiable { - let id = UUID() - - let text: String - let type: ChatMessageType - let date: Date - let username: String? - - static let parser = /^\s*([^\:]+)\:\s*(.+)/ - - init(text: String, type: ChatMessageType, date: Date) { - self.type = type - self.date = date - - if - type == .message, - let match = text.firstMatch(of: ChatMessage.parser) { - self.username = String(match.1) - self.text = String(match.2) - } - else { - self.username = nil - self.text = text - } - } -} - -struct UserStatus: OptionSet { - let rawValue: Int - - static let idle = UserStatus(rawValue: 1 << 0) - static let admin = UserStatus(rawValue: 1 << 1) -} - -struct User: Identifiable { - let id: UInt - var name: String - var iconID: UInt - var status: UserStatus - - init(hotlineUser: HotlineUser) { - var status: UserStatus = UserStatus() - if hotlineUser.isIdle { status.update(with: .idle) } - if hotlineUser.isAdmin { status.update(with: .admin) } - - self.id = UInt(hotlineUser.id) - self.name = hotlineUser.name - self.iconID = UInt(hotlineUser.iconID) - self.status = status - } - - init(id: UInt, name: String, iconID: UInt, status: UserStatus) { - self.id = id - self.name = name - self.iconID = iconID - self.status = status - } -} - @Observable final class Hotline: HotlineClientDelegate { let trackerClient: HotlineTrackerClient let client: HotlineClient diff --git a/Hotline/Models/NewsCategory.swift b/Hotline/Models/NewsCategory.swift new file mode 100644 index 0000000..0ba87a7 --- /dev/null +++ b/Hotline/Models/NewsCategory.swift @@ -0,0 +1,34 @@ +import SwiftUI + +enum NewsCategoryType { + case bundle + case category +} + +@Observable class NewsCategory: Identifiable, Hashable { + let id: UUID = UUID() + + let name: String + let count: UInt16 + let type: NewsCategoryType + + init(hotlineNewsCategory: HotlineNewsCategory) { + self.name = hotlineNewsCategory.name + self.count = hotlineNewsCategory.count + + if hotlineNewsCategory.type == 2 { + self.type = .bundle + } + else { + self.type = .category + } + } + + func hash(into hasher: inout Hasher) { + hasher.combine(self.id) + } + + static func == (lhs: NewsCategory, rhs: NewsCategory) -> Bool { + return lhs.id == rhs.id + } +} diff --git a/Hotline/Models/User.swift b/Hotline/Models/User.swift new file mode 100644 index 0000000..9011e56 --- /dev/null +++ b/Hotline/Models/User.swift @@ -0,0 +1,33 @@ +import SwiftUI + +struct UserStatus: OptionSet { + let rawValue: Int + + static let idle = UserStatus(rawValue: 1 << 0) + static let admin = UserStatus(rawValue: 1 << 1) +} + +struct User: Identifiable { + let id: UInt + var name: String + var iconID: UInt + var status: UserStatus + + init(hotlineUser: HotlineUser) { + var status: UserStatus = UserStatus() + if hotlineUser.isIdle { status.update(with: .idle) } + if hotlineUser.isAdmin { status.update(with: .admin) } + + self.id = UInt(hotlineUser.id) + self.name = hotlineUser.name + self.iconID = UInt(hotlineUser.iconID) + self.status = status + } + + init(id: UInt, name: String, iconID: UInt, status: UserStatus) { + self.id = id + self.name = name + self.iconID = iconID + self.status = status + } +} |