storage calculations

This commit is contained in:
Nihaal Sharma committed 2026-12-02 08:43:35 +00:00
1 parent 9fc03e4bd2
commit ef804d5332
8 files changed
+108 -41

No files matched your search

+8 -8
View File
@@ -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;
+40 -13
View File
@@ -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,22 +147,23 @@ class EmojiHoarder: BaseHoarder {
sendChangeNotif(for: .downloadedEmojis)
}
func buildDownloadedStickersSync() {
var newSet: Set<String> = []
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] {
@@ -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 {
+1 -1
View File
@@ -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<String> = []
+1 -6
View File
@@ -2,14 +2,9 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.associated-domains</key>
<array>
<string>applinks:stickerslack.neon443.xyz</string>
<string>applinks:nihaals.me</string>
</array>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.aarti.StickerSlack</string>
<string>group..8626DL2GW3</string>
</array>
</dict>
</plist>
@@ -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<UnitInformationStorage> = .init(value: 1, unit: .exabytes)
@State var usedStorage: Measurement<UnitInformationStorage> = .init(value: 1, unit: .exabytes)
@State var stickerSlackStorage: Measurement<UnitInformationStorage> = .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())
}
+1 -2
View File
@@ -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) {
@@ -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")
@@ -4,7 +4,7 @@
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.aarti.StickerSlack</string>
<string>group..8626DL2GW3</string>
</array>
</dict>
</plist>
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.