Skip to content

Commit c5c76cb

Browse files
authored
Merge pull request #1117 from kiwix/1116-ios-library-scroll-no-viewmodifier-fix
Fix library scroll issue - part 1
2 parents 594c2d5 + 97412d7 commit c5c76cb

File tree

7 files changed

+215
-97
lines changed

7 files changed

+215
-97
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This file is part of Kiwix for iOS & macOS.
2+
//
3+
// Kiwix is free software; you can redistribute it and/or modify it
4+
// under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation; either version 3 of the License, or
6+
// any later version.
7+
//
8+
// Kiwix is distributed in the hope that it will be useful, but
9+
// WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Kiwix; If not, see https://www.gnu.org/licenses/.
15+
16+
import SwiftUI
17+
18+
struct ArticleActions: View {
19+
20+
let zimFileID: UUID
21+
22+
var body: some View {
23+
AsyncButton {
24+
guard let url = await ZimFileService.shared.getMainPageURL(zimFileID: zimFileID) else { return }
25+
NotificationCenter.openURL(url, inNewTab: true)
26+
} label: {
27+
Label(LocalString.library_zim_file_context_main_page_label, systemImage: "house")
28+
}
29+
AsyncButton {
30+
guard let url = await ZimFileService.shared.getRandomPageURL(zimFileID: zimFileID) else { return }
31+
NotificationCenter.openURL(url, inNewTab: true)
32+
} label: {
33+
Label(LocalString.library_zim_file_context_random_label, systemImage: "die.face.5")
34+
}
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This file is part of Kiwix for iOS & macOS.
2+
//
3+
// Kiwix is free software; you can redistribute it and/or modify it
4+
// under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation; either version 3 of the License, or
6+
// any later version.
7+
//
8+
// Kiwix is distributed in the hope that it will be useful, but
9+
// WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with Kiwix; If not, see https://www.gnu.org/licenses/.
15+
16+
import SwiftUI
17+
import UniformTypeIdentifiers
18+
19+
struct CopyPasteMenu: View {
20+
21+
let downloadURL: URL
22+
23+
var body: some View {
24+
Button {
25+
#if os(macOS)
26+
NSPasteboard.general.clearContents()
27+
NSPasteboard.general.setString(downloadURL.absoluteString, forType: .string)
28+
#elseif os(iOS)
29+
UIPasteboard.general.setValue(downloadURL.absoluteString, forPasteboardType: UTType.url.identifier)
30+
#endif
31+
} label: {
32+
Label(LocalString.library_zim_file_context_copy_url, systemImage: "doc.on.doc")
33+
}
34+
}
35+
}

Views/Library/Library.swift

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -112,70 +112,47 @@ struct LibraryZimFileDetailSidePanel: ViewModifier {
112112

113113
/// On macOS, converts the modified view to a Button that modifies the currently selected zim file
114114
/// On iOS, converts the modified view to a NavigationLink that goes to the zim file detail.
115-
struct LibraryZimFileContext: ViewModifier {
115+
struct LibraryZimFileContext<Content: View>: View {
116116
@EnvironmentObject private var viewModel: LibraryViewModel
117-
@EnvironmentObject private var navigation: NavigationViewModel
118-
119-
let zimFile: ZimFile
120-
let dismiss: (() -> Void)? // iOS only
121-
122-
init(zimFile: ZimFile, dismiss: (() -> Void)?) {
117+
118+
private let content: Content
119+
private let zimFile: ZimFile
120+
/// iOS only
121+
private let dismiss: (() -> Void)?
122+
123+
init(
124+
@ViewBuilder content: () -> Content,
125+
zimFile: ZimFile,
126+
dismiss: (() -> Void)? = nil
127+
) {
128+
self.content = content()
123129
self.zimFile = zimFile
124130
self.dismiss = dismiss
125131
}
126-
127-
func body(content: Content) -> some View {
132+
133+
var body: some View {
128134
Group {
129-
#if os(macOS)
135+
#if os(macOS)
130136
Button {
131137
viewModel.selectedZimFile = zimFile
132138
} label: {
133139
content
134140
}.buttonStyle(.plain)
135-
#elseif os(iOS)
141+
#elseif os(iOS)
136142
NavigationLink {
137143
ZimFileDetail(zimFile: zimFile, dismissParent: dismiss)
138144
} label: {
139145
content
140146
}
141-
#endif
147+
#endif
142148
}.contextMenu {
143149
if zimFile.fileURLBookmark != nil, !zimFile.isMissing {
144-
Section { articleActions }
150+
Section { ArticleActions(zimFileID: zimFile.fileID) }
145151
}
146-
Section { supplementaryActions }
147-
}
148-
}
149-
150-
@ViewBuilder
151-
var articleActions: some View {
152-
AsyncButton {
153-
guard let url = await ZimFileService.shared.getMainPageURL(zimFileID: zimFile.fileID) else { return }
154-
NotificationCenter.openURL(url, inNewTab: true)
155-
} label: {
156-
Label(LocalString.library_zim_file_context_main_page_label, systemImage: "house")
157-
}
158-
AsyncButton {
159-
guard let url = await ZimFileService.shared.getRandomPageURL(zimFileID: zimFile.fileID) else { return }
160-
NotificationCenter.openURL(url, inNewTab: true)
161-
} label: {
162-
Label(LocalString.library_zim_file_context_random_label, systemImage: "die.face.5")
163-
}
164-
}
165-
166-
@ViewBuilder
167-
var supplementaryActions: some View {
168-
if let downloadURL = zimFile.downloadURL {
169-
Button {
170-
#if os(macOS)
171-
NSPasteboard.general.clearContents()
172-
NSPasteboard.general.setString(downloadURL.absoluteString, forType: .string)
173-
#elseif os(iOS)
174-
UIPasteboard.general.setValue(downloadURL.absoluteString, forPasteboardType: UTType.url.identifier)
175-
#endif
176-
} label: {
177-
Label(LocalString.library_zim_file_context_copy_url, systemImage: "doc.on.doc")
152+
if let downloadURL = zimFile.downloadURL {
153+
Section { CopyPasteMenu(downloadURL: downloadURL) }
178154
}
179155
}
180156
}
157+
181158
}

Views/Library/ZimFilesCategories.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,18 @@ private struct CategoryGrid: View {
139139
ForEach(sections) { section in
140140
if sections.count <= 1 {
141141
ForEach(section) { zimFile in
142-
ZimFileCell(zimFile, prominent: .size)
143-
.modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss))
142+
LibraryZimFileContext(
143+
content: { ZimFileCell(zimFile, prominent: .size) },
144+
zimFile: zimFile,
145+
dismiss: dismiss)
144146
}
145147
} else {
146148
Section {
147149
ForEach(section) { zimFile in
148-
ZimFileCell(zimFile, prominent: .size)
149-
.modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss))
150+
LibraryZimFileContext(
151+
content: { ZimFileCell(zimFile, prominent: .size) },
152+
zimFile: zimFile,
153+
dismiss: dismiss)
150154
}
151155
} header: {
152156
SectionHeader(
@@ -244,8 +248,10 @@ private struct CategoryList: View {
244248
}
245249
} else {
246250
List(zimFiles, id: \.self, selection: $viewModel.selectedZimFile) { zimFile in
247-
ZimFileRow(zimFile)
248-
.modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss))
251+
LibraryZimFileContext(
252+
content: { ZimFileRow(zimFile) },
253+
zimFile: zimFile,
254+
dismiss: dismiss)
249255
}
250256
#if os(macOS)
251257
.listStyle(.inset)

Views/Library/ZimFilesDownloads.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ struct ZimFilesDownloads: View {
3535
alignment: .leading,
3636
spacing: 12
3737
) {
38-
ForEach(downloadTasks) { downloadTask in
39-
if let zimFile = downloadTask.zimFile {
40-
DownloadTaskCell(zimFile).modifier(LibraryZimFileContext(zimFile: zimFile, dismiss: dismiss))
41-
}
38+
ForEach(downloadTasks.compactMap(\.zimFile)) { zimFile in
39+
LibraryZimFileContext(
40+
content: { DownloadTaskCell(zimFile) },
41+
zimFile: zimFile,
42+
dismiss: dismiss)
4243
}
4344
}
4445
.modifier(GridCommon())

0 commit comments

Comments
 (0)