Skip to content

Commit

Permalink
[016] Change implementation of .navStatusBar(isHidde: Bool) to more N…
Browse files Browse the repository at this point in the history
…ative SwiftUI.
  • Loading branch information
edon2005 committed Jan 10, 2025
1 parent 552dc9b commit 73684d6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
7 changes: 3 additions & 4 deletions Sources/CoordinatorX/Navigation/NavigationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
@preconcurrency import Combine
import SwiftUI

let statusBarHiddenSubject = CurrentValueSubject<Bool, Never>(false)

public struct NavigationContext<RouteType: Route,
CoordinatorType: NavigationCoordinator>: Context where CoordinatorType.RouteType == RouteType {

Expand Down Expand Up @@ -54,8 +52,9 @@ public struct NavigationContext<RouteType: Route,
.background(transition.backgroundColor)
.transition(transition.style)
}
.onReceive(statusBarHiddenSubject.eraseToAnyPublisher()) { value in
isStatusBarHidden = value
.onPreferenceChange(NavStatusBarHiddenKey.self) { [$isStatusBarHidden] value in
guard let value else { return }
$isStatusBarHidden.wrappedValue = value
}
.statusBar(hidden: isStatusBarHidden)
}
Expand Down
13 changes: 6 additions & 7 deletions Sources/CoordinatorX/Utils/StatusBarModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@

import SwiftUI

struct StatusBar: ViewModifier {

let isHidden: Bool
struct NavStatusBarHiddenKey: PreferenceKey {
static let defaultValue: Bool? = nil

func body(content: Content) -> some View {
let _ = statusBarHiddenSubject.send(isHidden)
content
static func reduce(value: inout Bool?, nextValue: () -> Bool?) {
guard let result = nextValue() else { return }
value = result
}
}

extension View {
public func navStatusBar(isHidden: Bool) -> some View {
let statusBar = StatusBar(isHidden: isHidden)
return modifier(statusBar)
preference(key: NavStatusBarHiddenKey.self, value: isHidden)
}
}

0 comments on commit 73684d6

Please sign in to comment.