Skip to content

Commit

Permalink
Merge pull request #1117 from kiwix/1116-ios-library-scroll-no-viewmo…
Browse files Browse the repository at this point in the history
…difier-fix

Fix library scroll issue - part 1
  • Loading branch information
kelson42 authored Feb 18, 2025
2 parents 594c2d5 + 97412d7 commit c5c76cb
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 97 deletions.
36 changes: 36 additions & 0 deletions Views/BuildingBlocks/ArticleActions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.

import SwiftUI

struct ArticleActions: View {

let zimFileID: UUID

var body: some View {
AsyncButton {
guard let url = await ZimFileService.shared.getMainPageURL(zimFileID: zimFileID) else { return }
NotificationCenter.openURL(url, inNewTab: true)
} label: {
Label(LocalString.library_zim_file_context_main_page_label, systemImage: "house")
}
AsyncButton {
guard let url = await ZimFileService.shared.getRandomPageURL(zimFileID: zimFileID) else { return }
NotificationCenter.openURL(url, inNewTab: true)
} label: {
Label(LocalString.library_zim_file_context_random_label, systemImage: "die.face.5")
}
}
}
35 changes: 35 additions & 0 deletions Views/BuildingBlocks/CopyPasteMenu.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.

import SwiftUI
import UniformTypeIdentifiers

struct CopyPasteMenu: View {

let downloadURL: URL

var body: some View {
Button {
#if os(macOS)
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(downloadURL.absoluteString, forType: .string)
#elseif os(iOS)
UIPasteboard.general.setValue(downloadURL.absoluteString, forPasteboardType: UTType.url.identifier)
#endif
} label: {
Label(LocalString.library_zim_file_context_copy_url, systemImage: "doc.on.doc")
}
}
}
67 changes: 22 additions & 45 deletions Views/Library/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,70 +112,47 @@ struct LibraryZimFileDetailSidePanel: ViewModifier {

/// On macOS, converts the modified view to a Button that modifies the currently selected zim file
/// On iOS, converts the modified view to a NavigationLink that goes to the zim file detail.
struct LibraryZimFileContext: ViewModifier {
struct LibraryZimFileContext<Content: View>: View {
@EnvironmentObject private var viewModel: LibraryViewModel
@EnvironmentObject private var navigation: NavigationViewModel

let zimFile: ZimFile
let dismiss: (() -> Void)? // iOS only

init(zimFile: ZimFile, dismiss: (() -> Void)?) {

private let content: Content
private let zimFile: ZimFile
/// iOS only
private let dismiss: (() -> Void)?

init(
@ViewBuilder content: () -> Content,
zimFile: ZimFile,
dismiss: (() -> Void)? = nil
) {
self.content = content()
self.zimFile = zimFile
self.dismiss = dismiss
}

func body(content: Content) -> some View {
var body: some View {
Group {
#if os(macOS)
#if os(macOS)
Button {
viewModel.selectedZimFile = zimFile
} label: {
content
}.buttonStyle(.plain)
#elseif os(iOS)
#elseif os(iOS)
NavigationLink {
ZimFileDetail(zimFile: zimFile, dismissParent: dismiss)
} label: {
content
}
#endif
#endif
}.contextMenu {
if zimFile.fileURLBookmark != nil, !zimFile.isMissing {
Section { articleActions }
Section { ArticleActions(zimFileID: zimFile.fileID) }
}
Section { supplementaryActions }
}
}

@ViewBuilder
var articleActions: some View {
AsyncButton {
guard let url = await ZimFileService.shared.getMainPageURL(zimFileID: zimFile.fileID) else { return }
NotificationCenter.openURL(url, inNewTab: true)
} label: {
Label(LocalString.library_zim_file_context_main_page_label, systemImage: "house")
}
AsyncButton {
guard let url = await ZimFileService.shared.getRandomPageURL(zimFileID: zimFile.fileID) else { return }
NotificationCenter.openURL(url, inNewTab: true)
} label: {
Label(LocalString.library_zim_file_context_random_label, systemImage: "die.face.5")
}
}

@ViewBuilder
var supplementaryActions: some View {
if let downloadURL = zimFile.downloadURL {
Button {
#if os(macOS)
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(downloadURL.absoluteString, forType: .string)
#elseif os(iOS)
UIPasteboard.general.setValue(downloadURL.absoluteString, forPasteboardType: UTType.url.identifier)
#endif
} label: {
Label(LocalString.library_zim_file_context_copy_url, systemImage: "doc.on.doc")
if let downloadURL = zimFile.downloadURL {
Section { CopyPasteMenu(downloadURL: downloadURL) }
}
}
}

}
18 changes: 12 additions & 6 deletions Views/Library/ZimFilesCategories.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,18 @@ private struct CategoryGrid: View {
ForEach(sections) { section in
if sections.count <= 1 {
ForEach(section) { zimFile in
ZimFileCell(zimFile, prominent: .size)
.modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss))
LibraryZimFileContext(
content: { ZimFileCell(zimFile, prominent: .size) },
zimFile: zimFile,
dismiss: dismiss)
}
} else {
Section {
ForEach(section) { zimFile in
ZimFileCell(zimFile, prominent: .size)
.modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss))
LibraryZimFileContext(
content: { ZimFileCell(zimFile, prominent: .size) },
zimFile: zimFile,
dismiss: dismiss)
}
} header: {
SectionHeader(
Expand Down Expand Up @@ -244,8 +248,10 @@ private struct CategoryList: View {
}
} else {
List(zimFiles, id: \.self, selection: $viewModel.selectedZimFile) { zimFile in
ZimFileRow(zimFile)
.modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss))
LibraryZimFileContext(
content: { ZimFileRow(zimFile) },
zimFile: zimFile,
dismiss: dismiss)
}
#if os(macOS)
.listStyle(.inset)
Expand Down
9 changes: 5 additions & 4 deletions Views/Library/ZimFilesDownloads.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ struct ZimFilesDownloads: View {
alignment: .leading,
spacing: 12
) {
ForEach(downloadTasks) { downloadTask in
if let zimFile = downloadTask.zimFile {
DownloadTaskCell(zimFile).modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss))
}
ForEach(downloadTasks.compactMap(\.zimFile)) { zimFile in
LibraryZimFileContext(
content: { DownloadTaskCell(zimFile) },
zimFile: zimFile,
dismiss: dismiss)
}
}
.modifier(GridCommon())
Expand Down
Loading

0 comments on commit c5c76cb

Please sign in to comment.