Fix lifecycle of environment objects (#790)

#779 was happening because environment objects were set on contentView,
which is not the _outmost_ root view. This clarifies why the Theme
object was not being found in ThemeLockScreenModifier.

Also, do not hardcode LogoView as lock view.
This commit is contained in:
Davide 2024-10-31 11:14:39 +01:00 committed by GitHub
parent 237277d4db
commit 9f22053fa9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 21 additions and 14 deletions

View File

@ -86,6 +86,5 @@ extension PassepartoutApp {
break break
} }
} }
.withEnvironment(from: context, theme: theme)
} }
} }

View File

@ -42,7 +42,8 @@ extension PassepartoutApp {
.onOpenURL { url in .onOpenURL { url in
ImporterPipe.shared.send([url]) ImporterPipe.shared.send([url])
} }
.themeLockScreen(theme) .themeLockScreen()
.withEnvironment(from: context, theme: theme)
} }
} }
} }

View File

@ -73,10 +73,12 @@ extension PassepartoutApp {
var body: some Scene { var body: some Scene {
Window(appName, id: appName, content: contentView) Window(appName, id: appName, content: contentView)
.defaultSize(width: 600, height: 400) .defaultSize(width: 600, height: 400)
.withEnvironment(from: context, theme: theme)
Settings { Settings {
SettingsView(profileManager: context.profileManager) SettingsView(profileManager: context.profileManager)
.frame(minWidth: 300, minHeight: 200) .frame(minWidth: 300, minHeight: 200)
.withEnvironment(from: context, theme: theme)
} }
MenuBarExtra { MenuBarExtra {
AppMenu() AppMenu()

View File

@ -37,7 +37,10 @@ extension AppDelegate: UIApplicationDelegate {
extension PassepartoutApp { extension PassepartoutApp {
var body: some Scene { var body: some Scene {
WindowGroup(content: contentView) WindowGroup {
contentView()
.withEnvironment(from: context, theme: theme)
}
} }
} }

View File

@ -68,7 +68,7 @@ struct ThemeBooleanModalModifier<Modal>: ViewModifier where Modal: View {
modal() modal()
.frame(minWidth: modalSize?.width, minHeight: modalSize?.height) .frame(minWidth: modalSize?.width, minHeight: modalSize?.height)
.interactiveDismissDisabled(!isInteractive) .interactiveDismissDisabled(!isInteractive)
.themeLockScreen(theme) .themeLockScreen()
} }
} }
@ -97,7 +97,7 @@ struct ThemeItemModalModifier<Modal, T>: ViewModifier where Modal: View, T: Iden
modal($0) modal($0)
.frame(minWidth: modalSize?.width, minHeight: modalSize?.height) .frame(minWidth: modalSize?.width, minHeight: modalSize?.height)
.interactiveDismissDisabled(!isInteractive) .interactiveDismissDisabled(!isInteractive)
.themeLockScreen(theme) .themeLockScreen()
} }
} }
@ -129,13 +129,13 @@ struct ThemeBooleanPopoverModifier<Popover>: ViewModifier, SizeClassProviding wh
.popover(isPresented: $isPresented) { .popover(isPresented: $isPresented) {
popover popover
.frame(minWidth: theme.popoverSize?.width, minHeight: theme.popoverSize?.height) .frame(minWidth: theme.popoverSize?.width, minHeight: theme.popoverSize?.height)
.themeLockScreen(theme) .themeLockScreen()
} }
} else { } else {
content content
.sheet(isPresented: $isPresented) { .sheet(isPresented: $isPresented) {
popover popover
.themeLockScreen(theme) .themeLockScreen()
} }
} }
} }
@ -319,13 +319,16 @@ struct ThemeHoverListRowModifier: ViewModifier {
} }
} }
struct ThemeLockScreenModifier: ViewModifier { struct ThemeLockScreenModifier<LockedContent>: ViewModifier where LockedContent: View {
@AppStorage(AppPreference.locksInBackground.key) @AppStorage(AppPreference.locksInBackground.key)
private var locksInBackground = false private var locksInBackground = false
@ObservedObject @EnvironmentObject
var theme: Theme private var theme: Theme
@ViewBuilder
let lockedContent: () -> LockedContent
func body(content: Content) -> some View { func body(content: Content) -> some View {
LockableView( LockableView(
@ -333,10 +336,9 @@ struct ThemeLockScreenModifier: ViewModifier {
content: { content: {
content content
}, },
lockedContent: LogoView.init, lockedContent: lockedContent,
unlockBlock: Self.unlockScreenBlock unlockBlock: Self.unlockScreenBlock
) )
.environmentObject(theme)
} }
private static func unlockScreenBlock() async -> Bool { private static func unlockScreenBlock() async -> Bool {

View File

@ -209,8 +209,8 @@ extension View {
modifier(ThemeHoverListRowModifier()) modifier(ThemeHoverListRowModifier())
} }
public func themeLockScreen(_ theme: Theme) -> some View { public func themeLockScreen() -> some View {
modifier(ThemeLockScreenModifier(theme: theme)) modifier(ThemeLockScreenModifier(lockedContent: LogoView.init))
} }
public func themeTip(_ text: String, edge: Edge) -> some View { public func themeTip(_ text: String, edge: Edge) -> some View {