Observe currentProfile in ProfileView

ProfileView is not interested in changes in other profiles
notified by ProfileManager. Set isLoading inside
ObservableObject for observable to be self-contained.

Loses observation of profile deletion, but dismiss on removal is
actually handled by OrganizerView, not ProfileView.

Also drop unused presentationMode.
This commit is contained in:
Davide De Rosa 2022-05-01 10:55:07 +02:00
parent 107de2d843
commit 8003b4a92d
3 changed files with 24 additions and 22 deletions

View File

@ -43,22 +43,20 @@ struct ProfileView: View {
} }
} }
@Environment(\.presentationMode) private var presentationMode @ObservedObject private var currentProfile: ObservableProfile
@ObservedObject private var profileManager: ProfileManager
private var isLoading: Bool { private var isLoading: Bool {
profileManager.isLoadingCurrentProfile currentProfile.isLoading
} }
private var isExisting: Bool { private var isExisting: Bool {
profileManager.isCurrentProfileExisting() !currentProfile.value.isPlaceholder
} }
@State private var modalType: ModalType? @State private var modalType: ModalType?
init() { init() {
profileManager = .shared currentProfile = ProfileManager.shared.currentProfile
} }
var body: some View { var body: some View {
@ -71,7 +69,7 @@ struct ProfileView: View {
} }
}.toolbar { }.toolbar {
MainMenu( MainMenu(
currentProfile: profileManager.currentProfile, currentProfile: currentProfile,
modalType: $modalType modalType: $modalType
).disabled(!isExisting) ).disabled(!isExisting)
}.sheet(item: $modalType, content: presentedModal) }.sheet(item: $modalType, content: presentedModal)
@ -80,23 +78,23 @@ struct ProfileView: View {
} }
private var title: String { private var title: String {
profileManager.currentProfile.name currentProfile.name
} }
private var mainView: some View { private var mainView: some View {
List { List {
VPNSection( VPNSection(
currentProfile: profileManager.currentProfile, currentProfile: currentProfile,
isLoading: isLoading isLoading: isLoading
) )
if !isLoading { if !isLoading {
ProviderSection(currentProfile: profileManager.currentProfile) ProviderSection(currentProfile: currentProfile)
ConfigurationSection( ConfigurationSection(
currentProfile: profileManager.currentProfile, currentProfile: currentProfile,
modalType: $modalType modalType: $modalType
) )
ExtraSection(currentProfile: profileManager.currentProfile) ExtraSection(currentProfile: currentProfile)
DiagnosticsSection(currentProfile: profileManager.currentProfile) DiagnosticsSection(currentProfile: currentProfile)
} }
} }
} }
@ -106,12 +104,12 @@ struct ProfileView: View {
switch modalType { switch modalType {
case .shortcuts: case .shortcuts:
NavigationView { NavigationView {
ShortcutsView(target: profileManager.currentProfile.value) ShortcutsView(target: currentProfile.value)
}.themeGlobal() }.themeGlobal()
case .rename: case .rename:
NavigationView { NavigationView {
RenameView(currentProfile: profileManager.currentProfile) RenameView(currentProfile: currentProfile)
}.themeGlobal() }.themeGlobal()
case .paywallShortcuts: case .paywallShortcuts:

View File

@ -58,8 +58,6 @@ public class ProfileManager: ObservableObject {
// MARK: Observables // MARK: Observables
@Published public private(set) var isLoadingCurrentProfile = false
public let currentProfile: ObservableProfile public let currentProfile: ObservableProfile
public let willUpdateActiveId = PassthroughSubject<UUID?, Never>() public let willUpdateActiveId = PassthroughSubject<UUID?, Never>()
@ -255,7 +253,7 @@ extension ProfileManager {
extension ProfileManager { extension ProfileManager {
public func loadCurrentProfile(withId id: UUID) throws { public func loadCurrentProfile(withId id: UUID) throws {
guard !isLoadingCurrentProfile else { guard !currentProfile.isLoading else {
pp_log.warning("Already loading another profile") pp_log.warning("Already loading another profile")
return return
} }
@ -263,7 +261,7 @@ extension ProfileManager {
pp_log.debug("Profile \(id) is already current profile") pp_log.debug("Profile \(id) is already current profile")
return return
} }
isLoadingCurrentProfile = true currentProfile.isLoading = true
if isExistingProfile(withId: currentProfile.value.id) { if isExistingProfile(withId: currentProfile.value.id) {
pp_log.info("Committing changes of former current profile \(currentProfile.value.logDescription)") pp_log.info("Committing changes of former current profile \(currentProfile.value.logDescription)")
saveCurrentProfile() saveCurrentProfile()
@ -274,20 +272,24 @@ extension ProfileManager {
currentProfile.value = result.profile currentProfile.value = result.profile
if result.isReady { if result.isReady {
isLoadingCurrentProfile = false currentProfile.isLoading = false
} else { } else {
Task { Task {
try await makeProfileReady(result.profile) try await makeProfileReady(result.profile)
isLoadingCurrentProfile = false currentProfile.isLoading = false
} }
} }
} catch { } catch {
currentProfile.value = .placeholder currentProfile.value = .placeholder
isLoadingCurrentProfile = false currentProfile.isLoading = false
throw error throw error
} }
} }
public func isCurrentProfileLoading() -> Bool {
currentProfile.isLoading
}
public func isCurrentProfileExisting() -> Bool { public func isCurrentProfileExisting() -> Bool {
isExistingProfile(withId: currentProfile.value.id) isExistingProfile(withId: currentProfile.value.id)
} }

View File

@ -27,6 +27,8 @@ import Foundation
import PassepartoutUtils import PassepartoutUtils
public class ObservableProfile: ValueHolder, ObservableObject { public class ObservableProfile: ValueHolder, ObservableObject {
@Published public internal(set) var isLoading = false
@Published public var value: Profile @Published public var value: Profile
public var name: String { public var name: String {