Skip to content

Commit

Permalink
Merge pull request #728 from kiwix/714-dimiss-fixes-part2
Browse files Browse the repository at this point in the history
714 dimiss fixes part2
  • Loading branch information
kelson42 authored Apr 14, 2024
2 parents 5024548 + 5bc7366 commit f8a5a26
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 22 deletions.
6 changes: 3 additions & 3 deletions App/App_macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions App/SplitViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
8 changes: 4 additions & 4 deletions Views/Library/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions Views/Library/ZimFileDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 19 additions & 9 deletions Views/Library/ZimFilesCategories.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -87,10 +90,12 @@ private struct CategoryGrid: View {
@EnvironmentObject private var viewModel: LibraryViewModel
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@SectionedFetchRequest private var sections: SectionedFetchResults<String, ZimFile>
private let dismiss: (() -> Void)? // iOS only

init(category: Binding<Category>, searchText: Binding<String>) {
init(category: Binding<Category>, searchText: Binding<String>, dismiss: (() -> Void)?) {
self._category = category
self._searchText = searchText
self.dismiss = dismiss
self._sections = SectionedFetchRequest<String, ZimFile>(
sectionIdentifier: \.name,
sortDescriptors: [SortDescriptor(\ZimFile.name), SortDescriptor(\.size, order: .reverse)],
Expand All @@ -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(
Expand Down Expand Up @@ -179,10 +184,15 @@ private struct CategoryList: View {
@Default(.libraryLanguageCodes) private var languageCodes
@EnvironmentObject private var viewModel: LibraryViewModel
@FetchRequest private var zimFiles: FetchedResults<ZimFile>
private let dismiss: (() -> Void)?

init(category: Binding<Category>, searchText: Binding<String>) {
init(category: Binding<Category>,
searchText: Binding<String>,
dismiss: (() -> Void)?
) {
self._category = category
self._searchText = searchText
self.dismiss = dismiss
self._zimFiles = FetchRequest<ZimFile>(
sortDescriptors: [
NSSortDescriptor(
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion Views/Library/ZimFilesDownloads.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ struct ZimFilesDownloads: View {
sortDescriptors: [NSSortDescriptor(keyPath: \DownloadTask.created, ascending: false)],
animation: .easeInOut
) private var downloadTasks: FetchedResults<DownloadTask>
private let dismiss: (() -> Void)?

init(dismiss: (() -> Void)?) {
self.dismiss = dismiss
}

var body: some View {
LazyVGrid(
Expand All @@ -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))
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions Views/Library/ZimFilesNew.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct ZimFilesNew: View {
animation: .easeInOut
) private var zimFiles: FetchedResults<ZimFile>
@State private var searchText = ""
let dismiss: (() -> Void)? // iOS only

var body: some View {
LazyVGrid(
Expand All @@ -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())
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit f8a5a26

Please sign in to comment.