From 33634a16aa740f57b18fd8c61d7fa8d5262baf8b Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Mon, 9 Jan 2023 19:47:54 +0100 Subject: [PATCH] Fetch server side preferences --- IceCubesApp/App/IceCubesApp.swift | 4 ++++ .../Env/Sources/Env/UserPreferences.swift | 20 ++++++++++++++++ .../Sources/Models/ServerPreferences.swift | 23 +++++++++++++++++++ .../Sources/Network/Endpoint/Accounts.swift | 3 +++ 4 files changed, 50 insertions(+) create mode 100644 Packages/Models/Sources/Models/ServerPreferences.swift diff --git a/IceCubesApp/App/IceCubesApp.swift b/IceCubesApp/App/IceCubesApp.swift index de0a62a8..3941e8e1 100644 --- a/IceCubesApp/App/IceCubesApp.swift +++ b/IceCubesApp/App/IceCubesApp.swift @@ -121,6 +121,7 @@ struct IceCubesApp: App { private func setNewClientsInEnv(client: Client) { currentAccount.setClient(client: client) currentInstance.setClient(client: client) + userPreferences.setClient(client: client) watcher.setClient(client: client) } @@ -131,6 +132,9 @@ struct IceCubesApp: App { case .active: watcher.watch(streams: [.user, .direct]) UIApplication.shared.applicationIconBadgeNumber = 0 + Task { + await userPreferences.refreshServerPreferences() + } case .inactive: break default: diff --git a/Packages/Env/Sources/Env/UserPreferences.swift b/Packages/Env/Sources/Env/UserPreferences.swift index 8e58cbac..0cc12318 100644 --- a/Packages/Env/Sources/Env/UserPreferences.swift +++ b/Packages/Env/Sources/Env/UserPreferences.swift @@ -1,11 +1,17 @@ import SwiftUI import Foundation +import Models +import Network +@MainActor public class UserPreferences: ObservableObject { private static let sharedDefault = UserDefaults.init(suiteName: "group.icecubesapps") + private var client: Client? + @AppStorage("remote_local_timeline") public var remoteLocalTimelines: [String] = [] @AppStorage("preferred_browser") public var preferredBrowser: PreferredBrowser = .inAppSafari + public var pushNotificationsCount: Int { get { Self.sharedDefault?.integer(forKey: "push_notifications_count") ?? 0 @@ -15,5 +21,19 @@ public class UserPreferences: ObservableObject { } } + @Published private var serverPreferences: ServerPreferences? + public init() { } + + public func setClient(client: Client) { + self.client = client + Task { + await refreshServerPreferences() + } + } + + public func refreshServerPreferences() async { + guard let client, client.isAuth else { return } + serverPreferences = try? await client.get(endpoint: Accounts.preferences) + } } diff --git a/Packages/Models/Sources/Models/ServerPreferences.swift b/Packages/Models/Sources/Models/ServerPreferences.swift new file mode 100644 index 00000000..02f6a76e --- /dev/null +++ b/Packages/Models/Sources/Models/ServerPreferences.swift @@ -0,0 +1,23 @@ +import Foundation + +public struct ServerPreferences: Decodable { + public let postVisibility: Visibility + public let postIsSensitive: Bool + public let postLanguage: String + public let autoExpandmedia: AutoExpandMedia + public let autoExpandSpoilers: Bool + + public enum AutoExpandMedia: String, Decodable { + case showAll = "show_all" + case hideAll = "hide_all" + case hideSensitive = "default" + } + + enum CodingKeys: String, CodingKey { + case postVisibility = "posting:default:visibility" + case postIsSensitive = "posting:default:sensitive" + case postLanguage = "posting:default:language" + case autoExpandmedia = "reading:expand:media" + case autoExpandSpoilers = "reading:expand:spoilers" + } +} diff --git a/Packages/Network/Sources/Network/Endpoint/Accounts.swift b/Packages/Network/Sources/Network/Endpoint/Accounts.swift index 7a120fc0..d22aee6a 100644 --- a/Packages/Network/Sources/Network/Endpoint/Accounts.swift +++ b/Packages/Network/Sources/Network/Endpoint/Accounts.swift @@ -21,6 +21,7 @@ public enum Accounts: Endpoint { case followers(id: String, maxId: String?) case following(id: String, maxId: String?) case lists(id: String) + case preferences public func path() -> String { switch self { @@ -54,6 +55,8 @@ public enum Accounts: Endpoint { return "accounts/\(id)/followers" case .lists(let id): return "accounts/\(id)/lists" + case .preferences: + return "preferences" } }