mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonCore/AppContext.swift

184 lines
6.4 KiB
Swift
Raw Normal View History

2021-01-27 07:50:13 +01:00
//
// AppContext.swift
//
2021-01-27 07:50:13 +01:00
//
// Created by MainasuK on 22/9/30.
2021-01-27 07:50:13 +01:00
//
import UIKit
import SwiftUI
2021-01-27 07:50:13 +01:00
import Combine
import CoreData
import CoreDataStack
2021-06-01 08:07:44 +02:00
import AlamofireImage
2021-01-27 07:50:13 +01:00
public class AppContext: ObservableObject {
2021-01-27 07:50:13 +01:00
2022-10-08 07:43:06 +02:00
public var disposeBag = Set<AnyCancellable>()
2021-01-27 07:50:13 +01:00
public let coreDataStack: CoreDataStack
public let managedObjectContext: NSManagedObjectContext
public let backgroundManagedObjectContext: NSManagedObjectContext
2021-01-27 07:50:13 +01:00
public let apiService: APIService
public let authenticationService: AuthenticationService
public let emojiService: EmojiService
2023-06-28 09:43:12 +02:00
public let publisherService: PublisherService
public let notificationService: NotificationService
public let settingService: SettingService
public let instanceService: InstanceService
public let blockDomainService: BlockDomainService
public let statusFilterService: StatusFilterService
public let photoLibraryService = PhotoLibraryService()
public let placeholderImageCacheService = PlaceholderImageCacheService()
public let blurhashImageCacheService = BlurhashImageCacheService.shared
public let documentStore: DocumentStore
2021-01-27 07:50:13 +01:00
private var documentStoreSubscription: AnyCancellable!
let overrideTraitCollection = CurrentValueSubject<UITraitCollection?, Never>(nil)
let timestampUpdatePublisher = Timer.publish(every: 1.0, on: .main, in: .common)
.autoconnect()
.share()
.eraseToAnyPublisher()
public init() {
let authProvider = AuthenticationServiceProvider.shared
let _coreDataStack = CoreDataStack()
if authProvider.authenticationMigrationRequired {
authProvider.migrateLegacyAuthentications(
in: _coreDataStack.persistentContainer.viewContext
)
}
2021-01-27 07:50:13 +01:00
let _managedObjectContext = _coreDataStack.persistentContainer.viewContext
let _backgroundManagedObjectContext = _coreDataStack.persistentContainer.newBackgroundContext()
2021-01-27 07:50:13 +01:00
coreDataStack = _coreDataStack
managedObjectContext = _managedObjectContext
backgroundManagedObjectContext = _backgroundManagedObjectContext
2021-01-28 09:10:30 +01:00
let _apiService = APIService(backgroundManagedObjectContext: _backgroundManagedObjectContext)
apiService = _apiService
2021-04-25 06:48:29 +02:00
let _authenticationService = AuthenticationService(
2021-02-03 09:01:08 +01:00
managedObjectContext: _managedObjectContext,
backgroundManagedObjectContext: _backgroundManagedObjectContext,
apiService: _apiService
)
2021-04-25 06:48:29 +02:00
authenticationService = _authenticationService
2021-01-28 09:10:30 +01:00
2021-03-15 07:40:10 +01:00
emojiService = EmojiService(
apiService: apiService
)
publisherService = .init(apiService: _apiService)
let _notificationService = NotificationService(
2021-04-27 11:27:03 +02:00
apiService: _apiService,
2021-04-25 06:48:29 +02:00
authenticationService: _authenticationService
)
notificationService = _notificationService
settingService = SettingService(
apiService: _apiService,
authenticationService: _authenticationService,
notificationService: _notificationService
)
2021-03-10 12:12:53 +01:00
instanceService = InstanceService(
apiService: _apiService,
authenticationService: _authenticationService
)
2021-04-30 08:55:02 +02:00
blockDomainService = BlockDomainService(
backgroundManagedObjectContext: _backgroundManagedObjectContext,
authenticationService: _authenticationService
)
statusFilterService = StatusFilterService(
apiService: _apiService,
authenticationService: _authenticationService
2021-04-30 08:55:02 +02:00
)
2021-01-27 07:50:13 +01:00
documentStore = DocumentStore()
documentStoreSubscription = documentStore.objectWillChange
.receive(on: DispatchQueue.main)
.sink { [unowned self] in
self.objectWillChange.send()
}
backgroundManagedObjectContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave, object: backgroundManagedObjectContext)
.sink { [weak self] notification in
guard let self = self else { return }
self.managedObjectContext.perform {
self.managedObjectContext.mergeChanges(fromContextDidSave: notification)
}
}
.store(in: &disposeBag)
}
}
2021-06-01 08:07:44 +02:00
extension AppContext {
2022-10-08 07:43:06 +02:00
public typealias ByteCount = Int
2021-06-01 08:07:44 +02:00
2022-10-08 07:43:06 +02:00
public static let byteCountFormatter: ByteCountFormatter = {
2021-06-01 08:07:44 +02:00
let formatter = ByteCountFormatter()
return formatter
}()
2023-06-28 09:02:14 +02:00
public func purgeCache() {
ImageDownloader.defaultURLCache().removeAllCachedResponses()
2023-06-28 09:02:14 +02:00
let fileManager = FileManager.default
let temporaryDirectoryURL = fileManager.temporaryDirectory
let fileKeys: [URLResourceKey] = [.fileSizeKey, .isDirectoryKey]
if let directoryEnumerator = fileManager.enumerator(
at: temporaryDirectoryURL,
includingPropertiesForKeys: fileKeys,
options: .skipsHiddenFiles) {
for case let fileURL as URL in directoryEnumerator {
guard let resourceValues = try? fileURL.resourceValues(forKeys: Set(fileKeys)),
resourceValues.isDirectory == false else {
continue
}
try? fileManager.removeItem(at: fileURL)
2021-06-01 08:07:44 +02:00
}
}
}
2023-06-28 09:02:14 +02:00
// In Bytes
public func currentDiskUsage() -> Int {
let alamoFireDiskBytes = ImageDownloader.defaultURLCache().currentDiskUsage
var tempFilesDiskBytes = 0
let fileManager = FileManager.default
let temporaryDirectoryURL = fileManager.temporaryDirectory
let fileKeys: [URLResourceKey] = [.fileSizeKey, .isDirectoryKey]
if let directoryEnumerator = fileManager.enumerator(
at: temporaryDirectoryURL,
includingPropertiesForKeys: fileKeys,
options: .skipsHiddenFiles) {
for case let fileURL as URL in directoryEnumerator {
guard let resourceValues = try? fileURL.resourceValues(forKeys: Set(fileKeys)),
resourceValues.isDirectory == false else {
continue
2021-06-01 08:07:44 +02:00
}
2023-06-28 09:02:14 +02:00
tempFilesDiskBytes += resourceValues.fileSize ?? 0
2021-06-01 08:07:44 +02:00
}
}
2021-06-21 14:27:43 +02:00
2023-06-28 09:02:14 +02:00
return alamoFireDiskBytes + tempFilesDiskBytes
}
2021-06-21 14:27:43 +02:00
}