fix width issue and warnings in gifview

This commit is contained in:
Nihaal Sharma committed 2026-04-29 09:41:52 +01:00
1 parent e148f8fff1
commit 7f24277728
3 files changed
+16 -23

No files matched your search

+6 -6
View File
@@ -18,7 +18,7 @@ struct GifView: View {
@State var timer: Timer? @State var timer: Timer?
nonisolated func run() async { nonisolated func run() async {
guard url.pathExtension == "gif" else { guard await url.pathExtension == "gif" else {
if let data = try? await URLSession.shared.data(from: url).0, if let data = try? await URLSession.shared.data(from: url).0,
let uiImage = UIImage(data: data), let uiImage = UIImage(data: data),
let cgImage = uiImage.cgImage { let cgImage = uiImage.cgImage {
@@ -30,7 +30,7 @@ struct GifView: View {
} }
do { do {
if gif.isEmpty { if await gif.isEmpty {
let frames = try await GifManager.gifFrom(url: url) let frames = try await GifManager.gifFrom(url: url)
withAnimation(.spring) { withAnimation(.spring) {
self.gif = frames self.gif = frames
@@ -42,8 +42,8 @@ struct GifView: View {
print("falied loading or empty gif") print("falied loading or empty gif")
return return
} }
if timer != nil { if await timer != nil {
timer!.invalidate() await timer!.invalidate()
timer = nil timer = nil
} }
@@ -54,7 +54,7 @@ struct GifView: View {
currentI += 1 currentI += 1
} }
} }
RunLoop.main.add(timer!, forMode: .common) await RunLoop.main.add(timer!, forMode: .common)
} }
var body: some View { var body: some View {
@@ -63,7 +63,6 @@ struct GifView: View {
if gif.isEmpty { if gif.isEmpty {
ProgressView() ProgressView()
// .controlSize(.large) // .controlSize(.large)
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else if url.pathExtension == "gif" { } else if url.pathExtension == "gif" {
TimelineView(.animation) { tl in TimelineView(.animation) { tl in
if currentI < gif.count { if currentI < gif.count {
@@ -82,6 +81,7 @@ struct GifView: View {
} }
} }
} }
.frame(maxWidth: .infinity, maxHeight: .infinity)
.transition(.scale) .transition(.scale)
.onAppear { .onAppear {
Task { Task {
@@ -15,7 +15,6 @@ struct EmojiCollectionView: UIViewRepresentable {
let items: [String] let items: [String]
let pack: EmojiPack? let pack: EmojiPack?
let width: CGFloat let width: CGFloat
let spacing: CGFloat = 16
let style: EmojiCollectionView.Style let style: EmojiCollectionView.Style
func makeUIView(context: Context) -> UICollectionView { func makeUIView(context: Context) -> UICollectionView {
@@ -40,7 +39,6 @@ struct EmojiCollectionView: UIViewRepresentable {
items: items, items: items,
pack: pack, pack: pack,
width: width, width: width,
spacing: spacing,
style: style style: style
) )
} }
@@ -56,7 +54,6 @@ struct EmojiCollectionView: UIViewRepresentable {
var items: [String] var items: [String]
var pack: EmojiPack? var pack: EmojiPack?
var width: CGFloat var width: CGFloat
var spacing: CGFloat
var style: EmojiCollectionView.Style var style: EmojiCollectionView.Style
var layout = UICollectionViewFlowLayout() var layout = UICollectionViewFlowLayout()
@@ -66,7 +63,6 @@ struct EmojiCollectionView: UIViewRepresentable {
items: [String], items: [String],
pack: EmojiPack?, pack: EmojiPack?,
width: CGFloat, width: CGFloat,
spacing: CGFloat,
style: EmojiCollectionView.Style style: EmojiCollectionView.Style
) { ) {
self.hoarder = hoarder self.hoarder = hoarder
@@ -76,15 +72,12 @@ struct EmojiCollectionView: UIViewRepresentable {
self.items = pack.items self.items = pack.items
} }
self.width = width self.width = width
self.spacing = spacing
self.style = style self.style = style
layout.sectionInset.left = 16
layout.sectionInset.right = 16
layout.itemSize = CGSize(width: width, height: width) layout.itemSize = CGSize(width: width, height: width)
super.init(collectionViewLayout: layout) super.init(collectionViewLayout: layout)
collectionView.contentInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
@@ -92,9 +85,6 @@ struct EmojiCollectionView: UIViewRepresentable {
} }
override func viewDidLayoutSubviews() { override func viewDidLayoutSubviews() {
layout.sectionInset.left = 16
layout.sectionInset.right = 16
layout.itemSize = CGSize(width: width, height: width) layout.itemSize = CGSize(width: width, height: width)
} }
@@ -127,7 +117,12 @@ struct EmojiCollectionView: UIViewRepresentable {
layout collectionViewLayout: UICollectionViewLayout, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath sizeForItemAt indexPath: IndexPath
) -> CGSize { ) -> CGSize {
let defaultSize = CGSize(width: width, height: width) let totalWidth = collectionView.bounds.width
let availWidth = totalWidth-(collectionView.contentInset.left*2)
let cols = max(1, floor(availWidth/width))
let totalSpacing = 1 * (cols - 1)
let itemWidth = (availWidth-totalSpacing)/cols
let defaultSize = CGSize(width: itemWidth, height: itemWidth)
guard self.style == .full else { return defaultSize } guard self.style == .full else { return defaultSize }
guard !hoarder.trie.dict.isEmpty else { return defaultSize } guard !hoarder.trie.dict.isEmpty else { return defaultSize }
@@ -137,11 +132,9 @@ struct EmojiCollectionView: UIViewRepresentable {
label.font = .preferredFont(forTextStyle: .caption1) label.font = .preferredFont(forTextStyle: .caption1)
label.numberOfLines = 0 label.numberOfLines = 0
label.text = emojiName label.text = emojiName
let labelHeight = label.sizeThatFits(CGSize(width: width, height: .infinity)).height let labelHeight = label.sizeThatFits(CGSize(width: itemWidth, height: .infinity)).height
print(width, width+labelHeight+4) return CGSize(width: itemWidth, height: itemWidth+labelHeight+4)
return CGSize(width: width, height: width+labelHeight+4)
} }
override func collectionView( override func collectionView(
@@ -34,9 +34,9 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell {
hostingController.view.translatesAutoresizingMaskIntoConstraints = false hostingController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
hostingController.view.topAnchor.constraint(equalTo: contentView.topAnchor), hostingController.view.topAnchor.constraint(equalTo: contentView.topAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
hostingController.view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), hostingController.view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), hostingController.view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
hostingController.view.heightAnchor.constraint(equalTo: hostingController.view.widthAnchor),
]) ])
} }
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.