diff --git a/StickerSlack/Emoji/GifView.swift b/StickerSlack/Emoji/GifView.swift index 6a4bbb7..f7fbbab 100644 --- a/StickerSlack/Emoji/GifView.swift +++ b/StickerSlack/Emoji/GifView.swift @@ -15,7 +15,7 @@ struct GifView: View { @State var url: URL @State var gif: [(frame: CGImage, showFor: Double)] = [] @State var currentI: Int = 0 - @State var isVisible: Bool = false + @State var animate: Bool @State var timer: Timer? @@ -25,7 +25,11 @@ struct GifView: View { if let data = try? await URLSession.shared.data(from: url).0, let uiImage = UIImage(data: data), let cgImage = uiImage.cgImage { - withAnimation(.spring(duration: 0.2)) { + if animate { + withAnimation(.spring(duration: 0.2)) { + self.gif = [(cgImage, 1)] + } + } else { self.gif = [(cgImage, 1)] } } @@ -36,7 +40,11 @@ struct GifView: View { do { if gif.isEmpty { let frames = try await GifManager.gifFrom(url: url) - withAnimation(.spring(duration: 0.2)) { + if animate { + withAnimation(.spring(duration: 0.2)) { + self.gif = frames + } + } else { self.gif = frames } } @@ -66,7 +74,7 @@ struct GifView: View { Group { if gif.isEmpty { ProgressView() - .modifier(fadeIn()) + .modifier(fadeIn(enabled: animate)) } else if url.pathExtension == "gif" { if currentI < gif.count { Image(uiImage: .init(cgImage: gif[currentI].frame)) @@ -95,9 +103,10 @@ struct GifView: View { #Preview { GifView( - url: URL(string: "https://emoji.slack-edge.com/T09V59WQY1E/clockrun/f6641714fa6748ac.gif")! + url: URL(string: "https://emoji.slack-edge.com/T09V59WQY1E/clockrun/f6641714fa6748ac.gif")!, + animate: true ) - GifView(url: Emoji.test.remoteImageURL) - GifView(url: Emoji.testLongName.localImageURL) - GifView(url: Gif.test.remoteImageURL) + GifView(url: Emoji.test.remoteImageURL, animate: true) + GifView(url: Emoji.testLongName.localImageURL, animate: true) + GifView(url: Gif.test.remoteImageURL, animate: true) } diff --git a/StickerSlack/JumboMoji.swift b/StickerSlack/JumboMoji.swift index 76feaf5..f4344f4 100644 --- a/StickerSlack/JumboMoji.swift +++ b/StickerSlack/JumboMoji.swift @@ -24,10 +24,9 @@ struct JumboMoji: CustomStringConvertible, Hashable { self.baseName = components.first!.baseName let coords = components.map { CGPoint(x: $0.x, y: $0.y) } - guard let width = Int(coords.sorted { $0.x > $1.x }.first?.x.formatted() ?? "") else { return nil } - self.width = width - guard let height = Int(coords.sorted { $0.y > $1.y }.first?.y.formatted() ?? "") else { return nil } - self.height = height + self.width = Int(coords.map { $0.x }.max() ?? -1) + self.height = Int(coords.map { $0.y }.max() ?? -1) + guard width != -1 && height != -1 else { return nil } self.type = .grid diff --git a/StickerSlack/SwiftUI/DownloadedView.swift b/StickerSlack/SwiftUI/DownloadedView.swift index 9d903b6..88d9d83 100644 --- a/StickerSlack/SwiftUI/DownloadedView.swift +++ b/StickerSlack/SwiftUI/DownloadedView.swift @@ -46,6 +46,7 @@ struct DownloadedView: View { pack: nil, width: 75, style: .plainWithMenu, + animate: false, edit: false ) // GeometryReader { geo in diff --git a/StickerSlack/SwiftUI/JumboMojiTestView.swift b/StickerSlack/SwiftUI/JumboMojiTestView.swift index b8635ae..f45808a 100644 --- a/StickerSlack/SwiftUI/JumboMojiTestView.swift +++ b/StickerSlack/SwiftUI/JumboMojiTestView.swift @@ -31,7 +31,8 @@ struct JumboMojiTestView: View { items: jumboMoji.items, pack: nil, width: CGFloat(jumboMoji.width), - style: .jumboMoji + style: .jumboMoji, + animate: false ) .frame(width: 100, height: 100) Text(jumboMoji.description) diff --git a/StickerSlack/SwiftUI/Misc/ViewModifiers.swift b/StickerSlack/SwiftUI/Misc/ViewModifiers.swift index 8ffb6ff..039f9d9 100644 --- a/StickerSlack/SwiftUI/Misc/ViewModifiers.swift +++ b/StickerSlack/SwiftUI/Misc/ViewModifiers.swift @@ -144,17 +144,26 @@ extension View { } struct fadeIn: ViewModifier { + @State private var enabled: Bool @State private var isVisible: Bool = false + init(enabled: Bool = true) { + self.enabled = enabled + } + func body(content: Content) -> some View { - content - .opacity(isVisible ? 1 : 0) - .scaleEffect(isVisible ? 1 : 0) - .onAppear { - withAnimation { isVisible = true } - } - .onDisappear { - withAnimation { isVisible = false } - } + if enabled { + content + .opacity(isVisible ? 1 : 0) + .scaleEffect(isVisible ? 1 : 0) + .onAppear { + withAnimation { isVisible = true } + } + .onDisappear { + withAnimation { isVisible = false } + } + } else { + content + } } } diff --git a/StickerSlack/SwiftUI/Shared/StickerPreview.swift b/StickerSlack/SwiftUI/Shared/StickerPreview.swift index 5246c00..9167dc6 100644 --- a/StickerSlack/SwiftUI/Shared/StickerPreview.swift +++ b/StickerSlack/SwiftUI/Shared/StickerPreview.swift @@ -11,23 +11,24 @@ import Haptics struct StickerPreview: View { @State var sticker: any StickerProtocol @State var gifImage: Image? + @State var animate: Bool = true var body: some View { if sticker.isLocal { //local - GifView(url: sticker.localImageURL) + GifView(url: sticker.localImageURL, animate: animate) } else { //remote if sticker.type == .slackEmoji { - GifView(url: sticker.remoteImageURL) + GifView(url: sticker.remoteImageURL, animate: animate) } else { let gif = sticker as! Gif if let giphyImages = gif.giphyImages, let preview_gif = giphyImages.preview_gif, let url = URL(string: preview_gif.url) { - GifView(url: url) + GifView(url: url, animate: animate) } else { - GifView(url: gif.remoteImageURL) + GifView(url: gif.remoteImageURL, animate: animate) .overlay { Image(systemName: "square.arrowtriangle.4.outward") diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift index a8f5bd8..832d033 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift @@ -16,6 +16,7 @@ struct EmojiCollectionView: UIViewRepresentable { let pack: EmojiPack? let width: CGFloat let style: EmojiCollectionView.Style + var animate: Bool = true var edit: Bool? var onRemoveSUI: ((String) -> Void)? @@ -32,6 +33,7 @@ struct EmojiCollectionView: UIViewRepresentable { context.coordinator.hoarder = hoarder context.coordinator.onRemove = onRemoveSUI + context.coordinator.animate = animate context.coordinator.edit = edit if edit ?? false { context.coordinator.startAnimating() @@ -77,6 +79,7 @@ struct EmojiCollectionView: UIViewRepresentable { var pack: EmojiPack? var width: CGFloat var style: EmojiCollectionView.Style + var animate: Bool = true var edit: Bool? = false var onRemove: ((String) -> Void)? @@ -153,6 +156,7 @@ struct EmojiCollectionView: UIViewRepresentable { let emojiName = items[indexPath.item] guard let emoji = hoarder.trie.dict[emojiName] else { return cell } + cell.animate = animate cell.configure(with: hoarder, emoji: emoji) return cell } diff --git a/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift b/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift index ed68699..a95f091 100644 --- a/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift +++ b/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift @@ -11,6 +11,7 @@ import SwiftUI class PlainEmojiCollectionViewCell: UICollectionViewCell { var hostingController: UIHostingController? + var animate: Bool = true override init(frame: CGRect) { super.init(frame: frame) @@ -25,7 +26,7 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell { func configure(with: EmojiHoarder, emoji: Emoji) { hostingController?.view.removeFromSuperview() - let swiftUIView = StickerPreview(sticker: emoji) + let swiftUIView = StickerPreview(sticker: emoji, animate: animate) let hostingController = UIHostingController(rootView: swiftUIView) self.hostingController = hostingController