aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Models
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2024-05-03 12:49:52 -0700
committerDustin Mierau <dustin@mierau.me>2024-05-03 12:49:52 -0700
commit02b452ccaf54cb24eb38fcb752126888994c0a1b (patch)
treec30e71a1df363092a369fd7847c6223914662ce4 /Hotline/Models
parent73208d70d21db7e526e371fa863acd0327caad9e (diff)
First pass at private messages UI.
Diffstat (limited to 'Hotline/Models')
-rw-r--r--Hotline/Models/Hotline.swift66
-rw-r--r--Hotline/Models/InstantMessage.swift18
-rw-r--r--Hotline/Models/User.swift6
3 files changed, 78 insertions, 12 deletions
diff --git a/Hotline/Models/Hotline.swift b/Hotline/Models/Hotline.swift
index 95a21c6..beed6a3 100644
--- a/Hotline/Models/Hotline.swift
+++ b/Hotline/Models/Hotline.swift
@@ -108,7 +108,6 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
]
var status: HotlineClientStatus = .disconnected
-
var server: Server? {
didSet {
self.updateServerTitle()
@@ -125,8 +124,6 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
var iconID: Int = 414
var access: HotlineUserAccessOptions?
var agreed: Bool = false
-
- var currentUser: User? = nil
var users: [User] = []
var chat: [ChatMessage] = []
var messageBoard: [String] = []
@@ -135,9 +132,10 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
var filesLoaded: Bool = false
var news: [NewsInfo] = []
var newsLoaded: Bool = false
-
+ var instantMessages: [UInt16:[InstantMessage]] = [:]
var transfers: [TransferInfo] = []
var downloads: [HotlineFileClient] = []
+ var unreadInstantMessages: [UInt16:UInt16] = [:]
@ObservationIgnored var bannerClient: HotlineFileClient?
#if os(macOS)
@@ -211,6 +209,31 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
self.client.sendAgree(username: self.username, iconID: UInt16(self.iconID), options: .none)
}
+ @MainActor func sendInstantMessage(_ text: String, userID: UInt16) {
+ let message = InstantMessage(direction: .outgoing, text: text, type: .message, date: Date())
+
+ if self.instantMessages[userID] == nil {
+ self.instantMessages[userID] = [message]
+ }
+ else {
+ self.instantMessages[userID]!.append(message)
+ }
+
+ self.client.sendInstantMessage(message: text, userID: userID)
+
+ if Prefs.shared.playPrivateMessageSound && Prefs.shared.playPrivateMessageSound {
+ SoundEffectPlayer.shared.playSoundEffect(.chatMessage)
+ }
+ }
+
+ func hasUnreadInstantMessages(userID: UInt16) -> Bool {
+ return self.unreadInstantMessages[userID] != nil
+ }
+
+ func markInstantMessagesAsRead(userID: UInt16) {
+ self.unreadInstantMessages.removeValue(forKey: userID)
+ }
+
@MainActor func sendChat(_ text: String) {
self.client.sendChat(message: text)
}
@@ -681,19 +704,43 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
}
func hotlineReceivedServerMessage(message: String) {
-// print("Hotline: received server message:\n\(message)")
-// self.chat.append(ChatMessage(text: message, type: .server, date: Date()))
+ if Prefs.shared.playChatSound && Prefs.shared.playChatSound {
+ SoundEffectPlayer.shared.playSoundEffect(.serverMessage)
+ }
+
+ print("Hotline: received server message:\n\(message)")
+ self.chat.append(ChatMessage(text: message, type: .server, date: Date()))
+ }
+
+ func hotlineReceivedPrivateMessage(userID: UInt16, message: String) {
+ if let existingUserIndex = self.users.firstIndex(where: { $0.id == UInt(userID) }) {
+ let user = self.users[existingUserIndex]
+ print("Hotline: received private message from \(user.name): \(message)")
+
+ if Prefs.shared.playPrivateMessageSound && Prefs.shared.playPrivateMessageSound {
+ SoundEffectPlayer.shared.playSoundEffect(.chatMessage)
+ }
+
+ let instantMessage = InstantMessage(direction: .incoming, text: message, type: .message, date: Date())
+ if self.instantMessages[userID] == nil {
+ self.instantMessages[userID] = [instantMessage]
+ }
+ else {
+ self.instantMessages[userID]!.append(instantMessage)
+ }
+ self.unreadInstantMessages[userID] = userID
+ }
}
func hotlineReceivedChatMessage(message: String) {
- if Prefs().playSounds && Prefs().playChatSound {
+ if Prefs.shared.playSounds && Prefs.shared.playChatSound {
SoundEffectPlayer.shared.playSoundEffect(.chatMessage)
}
self.chat.append(ChatMessage(text: message, type: .message, date: Date()))
}
func hotlineReceivedUserList(users: [HotlineUser]) {
- var existingUserIDs: [UInt] = []
+ var existingUserIDs: [UInt16] = []
var userList: [User] = []
for u in users {
@@ -701,7 +748,7 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
// If a user is already in the user list we have to assume
// they changed somehow before we received the user list
// which means let's keep their existing info.
- existingUserIDs.append(UInt(u.id))
+ existingUserIDs.append(u.id)
userList.append(self.users[i])
}
else {
@@ -827,6 +874,7 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
}
private func addOrUpdateHotlineUser(_ user: HotlineUser) {
+ print("Hotline: users: \n\(self.users)")
if let i = self.users.firstIndex(where: { $0.id == user.id }) {
print("Hotline: updating user \(self.users[i].name)")
self.users[i] = User(hotlineUser: user)
diff --git a/Hotline/Models/InstantMessage.swift b/Hotline/Models/InstantMessage.swift
new file mode 100644
index 0000000..c41e33b
--- /dev/null
+++ b/Hotline/Models/InstantMessage.swift
@@ -0,0 +1,18 @@
+import SwiftUI
+
+enum InstantMessageType {
+ case message
+}
+
+enum InstantMessageDirection {
+ case incoming
+ case outgoing
+}
+
+struct InstantMessage: Identifiable {
+ let id = UUID()
+ let direction: InstantMessageDirection
+ let text: String
+ let type: InstantMessageType
+ let date: Date
+}
diff --git a/Hotline/Models/User.swift b/Hotline/Models/User.swift
index 7fbecb0..21a8fff 100644
--- a/Hotline/Models/User.swift
+++ b/Hotline/Models/User.swift
@@ -8,7 +8,7 @@ struct UserStatus: OptionSet {
}
struct User: Identifiable {
- var id: UInt
+ var id: UInt16
var name: String
var iconID: UInt
var status: UserStatus
@@ -21,13 +21,13 @@ struct User: Identifiable {
if hotlineUser.isIdle { status.update(with: .idle) }
if hotlineUser.isAdmin { status.update(with: .admin) }
- self.id = UInt(hotlineUser.id)
+ self.id = hotlineUser.id
self.name = hotlineUser.name
self.iconID = UInt(hotlineUser.iconID)
self.status = status
}
- init(id: UInt, name: String, iconID: UInt, status: UserStatus) {
+ init(id: UInt16, name: String, iconID: UInt, status: UserStatus) {
self.id = id
self.name = name
self.iconID = iconID