-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathContentView.swift
More file actions
178 lines (154 loc) · 8.36 KB
/
ContentView.swift
File metadata and controls
178 lines (154 loc) · 8.36 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
//
// ContentView.swift
// VideoNavigation-Example
//
// Created by shayanbo on 2023/7/21.
//
import SwiftUI
import AVKit
import VideoPlayerContainer
struct ContentView: View {
@StateObject private var vm = FeedsViewModel()
private let listCoordinateSpaceName = UUID().uuidString
var body: some View {
NavigationStack(path: vm.navigatorBinding) {
GeometryReader { proxy in
ScrollView {
LazyVStack {
ForEach(vm.videos) { video in
HStack {
AsyncImage(url: URL(string: video.avatarUrl)!) { image in
image.resizable()
} placeholder: {
Color.gray
}
.frame(width: 30, height: 30)
.clipShape(Circle())
.overlay {
Circle().strokeBorder(.gray.opacity(0.3), lineWidth: 1)
}
VStack(alignment: .leading) {
Text(video.author)
Text(video.date)
.font(.system(size:12))
.foregroundColor(.gray)
}
Spacer()
Image(systemName: "ellipsis")
}
.padding(.horizontal, 15)
.padding(.top, 15)
HStack {
Text(video.desc)
.fixedSize(horizontal: false, vertical: true)
.multilineTextAlignment(.leading)
Spacer()
}
.padding(.horizontal, 15)
HStack {
PlayerWidget(vm.context(video: video))
.background(.gray)
.cornerRadius(10)
.frame(width: 200, height: 200 / video.aspectRatio)
.onAppear {
let context = vm.context(video: video)
context.gesture.simultaneousTapGesture = SpatialTapGesture()
.onEnded { _ in
vm.push(video)
vm.activeVideo = video
}
context.control.configure(displayStyle: .always)
context.control.configure(.halfScreen, insets: .init(top: 0, leading: 5, bottom: 10, trailing: 5))
context.control.configure(.halfScreen(.bottom1)) {[
CountdownWidget(),
Spacer(),
PlaybackWidget()
]}
}
.background(
GeometryReader {proxy in
Color.clear.videoFrameProvider(video: video, frame: proxy.frame(in: .named(listCoordinateSpaceName)))
}
)
Spacer()
}
.padding(.horizontal, 15)
HStack {
Label("\(video.likes)", systemImage: "suit.heart")
.labelStyle(ActionStyle())
.padding(.trailing, 20)
Label("\(video.comments)", systemImage:"ellipsis.message")
.labelStyle(ActionStyle())
.padding(.trailing, 20)
Label("\(video.favors)", systemImage:"star")
.labelStyle(ActionStyle())
.padding(.trailing, 20)
Label("\(video.shares)", systemImage:"arrowshape.turn.up.right")
.labelStyle(ActionStyle())
.padding(.trailing, 20)
}
.padding(.horizontal, 15)
.padding(.vertical, 5)
HStack {
VStack {
AsyncImage(url: URL(string: video.avatarUrl)!) { image in
image.resizable()
} placeholder: {
Color.gray
}
.frame(width: 30, height: 30)
.clipShape(Circle())
.overlay {
Circle().strokeBorder(.gray.opacity(0.3), lineWidth: 1)
}
Spacer()
}
VStack(alignment: .leading) {
Text(video.topComment.author)
.font(.system(size:13))
.foregroundColor(.gray)
.fontWeight(.light)
Text(video.topComment.comment)
.fixedSize(horizontal: false, vertical: true)
.font(.system(size:14))
.fontWeight(.light)
Spacer()
}
Spacer()
Label("\(video.topComment.likes)", systemImage: "suit.heart")
.labelStyle(CommentStyle())
.padding(.leading, 10)
}
.padding(.horizontal, 15)
Rectangle()
.fill(.gray.opacity(0.3))
.frame(height: 10)
}
}
}
.coordinateSpace(name: listCoordinateSpaceName)
.navigationDestination(for: Video.self) { video in
let context = self.vm.context(video: video)
VideoDetail(video: video, player: context.render.player)
}
.onPreferenceChange(VideoFramePreferenceKey.self) { videoFrames in
let listBounds = proxy.frame(in: .local)
guard let videoFrame = videoFrames.sorted(by: { lhs, rhs in
let lhsExpose = listBounds.intersection(lhs.frame).height / lhs.frame.height
let rhsExpose = listBounds.intersection(rhs.frame).height / rhs.frame.height
return lhsExpose > rhsExpose
}).first else {
return
}
vm.activeVideo = videoFrame.video
}
}
}
.searchable(text: vm.searchTextBinding)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}