-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SwiftUI CachedAsyncImage and AvatarView (#310)
* Add CachedAsyncImage and AvatarView * Some improvements * Add demo page * Format * Small update * Remove unused * Fix typo * Replace the init parameters with a decorator function * Format * Remove DefaultAvatarContent * Simplification * One more simplification * Revert "One more simplification" This reverts commit 43880de. * Make swiftformat
- Loading branch information
Showing
9 changed files
with
346 additions
and
6 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Demo/Demo/Gravatar-SwiftUI-Demo/Assets.xcassets/profileAvatar.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"filename" : "[email protected]", | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"filename" : "[email protected]", | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+749 Bytes
...avatar-SwiftUI-Demo/Assets.xcassets/profileAvatar.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.27 KB
...avatar-SwiftUI-Demo/Assets.xcassets/profileAvatar.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// DemoAvatarView.swift | ||
// Gravatar-SwiftUI-Demo | ||
// | ||
// Created by Pinar Olguc on 8.07.2024. | ||
// | ||
|
||
import SwiftUI | ||
@testable import GravatarUI | ||
|
||
struct DemoAvatarView: View { | ||
enum Constants { | ||
static let avatarWidth: CGFloat = 120 | ||
static let borderWidth: CGFloat = 2 | ||
} | ||
|
||
@AppStorage("email") private var email: String = "" | ||
@State var borderWidthDouble: Double? = Constants.borderWidth | ||
@State var borderWidth: CGFloat = Constants.borderWidth | ||
@State var forceRefresh: Bool = false | ||
@State var isAnimated: Bool = true | ||
|
||
var avatarURL: AvatarURL? { | ||
AvatarURL( | ||
with: .email(email), | ||
options: .init( | ||
preferredSize: .points(Constants.avatarWidth), | ||
defaultAvatarOption: .status404 | ||
) | ||
) | ||
} | ||
|
||
var body: some View { | ||
VStack(alignment: .leading, spacing: 20) { | ||
TextField("Email", text: $email) | ||
.textInputAutocapitalization(.never) | ||
.keyboardType(.emailAddress) | ||
.disableAutocorrection(true) | ||
TextField("Border width", value: $borderWidthDouble, format: .number) | ||
.disableAutocorrection(true) | ||
Toggle("Force refresh", isOn: $forceRefresh) | ||
Toggle("Animated", isOn: $isAnimated) | ||
|
||
AvatarView( | ||
avatarURL: avatarURL, | ||
placeholder: Image("profileAvatar").renderingMode(.template), | ||
forceRefresh: $forceRefresh, | ||
loadingView: { | ||
ProgressView() | ||
.progressViewStyle(CircularProgressViewStyle()) | ||
}, | ||
transaction: Transaction(animation: isAnimated ? .easeInOut(duration: 0.3) : nil) | ||
) | ||
.avatarShape(RoundedRectangle(cornerRadius: 8), | ||
borderColor: .purple, | ||
borderWidth: borderWidth) | ||
.foregroundColor(.purple) | ||
.frame(width: Constants.avatarWidth) | ||
} | ||
.padding() | ||
.onChange(of: borderWidthDouble) { oldValue, newValue in | ||
self.borderWidth = CGFloat(newValue ?? 0) | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
DemoAvatarView() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import Gravatar | ||
import SwiftUI | ||
|
||
@MainActor | ||
struct AvatarView<LoadingView: View>: View { | ||
typealias LoadingViewBlock = () -> LoadingView | ||
@ViewBuilder private let loadingView: LoadingViewBlock? | ||
@Binding private var forceRefresh: Bool | ||
@State private var isLoading: Bool = false | ||
private var avatarURL: AvatarURL? | ||
private let placeholder: Image? | ||
private let cache: ImageCaching | ||
private let urlSession: URLSession | ||
private let transaction: Transaction | ||
|
||
init( | ||
avatarURL: AvatarURL?, | ||
placeholder: Image?, | ||
cache: ImageCaching = ImageCache.shared, | ||
urlSession: URLSession = .shared, | ||
forceRefresh: Binding<Bool> = .constant(false), | ||
loadingView: LoadingViewBlock?, | ||
transaction: Transaction = Transaction() | ||
) { | ||
self.avatarURL = avatarURL | ||
self.placeholder = placeholder | ||
self.cache = cache | ||
self.loadingView = loadingView | ||
self.urlSession = urlSession | ||
self._forceRefresh = forceRefresh | ||
self.transaction = transaction | ||
} | ||
|
||
var body: some View { | ||
CachedAsyncImage( | ||
url: avatarURL?.url, | ||
cache: cache, | ||
urlSession: urlSession, | ||
forceRefresh: $forceRefresh, | ||
transaction: transaction, | ||
isLoading: $isLoading | ||
) { phase in | ||
ZStack { | ||
content(for: phase) | ||
|
||
if isLoading { | ||
if let loadingView = loadingView?() { | ||
loadingView | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
private func content(for phase: AsyncImagePhase) -> some View { | ||
switch phase { | ||
case .success(let image): | ||
scaledImage(image) | ||
case .failure, .empty: | ||
if let placeholder { | ||
scaledImage(placeholder) | ||
} | ||
@unknown default: | ||
if let placeholder { | ||
scaledImage(placeholder) | ||
} | ||
} | ||
} | ||
|
||
private func scaledImage(_ image: Image) -> some View { | ||
image | ||
.resizable() | ||
.scaledToFit() | ||
} | ||
|
||
func avatarShape(_ shape: some Shape, borderColor: Color = .clear, borderWidth: CGFloat = 0) -> some View { | ||
self | ||
.clipShape(shape) | ||
.overlay( | ||
shape | ||
.stroke(borderColor, lineWidth: borderWidth) | ||
) | ||
} | ||
} | ||
|
||
#Preview { | ||
let avatarURL = AvatarURL( | ||
with: .email("[email protected]"), | ||
options: .init(preferredSize: .points(100)) | ||
) | ||
return AvatarView( | ||
avatarURL: avatarURL, | ||
placeholder: Image(systemName: "person") | ||
.renderingMode(.template) | ||
.resizable(), | ||
loadingView: { | ||
ProgressView() | ||
.progressViewStyle(CircularProgressViewStyle()) | ||
}, | ||
transaction: Transaction(animation: .easeInOut(duration: 1)) | ||
) | ||
.avatarShape(RoundedRectangle(cornerRadius: 20), borderColor: Color.accentColor, borderWidth: 2) | ||
.frame(width: 100, height: 100, alignment: .center) | ||
} |
Oops, something went wrong.