-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAsyncImageView.swift
115 lines (104 loc) · 3.97 KB
/
AsyncImageView.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
// Copyright 2022 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import SwiftUI
import ArcGIS
/// A view displaying an async image, with error display and progress view.
public struct AsyncImageView: View {
/// The `URL` of the image.
private var url: URL?
/// The `LoadableImage` representing the view.
private var loadableImage: LoadableImage?
/// The `ContentMode` defining how the image fills the available space.
private let contentMode: ContentMode
/// The refresh interval, in milliseconds. A refresh interval of 0 means never refresh.
private let refreshInterval: TimeInterval?
/// The size of the media's frame.
private let mediaSize: CGSize?
/// The data model for an `AsyncImageView`.
@StateObject private var viewModel = AsyncImageViewModel()
/// Creates an `AsyncImageView`.
/// - Parameters:
/// - url: The `URL` of the image.
/// - contentMode: The `ContentMode` defining how the image fills the available space.
/// - refreshInterval: The refresh interval, in seconds. A `nil` interval means never refresh.
/// - mediaSize: The size of the media's frame.
public init(
url: URL,
contentMode: ContentMode = .fit,
refreshInterval: TimeInterval? = nil,
mediaSize: CGSize? = nil
) {
self.contentMode = contentMode
self.mediaSize = mediaSize
self.url = url
self.refreshInterval = refreshInterval
loadableImage = nil
}
/// Creates an `AsyncImageView`.
/// - Parameters:
/// - loadableImage: The `LoadableImage` representing the image.
/// - contentMode: The `ContentMode` defining how the image fills the available space.
/// - mediaSize: The size of the media's frame.
public init(
loadableImage: LoadableImage,
contentMode: ContentMode = .fit,
mediaSize: CGSize? = nil
) {
self.contentMode = contentMode
self.mediaSize = mediaSize
self.loadableImage = loadableImage
refreshInterval = nil
url = nil
_viewModel = StateObject(wrappedValue: AsyncImageViewModel())
}
public var body: some View {
ZStack {
switch viewModel.result {
case .success(let image):
if let image {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: contentMode)
} else {
ProgressView()
}
case .failure(_):
HStack(alignment: .center) {
Image(systemName: "exclamationmark.circle")
.aspectRatio(contentMode: .fit)
.foregroundStyle(.red)
}
.padding([.top, .bottom])
}
if let progressInterval = viewModel.progressInterval {
VStack {
ProgressView(
timerInterval: progressInterval,
countsDown: true
)
.tint(.white)
.opacity(0.5)
.padding([.top], 4)
.frame(width: mediaSize?.width)
Spacer()
}
}
}
.onAppear() {
viewModel.url = url
viewModel.loadableImage = loadableImage
viewModel.refreshInterval = refreshInterval
}
}
}