From 939e80d66d0e4ea40a8949c79057428a8d3bea04 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 2 Mar 2023 10:58:17 +0100 Subject: [PATCH] manage split view column visibility --- .../Model/NSManagedContext+Extensions.swift | 5 +++++ LeCountdown/Views/HomeView.swift | 22 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/LeCountdown/Model/NSManagedContext+Extensions.swift b/LeCountdown/Model/NSManagedContext+Extensions.swift index 3531876..285fa24 100644 --- a/LeCountdown/Model/NSManagedContext+Extensions.swift +++ b/LeCountdown/Model/NSManagedContext+Extensions.swift @@ -37,6 +37,11 @@ extension NSManagedObjectContext { return try self.fetch(request) } + func count(entityName: String) throws -> Int { + let fetchRequest = NSFetchRequest(entityName: entityName) + return try self.count(for: fetchRequest) + } + } extension NSManagedObject { diff --git a/LeCountdown/Views/HomeView.swift b/LeCountdown/Views/HomeView.swift index caaaee3..bbe927e 100644 --- a/LeCountdown/Views/HomeView.swift +++ b/LeCountdown/Views/HomeView.swift @@ -58,14 +58,16 @@ struct RegularHomeView: View { @State private var tabSelection: Int = 1 + @State private var columnVisibility: NavigationSplitViewVisibility = .detailOnly + var body: some View { - NavigationView { + NavigationSplitView(columnVisibility: $columnVisibility) { PresetsView(tabSelection: $tabSelection) .environment(\.managedObjectContext, viewContext) .tabItem { Label("Presets", systemImage: "globe") } .tag(0) - + } detail: { TabView(selection: $tabSelection) { ContentView() .environment(\.managedObjectContext, viewContext) @@ -78,12 +80,28 @@ struct RegularHomeView: View { .tag(2) }.tabViewStyle(.page(indexDisplayMode: .never)) } + .onAppear { + self._setColumnVisibility() + } .onContinueUserActivity(Shortcut.newCountdown.rawValue, perform: _handleNewCountdown) .onOpenURL { _ in self.tabSelection = 1 } } + fileprivate func _setColumnVisibility() { + do { + let count = try viewContext.count(entityName: "AbstractTimer") + if count > 0 { + self.columnVisibility = .detailOnly + } else { + self.columnVisibility = .automatic + } + } catch { + Logger.error(error) + } + } + fileprivate func _handleNewCountdown(_ userActivity: NSUserActivity) { print(">>> new countdown") }