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
+83
-39
No files matched your search
@@ -12,9 +12,8 @@ class GifManager {
|
|||||||
//from clock-run, 12 frames one second
|
//from clock-run, 12 frames one second
|
||||||
static let defaultDuration: Double = 0.083333333333333329
|
static let defaultDuration: Double = 0.083333333333333329
|
||||||
|
|
||||||
static func gifFrom(url: URL) async -> [(frame: CGImage, showFor: Double)] {
|
static func gifFrom(url: URL) async throws -> [(frame: CGImage, showFor: Double)] {
|
||||||
guard let (data, _) = try? await URLSession.shared.data(from: url) else { return [] }
|
let (data, _) = try await URLSession.shared.data(from: url)
|
||||||
|
|
||||||
return gifFrom(data: data)
|
return gifFrom(data: data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,25 +16,24 @@ struct GifView: View {
|
|||||||
@State var currentI: Int = 0
|
@State var currentI: Int = 0
|
||||||
|
|
||||||
@State var timer: Timer?
|
@State var timer: Timer?
|
||||||
|
@State var error: Error?
|
||||||
|
@State var failed: Bool = false
|
||||||
|
|
||||||
var body: some View {
|
func run() async {
|
||||||
// /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Hello, world!@*/Text("Hello, world!")/*@END_MENU_TOKEN@*/
|
do {
|
||||||
TimelineView(.animation) { tl in
|
|
||||||
VStack {
|
|
||||||
if currentI < gif.count {
|
|
||||||
let image = Image(uiImage: .init(cgImage: gif[currentI].frame))
|
|
||||||
image
|
|
||||||
.resizable().scaledToFit()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onDisappear {
|
|
||||||
timer?.invalidate()
|
|
||||||
timer = nil
|
|
||||||
}
|
|
||||||
.task {
|
|
||||||
if gif.isEmpty {
|
if gif.isEmpty {
|
||||||
self.gif = await GifManager.gifFrom(url: url)
|
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 }
|
guard gif.count > 0 else { return }
|
||||||
|
|
||||||
@@ -52,11 +51,46 @@ struct GifView: View {
|
|||||||
}
|
}
|
||||||
RunLoop.main.add(timer!, forMode: .common)
|
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 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 {
|
||||||
|
await run()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
GifView(
|
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
|
downloadedStickers = decoded
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var newDownloadedStickers: Set<String> = []
|
var newSet: Set<String> = []
|
||||||
if let files = try? await FileManager.default.contentsOfDirectory(atPath: BaseHoarder.library.appendingPathComponent(stickerType, conformingTo: .directory).path()) {
|
let url = await BaseHoarder.library.appendingPathComponent(stickerType, conformingTo: .directory)
|
||||||
|
if let files = try? FileManager.default.contentsOfDirectory(atPath: url.path()) {
|
||||||
for file in files {
|
for file in files {
|
||||||
let name = String(file.split(separator: ".")[0])
|
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 {
|
await MainActor.run {
|
||||||
downloadedStickers = []
|
self.downloadedStickers = []
|
||||||
downloadedStickers = newDownloadedStickers
|
self.downloadedStickers = immutable
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,8 @@ extension StickerProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var image: UIImage? {
|
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) {
|
let img = UIImage(data: data) {
|
||||||
return img
|
return img
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -12,14 +12,25 @@ struct StickerPreview: View {
|
|||||||
@State var sticker: any StickerProtocol
|
@State var sticker: any StickerProtocol
|
||||||
@State var gifImage: Image?
|
@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 {
|
var body: some View {
|
||||||
if type(of: sticker) == Emoji.self {
|
|
||||||
if sticker.image != nil {
|
if sticker.image != nil {
|
||||||
|
//local
|
||||||
GifView(url: sticker.localImageURL)
|
GifView(url: sticker.localImageURL)
|
||||||
} else {
|
} else {
|
||||||
|
//remote
|
||||||
|
if type == .slackEmoji {
|
||||||
GifView(url: sticker.remoteImageURL)
|
GifView(url: sticker.remoteImageURL)
|
||||||
}
|
} else {
|
||||||
} else if type(of: sticker) == Gif.self {
|
|
||||||
let gif = sticker as! Gif
|
let gif = sticker as! Gif
|
||||||
if let giphyImages = gif.giphyImages,
|
if let giphyImages = gif.giphyImages,
|
||||||
let preview_gif = giphyImages.preview_gif,
|
let preview_gif = giphyImages.preview_gif,
|
||||||
@@ -28,19 +39,15 @@ struct StickerPreview: View {
|
|||||||
} else {
|
} else {
|
||||||
GifView(url: gif.remoteImageURL)
|
GifView(url: gif.remoteImageURL)
|
||||||
.overlay {
|
.overlay {
|
||||||
Image(systemName: "arrow.up.left.and.down.right.and.arrow.up.right.and.down.left")
|
Image(systemName:
|
||||||
|
"square.arrowtriangle.4.outward")
|
||||||
.resizable().scaledToFit()
|
.resizable().scaledToFit()
|
||||||
.foregroundStyle(.red)
|
.foregroundStyle(.red)
|
||||||
.padding(1)
|
|
||||||
.background(.black)
|
.background(.black)
|
||||||
.aspectRatio(1, contentMode: .fit)
|
|
||||||
.padding()
|
.padding()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
Image(systemName: "xmark.app.fill")
|
|
||||||
.resizable().scaledToFit()
|
|
||||||
.foregroundStyle(.red)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ struct WelcomeView: View {
|
|||||||
.monospaced()
|
.monospaced()
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
.padding(.top, 20)
|
.padding(.vertical, 20)
|
||||||
.padding(.leading, 20)
|
.padding(.leading, 20)
|
||||||
List {
|
List {
|
||||||
Section("How to use") {
|
Section("How to use") {
|
||||||
@@ -60,6 +60,7 @@ struct WelcomeView: View {
|
|||||||
}
|
}
|
||||||
.padding(.bottom)
|
.padding(.bottom)
|
||||||
.tint(.purple)
|
.tint(.purple)
|
||||||
|
.interactiveDismissDisabled()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||