mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 20:54:51 +00:00
added loading and error state for gifview (stickerpreview) with animations
redid stickerpreview logic cos it was messy made gifFrom throwing faster sticker.image when it doesnt exist fix swift6 warning in hoarder downloaded stickers added better padding to welcome view and disabled interactive dismiss
This commit is contained in:
6 files changed
+99
-55
No files matched your search
@@ -12,9 +12,8 @@ class GifManager {
|
||||
//from clock-run, 12 frames one second
|
||||
static let defaultDuration: Double = 0.083333333333333329
|
||||
|
||||
static func gifFrom(url: URL) async -> [(frame: CGImage, showFor: Double)] {
|
||||
guard let (data, _) = try? await URLSession.shared.data(from: url) else { return [] }
|
||||
|
||||
static func gifFrom(url: URL) async throws -> [(frame: CGImage, showFor: Double)] {
|
||||
let (data, _) = try await URLSession.shared.data(from: url)
|
||||
return gifFrom(data: data)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,47 +16,81 @@ struct GifView: View {
|
||||
@State var currentI: Int = 0
|
||||
|
||||
@State var timer: Timer?
|
||||
@State var error: Error?
|
||||
@State var failed: Bool = false
|
||||
|
||||
func run() async {
|
||||
do {
|
||||
if gif.isEmpty {
|
||||
let frames = try await GifManager.gifFrom(url: url)
|
||||
withAnimation(.spring) {
|
||||
self.gif = frames
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
self.error = error
|
||||
}
|
||||
guard self.error == nil && !self.gif.isEmpty else {
|
||||
print("falied loading or empty gif")
|
||||
withAnimation(.spring) { self.failed = true }
|
||||
return
|
||||
}
|
||||
guard gif.count > 0 else { return }
|
||||
|
||||
if timer != nil {
|
||||
timer!.invalidate()
|
||||
timer = nil
|
||||
}
|
||||
|
||||
timer = Timer(timeInterval: gif[0].showFor, repeats: true) { timer in
|
||||
if currentI == (gif.count-1) {
|
||||
currentI = 0
|
||||
} else {
|
||||
currentI += 1
|
||||
}
|
||||
}
|
||||
RunLoop.main.add(timer!, forMode: .common)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
// /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Hello, world!@*/Text("Hello, world!")/*@END_MENU_TOKEN@*/
|
||||
TimelineView(.animation) { tl in
|
||||
VStack {
|
||||
if currentI < gif.count {
|
||||
let image = Image(uiImage: .init(cgImage: gif[currentI].frame))
|
||||
image
|
||||
if gif.isEmpty && error == nil {
|
||||
ProgressView()
|
||||
.controlSize(.large)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
} else if failed {
|
||||
Image(systemName: "xmark")
|
||||
.resizable().scaledToFit()
|
||||
.padding()
|
||||
.padding()
|
||||
.background(.red)
|
||||
} else {
|
||||
if currentI < gif.count {
|
||||
let image = Image(uiImage: .init(cgImage: gif[currentI].frame))
|
||||
image
|
||||
.resizable().scaledToFit()
|
||||
}
|
||||
}
|
||||
}
|
||||
.transition(.scale)
|
||||
}
|
||||
.onDisappear {
|
||||
timer?.invalidate()
|
||||
timer = nil
|
||||
}
|
||||
.task {
|
||||
if gif.isEmpty {
|
||||
self.gif = await GifManager.gifFrom(url: url)
|
||||
}
|
||||
guard gif.count > 0 else { return }
|
||||
|
||||
if timer != nil {
|
||||
timer!.invalidate()
|
||||
timer = nil
|
||||
}
|
||||
|
||||
timer = Timer(timeInterval: gif[0].showFor, repeats: true) { timer in
|
||||
if currentI == (gif.count-1) {
|
||||
currentI = 0
|
||||
} else {
|
||||
currentI += 1
|
||||
}
|
||||
}
|
||||
RunLoop.main.add(timer!, forMode: .common)
|
||||
await run()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
GifView(
|
||||
url: URL(string: "https://emoji.slack-edge.com/T0266FRGM/clockrun/ec33c513d30a6ed8.gif")!
|
||||
url: URL(string: "https://emoji.slack-edge.com/T09V59WQY1E/clockrun/f6641714fa6748ac.gif")!
|
||||
)
|
||||
GifView(url: Emoji.test.remoteImageURL)
|
||||
GifView(url: Emoji.testLongName.localImageURL)
|
||||
GifView(url: Gif.test.remoteImageURL)
|
||||
}
|
||||
@@ -48,17 +48,19 @@ class BaseHoarder: Hoarder {
|
||||
downloadedStickers = decoded
|
||||
}
|
||||
} else {
|
||||
var newDownloadedStickers: Set<String> = []
|
||||
if let files = try? await FileManager.default.contentsOfDirectory(atPath: BaseHoarder.library.appendingPathComponent(stickerType, conformingTo: .directory).path()) {
|
||||
var newSet: Set<String> = []
|
||||
let url = await BaseHoarder.library.appendingPathComponent(stickerType, conformingTo: .directory)
|
||||
if let files = try? FileManager.default.contentsOfDirectory(atPath: url.path()) {
|
||||
for file in files {
|
||||
let name = String(file.split(separator: ".")[0])
|
||||
newDownloadedStickers.insert(name)
|
||||
newSet.insert(name)
|
||||
}
|
||||
}
|
||||
newDownloadedStickers.remove("DS_Store")
|
||||
newSet.remove("DS_Store")
|
||||
let immutable = newSet
|
||||
await MainActor.run {
|
||||
downloadedStickers = []
|
||||
downloadedStickers = newDownloadedStickers
|
||||
self.downloadedStickers = []
|
||||
self.downloadedStickers = immutable
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -21,7 +21,8 @@ extension StickerProtocol {
|
||||
}
|
||||
|
||||
var image: UIImage? {
|
||||
if let data = try? Data(contentsOf: localImageURL),
|
||||
if FileManager.default.fileExists(atPath: localImageURLString),
|
||||
let data = try? Data(contentsOf: localImageURL),
|
||||
let img = UIImage(data: data) {
|
||||
return img
|
||||
} else {
|
||||
|
||||
@@ -11,36 +11,43 @@ import Haptics
|
||||
struct StickerPreview: View {
|
||||
@State var sticker: any StickerProtocol
|
||||
@State var gifImage: Image?
|
||||
|
||||
|
||||
var type: StickerType {
|
||||
let type = Swift.type(of: sticker)
|
||||
if type == Emoji.self {
|
||||
return .slackEmoji
|
||||
} else if type == Gif.self {
|
||||
return .giphyGifs
|
||||
} else {
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
var body: some View {
|
||||
if type(of: sticker) == Emoji.self {
|
||||
if sticker.image != nil {
|
||||
GifView(url: sticker.localImageURL)
|
||||
} else {
|
||||
if sticker.image != nil {
|
||||
//local
|
||||
GifView(url: sticker.localImageURL)
|
||||
} else {
|
||||
//remote
|
||||
if type == .slackEmoji {
|
||||
GifView(url: sticker.remoteImageURL)
|
||||
}
|
||||
} else if type(of: sticker) == Gif.self {
|
||||
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)
|
||||
} else {
|
||||
GifView(url: gif.remoteImageURL)
|
||||
.overlay {
|
||||
Image(systemName: "arrow.up.left.and.down.right.and.arrow.up.right.and.down.left")
|
||||
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)
|
||||
} else {
|
||||
GifView(url: gif.remoteImageURL)
|
||||
.overlay {
|
||||
Image(systemName:
|
||||
"square.arrowtriangle.4.outward")
|
||||
.resizable().scaledToFit()
|
||||
.foregroundStyle(.red)
|
||||
.padding(1)
|
||||
.background(.black)
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Image(systemName: "xmark.app.fill")
|
||||
.resizable().scaledToFit()
|
||||
.foregroundStyle(.red)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ struct WelcomeView: View {
|
||||
.monospaced()
|
||||
Spacer()
|
||||
}
|
||||
.padding(.top, 20)
|
||||
.padding(.vertical, 20)
|
||||
.padding(.leading, 20)
|
||||
List {
|
||||
Section("How to use") {
|
||||
@@ -60,6 +60,7 @@ struct WelcomeView: View {
|
||||
}
|
||||
.padding(.bottom)
|
||||
.tint(.purple)
|
||||
.interactiveDismissDisabled()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||