aboutsummaryrefslogtreecommitdiff
path: root/Hotline
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2024-05-03 13:04:56 -0700
committerDustin Mierau <dustin@mierau.me>2024-05-03 13:04:56 -0700
commit5d61fbfd67c3f59bcffa997590d1bc7d41e61528 (patch)
tree799877739a5b5c80d8381352897ce3510f71d8d3 /Hotline
parent02b452ccaf54cb24eb38fcb752126888994c0a1b (diff)
Remember banner toolbar displayed state. Clean up use of preferences throughout project.
Diffstat (limited to 'Hotline')
-rw-r--r--Hotline/Application-macOS.swift10
-rw-r--r--Hotline/Models/Hotline.swift8
-rw-r--r--Hotline/Models/Preferences.swift59
-rw-r--r--Hotline/Sounds/Application-iOS.swift1
-rw-r--r--Hotline/macOS/ServerView.swift39
-rw-r--r--Hotline/macOS/SettingsView.swift12
6 files changed, 31 insertions, 98 deletions
diff --git a/Hotline/Application-macOS.swift b/Hotline/Application-macOS.swift
index 4cad224..ec42a89 100644
--- a/Hotline/Application-macOS.swift
+++ b/Hotline/Application-macOS.swift
@@ -35,7 +35,6 @@ struct Application: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
- @State private var preferences = Prefs()
@State private var soundEffects = SoundEffectPlayer()
@State private var bookmarks = Bookmarks()
@State private var hotlinePanel: HotlinePanel? = nil
@@ -55,7 +54,9 @@ struct Application: App {
.defaultPosition(.center)
.onChange(of: AppLaunchState.shared.launchState) {
if AppLaunchState.shared.launchState == .launched {
- showBannerWindow()
+ if Prefs.shared.showBannerToolbar {
+ showBannerWindow()
+ }
}
}
@@ -63,7 +64,6 @@ struct Application: App {
WindowGroup(id: "server", for: Server.self) { server in
ServerView(server: server)
.frame(minWidth: 430, minHeight: 300)
- .environment(preferences)
.environment(soundEffects)
.environment(bookmarks)
} defaultValue: {
@@ -163,7 +163,6 @@ struct Application: App {
// MARK: Settings Window
Settings {
SettingsView()
- .environment(preferences)
}
// MARK: News Editor Window
@@ -204,6 +203,7 @@ struct Application: App {
if hotlinePanel?.isVisible == false {
hotlinePanel?.orderFront(nil)
+ Prefs.shared.showBannerToolbar = true
}
}
@@ -214,9 +214,11 @@ struct Application: App {
if hotlinePanel?.isVisible == true {
hotlinePanel?.orderOut(nil)
+ Prefs.shared.showBannerToolbar = false
}
else {
hotlinePanel?.orderFront(nil)
+ Prefs.shared.showBannerToolbar = true
}
}
}
diff --git a/Hotline/Models/Hotline.swift b/Hotline/Models/Hotline.swift
index beed6a3..564845a 100644
--- a/Hotline/Models/Hotline.swift
+++ b/Hotline/Models/Hotline.swift
@@ -673,7 +673,7 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
self.deleteAllTransfers()
}
else if status == .loggedIn {
- if Prefs().playSounds && Prefs().playLoggedInSound {
+ if Prefs.shared.playSounds && Prefs.shared.playLoggedInSound {
SoundEffectPlayer.shared.playSoundEffect(.loggedIn)
}
}
@@ -772,7 +772,7 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
let user = self.users.remove(at: existingUserIndex)
self.chat.append(ChatMessage(text: "\(user.name) left", type: .status, date: Date()))
- if Prefs().playSounds && Prefs().playLeaveSound {
+ if Prefs.shared.playSounds && Prefs.shared.playLeaveSound {
SoundEffectPlayer.shared.playSoundEffect(.userLogout)
}
}
@@ -857,7 +857,7 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
let transfer = self.transfers[i]
transfer.fileURL = at
transfer.downloadCallback?(transfer, at)
- if Prefs().playSounds && Prefs().playFileTransferCompleteSound {
+ if Prefs.shared.playSounds && Prefs.shared.playFileTransferCompleteSound {
SoundEffectPlayer.shared.playSoundEffect(.transferComplete)
}
}
@@ -881,7 +881,7 @@ class Hotline: Equatable, HotlineClientDelegate, HotlineFileClientDelegate {
}
else {
if !self.users.isEmpty {
- if Prefs().playSounds && Prefs().playJoinSound {
+ if Prefs.shared.playSounds && Prefs.shared.playJoinSound {
SoundEffectPlayer.shared.playSoundEffect(.userLogin)
}
}
diff --git a/Hotline/Models/Preferences.swift b/Hotline/Models/Preferences.swift
index 2266f83..3925bc7 100644
--- a/Hotline/Models/Preferences.swift
+++ b/Hotline/Models/Preferences.swift
@@ -16,6 +16,7 @@ enum PrefsKeys: String {
case playLoggedInSound = "play logged in sound"
case playErrorSound = "play error sound"
case playChatInvitationSound = "play chat invitation sound"
+ case showBannerToolbar = "show banner toolbar"
}
@Observable
@@ -37,6 +38,7 @@ class Prefs {
PrefsKeys.playLoggedInSound.rawValue: true,
PrefsKeys.playErrorSound.rawValue: true,
PrefsKeys.playChatInvitationSound.rawValue: true,
+ PrefsKeys.showBannerToolbar.rawValue: true,
])
self.username = UserDefaults.standard.string(forKey: PrefsKeys.username.rawValue)!
@@ -54,6 +56,7 @@ class Prefs {
self.playLoggedInSound = UserDefaults.standard.bool(forKey: PrefsKeys.playLoggedInSound.rawValue)
self.playErrorSound = UserDefaults.standard.bool(forKey: PrefsKeys.playErrorSound.rawValue)
self.playChatInvitationSound = UserDefaults.standard.bool(forKey: PrefsKeys.playChatInvitationSound.rawValue)
+ self.showBannerToolbar = UserDefaults.standard.bool(forKey: PrefsKeys.showBannerToolbar.rawValue)
}
public static let shared = Prefs()
@@ -117,56 +120,8 @@ class Prefs {
var automaticMessage: String {
didSet { UserDefaults.standard.set(self.automaticMessage, forKey: PrefsKeys.automaticMessage.rawValue) }
}
+
+ var showBannerToolbar: Bool {
+ didSet { UserDefaults.standard.set(self.showBannerToolbar, forKey: PrefsKeys.showBannerToolbar.rawValue) }
+ }
}
-
-//@Observable
-//final class Preferences {
-//
-// var username: String {
-// get {
-// access(keyPath: \.username)
-// return UserDefaults.standard.object(forKey: "username") as? String ?? "guest"
-// }
-// set {
-// withMutation(keyPath: \.username) {
-// UserDefaults.standard.set(newValue, forKey: "username")
-// }
-// }
-// }
-//
-// var refusePrivateMessages: Bool {
-// get { return UserDefaults.standard.object(forKey: "refuse private messages") as? Bool ?? false }
-// set { UserDefaults.standard.set(newValue, forKey: "refuse private messages") }
-// }
-//
-// var refusePrivateChat: Bool {
-// get { return UserDefaults.standard.object(forKey: "refuse private chat") as? Bool ?? false }
-// set { UserDefaults.standard.set(newValue, forKey: "refuse private chat") }
-// }
-//
-// var automaticResponseEnabled: Bool {
-// get { return UserDefaults.standard.object(forKey: "enable automatic response") as? Bool ?? false }
-// set { UserDefaults.standard.set(newValue, forKey: "enable automatic response") }
-// }
-//
-// var automaticResponse: String {
-// get { return UserDefaults.standard.object(forKey: "automatic response") as? String ?? "" }
-// set { UserDefaults.standard.set(newValue, forKey: "automatic response") }
-// }
-//
-// var userIconID: Int {
-// get { return UserDefaults.standard.object(forKey: "user icon") as? Int ?? 404 }
-// set { UserDefaults.standard.set(newValue, forKey: "user icon") }
-// }
-//
-//// @AppStorage("username") public var username: String = "guest"
-//// @AppStorage("refuse private messages") public var refusePrivateMessages: Bool = false
-//// @AppStorage("refuse private chat") public var refusePrivateChat: Bool = false
-//// @AppStorage("enable automatic response") public var enableAutomaticResponse: Bool = false
-//// @AppStorage("automatic response") public var automaticResponse: String = ""
-////
-//// // Icon
-//// @AppStorage("user icon id") public var iconID: Int = 404
-//
-// public static let shared = Preferences()
-//}
diff --git a/Hotline/Sounds/Application-iOS.swift b/Hotline/Sounds/Application-iOS.swift
index 5d9c56d..055c8f1 100644
--- a/Hotline/Sounds/Application-iOS.swift
+++ b/Hotline/Sounds/Application-iOS.swift
@@ -8,7 +8,6 @@ import UniformTypeIdentifiers
struct Application: App {
private var model = Hotline(trackerClient: HotlineTrackerClient(), client: HotlineClient())
- @State private var preferences = Prefs()
@State private var soundEffects = SoundEffectPlayer()
@State private var bookmarks = Bookmarks()
diff --git a/Hotline/macOS/ServerView.swift b/Hotline/macOS/ServerView.swift
index b3ab302..01651d3 100644
--- a/Hotline/macOS/ServerView.swift
+++ b/Hotline/macOS/ServerView.swift
@@ -119,7 +119,6 @@ enum ServerNavigationType: Identifiable, Hashable, Equatable {
}
struct ServerView: View {
- @Environment(Prefs.self) private var preferences: Prefs
@Environment(SoundEffectPlayer.self) private var soundEffects: SoundEffectPlayer
@Environment(Bookmarks.self) private var bookmarks: Bookmarks
@Environment(\.dismiss) var dismiss
@@ -182,12 +181,12 @@ struct ServerView: View {
else {
serverView
.environment(model)
- .onChange(of: preferences.userIconID) { sendPreferences() }
- .onChange(of: preferences.username) { sendPreferences() }
- .onChange(of: preferences.refusePrivateMessages) { sendPreferences() }
- .onChange(of: preferences.refusePrivateChat) { sendPreferences() }
- .onChange(of: preferences.enableAutomaticMessage) { sendPreferences() }
- .onChange(of: preferences.automaticMessage) { sendPreferences() }
+ .onChange(of: Prefs.shared.userIconID) { sendPreferences() }
+ .onChange(of: Prefs.shared.username) { sendPreferences() }
+ .onChange(of: Prefs.shared.refusePrivateMessages) { sendPreferences() }
+ .onChange(of: Prefs.shared.refusePrivateChat) { sendPreferences() }
+ .onChange(of: Prefs.shared.enableAutomaticMessage) { sendPreferences() }
+ .onChange(of: Prefs.shared.automaticMessage) { sendPreferences() }
.toolbar {
ToolbarItem(placement: .navigation) {
Image(systemName: "globe.americas.fill")
@@ -453,7 +452,7 @@ struct ServerView: View {
return
}
- model.login(server: server, username: preferences.username, iconID: preferences.userIconID) { success in
+ model.login(server: server, username: Prefs.shared.username, iconID: Prefs.shared.userIconID) { success in
if !success {
print("FAILED LOGIN??")
model.disconnect()
@@ -482,7 +481,6 @@ struct ServerView: View {
}
private func connectionStatusToLabel(status: HotlineClientStatus) -> String {
-// if let s = self.server {
let n = server.name ?? server.address
switch status {
case .disconnected:
@@ -496,42 +494,27 @@ struct ServerView: View {
case .loggedIn:
return "Logged in to \(n)"
}
-// }
-// else {
-// switch status {
-// case .disconnected:
-// return "Disconnected"
-// case .connecting:
-// return "Connecting..."
-// case .connected:
-// return "Connected"
-// case .loggingIn:
-// return "Logging in..."
-// case .loggedIn:
-// return "Logged in"
-// }
-// }
}
@MainActor func sendPreferences() {
if self.model.status == .loggedIn {
var options: HotlineUserOptions = HotlineUserOptions()
- if preferences.refusePrivateMessages {
+ if Prefs.shared.refusePrivateMessages {
options.update(with: .refusePrivateMessages)
}
- if preferences.refusePrivateChat {
+ if Prefs.shared.refusePrivateChat {
options.update(with: .refusePrivateChat)
}
- if preferences.enableAutomaticMessage {
+ if Prefs.shared.enableAutomaticMessage {
options.update(with: .automaticResponse)
}
print("Updating preferences with server")
- self.model.sendUserInfo(username: preferences.username, iconID: preferences.userIconID, options: options, autoresponse: preferences.automaticMessage)
+ self.model.sendUserInfo(username: Prefs.shared.username, iconID: Prefs.shared.userIconID, options: options, autoresponse: Prefs.shared.automaticMessage)
}
}
}
diff --git a/Hotline/macOS/SettingsView.swift b/Hotline/macOS/SettingsView.swift
index e2fc8db..99f1461 100644
--- a/Hotline/macOS/SettingsView.swift
+++ b/Hotline/macOS/SettingsView.swift
@@ -1,15 +1,13 @@
import SwiftUI
struct GeneralSettingsView: View {
- @Environment(Prefs.self) private var preferences: Prefs
-
@State private var username: String = ""
@State private var usernameChanged: Bool = false
let saveTimer = Timer.publish(every: 5, on: .main, in: .common).autoconnect()
var body: some View {
- @Bindable var preferences = preferences
+ @Bindable var preferences = Prefs.shared
Form {
TextField("Your Name", text: $username, prompt: Text("guest"))
@@ -49,12 +47,10 @@ struct GeneralSettingsView: View {
}
struct IconSettingsView: View {
- @Environment(Prefs.self) private var preferences: Prefs
-
@State private var hoveredUserIconID: Int = -1
var body: some View {
- @Bindable var preferences = preferences
+ @Bindable var preferences = Prefs.shared
Form {
ScrollViewReader { scrollProxy in
@@ -107,10 +103,8 @@ struct IconSettingsView: View {
}
struct SoundSettingsView: View {
- @Environment(Prefs.self) private var preferences: Prefs
-
var body: some View {
- @Bindable var preferences = preferences
+ @Bindable var preferences = Prefs.shared
Form {
Toggle("Play Sounds for:", isOn: $preferences.playSounds)