Fetch server side preferences

This commit is contained in:
Thomas Ricouard 2023-01-09 19:47:54 +01:00
parent 662f4be29d
commit 33634a16aa
4 changed files with 50 additions and 0 deletions

View file

@ -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:

View file

@ -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)
}
}

View file

@ -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"
}
}

View file

@ -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"
}
}