mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 20:54:51 +00:00
redid share link parsing
This commit is contained in:
3 files changed
+30
-20
No files matched your search
@@ -14,12 +14,6 @@ struct EmojiPack: Identifiable, Codable {
|
|||||||
var name: String
|
var name: String
|
||||||
var description: String
|
var description: String
|
||||||
var items: [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]) {
|
init(id: UUID, name: String, description: String, items: [String]) {
|
||||||
self.id = id
|
self.id = id
|
||||||
@@ -29,22 +23,36 @@ struct EmojiPack: Identifiable, Codable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init(fromShareLink url: URL) {
|
init(fromShareLink url: URL) {
|
||||||
guard var querySplit = url.query()?.split(separator: "&") else { fatalError() }
|
guard url.scheme == "stickerslack" && url.host() == "shared.pack" else { fatalError("invalid share url") }
|
||||||
var queries: [String] = []
|
guard let components = URLComponents(string: url.absoluteString),
|
||||||
for split in querySplit {
|
let queryItems = components.queryItems else { fatalError("no query items or components") }
|
||||||
if split.contains("%") {
|
|
||||||
queries.append(split.removingPercentEncoding!)
|
|
||||||
} else {
|
|
||||||
queries.append(String(split))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for query in queries {
|
|
||||||
print(query)
|
|
||||||
}
|
|
||||||
self.id = UUID()
|
self.id = UUID()
|
||||||
self.name = ""
|
self.name = ""
|
||||||
self.description = ""
|
self.description = ""
|
||||||
self.items = []
|
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) {
|
mutating func add(_ newItem: String) {
|
||||||
@@ -74,8 +82,6 @@ struct EmojiPack: Identifiable, Codable {
|
|||||||
.init(name: "items", value: data.base64EncodedString())
|
.init(name: "items", value: data.base64EncodedString())
|
||||||
]
|
]
|
||||||
url = url.appending(queryItems: queries)
|
url = url.appending(queryItems: queries)
|
||||||
print(url.path(percentEncoded: true))
|
|
||||||
let _ = Self.init(fromShareLink: url)
|
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ struct StickerSlackApp: App {
|
|||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
ContentView()
|
ContentView()
|
||||||
|
.onOpenURL { url in
|
||||||
|
print(EmojiPack(fromShareLink: url))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,6 +147,7 @@ struct EmojiPackDetailView: View {
|
|||||||
Text("\(pack.items.count) Emoji\(pack.items.count.plural)")
|
Text("\(pack.items.count) Emoji\(pack.items.count.plural)")
|
||||||
.bold()
|
.bold()
|
||||||
.multilineTextAlignment(.center)
|
.multilineTextAlignment(.center)
|
||||||
|
.padding(.bottom)
|
||||||
}
|
}
|
||||||
.navigationTitle(edit ? "Editing" : "Pack Details")
|
.navigationTitle(edit ? "Editing" : "Pack Details")
|
||||||
.navigationBarTitleDisplayMode(.inline)
|
.navigationBarTitleDisplayMode(.inline)
|
||||||
|
|||||||