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) { private func setNewClientsInEnv(client: Client) {
currentAccount.setClient(client: client) currentAccount.setClient(client: client)
currentInstance.setClient(client: client) currentInstance.setClient(client: client)
userPreferences.setClient(client: client)
watcher.setClient(client: client) watcher.setClient(client: client)
} }
@ -131,6 +132,9 @@ struct IceCubesApp: App {
case .active: case .active:
watcher.watch(streams: [.user, .direct]) watcher.watch(streams: [.user, .direct])
UIApplication.shared.applicationIconBadgeNumber = 0 UIApplication.shared.applicationIconBadgeNumber = 0
Task {
await userPreferences.refreshServerPreferences()
}
case .inactive: case .inactive:
break break
default: default:

View File

@ -1,11 +1,17 @@
import SwiftUI import SwiftUI
import Foundation import Foundation
import Models
import Network
@MainActor
public class UserPreferences: ObservableObject { public class UserPreferences: ObservableObject {
private static let sharedDefault = UserDefaults.init(suiteName: "group.icecubesapps") private static let sharedDefault = UserDefaults.init(suiteName: "group.icecubesapps")
private var client: Client?
@AppStorage("remote_local_timeline") public var remoteLocalTimelines: [String] = [] @AppStorage("remote_local_timeline") public var remoteLocalTimelines: [String] = []
@AppStorage("preferred_browser") public var preferredBrowser: PreferredBrowser = .inAppSafari @AppStorage("preferred_browser") public var preferredBrowser: PreferredBrowser = .inAppSafari
public var pushNotificationsCount: Int { public var pushNotificationsCount: Int {
get { get {
Self.sharedDefault?.integer(forKey: "push_notifications_count") ?? 0 Self.sharedDefault?.integer(forKey: "push_notifications_count") ?? 0
@ -15,5 +21,19 @@ public class UserPreferences: ObservableObject {
} }
} }
@Published private var serverPreferences: ServerPreferences?
public init() { } 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 followers(id: String, maxId: String?)
case following(id: String, maxId: String?) case following(id: String, maxId: String?)
case lists(id: String) case lists(id: String)
case preferences
public func path() -> String { public func path() -> String {
switch self { switch self {
@ -54,6 +55,8 @@ public enum Accounts: Endpoint {
return "accounts/\(id)/followers" return "accounts/\(id)/followers"
case .lists(let id): case .lists(let id):
return "accounts/\(id)/lists" return "accounts/\(id)/lists"
case .preferences:
return "preferences"
} }
} }