From 9dd67a6c693631fab343e563c0bbdd71ee8cc9e0 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Mon, 3 Aug 2020 17:48:22 -0700 Subject: [PATCH] Refactoring --- Shared/Publishers/CurrentValuePublisher.swift | 18 +++++++++--------- Shared/Views/RootView.swift | 4 ++-- iOS/TabNavigation.swift | 11 +++++++---- macOS/SidebarNavigation.swift | 13 +++++++++---- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Shared/Publishers/CurrentValuePublisher.swift b/Shared/Publishers/CurrentValuePublisher.swift index b563712..5c2b6b4 100644 --- a/Shared/Publishers/CurrentValuePublisher.swift +++ b/Shared/Publishers/CurrentValuePublisher.swift @@ -2,23 +2,23 @@ import Combine -// This publisher acts as a `@Published private var` inside ObservableObjects that doesn't trigger `objectWillChange` - class CurrentValuePublisher { - @Published private(set) var value: Output - private let internalPublisher: AnyPublisher + @Published private var wrappedValue: Output init

(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(subscriber: S) where S: Subscriber, Output == S.Input, S.Failure == Never { - internalPublisher.receive(subscriber: subscriber) + func receive(subscriber: S) where S: Subscriber, S.Input == Output, S.Failure == Never { + $wrappedValue.receive(subscriber: subscriber) } } diff --git a/Shared/Views/RootView.swift b/Shared/Views/RootView.swift index 8e69091..5027aa6 100644 --- a/Shared/Views/RootView.swift +++ b/Shared/Views/RootView.swift @@ -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 } diff --git a/iOS/TabNavigation.swift b/iOS/TabNavigation.swift index 86e5da8..aa313a2 100644 --- a/iOS/TabNavigation.swift +++ b/iOS/TabNavigation.swift @@ -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 diff --git a/macOS/SidebarNavigation.swift b/macOS/SidebarNavigation.swift index 96426f9..c2da3aa 100644 --- a/macOS/SidebarNavigation.swift +++ b/macOS/SidebarNavigation.swift @@ -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