From af4849642ce7bb643bf71540f37d878d5b03fdb2 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Wed, 8 Jul 2020 21:39:39 +0800 Subject: [PATCH] Adds multi-select to macOS sidebar This hacks around property observers to get to a model that almost works. --- .../Shared/Sidebar/SidebarModel.swift | 3 +- .../Sidebar/SidebarSelectionModel.swift | 35 +++++++++ .../Shared/Sidebar/SidebarView.swift | 76 ++++++++++++------- NetNewsWire.xcodeproj/project.pbxproj | 28 ++++--- 4 files changed, 102 insertions(+), 40 deletions(-) create mode 100644 Multiplatform/Shared/Sidebar/SidebarSelectionModel.swift diff --git a/Multiplatform/Shared/Sidebar/SidebarModel.swift b/Multiplatform/Shared/Sidebar/SidebarModel.swift index 1bbbd92a4..dfa9844ee 100644 --- a/Multiplatform/Shared/Sidebar/SidebarModel.swift +++ b/Multiplatform/Shared/Sidebar/SidebarModel.swift @@ -20,6 +20,8 @@ class SidebarModel: ObservableObject { @Published var sidebarItems = [SidebarItem]() + + init() { NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidInitialize(_:)), name: .UnreadCountDidInitialize, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(containerChildrenDidChange(_:)), name: .ChildrenDidChange, object: nil) @@ -62,7 +64,6 @@ class SidebarModel: ObservableObject { sidebarItems = items } - } // MARK: Private diff --git a/Multiplatform/Shared/Sidebar/SidebarSelectionModel.swift b/Multiplatform/Shared/Sidebar/SidebarSelectionModel.swift new file mode 100644 index 000000000..73bdea034 --- /dev/null +++ b/Multiplatform/Shared/Sidebar/SidebarSelectionModel.swift @@ -0,0 +1,35 @@ +// +// SidebarSelectionModel.swift +// NetNewsWire +// +// Created by Stuart Breckenridge on 8/7/20. +// Copyright © 2020 Ranchero Software. All rights reserved. +// + +import Foundation +import RSCore +import Account + + +class SidebarSelectionModel: ObservableObject { + #if os(macOS) + @Published var selectedSidebarItems = Set() { + didSet { + print(selectedSidebarItems) + } + } + #endif + + private var items = Set() + + @Published var selectedSidebarItem: FeedIdentifier? = .none { + willSet { + if newValue != nil { + items.insert(newValue!) + } else { + selectedSidebarItems = items + items.removeAll() + } + } + } +} diff --git a/Multiplatform/Shared/Sidebar/SidebarView.swift b/Multiplatform/Shared/Sidebar/SidebarView.swift index 4f5073f88..76768f090 100644 --- a/Multiplatform/Shared/Sidebar/SidebarView.swift +++ b/Multiplatform/Shared/Sidebar/SidebarView.swift @@ -12,54 +12,74 @@ import Account struct SidebarView: View { // I had to comment out SceneStorage because it blows up if used on macOS -// @SceneStorage("expandedContainers") private var expandedContainerData = Data() + // @SceneStorage("expandedContainers") private var expandedContainerData = Data() @StateObject private var expandedContainers = SidebarExpandedContainers() @EnvironmentObject private var sidebarModel: SidebarModel + @StateObject private var sidebarSelectionModel = SidebarSelectionModel() + + @ViewBuilder var body: some View { - List() { - ForEach(sidebarModel.sidebarItems) { sidebarItem in - if let containerID = sidebarItem.containerID { - DisclosureGroup(isExpanded: $expandedContainers[containerID]) { - ForEach(sidebarItem.children) { sidebarItem in - if let containerID = sidebarItem.containerID { - DisclosureGroup(isExpanded: $expandedContainers[containerID]) { - ForEach(sidebarItem.children) { sidebarItem in - ZStack { - SidebarItemView(sidebarItem: sidebarItem) - NavigationLink(destination: (TimelineContainerView(feed: sidebarItem.feed))) { - EmptyView() - }.buttonStyle(PlainButtonStyle()) - } - } - } label: { + #if os(macOS) + List(selection: $sidebarSelectionModel.selectedSidebarItems) { + containedList + } + #else + List { + containedList + } + #endif + // .onAppear { + // expandedContainers.data = expandedContainerData + // } + // .onReceive(expandedContainers.objectDidChange) { + // expandedContainerData = expandedContainers.data + // } + } + + var containedList: some View { + ForEach(sidebarModel.sidebarItems) { sidebarItem in + if let containerID = sidebarItem.containerID { + DisclosureGroup(isExpanded: $expandedContainers[containerID]) { + ForEach(sidebarItem.children) { sidebarItem in + if let containerID = sidebarItem.containerID { + DisclosureGroup(isExpanded: $expandedContainers[containerID]) { + ForEach(sidebarItem.children) { sidebarItem in ZStack { SidebarItemView(sidebarItem: sidebarItem) - NavigationLink(destination: (TimelineContainerView(feed: sidebarItem.feed))) { + NavigationLink(destination: (TimelineContainerView(feed: sidebarItem.feed)), + tag: sidebarItem.feed!.feedID!, + selection: $sidebarSelectionModel.selectedSidebarItem) { EmptyView() }.buttonStyle(PlainButtonStyle()) } } - } else { + } label: { ZStack { SidebarItemView(sidebarItem: sidebarItem) - NavigationLink(destination: (TimelineContainerView(feed: sidebarItem.feed))) { + NavigationLink(destination: (TimelineContainerView(feed: sidebarItem.feed)), + tag: sidebarItem.feed!.feedID!, + selection: $sidebarSelectionModel.selectedSidebarItem) { EmptyView() }.buttonStyle(PlainButtonStyle()) } } + } else { + ZStack { + SidebarItemView(sidebarItem: sidebarItem) + NavigationLink(destination: (TimelineContainerView(feed: sidebarItem.feed)), + tag: sidebarItem.feed!.feedID!, + selection: $sidebarSelectionModel.selectedSidebarItem) { + EmptyView() + }.buttonStyle(PlainButtonStyle()) + } } - } label: { - SidebarItemView(sidebarItem: sidebarItem) } + } label: { + SidebarItemView(sidebarItem: sidebarItem) } } } -// .onAppear { -// expandedContainers.data = expandedContainerData -// } -// .onReceive(expandedContainers.objectDidChange) { -// expandedContainerData = expandedContainers.data -// } } + } diff --git a/NetNewsWire.xcodeproj/project.pbxproj b/NetNewsWire.xcodeproj/project.pbxproj index a8be3d0c1..35c6d1c27 100644 --- a/NetNewsWire.xcodeproj/project.pbxproj +++ b/NetNewsWire.xcodeproj/project.pbxproj @@ -17,6 +17,8 @@ 1729529B24AA1FD200D65E66 /* MacSearchField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1729529A24AA1FD200D65E66 /* MacSearchField.swift */; }; 175942AA24AD533200585066 /* RefreshInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5183CCE4226F4DFA0010922C /* RefreshInterval.swift */; }; 175942AB24AD533200585066 /* RefreshInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5183CCE4226F4DFA0010922C /* RefreshInterval.swift */; }; + 1770CAFA24B5CFEC007D398B /* SidebarSelectionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1770CAF924B5CFEC007D398B /* SidebarSelectionModel.swift */; }; + 1770CAFB24B5CFEC007D398B /* SidebarSelectionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1770CAF924B5CFEC007D398B /* SidebarSelectionModel.swift */; }; 1776E88E24AC5F8A00E78166 /* AppDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1776E88D24AC5F8A00E78166 /* AppDefaults.swift */; }; 1776E88F24AC5F8A00E78166 /* AppDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1776E88D24AC5F8A00E78166 /* AppDefaults.swift */; }; 17930ED424AF10EE00A9BA52 /* AddWebFeedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17930ED324AF10EE00A9BA52 /* AddWebFeedView.swift */; }; @@ -1769,6 +1771,7 @@ 1729529224AA1CAA00D65E66 /* GeneralPreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneralPreferencesView.swift; sourceTree = ""; }; 1729529624AA1CD000D65E66 /* MacPreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MacPreferencesView.swift; sourceTree = ""; }; 1729529A24AA1FD200D65E66 /* MacSearchField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacSearchField.swift; sourceTree = ""; }; + 1770CAF924B5CFEC007D398B /* SidebarSelectionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarSelectionModel.swift; sourceTree = ""; }; 1776E88D24AC5F8A00E78166 /* AppDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDefaults.swift; sourceTree = ""; }; 17930ED324AF10EE00A9BA52 /* AddWebFeedView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddWebFeedView.swift; sourceTree = ""; }; 179DBBA2B22A659F81EED6F9 /* AccountsNewsBlurWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccountsNewsBlurWindowController.swift; sourceTree = ""; }; @@ -3090,6 +3093,7 @@ 17D5F17024B0BC6700375168 /* SidebarToolbarModel.swift */, 172199F024AB716900A31D04 /* SidebarToolbarModifier.swift */, 51919FA524AA64B000541E64 /* SidebarView.swift */, + 1770CAF924B5CFEC007D398B /* SidebarSelectionModel.swift */, 51919FAB24AA8CCA00541E64 /* UnreadCountView.swift */, ); path = Sidebar; @@ -4098,46 +4102,46 @@ TargetAttributes = { 51314636235A7BBE00387FDC = { CreatedOnToolsVersion = 11.2; - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; LastSwiftMigration = 1120; ProvisioningStyle = Automatic; }; 513C5CE5232571C2003D4054 = { CreatedOnToolsVersion = 11.0; - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; }; 518B2ED12351B3DD00400001 = { CreatedOnToolsVersion = 11.2; - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; TestTargetID = 840D617B2029031C009BC708; }; 51C0513C24A77DF800194D5E = { CreatedOnToolsVersion = 12.0; - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; }; 51C0514324A77DF800194D5E = { CreatedOnToolsVersion = 12.0; - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; }; 6581C73220CED60000F4AD34 = { - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; }; 65ED3FA2235DEF6C0081F399 = { - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; }; 65ED4090235DEF770081F399 = { - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; }; 840D617B2029031C009BC708 = { CreatedOnToolsVersion = 9.3; - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; SystemCapabilities = { com.apple.BackgroundModes = { @@ -4147,7 +4151,7 @@ }; 849C645F1ED37A5D003D8FC0 = { CreatedOnToolsVersion = 8.2.1; - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; SystemCapabilities = { com.apple.HardenedRuntime = { @@ -4157,7 +4161,7 @@ }; 849C64701ED37A5D003D8FC0 = { CreatedOnToolsVersion = 8.2.1; - DevelopmentTeam = SHJK2V3AJG; + DevelopmentTeam = FQLBNX3GP7; ProvisioningStyle = Automatic; TestTargetID = 849C645F1ED37A5D003D8FC0; }; @@ -4982,6 +4986,7 @@ 51E4991B24A8091000B667CB /* IconImage.swift in Sources */, 51E4995424A8734D00B667CB /* ExtensionPointIdentifer.swift in Sources */, 51E4996924A8760C00B667CB /* ArticleStylesManager.swift in Sources */, + 1770CAFA24B5CFEC007D398B /* SidebarSelectionModel.swift in Sources */, 5177471E24B387E100EB0F74 /* ImageTransition.swift in Sources */, 51E498F324A8085D00B667CB /* PseudoFeed.swift in Sources */, 51A5769624AE617200078888 /* ArticleContainerView.swift in Sources */, @@ -5094,6 +5099,7 @@ 51E4990524A808C300B667CB /* FeaturedImageDownloader.swift in Sources */, 5181C5AE24AF89B1002E0F70 /* PreferredColorSchemeModifier.swift in Sources */, 51E4991624A8090300B667CB /* ArticleUtilities.swift in Sources */, + 1770CAFB24B5CFEC007D398B /* SidebarSelectionModel.swift in Sources */, 51919FF224AB864A00541E64 /* TimelineModel.swift in Sources */, 51E4991A24A8090F00B667CB /* IconImage.swift in Sources */, 51E4992724A80AAB00B667CB /* AppAssets.swift in Sources */,