redid share link parsing

This commit is contained in:
neon443 committed 2026-04-10 16:18:37 +01:00
1 parent 38924dd246
commit c8648acbfd
3 files changed
+35 -25

No files matched your search

@@ -14,12 +14,6 @@ struct EmojiPack: Identifiable, Codable {
var name: String
var description: String
var items: [String]
// var items: [EmojiPack.Item]
// struct Item: Identifiable, Codable, Equatable {
// var id: UUID
// var name: String
// }
init(id: UUID, name: String, description: String, items: [String]) {
self.id = id
@@ -29,22 +23,36 @@ struct EmojiPack: Identifiable, Codable {
}
init(fromShareLink url: URL) {
guard var querySplit = url.query()?.split(separator: "&") else { fatalError() }
var queries: [String] = []
for split in querySplit {
if split.contains("%") {
queries.append(split.removingPercentEncoding!)
} else {
queries.append(String(split))
}
}
for query in queries {
print(query)
}
guard url.scheme == "stickerslack" && url.host() == "shared.pack" else { fatalError("invalid share url") }
guard let components = URLComponents(string: url.absoluteString),
let queryItems = components.queryItems else { fatalError("no query items or components") }
self.id = UUID()
self.name = ""
self.description = ""
self.items = []
for item in queryItems {
guard let value = item.value else { fatalError("nil value for param \(item.name)") }
switch item.name {
case "id":
if let uuid = UUID(uuidString: value) {
self.id = uuid
}
case "name":
self.name = value
case "description":
self.description = value
case "items":
if let data = Data(base64Encoded: value),
let decoded = try? JSONDecoder().decode([String].self, from: data) {
self.items = decoded
}
default:
fatalError("unrecognised parameter in share link")
}
}
}
mutating func add(_ newItem: String) {
@@ -74,8 +82,6 @@ struct EmojiPack: Identifiable, Codable {
.init(name: "items", value: data.base64EncodedString())
]
url = url.appending(queryItems: queries)
print(url.path(percentEncoded: true))
let _ = Self.init(fromShareLink: url)
return url
}
}
+8 -5
View File
@@ -9,9 +9,12 @@ import SwiftUI
@main
struct StickerSlackApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
print(EmojiPack(fromShareLink: url))
}
}
}
}
@@ -147,6 +147,7 @@ struct EmojiPackDetailView: View {
Text("\(pack.items.count) Emoji\(pack.items.count.plural)")
.bold()
.multilineTextAlignment(.center)
.padding(.bottom)
}
.navigationTitle(edit ? "Editing" : "Pack Details")
.navigationBarTitleDisplayMode(.inline)
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.