Skip to content

Commit 146f5da

Browse files
committed
Make suggested change to use indices
1 parent d538e14 commit 146f5da

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

TestApp/Sources/OPDS/OPDSFeeds/OPDSFeedView.swift

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ struct OPDSFeedView: View {
1818
let id: String
1919
let publication: ReadiumShared.Publication
2020

21-
init(publication: ReadiumShared.Publication) {
21+
init(id: String, publication: ReadiumShared.Publication) {
22+
self.id = id
2223
self.publication = publication
23-
24-
id = publication.links.first(where: { $0.rels.contains(.self) })?.href
25-
?? publication.metadata.identifier
26-
?? publication.metadata.title
27-
?? UUID().uuidString
2824
}
2925

3026
func hash(into hasher: inout Hasher) {
@@ -163,7 +159,9 @@ struct OPDSFeedView: View {
163159
let columns = [
164160
GridItem(.adaptive(minimum: 140), spacing: 16),
165161
]
166-
let navPublications = publications.map(NavigablePublication.init).unique()
162+
let navPublications = publications.enumerated().map { index, publication in
163+
NavigablePublication(id: "\(index)", publication: publication)
164+
}
167165

168166
ScrollView {
169167
LazyVGrid(columns: columns, spacing: 20) {
@@ -210,7 +208,7 @@ struct OPDSFeedView: View {
210208

211209
@ViewBuilder
212210
private func buildGroupsSection(_ groups: [ReadiumShared.Group]) -> some View {
213-
ForEach(groups, id: \.metadata.title) { group in
211+
ForEach(Array(groups.enumerated()), id: \.element.metadata.title) { groupIndex, group in
214212
HStack {
215213
Text(group.metadata.title)
216214
.font(.title3.bold())
@@ -235,7 +233,9 @@ struct OPDSFeedView: View {
235233
.padding(.bottom, 8)
236234

237235
if !group.publications.isEmpty {
238-
let navPublications = group.publications.map(NavigablePublication.init).unique()
236+
let navPublications = group.publications.enumerated().map { pubIndex, publication in
237+
NavigablePublication(id: "\(groupIndex)-\(pubIndex)", publication: publication)
238+
}
239239

240240
OPDSGroupRow(
241241
group: group,
@@ -276,11 +276,3 @@ struct OPDSFeedView: View {
276276
}
277277
}
278278
}
279-
280-
extension Sequence where Element: Hashable {
281-
/// Returns an array containing only the unique elements of the sequence.
282-
func unique() -> [Element] {
283-
var seen = Set<Element>()
284-
return filter { seen.insert($0).inserted }
285-
}
286-
}

0 commit comments

Comments
 (0)