refactored to storeDB loadLocalDB and fetchRemoteDB for more readability

added more stuff to slackresponse cos cachets got some cool stuff
guards on toEmojis and it can take in and return nil
This commit is contained in:
neon443 committed 2025-10-27 20:30:26 +00:00
1 parent 2ddcd49ad0
commit f4ab22a789
6 files changed
+44 -27

No files matched your search

+9 -9
View File
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
A9112EAC2EAFFDB0006739E2 /* Haptics in Frameworks */ = {isa = PBXBuildFile; productRef = A9112EAB2EAFFDB0006739E2 /* Haptics */; };
A924C3722EA9127200F20781 /* Emoji.swift in Sources */ = {isa = PBXBuildFile; fileRef = A924C3712EA9127200F20781 /* Emoji.swift */; };
A924C3732EA9127200F20781 /* Emoji.swift in Sources */ = {isa = PBXBuildFile; fileRef = A924C3712EA9127200F20781 /* Emoji.swift */; };
A924C3782EA9225800F20781 /* Haptics in Frameworks */ = {isa = PBXBuildFile; productRef = A924C3772EA9225800F20781 /* Haptics */; };
@@ -23,7 +24,6 @@
A969D6AE2EA3F1AF00399C05 /* MainInterface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A969D6A92EA3F1AF00399C05 /* MainInterface.storyboard */; };
A969D6AF2EA3F1AF00399C05 /* MessagesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A969D6AA2EA3F1AF00399C05 /* MessagesViewController.swift */; };
A9773C2F2EA54AF000F3B753 /* EmojiPreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9773C2E2EA54AF000F3B753 /* EmojiPreview.swift */; };
A986A6682EAAB41000B6E0FA /* Haptics in Frameworks */ = {isa = PBXBuildFile; productRef = A986A6672EAAB41000B6E0FA /* Haptics */; };
A9BE06DB2EA656B80033B213 /* EmojiHoarder.swift in Sources */ = {isa = PBXBuildFile; fileRef = A949B1F72EA04F2300215164 /* EmojiHoarder.swift */; };
A9BE06DC2EA657C70033B213 /* ApiEmoji.swift in Sources */ = {isa = PBXBuildFile; fileRef = A940FE3C2EA232590016870B /* ApiEmoji.swift */; };
A9BE06DD2EA657CF0033B213 /* SlackResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = A949B1FA2EA0518800215164 /* SlackResponse.swift */; };
@@ -80,7 +80,7 @@
buildActionMask = 2147483647;
files = (
A924C3782EA9225800F20781 /* Haptics in Frameworks */,
A986A6682EAAB41000B6E0FA /* Haptics in Frameworks */,
A9112EAC2EAFFDB0006739E2 /* Haptics in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -195,7 +195,7 @@
name = StickerSlack;
packageProductDependencies = (
A924C3772EA9225800F20781 /* Haptics */,
A986A6672EAAB41000B6E0FA /* Haptics */,
A9112EAB2EAFFDB0006739E2 /* Haptics */,
);
productName = StickerSlack;
productReference = A949B1DF2EA04C0B00215164 /* StickerSlack.app */;
@@ -248,7 +248,7 @@
mainGroup = A949B1D62EA04C0B00215164;
minimizedProjectReferenceProxies = 1;
packageReferences = (
A986A6662EAAB41000B6E0FA /* XCRemoteSwiftPackageReference "Haptics" */,
A9112EAA2EAFFDB0006739E2 /* XCRemoteSwiftPackageReference "Haptics" */,
);
preferredProjectObjectVersion = 77;
productRefGroup = A949B1E02EA04C0B00215164 /* Products */;
@@ -635,9 +635,9 @@
/* End XCConfigurationList section */
/* Begin XCRemoteSwiftPackageReference section */
A986A6662EAAB41000B6E0FA /* XCRemoteSwiftPackageReference "Haptics" */ = {
A9112EAA2EAFFDB0006739E2 /* XCRemoteSwiftPackageReference "Haptics" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "http://github.com/neon443/Haptics";
repositoryURL = "https://github.com/neon443/Haptics";
requirement = {
branch = main;
kind = branch;
@@ -646,13 +646,13 @@
/* End XCRemoteSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
A924C3772EA9225800F20781 /* Haptics */ = {
A9112EAB2EAFFDB0006739E2 /* Haptics */ = {
isa = XCSwiftPackageProductDependency;
package = A9112EAA2EAFFDB0006739E2 /* XCRemoteSwiftPackageReference "Haptics" */;
productName = Haptics;
};
A986A6672EAAB41000B6E0FA /* Haptics */ = {
A924C3772EA9225800F20781 /* Haptics */ = {
isa = XCSwiftPackageProductDependency;
package = A986A6662EAAB41000B6E0FA /* XCRemoteSwiftPackageReference "Haptics" */;
productName = Haptics;
};
/* End XCSwiftPackageProductDependency section */
@@ -1,10 +1,10 @@
{
"originHash" : "62caf5d3b5c5f962753f0e93862d5d90e6fcd2157c5fef072880f7c4ca348c7c",
"originHash" : "1d3269ec6ef7a3d9a10b9d3cc53bfd207a07ae67b8f251b18da218e827fd0bff",
"pins" : [
{
"identity" : "haptics",
"kind" : "remoteSourceControl",
"location" : "http://github.com/neon443/Haptics",
"location" : "https://github.com/neon443/Haptics",
"state" : {
"branch" : "main",
"revision" : "90e080694752c1af657d2f7ac2dce22eaf439391"
@@ -74,7 +74,6 @@
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES"
askForAppToLaunch = "Yes"
launchAutomaticallySubstyle = "2">
<RemoteRunnable
runnableDebuggingMode = "1"
-1
View File
@@ -8,7 +8,6 @@
import Foundation
import UIKit
import UniformTypeIdentifiers
import Haptics
struct Emoji: Codable, Identifiable, Hashable {
var id: UUID
+7 -4
View File
@@ -7,13 +7,16 @@
import Foundation
struct SlackResponse: Codable {
struct SlackResponse: Identifiable, Codable {
var type: String
var id: UUID
var name: String
var imageUrl: String
var alias: String?
var expiration: Date
static func toEmojis(from: [SlackResponse]) -> [Emoji] {
return from.map { ApiEmoji(name: $0.name, url: $0.imageUrl).toEmoji() }
static func toEmojis(from response: [SlackResponse]?) -> [Emoji]? {
guard let response else { return nil }
return response.map { ApiEmoji(name: $0.name, url: $0.imageUrl).toEmoji() }
}
}
+26 -10
View File
@@ -15,22 +15,38 @@ class EmojiHoarder: ObservableObject {
static let localEmojiDB: URL = EmojiHoarder.container.appendingPathExtension("localEmojiDB.json")
private let endpoint: URL = URL(string: "https://cachet.dunkirk.sh/emojis")!
@Published var emojis: [Emoji]
@Published var emojis: [Emoji] = []
init() {
if let localEmojiDB = try? Data(contentsOf: EmojiHoarder.localEmojiDB) {
let decoded = try! JSONDecoder().decode([Emoji].self, from: localEmojiDB)
self.emojis = decoded
guard let fetched = fetchRemoteDB() else {
self.emojis = loadLocalDB()
return
}
guard let data = try? Data(contentsOf: endpoint) else { fatalError("cachet unreachable") }
let decoded: [SlackResponse] = try! JSONDecoder().decode([SlackResponse].self, from: data)
self.emojis = SlackResponse.toEmojis(from: decoded)
self.emojis = fetched
}
func storeStickers(_ toStore: [UUID]) {
for stickerId in toStore {
print(stickerId)
}
}
func storeDB() {
try! JSONEncoder().encode(emojis).write(to: EmojiHoarder.localEmojiDB)
}
// func storeStickers
func loadLocalDB() -> [Emoji] {
if let localEmojiDB = try? Data(contentsOf: EmojiHoarder.localEmojiDB) {
let decoded = try! JSONDecoder().decode([Emoji].self, from: localEmojiDB)
return decoded
}
return []
}
func fetchRemoteDB() -> [Emoji]? {
guard let data = try? Data(contentsOf: endpoint) else { fatalError("cachet unreachable") }
let decoded: [SlackResponse] = try! JSONDecoder().decode([SlackResponse].self, from: data)
return SlackResponse.toEmojis(from: decoded)
}
}
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.