mirror of
https://github.com/cranci1/Sora.git
synced 2026-04-21 00:22:12 +00:00
module sync right?
This commit is contained in:
parent
842fd5b01c
commit
0415df2cd9
3 changed files with 95 additions and 8 deletions
|
|
@ -215,7 +215,7 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
|
||||||
do {
|
do {
|
||||||
try audioSession.setActive(true)
|
try audioSession.setActive(true)
|
||||||
} catch {
|
} catch {
|
||||||
print("Error activating audio session: \(error)")
|
Logger.shared.log("Error activating audio session: \(error)", type: "Debug")
|
||||||
}
|
}
|
||||||
|
|
||||||
volumeViewModel.value = Double(audioSession.outputVolume)
|
volumeViewModel.value = Double(audioSession.outputVolume)
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,24 @@ class ModuleManager: ObservableObject {
|
||||||
let url = getModulesFilePath()
|
let url = getModulesFilePath()
|
||||||
guard let data = try? Data(contentsOf: url) else { return }
|
guard let data = try? Data(contentsOf: url) else { return }
|
||||||
modules = (try? JSONDecoder().decode([ScrapingModule].self, from: data)) ?? []
|
modules = (try? JSONDecoder().decode([ScrapingModule].self, from: data)) ?? []
|
||||||
|
|
||||||
|
Task {
|
||||||
|
for module in modules {
|
||||||
|
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
|
||||||
|
if (!fileManager.fileExists(atPath: localUrl.path)) {
|
||||||
|
do {
|
||||||
|
let scriptUrl = URL(string: module.metadata.scriptUrl)
|
||||||
|
guard let scriptUrl = scriptUrl else { continue }
|
||||||
|
let (scriptData, _) = try await URLSession.custom.data(from: scriptUrl)
|
||||||
|
guard let jsContent = String(data: scriptData, encoding: .utf8) else { continue }
|
||||||
|
try jsContent.write(to: localUrl, atomically: true, encoding: .utf8)
|
||||||
|
Logger.shared.log("Recovered missing JS file for module: \(module.metadata.sourceName)")
|
||||||
|
} catch {
|
||||||
|
Logger.shared.log("Failed to recover JS file for module: \(module.metadata.sourceName) - \(error.localizedDescription)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func saveModules() {
|
private func saveModules() {
|
||||||
|
|
|
||||||
|
|
@ -29,24 +29,30 @@ class iCloudSyncManager {
|
||||||
"continueWatchingItems"
|
"continueWatchingItems"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
private let modulesFileName = "modules.json"
|
||||||
|
|
||||||
|
private var ubiquityContainerURL: URL? {
|
||||||
|
FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
|
||||||
|
}
|
||||||
|
|
||||||
private init() {
|
private init() {
|
||||||
setupSync()
|
setupSync()
|
||||||
|
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(willEnterBackground), name: UIApplication.willResignActiveNotification, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(willEnterBackground), name: UIApplication.willResignActiveNotification, object: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private func setupSync() {
|
private func setupSync() {
|
||||||
NSUbiquitousKeyValueStore.default.synchronize()
|
NSUbiquitousKeyValueStore.default.synchronize()
|
||||||
|
|
||||||
syncFromiCloud()
|
syncFromiCloud()
|
||||||
|
syncModulesFromiCloud()
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(iCloudDidChangeExternally), name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: NSUbiquitousKeyValueStore.default)
|
NotificationCenter.default.addObserver(self, selector: #selector(iCloudDidChangeExternally), name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: NSUbiquitousKeyValueStore.default)
|
||||||
|
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange), name: UserDefaults.didChangeNotification, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange), name: UserDefaults.didChangeNotification, object: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc private func willEnterBackground() {
|
@objc private func willEnterBackground() {
|
||||||
syncToiCloud()
|
syncToiCloud()
|
||||||
|
syncModulesToiCloud()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func syncFromiCloud() {
|
private func syncFromiCloud() {
|
||||||
|
|
@ -79,16 +85,79 @@ class iCloudSyncManager {
|
||||||
@objc private func iCloudDidChangeExternally(_ notification: Notification) {
|
@objc private func iCloudDidChangeExternally(_ notification: Notification) {
|
||||||
guard let userInfo = notification.userInfo,
|
guard let userInfo = notification.userInfo,
|
||||||
let reason = userInfo[NSUbiquitousKeyValueStoreChangeReasonKey] as? Int else {
|
let reason = userInfo[NSUbiquitousKeyValueStoreChangeReasonKey] as? Int else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if reason == NSUbiquitousKeyValueStoreServerChange ||
|
if reason == NSUbiquitousKeyValueStoreServerChange ||
|
||||||
reason == NSUbiquitousKeyValueStoreInitialSyncChange {
|
reason == NSUbiquitousKeyValueStoreInitialSyncChange {
|
||||||
syncFromiCloud()
|
syncFromiCloud()
|
||||||
|
syncModulesFromiCloud()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc private func userDefaultsDidChange(_ notification: Notification) {
|
@objc private func userDefaultsDidChange(_ notification: Notification) {
|
||||||
syncToiCloud()
|
syncToiCloud()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func syncModulesToiCloud() {
|
||||||
|
DispatchQueue.global(qos: .background).async {
|
||||||
|
guard let iCloudURL = self.ubiquityContainerURL else { return }
|
||||||
|
let localModulesURL = self.getLocalModulesFileURL()
|
||||||
|
let iCloudModulesURL = iCloudURL.appendingPathComponent(self.modulesFileName)
|
||||||
|
do {
|
||||||
|
guard FileManager.default.fileExists(atPath: localModulesURL.path) else { return }
|
||||||
|
|
||||||
|
let shouldCopy: Bool
|
||||||
|
if FileManager.default.fileExists(atPath: iCloudModulesURL.path) {
|
||||||
|
let localData = try Data(contentsOf: localModulesURL)
|
||||||
|
let iCloudData = try Data(contentsOf: iCloudModulesURL)
|
||||||
|
shouldCopy = localData != iCloudData
|
||||||
|
} else {
|
||||||
|
shouldCopy = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if shouldCopy {
|
||||||
|
if FileManager.default.fileExists(atPath: iCloudModulesURL.path) {
|
||||||
|
try FileManager.default.removeItem(at: iCloudModulesURL)
|
||||||
|
}
|
||||||
|
try FileManager.default.copyItem(at: localModulesURL, to: iCloudModulesURL)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
Logger.shared.log("iCloud modules sync error: \(error)", type: "Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func syncModulesFromiCloud() {
|
||||||
|
DispatchQueue.global(qos: .background).async {
|
||||||
|
guard let iCloudURL = self.ubiquityContainerURL else { return }
|
||||||
|
let localModulesURL = self.getLocalModulesFileURL()
|
||||||
|
let iCloudModulesURL = iCloudURL.appendingPathComponent(self.modulesFileName)
|
||||||
|
do {
|
||||||
|
guard FileManager.default.fileExists(atPath: iCloudModulesURL.path) else { return }
|
||||||
|
|
||||||
|
let shouldCopy: Bool
|
||||||
|
if FileManager.default.fileExists(atPath: localModulesURL.path) {
|
||||||
|
let localData = try Data(contentsOf: localModulesURL)
|
||||||
|
let iCloudData = try Data(contentsOf: iCloudModulesURL)
|
||||||
|
shouldCopy = localData != iCloudData
|
||||||
|
} else {
|
||||||
|
shouldCopy = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if shouldCopy {
|
||||||
|
if FileManager.default.fileExists(atPath: localModulesURL.path) {
|
||||||
|
try FileManager.default.removeItem(at: localModulesURL)
|
||||||
|
}
|
||||||
|
try FileManager.default.copyItem(at: iCloudModulesURL, to: localModulesURL)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
Logger.shared.log("iCloud modules fetch error: \(error)", type: "Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func getLocalModulesFileURL() -> URL {
|
||||||
|
let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||||
|
return docs.appendingPathComponent(modulesFileName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue