From 1b7ff94f93a0c29d43a5f251f8392e05f1f921de Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Sun, 14 Apr 2024 14:57:14 +0200 Subject: [PATCH 1/2] Fix dismissal on iOS --- App/SplitViewController.swift | 6 +++--- Views/Library/Library.swift | 8 ++++---- Views/Library/ZimFileDetail.swift | 5 +++++ Views/Library/ZimFilesCategories.swift | 28 +++++++++++++++++--------- Views/Library/ZimFilesDownloads.swift | 7 ++++++- Views/Library/ZimFilesNew.swift | 5 +++-- 6 files changed, 40 insertions(+), 19 deletions(-) diff --git a/App/SplitViewController.swift b/App/SplitViewController.swift index 135710b53..9f44df157 100644 --- a/App/SplitViewController.swift +++ b/App/SplitViewController.swift @@ -123,13 +123,13 @@ final class SplitViewController: UISplitViewController { let controller = UIHostingController(rootView: ZimFilesOpened(dismiss: nil)) setViewController(UINavigationController(rootViewController: controller), for: .secondary) case .categories: - let controller = UIHostingController(rootView: ZimFilesCategories()) + let controller = UIHostingController(rootView: ZimFilesCategories(dismiss: nil)) setViewController(UINavigationController(rootViewController: controller), for: .secondary) case .downloads: - let controller = UIHostingController(rootView: ZimFilesDownloads()) + let controller = UIHostingController(rootView: ZimFilesDownloads(dismiss: nil)) setViewController(UINavigationController(rootViewController: controller), for: .secondary) case .new: - let controller = UIHostingController(rootView: ZimFilesNew()) + let controller = UIHostingController(rootView: ZimFilesNew(dismiss: nil)) setViewController(UINavigationController(rootViewController: controller), for: .secondary) case .settings: let controller = UIHostingController(rootView: Settings()) diff --git a/Views/Library/Library.swift b/Views/Library/Library.swift index 5f7569be9..a41bd12e8 100644 --- a/Views/Library/Library.swift +++ b/Views/Library/Library.swift @@ -43,7 +43,7 @@ struct Library: View { case .categories: List(categories) { category in NavigationLink { - ZimFilesCategory(category: .constant(category)) + ZimFilesCategory(category: .constant(category), dismiss: dismiss) .navigationTitle(category.name) .navigationBarTitleDisplayMode(.inline) } label: { @@ -56,9 +56,9 @@ struct Library: View { .listStyle(.plain) .navigationTitle(NavigationItem.categories.name) case .downloads: - ZimFilesDownloads() + ZimFilesDownloads(dismiss: dismiss) case .new: - ZimFilesNew() + ZimFilesNew(dismiss: dismiss) } } .tag(tabItem) @@ -117,7 +117,7 @@ struct LibraryZimFileContext: ViewModifier { let zimFile: ZimFile let dismiss: (() -> Void)? // iOS only - init(zimFile: ZimFile, dismiss: (() -> Void)? = nil) { + init(zimFile: ZimFile, dismiss: (() -> Void)?) { self.zimFile = zimFile self.dismiss = dismiss } diff --git a/Views/Library/ZimFileDetail.swift b/Views/Library/ZimFileDetail.swift index 003fa6c5e..6007010b0 100644 --- a/Views/Library/ZimFileDetail.swift +++ b/Views/Library/ZimFileDetail.swift @@ -30,6 +30,11 @@ struct ZimFileDetail: View { @State private var isPresentingUnlinkAlert = false let dismissParent: (() -> Void)? // iOS only + init(zimFile: ZimFile, dismissParent: (() -> Void)?) { + self.zimFile = zimFile + self.dismissParent = dismissParent + } + var body: some View { #if os(macOS) List { diff --git a/Views/Library/ZimFilesCategories.swift b/Views/Library/ZimFilesCategories.swift index a29ac4b26..a9d19d6e7 100644 --- a/Views/Library/ZimFilesCategories.swift +++ b/Views/Library/ZimFilesCategories.swift @@ -21,14 +21,16 @@ import Defaults struct ZimFilesCategories: View { @State private var selected: Category private var categories: [Category] + private let dismiss: (() -> Void)? - init() { + init(dismiss: (() -> Void)?) { categories = CategoriesToLanguages.allCategories() selected = categories.first ?? .wikipedia + self.dismiss = dismiss } var body: some View { - ZimFilesCategory(category: $selected) + ZimFilesCategory(category: $selected, dismiss: dismiss) .modifier(ToolbarRoleBrowser()) .navigationTitle(NavigationItem.categories.name) .toolbar { @@ -58,12 +60,13 @@ struct ZimFilesCategories: View { struct ZimFilesCategory: View { @Binding var category: Category @State private var searchText = "" + let dismiss: (() -> Void)? // iOS only var body: some View { if category == .ted || category == .stackExchange || category == .other { - CategoryList(category: $category, searchText: $searchText) + CategoryList(category: $category, searchText: $searchText, dismiss: dismiss) } else { - CategoryGrid(category: $category, searchText: $searchText) + CategoryGrid(category: $category, searchText: $searchText, dismiss: dismiss) } } @@ -87,10 +90,12 @@ private struct CategoryGrid: View { @EnvironmentObject private var viewModel: LibraryViewModel @Environment(\.horizontalSizeClass) private var horizontalSizeClass @SectionedFetchRequest private var sections: SectionedFetchResults + private let dismiss: (() -> Void)? // iOS only - init(category: Binding, searchText: Binding) { + init(category: Binding, searchText: Binding, dismiss: (() -> Void)?) { self._category = category self._searchText = searchText + self.dismiss = dismiss self._sections = SectionedFetchRequest( sectionIdentifier: \.name, sortDescriptors: [SortDescriptor(\ZimFile.name), SortDescriptor(\.size, order: .reverse)], @@ -111,13 +116,13 @@ private struct CategoryGrid: View { if sections.count <= 1 { ForEach(section) { zimFile in ZimFileCell(zimFile, prominent: .size) - .modifier(LibraryZimFileContext(zimFile: zimFile)) + .modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss)) } } else { Section { ForEach(section) { zimFile in ZimFileCell(zimFile, prominent: .size) - .modifier(LibraryZimFileContext(zimFile: zimFile)) + .modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss)) } } header: { SectionHeader( @@ -179,10 +184,15 @@ private struct CategoryList: View { @Default(.libraryLanguageCodes) private var languageCodes @EnvironmentObject private var viewModel: LibraryViewModel @FetchRequest private var zimFiles: FetchedResults + private let dismiss: (() -> Void)? - init(category: Binding, searchText: Binding) { + init(category: Binding, + searchText: Binding, + dismiss: (() -> Void)? + ) { self._category = category self._searchText = searchText + self.dismiss = dismiss self._zimFiles = FetchRequest( sortDescriptors: [ NSSortDescriptor( @@ -204,7 +214,7 @@ private struct CategoryList: View { } else { List(zimFiles, id: \.self, selection: $viewModel.selectedZimFile) { zimFile in ZimFileRow(zimFile) - .modifier(LibraryZimFileContext(zimFile: zimFile)) + .modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss)) } #if os(macOS) .listStyle(.inset) diff --git a/Views/Library/ZimFilesDownloads.swift b/Views/Library/ZimFilesDownloads.swift index 5694a2b20..a7275f008 100644 --- a/Views/Library/ZimFilesDownloads.swift +++ b/Views/Library/ZimFilesDownloads.swift @@ -23,6 +23,11 @@ struct ZimFilesDownloads: View { sortDescriptors: [NSSortDescriptor(keyPath: \DownloadTask.created, ascending: false)], animation: .easeInOut ) private var downloadTasks: FetchedResults + private let dismiss: (() -> Void)? + + init(dismiss: (() -> Void)?) { + self.dismiss = dismiss + } var body: some View { LazyVGrid( @@ -32,7 +37,7 @@ struct ZimFilesDownloads: View { ) { ForEach(downloadTasks) { downloadTask in if let zimFile = downloadTask.zimFile { - DownloadTaskCell(downloadTask).modifier(LibraryZimFileContext(zimFile: zimFile)) + DownloadTaskCell(downloadTask).modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss)) } } } diff --git a/Views/Library/ZimFilesNew.swift b/Views/Library/ZimFilesNew.swift index f014c7db6..d899b1664 100644 --- a/Views/Library/ZimFilesNew.swift +++ b/Views/Library/ZimFilesNew.swift @@ -32,6 +32,7 @@ struct ZimFilesNew: View { animation: .easeInOut ) private var zimFiles: FetchedResults @State private var searchText = "" + let dismiss: (() -> Void)? // iOS only var body: some View { LazyVGrid( @@ -41,7 +42,7 @@ struct ZimFilesNew: View { ) { ForEach(zimFiles) { zimFile in ZimFileCell(zimFile, prominent: .name) - .modifier(LibraryZimFileContext(zimFile: zimFile)) + .modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss)) } } .modifier(GridCommon()) @@ -112,7 +113,7 @@ struct ZimFilesNew: View { struct ZimFilesNew_Previews: PreviewProvider { static var previews: some View { NavigationStack { - ZimFilesNew() + ZimFilesNew(dismiss: nil) .environmentObject(LibraryViewModel()) .environment(\.managedObjectContext, Database.viewContext) } From 5bc7366686f3d18172f8f5efbda7dc49e74758f4 Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Sun, 14 Apr 2024 15:19:16 +0200 Subject: [PATCH 2/2] Update for macOS --- App/App_macOS.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/App/App_macOS.swift b/App/App_macOS.swift index 22a68fc94..0b7df0557 100644 --- a/App/App_macOS.swift +++ b/App/App_macOS.swift @@ -160,11 +160,11 @@ struct RootView: View { case .opened: ZimFilesOpened(dismiss: nil).modifier(LibraryZimFileDetailSidePanel()) case .categories: - ZimFilesCategories().modifier(LibraryZimFileDetailSidePanel()) + ZimFilesCategories(dismiss: nil).modifier(LibraryZimFileDetailSidePanel()) case .downloads: - ZimFilesDownloads().modifier(LibraryZimFileDetailSidePanel()) + ZimFilesDownloads(dismiss: nil).modifier(LibraryZimFileDetailSidePanel()) case .new: - ZimFilesNew().modifier(LibraryZimFileDetailSidePanel()) + ZimFilesNew(dismiss: nil).modifier(LibraryZimFileDetailSidePanel()) default: EmptyView() }