Refactor current account handling

This commit is contained in:
Thomas Ricouard 2022-12-22 11:19:56 +01:00
parent 66e314c2be
commit 084dd18362
5 changed files with 48 additions and 29 deletions

View file

@ -384,7 +384,7 @@
CODE_SIGN_IDENTITY = "-";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 251;
CURRENT_PROJECT_VERSION = 255;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"IceCubesApp/Resources\"";
DEVELOPMENT_TEAM = Z6P74P6T99;
@ -406,7 +406,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 0.2;
MARKETING_VERSION = 0.2.5;
PRODUCT_BUNDLE_IDENTIFIER = com.thomasricouard.IceCubesApp;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = auto;
@ -428,7 +428,7 @@
CODE_SIGN_IDENTITY = "-";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 251;
CURRENT_PROJECT_VERSION = 255;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"IceCubesApp/Resources\"";
DEVELOPMENT_TEAM = Z6P74P6T99;
@ -450,7 +450,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 0.2;
MARKETING_VERSION = 0.2.5;
PRODUCT_BUNDLE_IDENTIFIER = com.thomasricouard.IceCubesApp;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = auto;

View file

@ -9,8 +9,9 @@ struct IceCubesApp: App {
public static let defaultServer = "mastodon.social"
@StateObject private var appAccountsManager = AppAccountsManager()
@StateObject private var currentAccount = CurrentAccount()
@StateObject private var quickLook = QuickLook()
var body: some Scene {
WindowGroup {
TabView {
@ -34,10 +35,17 @@ struct IceCubesApp: App {
}
}
.tint(.brand)
.quickLookPreview($quickLook.url, in: quickLook.urls)
.onChange(of: appAccountsManager.currentClient) { newClient in
currentAccount.setClient(client: newClient)
}
.onAppear {
currentAccount.setClient(client: appAccountsManager.currentClient)
}
.environmentObject(appAccountsManager)
.environmentObject(appAccountsManager.currentClient)
.environmentObject(quickLook)
.environmentObject(currentAccount)
.quickLookPreview($quickLook.url, in: quickLook.urls)
}
}
}

View file

@ -6,14 +6,13 @@ import Models
import Shimmer
struct AccountTab: View {
@EnvironmentObject private var client: Client
@EnvironmentObject private var currentAccount: CurrentAccount
@StateObject private var routeurPath = RouterPath()
@State private var loggedUser: Account?
var body: some View {
NavigationStack(path: $routeurPath.path) {
if let loggedUser {
AccountDetailView(account: loggedUser, isCurrentUser: true)
if let account = currentAccount.account {
AccountDetailView(account: account, isCurrentUser: true)
.withAppRouteur()
.withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet)
} else {
@ -22,24 +21,6 @@ struct AccountTab: View {
.shimmering()
}
}
.onAppear {
Task {
await fetchUser(client: client)
}
}
.onChange(of: client) { newClient in
Task {
await fetchUser(client: newClient)
}
}
.environmentObject(routeurPath)
}
private func fetchUser(client: Client) async {
guard client.isAuth else { return }
Task {
loggedUser = try? await client.get(endpoint: Accounts.verifyCredentials)
}
}
}

View file

@ -14,13 +14,15 @@ let package = Package(
targets: ["Env"]),
],
dependencies: [
.package(name: "Models", path: "../Models")
.package(name: "Models", path: "../Models"),
.package(name: "Network", path: "../Network")
],
targets: [
.target(
name: "Env",
dependencies: [
.product(name: "Models", package: "Models"),
.product(name: "Network", package: "Network"),
]),
]
)

View file

@ -0,0 +1,28 @@
import Foundation
import Models
import Network
@MainActor
public class CurrentAccount: ObservableObject {
@Published public private(set) var account: Account?
private var client: Client?
public init() {
}
public func setClient(client: Client) {
self.client = client
Task {
await fetchCurrentAccount()
}
}
public func fetchCurrentAccount() async {
guard let client = client, client.isAuth else { return }
Task {
account = try? await client.get(endpoint: Accounts.verifyCredentials)
}
}
}