Communication notifications

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2025-03-01 14:23:53 -08:00
parent 6ef4b60d14
commit af4949e26a
11 changed files with 356 additions and 19 deletions

View File

@@ -5,6 +5,7 @@
// Created by William Casarin on 2022-04-01.
//
import Kingfisher
import SwiftUI
import StoreKit
@@ -59,13 +60,28 @@ struct MainView: View {
}
}
func registerNotificationCategories() {
// Define the communication category
let communicationCategory = UNNotificationCategory(
identifier: "COMMUNICATION",
actions: [],
intentIdentifiers: ["INSendMessageIntent"],
options: []
)
// Register the category with the notification center
UNUserNotificationCenter.current().setNotificationCategories([communicationCategory])
}
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var state: DamusState? = nil
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
SKPaymentQueue.default().add(StoreObserver.standard)
registerNotificationCategories()
migrateKingfisherCacheIfNeeded()
configureKingfisherCache()
return true
}
@@ -96,6 +112,55 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele
Task { await QueueableNotify<LossyLocalNotification>.shared.add(item: notification) }
completionHandler()
}
private func migrateKingfisherCacheIfNeeded() {
let fileManager = FileManager.default
let defaults = UserDefaults.standard
let migrationKey = "KingfisherCacheMigrated"
// Check if migration has already been done
guard !defaults.bool(forKey: migrationKey) else { return }
// Get the default Kingfisher cache (before we override it)
let defaultCache = ImageCache.default
let oldCachePath = defaultCache.diskStorage.directoryURL.path
// New shared cache location
guard let groupURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: Constants.DAMUS_APP_GROUP_IDENTIFIER) else { return }
let newCachePath = groupURL.appendingPathComponent(Constants.IMAGE_CACHE_DIRNAME).path
// Check if the old cache exists
if fileManager.fileExists(atPath: oldCachePath) {
do {
// Move the old cache to the new location
try fileManager.moveItem(atPath: oldCachePath, toPath: newCachePath)
print("Successfully migrated Kingfisher cache to \(newCachePath)")
} catch {
print("Failed to migrate cache: \(error)")
// Optionally, copy instead of move if you want to preserve the old cache as a fallback
do {
try fileManager.copyItem(atPath: oldCachePath, toPath: newCachePath)
print("Copied cache instead due to error")
} catch {
print("Failed to copy cache: \(error)")
}
}
}
// Mark migration as complete
defaults.set(true, forKey: migrationKey)
}
private func configureKingfisherCache() {
guard let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: Constants.DAMUS_APP_GROUP_IDENTIFIER) else {
return
}
let cachePath = groupURL.appendingPathComponent(Constants.IMAGE_CACHE_DIRNAME)
if let cache = try? ImageCache(name: "sharedCache", cacheDirectoryURL: cachePath) {
KingfisherManager.shared.cache = cache
}
}
}
class OrientationTracker: ObservableObject {