Refactoring

This commit is contained in:
Justin Mazzocchi 2020-08-03 17:48:22 -07:00
parent 09c518d712
commit 9dd67a6c69
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
4 changed files with 27 additions and 19 deletions

View File

@ -2,23 +2,23 @@
import Combine
// This publisher acts as a `@Published private var` inside ObservableObjects that doesn't trigger `objectWillChange`
class CurrentValuePublisher<Output> {
@Published private(set) var value: Output
private let internalPublisher: AnyPublisher<Output, Never>
@Published private var wrappedValue: Output
init<P>(initial: Output, then: P) where P: Publisher, P.Output == Output, P.Failure == Never {
value = initial
internalPublisher = then.eraseToAnyPublisher()
then.assign(to: &$value)
wrappedValue = initial
then.assign(to: &$wrappedValue)
}
}
extension CurrentValuePublisher {
var value: Output { wrappedValue }
}
extension CurrentValuePublisher: Publisher {
typealias Failure = Never
func receive<S>(subscriber: S) where S: Subscriber, Output == S.Input, S.Failure == Never {
internalPublisher.receive(subscriber: subscriber)
func receive<S>(subscriber: S) where S: Subscriber, S.Input == Output, S.Failure == Never {
$wrappedValue.receive(subscriber: subscriber)
}
}

View File

@ -19,10 +19,10 @@ struct RootView: View {
private extension RootView {
private static func mainNavigation(viewModel: MainNavigationViewModel) -> some View {
#if os(macOS)
return SidebarNavigation().environmentObject(viewModel)
return SidebarNavigation(viewModel: viewModel)
.frame(minWidth: 900, maxWidth: .infinity, minHeight: 500, maxHeight: .infinity)
#else
return TabNavigation().environmentObject(viewModel)
return TabNavigation(viewModel: viewModel)
#endif
}

View File

@ -5,7 +5,7 @@ import KingfisherSwiftUI
import struct Kingfisher.DownsamplingImageProcessor
struct TabNavigation: View {
@EnvironmentObject var viewModel: MainNavigationViewModel
@StateObject var viewModel: MainNavigationViewModel
var body: some View {
TabView(selection: $viewModel.selectedTab) {
@ -25,7 +25,11 @@ struct TabNavigation: View {
SettingsView(viewModel: viewModel.settingsViewModel())
.environmentObject(viewModel)
}
.onAppear { viewModel.refreshIdentity() }
.onAppear(perform: viewModel.refreshIdentity)
.onReceive(NotificationCenter.default
.publisher(for: UIScene.willEnterForegroundNotification)
.map { _ in () },
perform: viewModel.refreshIdentity)
}
}
@ -61,8 +65,7 @@ private extension TabNavigation {
#if DEBUG
struct TabNavigation_Previews: PreviewProvider {
static var previews: some View {
TabNavigation()
.environmentObject(MainNavigationViewModel.development)
TabNavigation(viewModel: .development)
}
}
#endif

View File

@ -6,7 +6,7 @@ import struct Kingfisher.DownsamplingImageProcessor
import struct Kingfisher.RoundCornerImageProcessor
struct SidebarNavigation: View {
@EnvironmentObject var viewModel: MainNavigationViewModel
@StateObject var viewModel: MainNavigationViewModel
var sidebar: some View {
List(selection: $viewModel.selectedTab) {
@ -18,8 +18,14 @@ struct SidebarNavigation: View {
.tag(tab)
}
}
.overlay(Pocket(), alignment: .bottom)
.overlay(Pocket().environmentObject(viewModel), alignment: .bottom)
.listStyle(SidebarListStyle())
.onAppear(perform: viewModel.refreshIdentity)
.onReceive(NotificationCenter.default
.publisher(for: NSWindow.didBecomeKeyNotification)
.dropFirst()
.map { _ in () },
perform: viewModel.refreshIdentity)
}
var body: some View {
@ -82,8 +88,7 @@ private extension SidebarNavigation {
#if DEBUG
struct SidebarNavigation_Previews: PreviewProvider {
static var previews: some View {
SidebarNavigation()
.environmentObject(MainNavigationViewModel.development)
SidebarNavigation(viewModel: .development)
}
}
#endif