-
-
Notifications
You must be signed in to change notification settings - Fork 535
Expand file tree
/
Copy pathLibraryElement.swift
More file actions
179 lines (152 loc) · 5.65 KB
/
Copy pathLibraryElement.swift
File metadata and controls
179 lines (152 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// Swiftfin is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2026 Jellyfin & Jellyfin Contributors
//
import CollectionVGrid
import SwiftUI
private let libraryListLandscapeWidth: CGFloat = 110
private let libraryListPortraitWidth: CGFloat = 60
@MainActor
protocol LibraryElement: Poster {
associatedtype GridBody: View = DefaultLibraryGridElement<Self>
associatedtype ListBody: View = DefaultLibraryListElement<Self>
func libraryDidSelectElement(router: Router.Wrapper, in namespace: Namespace.ID)
@ViewBuilder
func makeGridBody(libraryStyle: LibraryStyle) -> GridBody
@ViewBuilder
func makeListBody(libraryStyle: LibraryStyle) -> ListBody
static func layout(for libraryStyle: LibraryStyle) -> CollectionVGridLayout
}
extension LibraryElement {
func makeGridBody(libraryStyle: LibraryStyle) -> DefaultLibraryGridElement<Self> {
DefaultLibraryGridElement(element: self, libraryStyle: libraryStyle)
}
func makeListBody(libraryStyle: LibraryStyle) -> DefaultLibraryListElement<Self> {
DefaultLibraryListElement(element: self, libraryStyle: libraryStyle)
}
static func layout(for libraryStyle: LibraryStyle) -> CollectionVGridLayout {
#if os(iOS)
let gridLayout: CollectionVGridLayout = {
switch libraryStyle.posterDisplayType {
case .landscape:
.minWidth(220)
case .portrait, .square:
.minWidth(140)
}
}()
let phoneGridLayout: CollectionVGridLayout = {
switch libraryStyle.posterDisplayType {
case .landscape:
.columns(2)
case .portrait, .square:
.columns(3)
}
}()
switch libraryStyle.displayType {
case .grid:
return UIDevice.isPhone ? phoneGridLayout : gridLayout
case .list:
return .columns(libraryStyle.listColumnCount, insets: .zero, itemSpacing: 0, lineSpacing: 0)
}
#else
switch libraryStyle.displayType {
case .grid:
switch libraryStyle.posterDisplayType {
case .landscape:
return .columns(
5,
insets: EdgeInsets.edgeInsets,
itemSpacing: EdgeInsets.edgePadding,
lineSpacing: EdgeInsets.edgePadding
)
case .portrait, .square:
return .columns(
7,
insets: EdgeInsets.edgeInsets,
itemSpacing: EdgeInsets.edgePadding,
lineSpacing: EdgeInsets.edgePadding
)
}
case .list:
return .columns(
libraryStyle.listColumnCount,
insets: EdgeInsets.edgeInsets,
itemSpacing: EdgeInsets.edgePadding,
lineSpacing: EdgeInsets.edgePadding
)
}
#endif
}
}
struct DefaultLibraryGridElement<Element: LibraryElement>: View {
@Namespace
private var namespace
@Router
private var router
let element: Element
let libraryStyle: LibraryStyle
var body: some View {
Button {
element.libraryDidSelectElement(router: router, in: namespace)
} label: {
VStack(alignment: .leading, spacing: 6) {
PosterImage(item: element, type: libraryStyle.posterDisplayType)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.posterStyle(libraryStyle.posterDisplayType)
.backport
.matchedTransitionSource(id: "item", in: namespace)
.posterShadow()
if element.showTitle {
Text(element.displayTitle)
.font(.footnote)
.foregroundStyle(.primary)
.lineLimit(1, reservesSpace: true)
}
}
}
.buttonStyle(.plain)
.foregroundStyle(.primary, .secondary)
}
}
struct DefaultLibraryListElement<Element: LibraryElement>: View {
@Namespace
private var namespace
@Router
private var router
let element: Element
let libraryStyle: LibraryStyle
private var posterWidth: CGFloat {
libraryStyle.posterDisplayType == .landscape ? libraryListLandscapeWidth : libraryListPortraitWidth
}
var body: some View {
ListRow(insets: .init(vertical: 8, horizontal: EdgeInsets.edgePadding)) {
PosterImage(item: element, type: libraryStyle.posterDisplayType)
.posterStyle(libraryStyle.posterDisplayType)
.frame(width: posterWidth)
.backport
.matchedTransitionSource(id: "item", in: namespace)
.posterShadow()
} content: {
VStack(alignment: .leading, spacing: 5) {
Text(element.displayTitle)
.font(.callout)
.fontWeight(.semibold)
.foregroundStyle(.primary)
.lineLimit(2)
.multilineTextAlignment(.leading)
if let subtitle = element.subtitle {
Text(subtitle)
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
} action: {
element.libraryDidSelectElement(router: router, in: namespace)
}
}
}