-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathContentView.swift
178 lines (170 loc) · 6.92 KB
/
ContentView.swift
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
/*
* This file is part of the SDWebImage package.
* (c) DreamPiggy <lizhuoli1126@126.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import SwiftUI
import SDWebImage
import SDWebImageSwiftUI
extension String : Identifiable {
public typealias ID = Int
public var id: Int {
self.hashValue
}
}
struct ContentView: View {
@State var imageURLs = [
"http://assets.sbnation.com/assets/2512203/dogflops.gif",
"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
"http://apng.onevcat.com/assets/elephant.png",
"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp",
"http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp",
"http://littlesvr.ca/apng/images/SteamEngine.webp",
"http://littlesvr.ca/apng/images/world-cup-2014-42.webp",
"https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp",
"https://nokiatech.github.io/heif/content/images/ski_jump_1440x960.heic",
"https://nokiatech.github.io/heif/content/image_sequences/starfield_animation.heic",
"https://www.sample-videos.com/img/Sample-png-image-1mb.png",
"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png",
"https://raw.githubusercontent.com/ibireme/YYImage/master/Demo/YYImageDemo/mew_baseline.jpg",
"http://via.placeholder.com/200x200.jpg",
"https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/w3c.svg",
"https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/wikimedia.svg",
"https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/stack_of_photos.pdf",
"https://raw.githubusercontent.com/icons8/flat-color-icons/master/pdf/smartphone_tablet.pdf"
]
@State var animated: Bool = true // You can change between WebImage/AnimatedImage
var body: some View {
#if os(iOS) || os(tvOS)
return NavigationView {
contentView()
.navigationBarTitle(animated ? "AnimatedImage" : "WebImage")
.navigationBarItems(leading:
Button(action: { self.reloadCache() }) {
Text("Reload")
}, trailing:
Button(action: { self.switchView() }) {
Text("Switch")
}
)
}
#endif
#if os(macOS)
return NavigationView {
contentView()
.frame(minWidth: 200)
.listStyle(SidebarListStyle())
.contextMenu {
Button(action: { self.reloadCache() }) {
Text("Reload")
}
Button(action: { self.switchView() }) {
Text("Switch")
}
}
}
#endif
#if os(watchOS)
return contentView()
.contextMenu {
Button(action: { self.reloadCache() }) {
Text("Reload")
}
Button(action: { self.switchView() }) {
Text("Switch")
}
}
#endif
}
func contentView() -> some View {
List {
ForEach(imageURLs) { url in
NavigationLink(destination: DetailView(url: url, animated: self.animated)) {
HStack {
if self.animated {
#if os(macOS) || os(iOS) || os(tvOS)
AnimatedImage(url: URL(string:url))
.onViewUpdate { view, context in
#if os(macOS)
view.toolTip = url
#endif
}
.indicator(SDWebImageActivityIndicator.medium)
/**
.placeholder(UIImage(systemName: "photo"))
*/
.transition(.fade)
.resizable()
.scaledToFit()
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
#else
WebImage(url: URL(string:url), isAnimating: self.$animated)
.resizable()
.indicator { _, _ in
ActivityBar()
.foregroundColor(Color.white)
.frame(width: 50, height: 50)
}
.animation(.easeInOut(duration: 0.5))
.transition(.fade)
.scaledToFit()
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
#endif
} else {
#if os(macOS) || os(iOS) || os(tvOS)
WebImage(url: URL(string:url))
.resizable()
/**
.placeholder {
Image(systemName: "photo")
}
*/
.indicator(.activity)
.animation(.easeInOut(duration: 0.5))
.transition(.fade)
.scaledToFit()
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
#else
WebImage(url: URL(string:url))
.resizable()
.indicator { _, _ in
ActivityBar()
.foregroundColor(Color.white)
.frame(width: 50, height: 50)
}
.animation(.easeInOut(duration: 0.5))
.transition(.fade)
.scaledToFit()
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)
#endif
}
Text((url as NSString).lastPathComponent)
}
}
.buttonStyle(PlainButtonStyle())
}
.onDelete { indexSet in
indexSet.forEach { index in
self.imageURLs.remove(at: index)
}
}
}
}
func reloadCache() {
SDImageCache.shared.clearMemory()
SDImageCache.shared.clearDisk(onCompletion: nil)
}
func switchView() {
SDImageCache.shared.clearMemory()
animated.toggle()
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif