From ef804d53326263f26bf5981e4fe2dd11fb1805ab Mon Sep 17 00:00:00 2001 From: Nihaal Sharma <69979447+neon443@users.noreply.github.com> Date: Wed, 2 Dec 2026 08:43:35 +0000 Subject: [PATCH] storage calculations --- StickerSlack.xcodeproj/project.pbxproj | 16 ++--- StickerSlack/EmojiHoarder/EmojiHoarder.swift | 55 ++++++++++++----- StickerSlack/Hoarder.swift | 2 +- StickerSlack/StickerSlack.entitlements | 7 +-- .../SwiftUI/Misc/StorageCellView.swift | 61 ++++++++++++++++--- StickerSlack/SwiftUI/SettingsView.swift | 3 +- .../MessagesAppViewController.swift | 5 +- .../StickerSlackiMessageApp.entitlements | 2 +- 8 files changed, 109 insertions(+), 42 deletions(-) diff --git a/StickerSlack.xcodeproj/project.pbxproj b/StickerSlack.xcodeproj/project.pbxproj index 305e773..52bf746 100644 --- a/StickerSlack.xcodeproj/project.pbxproj +++ b/StickerSlack.xcodeproj/project.pbxproj @@ -1088,7 +1088,7 @@ CODE_SIGN_ENTITLEMENTS = StickerSlack/StickerSlack.entitlements; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = "$(BUILD)"; - DEVELOPMENT_TEAM = 4UMKR5C7LL; + DEVELOPMENT_TEAM = 8626DL2GW3; ENABLE_APP_SANDBOX = YES; ENABLE_HARDENED_RUNTIME = YES; ENABLE_ON_DEMAND_RESOURCES = YES; @@ -1114,7 +1114,7 @@ MARKETING_VERSION = "$(VERSION)"; ON_DEMAND_RESOURCES_INITIAL_INSTALL_TAGS = ""; ON_DEMAND_RESOURCES_PREFETCH_ORDER = ""; - PRODUCT_BUNDLE_IDENTIFIER = com.aarti.StickerSlack; + PRODUCT_BUNDLE_IDENTIFIER = com.example.StickerSlack; PRODUCT_NAME = "$(TARGET_NAME)"; REGISTER_APP_GROUPS = YES; SDKROOT = auto; @@ -1140,7 +1140,7 @@ CODE_SIGN_ENTITLEMENTS = StickerSlack/StickerSlack.entitlements; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = "$(BUILD)"; - DEVELOPMENT_TEAM = 4UMKR5C7LL; + DEVELOPMENT_TEAM = 8626DL2GW3; ENABLE_APP_SANDBOX = YES; ENABLE_HARDENED_RUNTIME = YES; ENABLE_ON_DEMAND_RESOURCES = YES; @@ -1166,7 +1166,7 @@ MARKETING_VERSION = "$(VERSION)"; ON_DEMAND_RESOURCES_INITIAL_INSTALL_TAGS = ""; ON_DEMAND_RESOURCES_PREFETCH_ORDER = ""; - PRODUCT_BUNDLE_IDENTIFIER = com.aarti.StickerSlack; + PRODUCT_BUNDLE_IDENTIFIER = com.example.StickerSlack; PRODUCT_NAME = "$(TARGET_NAME)"; REGISTER_APP_GROUPS = YES; SDKROOT = auto; @@ -1191,7 +1191,7 @@ CODE_SIGN_ENTITLEMENTS = StickerSlackiMessageApp/StickerSlackiMessageApp.entitlements; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = "$(BUILD)"; - DEVELOPMENT_TEAM = 4UMKR5C7LL; + DEVELOPMENT_TEAM = 8626DL2GW3; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = StickerSlackiMessageApp/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = StickerSlack; @@ -1203,7 +1203,7 @@ "@executable_path/../../Frameworks", ); MARKETING_VERSION = "$(VERSION)"; - PRODUCT_BUNDLE_IDENTIFIER = com.aarti.StickerSlack.iMessage; + PRODUCT_BUNDLE_IDENTIFIER = com.example.StickerSlack.iMessage; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SKIP_INSTALL = YES; @@ -1223,7 +1223,7 @@ CODE_SIGN_ENTITLEMENTS = StickerSlackiMessageApp/StickerSlackiMessageApp.entitlements; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = "$(BUILD)"; - DEVELOPMENT_TEAM = 4UMKR5C7LL; + DEVELOPMENT_TEAM = 8626DL2GW3; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = StickerSlackiMessageApp/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = StickerSlack; @@ -1235,7 +1235,7 @@ "@executable_path/../../Frameworks", ); MARKETING_VERSION = "$(VERSION)"; - PRODUCT_BUNDLE_IDENTIFIER = com.aarti.StickerSlack.iMessage; + PRODUCT_BUNDLE_IDENTIFIER = com.example.StickerSlack.iMessage; PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SKIP_INSTALL = YES; diff --git a/StickerSlack/EmojiHoarder/EmojiHoarder.swift b/StickerSlack/EmojiHoarder/EmojiHoarder.swift index 1fe1caf..6849c38 100644 --- a/StickerSlack/EmojiHoarder/EmojiHoarder.swift +++ b/StickerSlack/EmojiHoarder/EmojiHoarder.swift @@ -29,9 +29,12 @@ class EmojiHoarder: BaseHoarder { @Published var showWelcome: Bool = false + @Published var usedStorage: Double = 0 + init(localOnly: Bool = false, skipIndex: Bool = false) { super.init() self.showWelcome = !UserDefaults.standard.bool(forKey: "showWelcome") + self.usedStorage = UserDefaults.standard.double(forKey: "usedStorage") if !FileManager.default.fileExists(atPath: EmojiHoarder.container.safePath) { try! FileManager.default.createDirectory(at: EmojiHoarder.container, withIntermediateDirectories: true) @@ -77,6 +80,7 @@ class EmojiHoarder: BaseHoarder { downloadedStickers = [] downloadedStickersArr = [] sendChangeNotif(for: .downloadedEmojis) + usedStorage = 0 } } @@ -94,6 +98,8 @@ class EmojiHoarder: BaseHoarder { downloadedStickers = [] sendChangeNotif(for: .downloadedEmojis) letterStats = [] + usedStorage = 0 + UserDefaults.standard.set(0.0, forKey: "usedStorage") } nonisolated private func saveTrieDict() async { @@ -117,7 +123,6 @@ class EmojiHoarder: BaseHoarder { var wordlist = trie.wordlist var dict = trie.dict for emoji in emojis { - // trie.insert(word: emoji.name) wordlist.insert(emoji.name) dict[emoji.name] = emoji } @@ -125,6 +130,12 @@ class EmojiHoarder: BaseHoarder { trie.dict = dict await buildDownloadedStickers() + + self.usedStorage = UserDefaults.standard.double(forKey: "usedStorage") + if usedStorage == 0 { + await calculateStorageUsage() + } + UserDefaults.standard.set(usedStorage, forKey: "usedStorage") print("done building trie in", Date().timeIntervalSince1970-start) } @@ -136,24 +147,25 @@ class EmojiHoarder: BaseHoarder { sendChangeNotif(for: .downloadedEmojis) } - func buildDownloadedStickersSync() { - var newSet: Set = [] + nonisolated func calculateStorageUsage() async { + var storageUsage: Double = 0 - if let files = try? FileManager.default.contentsOfDirectory(atPath: EmojiHoarder.container.safePath) { + let url = BaseHoarder.library.appendingPathComponent("Emojis", conformingTo: .directory) + + if let files = try? FileManager.default.contentsOfDirectory(atPath: url.safePath) { for file in files { - let name = String(file.split(separator: ".")[0]) - newSet.insert(name) + + guard let attr = try? FileManager.default.attributesOfItem(atPath: url.appendingPathComponent(file, conformingTo: .image).safePath) else { continue } + storageUsage += Double(attr[FileAttributeKey.size] as! UInt64) } } - newSet.remove("DS_Store") - let immutable = newSet - self.downloadedStickers = [] - self.downloadedStickers = immutable - downloadedStickersArr = Array(downloadedStickers).sorted() - sendChangeNotif(for: .downloadedEmojis) - return + print(storageUsage, "skgbp") + UserDefaults.standard.set(storageUsage, forKey: "usedStorage") + await Task { @MainActor in + self.usedStorage = storageUsage + } } - + private func loadLocalDB() -> [Emoji] { do { let localEmojiDB = try Data(contentsOf: EmojiHoarder.localEmojiDB) @@ -227,6 +239,7 @@ class EmojiHoarder: BaseHoarder { } } await buildDownloadedStickers() + await calculateStorageUsage() } override nonisolated func download(emoji: (any StickerProtocol)?, skipStoreIndex: Bool = false) async { @@ -236,6 +249,11 @@ class EmojiHoarder: BaseHoarder { await MainActor.run { if !skipStoreIndex { + if let size = try? FileManager.default.attributesOfItem(atPath: emoji.localImageURL.safePath)[FileAttributeKey.size] as! UInt64 { + self.usedStorage += Double(size) + UserDefaults.standard.set(self.usedStorage, forKey: "usedStorage") + } + let _ = withAnimation(.snappy) { self.downloadedStickers.insert(emoji.name) self.downloadedStickersArr.append(emoji.name) @@ -254,10 +272,19 @@ class EmojiHoarder: BaseHoarder { } } await buildDownloadedStickers() + await calculateStorageUsage() } nonisolated override func delete(emoji: (any StickerProtocol)?, skipStoreIndex: Bool = false) async { guard let emoji else { return } + + await MainActor.run { + if let size = try? FileManager.default.attributesOfItem(atPath: emoji.localImageURL.safePath)[FileAttributeKey.size] as? UInt64 { + self.usedStorage -= Double(size) + UserDefaults.standard.set(self.usedStorage, forKey: "usedStorage") + } + } + await super.delete(emoji: emoji, skipStoreIndex: skipStoreIndex) if !skipStoreIndex { diff --git a/StickerSlack/Hoarder.swift b/StickerSlack/Hoarder.swift index 688c2b6..10863ac 100644 --- a/StickerSlack/Hoarder.swift +++ b/StickerSlack/Hoarder.swift @@ -22,7 +22,7 @@ protocol Hoarder: ObservableObject { } class BaseHoarder: Hoarder { - static let library: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.aarti.StickerSlack")!.appendingPathComponent("Library", conformingTo: .directory) + static let library: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group..8626DL2GW3")!.appendingPathComponent("Library", conformingTo: .directory) @Published var downloadedStickers: Set = [] diff --git a/StickerSlack/StickerSlack.entitlements b/StickerSlack/StickerSlack.entitlements index 74d5cc5..4f91310 100644 --- a/StickerSlack/StickerSlack.entitlements +++ b/StickerSlack/StickerSlack.entitlements @@ -2,14 +2,9 @@ - com.apple.developer.associated-domains - - applinks:stickerslack.neon443.xyz - applinks:nihaals.me - com.apple.security.application-groups - group.com.aarti.StickerSlack + group..8626DL2GW3 diff --git a/StickerSlack/SwiftUI/Misc/StorageCellView.swift b/StickerSlack/SwiftUI/Misc/StorageCellView.swift index 54ea63f..31b7d13 100644 --- a/StickerSlack/SwiftUI/Misc/StorageCellView.swift +++ b/StickerSlack/SwiftUI/Misc/StorageCellView.swift @@ -9,29 +9,72 @@ import Foundation import SwiftUI struct StorageCellView: View { - @State var totalStorage: Int = 0 - @State var usedStorage: CGFloat = 0 - @State var stickerSlackStorage: CGFloat = 0 + @ObservedObject var hoarder: EmojiHoarder + + @State var totalStorage: Measurement = .init(value: 1, unit: .exabytes) + @State var usedStorage: Measurement = .init(value: 1, unit: .exabytes) + @State var stickerSlackStorage: Measurement = .init(value: 1, unit: .exabytes) var body: some View { VStack { HStack { - Text("") + Text("Storage") Spacer() - Text("\(totalStorage)") + Text("\(Int(stickerSlackStorage.value.rounded(.up))) \(stickerSlackStorage.unit.symbol)") + .foregroundStyle(.gray) .onAppear { - var totalStorage: AnyObject? - try? (EmojiHoarder.localTrieDict as NSURL).getResourceValue(&totalStorage, forKey: URLResourceKey.volumeTotalCapacityKey) + stickerSlackStorage = Measurement(value: hoarder.usedStorage, unit: UnitInformationStorage.bytes).converted(to: .megabytes) } + Text("of") + Text("\(Int(totalStorage.value.rounded())) \(totalStorage.unit.symbol)") + .foregroundStyle(.gray) + .onAppear { + var storage: AnyObject? + try? (EmojiHoarder.localTrieDict as NSURL).getResourceValue(&storage, forKey: URLResourceKey.volumeTotalCapacityKey) + let measurement = Measurement(value: storage as? Double ?? 0, unit: UnitInformationStorage.bytes) + totalStorage = measurement.converted(to: UnitInformationStorage.gigabytes) + print(totalStorage) + + var availStorage: AnyObject? + try? (EmojiHoarder.localTrieDict as NSURL).getResourceValue(&availStorage, forKey: URLResourceKey.volumeAvailableCapacityKey) + let unit = Measurement(value: availStorage as? Double ?? 0, unit: UnitInformationStorage.bytes) + usedStorage = unit.converted(to: UnitInformationStorage.gigabytes) + print(totalStorage - usedStorage) + } + Text("used") } - HStack(spacing: 0) { + .padding(.top, 8) + HStack(spacing: 1) { Rectangle() + .foregroundStyle(.red) + Rectangle() + .foregroundStyle(.gray) + Rectangle() + .foregroundStyle(.black) } + .background(.black) .clipShape(RoundedRectangle(cornerRadius: 5)) + .frame(maxHeight: 30) + HStack { + HStack { + Circle() + .foregroundStyle(.red) + .frame(maxHeight: 20) + Text("StickerSlack") + } + HStack { + Circle() + .foregroundStyle(.gray) + .frame(maxHeight: 16) + Text("Other Apps") + } + Spacer() + } + .padding(.vertical, 8) } } } #Preview { - StorageCellView() + StorageCellView(hoarder: EmojiHoarder()) } diff --git a/StickerSlack/SwiftUI/SettingsView.swift b/StickerSlack/SwiftUI/SettingsView.swift index b78956d..7d1b83c 100644 --- a/StickerSlack/SwiftUI/SettingsView.swift +++ b/StickerSlack/SwiftUI/SettingsView.swift @@ -44,6 +44,7 @@ struct SettingsView: View { } Section("Stats") { + StorageCellView(hoarder: hoarder) Text("\(hoarder.emojis.count) total Emoji") Text("\(hoarder.downloadedStickers.count) downloaded Emoji") if hoarder.downloadedStickers.count == hoarder.emojis.count { @@ -54,8 +55,6 @@ struct SettingsView: View { } } - - // NavigationLink { // List { // Picker(selection: $hoarder.letterStatsSorting.by) { diff --git a/StickerSlackiMessageApp/MessagesAppViewController.swift b/StickerSlackiMessageApp/MessagesAppViewController.swift index d91dc84..b419d63 100644 --- a/StickerSlackiMessageApp/MessagesAppViewController.swift +++ b/StickerSlackiMessageApp/MessagesAppViewController.swift @@ -16,7 +16,10 @@ class MessagesAppViewController: MSMessagesAppViewController { override func viewDidLoad() { super.viewDidLoad() - emojiHoarder.buildDownloadedStickersSync() + Task { + await emojiHoarder.buildDownloadedStickers() + } +// emojiHoarder.buildDownloadedStickersSync() self.tableView = MessagesPackListView(hoarder: emojiHoarder) let selRow = UserDefaults.standard.integer(forKey: "selectedPackRowIndex") diff --git a/StickerSlackiMessageApp/StickerSlackiMessageApp.entitlements b/StickerSlackiMessageApp/StickerSlackiMessageApp.entitlements index d8692aa..4f91310 100644 --- a/StickerSlackiMessageApp/StickerSlackiMessageApp.entitlements +++ b/StickerSlackiMessageApp/StickerSlackiMessageApp.entitlements @@ -4,7 +4,7 @@ com.apple.security.application-groups - group.com.aarti.StickerSlack + group..8626DL2GW3