mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 12:44: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
|
//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,47 +16,81 @@ 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
|
||||||
|
|
||||||
|
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 {
|
var body: some View {
|
||||||
// /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Hello, world!@*/Text("Hello, world!")/*@END_MENU_TOKEN@*/
|
// /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Hello, world!@*/Text("Hello, world!")/*@END_MENU_TOKEN@*/
|
||||||
TimelineView(.animation) { tl in
|
TimelineView(.animation) { tl in
|
||||||
VStack {
|
VStack {
|
||||||
if currentI < gif.count {
|
if gif.isEmpty && error == nil {
|
||||||
let image = Image(uiImage: .init(cgImage: gif[currentI].frame))
|
ProgressView()
|
||||||
image
|
.controlSize(.large)
|
||||||
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||||
|
} else if failed {
|
||||||
|
Image(systemName: "xmark")
|
||||||
.resizable().scaledToFit()
|
.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 {
|
.onDisappear {
|
||||||
timer?.invalidate()
|
timer?.invalidate()
|
||||||
timer = nil
|
timer = nil
|
||||||
}
|
}
|
||||||
.task {
|
.task {
|
||||||
if gif.isEmpty {
|
await run()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#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 {
|
||||||
|
|||||||
@@ -11,36 +11,43 @@ import Haptics
|
|||||||
struct StickerPreview: View {
|
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 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 {
|
} else {
|
||||||
GifView(url: gif.remoteImageURL)
|
let gif = sticker as! Gif
|
||||||
.overlay {
|
if let giphyImages = gif.giphyImages,
|
||||||
Image(systemName: "arrow.up.left.and.down.right.and.arrow.up.right.and.down.left")
|
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()
|
.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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||