This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 placeholder and failure support alse README updated
- Loading branch information
1 parent
84ce935
commit 2275708
Showing
8 changed files
with
149 additions
and
109 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -52,7 +52,6 @@ struct ImageUIView: View { | |
}) | ||
} | ||
.scaledToFill() | ||
.sizeInto(box: boundingBox) | ||
} else { | ||
fallback | ||
} | ||
|
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
55 changes: 55 additions & 0 deletions
55
Sources/SAPCAI/UI/Common/UIKit/ImageViewRepresentable.swift
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,55 @@ | ||
import SDWebImage | ||
import SwiftUI | ||
|
||
struct ImageViewRepresentable: UIViewRepresentable { | ||
var url: URL? | ||
var imageManager: ImageManager | ||
let wrapper = UIImageViewWrapper() | ||
|
||
func makeUIView(context: Self.Context) -> UIImageViewWrapper { | ||
self.wrapper | ||
} | ||
|
||
func updateUIView(_ uiView: UIImageViewWrapper, context: UIViewRepresentableContext<ImageViewRepresentable>) { | ||
uiView.imageView.contentMode = self.imageManager.contentMode | ||
if case ImageManager.ImageState.error = self.imageManager.state, uiView.imageView.image == nil { | ||
self.loadImage() | ||
} | ||
} | ||
|
||
func loadImage() { | ||
self.wrapper.imageView.sd_setImage(with: self.url, | ||
placeholderImage: nil) { image, error, _, _ in | ||
if let error = error { | ||
imageManager.state = .error(error) | ||
} else if let image = image { | ||
imageManager.state = .completed(image: image) | ||
} else { | ||
imageManager.state = .error(NSError(domain: "cai.com", code: 1, userInfo: nil)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
class UIImageViewWrapper: UIView { | ||
lazy var imageView: UIImageView = { | ||
let imageView = UIImageView() | ||
imageView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) | ||
imageView.setContentCompressionResistancePriority(.defaultLow, for: .vertical) | ||
imageView.setContentHuggingPriority(.defaultLow, for: .vertical) | ||
imageView.setContentHuggingPriority(.defaultLow, for: .horizontal) | ||
return imageView | ||
}() | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
addSubview(self.imageView) | ||
self.imageView.bindFrameToSuperviewBounds() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
addSubview(self.imageView) | ||
self.imageView.bindFrameToSuperviewBounds() | ||
} | ||
} |
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,15 @@ | ||
import UIKit | ||
|
||
extension UIView { | ||
func bindFrameToSuperviewBounds() { | ||
guard let superview = self.superview else { | ||
print("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.") | ||
return | ||
} | ||
self.translatesAutoresizingMaskIntoConstraints = false | ||
self.topAnchor.constraint(equalTo: superview.topAnchor, constant: 0).isActive = true | ||
self.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: 0).isActive = true | ||
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 0).isActive = true | ||
self.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: 0).isActive = true | ||
} | ||
} |