From af71cb6a2fb612885652404958a23eeee0b04071 Mon Sep 17 00:00:00 2001 From: cranci1 <100066266+cranci1@users.noreply.github.com> Date: Fri, 31 Jan 2025 14:37:09 +0100 Subject: [PATCH] added module refresh. It does only check the version string --- Sora.xcodeproj/project.pbxproj | 8 ++-- Sora/SoraApp.swift | 5 +++ Sora/Utils/Modules/ModuleManager.swift | 39 +++++++++++++++++++ ...ViewUI.swift => SettingsViewGeneral.swift} | 13 +++++-- .../SettingsSubViews/SettingsViewModule.swift | 8 +++- Sora/Views/SettingsView/SettingsView.swift | 4 +- 6 files changed, 66 insertions(+), 11 deletions(-) rename Sora/Views/SettingsView/SettingsSubViews/{SettingsViewUI.swift => SettingsViewGeneral.swift} (84%) diff --git a/Sora.xcodeproj/project.pbxproj b/Sora.xcodeproj/project.pbxproj index ab319d6..6741628 100644 --- a/Sora.xcodeproj/project.pbxproj +++ b/Sora.xcodeproj/project.pbxproj @@ -7,7 +7,7 @@ objects = { /* Begin PBXBuildFile section */ - 131845F92D47C62D00CA7A54 /* SettingsViewUI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 131845F82D47C62D00CA7A54 /* SettingsViewUI.swift */; }; + 131845F92D47C62D00CA7A54 /* SettingsViewGeneral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 131845F82D47C62D00CA7A54 /* SettingsViewGeneral.swift */; }; 133D7C6E2D2BE2500075467E /* SoraApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 133D7C6D2D2BE2500075467E /* SoraApp.swift */; }; 133D7C702D2BE2500075467E /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 133D7C6F2D2BE2500075467E /* ContentView.swift */; }; 133D7C722D2BE2520075467E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 133D7C712D2BE2520075467E /* Assets.xcassets */; }; @@ -41,7 +41,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 131845F82D47C62D00CA7A54 /* SettingsViewUI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewUI.swift; sourceTree = ""; }; + 131845F82D47C62D00CA7A54 /* SettingsViewGeneral.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewGeneral.swift; sourceTree = ""; }; 133D7C6A2D2BE2500075467E /* Sora.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Sora.app; sourceTree = BUILT_PRODUCTS_DIR; }; 133D7C6D2D2BE2500075467E /* SoraApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoraApp.swift; sourceTree = ""; }; 133D7C6F2D2BE2500075467E /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -154,7 +154,7 @@ 1E9FF1D22D403E42008AC100 /* SettingsViewLoggerFilter.swift */, 1399FAD32D3AB38C00E97C31 /* SettingsViewLogger.swift */, 133D7C842D2BE2630075467E /* SettingsViewModule.swift */, - 131845F82D47C62D00CA7A54 /* SettingsViewUI.swift */, + 131845F82D47C62D00CA7A54 /* SettingsViewGeneral.swift */, ); path = SettingsSubViews; sourceTree = ""; @@ -371,7 +371,7 @@ 133D7C702D2BE2500075467E /* ContentView.swift in Sources */, 13EA2BD62D32D97400C1EBD7 /* Double+Extension.swift in Sources */, 133D7C8F2D2BE2640075467E /* MediaInfoView.swift in Sources */, - 131845F92D47C62D00CA7A54 /* SettingsViewUI.swift in Sources */, + 131845F92D47C62D00CA7A54 /* SettingsViewGeneral.swift in Sources */, 133D7C8D2D2BE2640075467E /* HomeView.swift in Sources */, 13EA2BDC2D32D9FF00C1EBD7 /* MiruDataStruct.swift in Sources */, 13D842552D45267500EBBFA6 /* DropManager.swift in Sources */, diff --git a/Sora/SoraApp.swift b/Sora/SoraApp.swift index ff3c759..39d5df8 100644 --- a/Sora/SoraApp.swift +++ b/Sora/SoraApp.swift @@ -22,6 +22,11 @@ struct SoraApp: App { .accentColor(settings.accentColor) .onAppear { settings.updateAppearance() + if UserDefaults.standard.bool(forKey: "refreshModulesOnLaunch") { + Task { + await moduleManager.refreshModules() + } + } } .onOpenURL { url in handleURL(url) diff --git a/Sora/Utils/Modules/ModuleManager.swift b/Sora/Utils/Modules/ModuleManager.swift index 9a2371e..de1f279 100644 --- a/Sora/Utils/Modules/ModuleManager.swift +++ b/Sora/Utils/Modules/ModuleManager.swift @@ -90,4 +90,43 @@ class ModuleManager: ObservableObject { let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath) return try String(contentsOf: localUrl, encoding: .utf8) } + + func refreshModules() async { + for (index, module) in modules.enumerated() { + do { + let (metadataData, _) = try await URLSession.custom.data(from: URL(string: module.metadataUrl)!) + let newMetadata = try JSONDecoder().decode(ModuleMetadata.self, from: metadataData) + + if newMetadata.version != module.metadata.version { + guard let scriptUrl = URL(string: newMetadata.scriptUrl) else { + throw NSError(domain: "Invalid script URL", code: -1) + } + + let (scriptData, _) = try await URLSession.custom.data(from: scriptUrl) + guard let jsContent = String(data: scriptData, encoding: .utf8) else { + throw NSError(domain: "Invalid script encoding", code: -1) + } + + let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath) + try jsContent.write(to: localUrl, atomically: true, encoding: .utf8) + + modules[index] = ScrapingModule( + id: module.id, + metadata: newMetadata, + localPath: module.localPath, + metadataUrl: module.metadataUrl, + isActive: module.isActive + ) + + Logger.shared.log("Updated module: \(module.metadata.sourceName) to version \(newMetadata.version)") + } + } catch { + Logger.shared.log("Failed to refresh module: \(module.metadata.sourceName) - \(error.localizedDescription)") + } + } + + DispatchQueue.main.async { + self.saveModules() + } +} } diff --git a/Sora/Views/SettingsView/SettingsSubViews/SettingsViewUI.swift b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewGeneral.swift similarity index 84% rename from Sora/Views/SettingsView/SettingsSubViews/SettingsViewUI.swift rename to Sora/Views/SettingsView/SettingsSubViews/SettingsViewGeneral.swift index bf4c69f..1f8da68 100644 --- a/Sora/Views/SettingsView/SettingsSubViews/SettingsViewUI.swift +++ b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewGeneral.swift @@ -1,5 +1,5 @@ // -// SettingsViewUI.swift +// SettingsViewGeneral.swift // Sora // // Created by Francesco on 27/01/25. @@ -7,8 +7,9 @@ import SwiftUI -struct SettingsViewUI: View { +struct SettingsViewGeneral: View { @AppStorage("episodeChunkSize") private var episodeChunkSize: Int = 100 + @AppStorage("refreshModulesOnLaunch") private var refreshModulesOnLaunch: Bool = false @EnvironmentObject var settings: Settings var body: some View { @@ -48,7 +49,11 @@ struct SettingsViewUI: View { } } } + + Section(header: Text("Modules")) { + Toggle("Refresh Modules on Launch", isOn: $refreshModulesOnLaunch) + } } - .navigationTitle("UI Settings") + .navigationTitle("General") } -} +} \ No newline at end of file diff --git a/Sora/Views/SettingsView/SettingsSubViews/SettingsViewModule.swift b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewModule.swift index da020b8..f7131b6 100644 --- a/Sora/Views/SettingsView/SettingsSubViews/SettingsViewModule.swift +++ b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewModule.swift @@ -14,6 +14,7 @@ struct SettingsViewModule: View { @State private var errorMessage: String? @State private var isLoading = false + @State private var isRefreshing = false var body: some View { VStack { @@ -109,6 +110,11 @@ struct SettingsViewModule: View { .resizable() .padding(5) }) + .refreshable { + isRefreshing = true + await moduleManager.refreshModules() + isRefreshing = false + } } .alert(isPresented: .constant(errorMessage != nil)) { Alert( @@ -163,4 +169,4 @@ struct SettingsViewModule: View { } } } -} +} \ No newline at end of file diff --git a/Sora/Views/SettingsView/SettingsView.swift b/Sora/Views/SettingsView/SettingsView.swift index 1d8b2f1..a8e7565 100644 --- a/Sora/Views/SettingsView/SettingsView.swift +++ b/Sora/Views/SettingsView/SettingsView.swift @@ -13,8 +13,8 @@ struct SettingsView: View { NavigationView { Form { Section(header: Text("Main Settings")) { - NavigationLink(destination: SettingsViewUI()) { - Text("UI Settings") + NavigationLink(destination: SettingsViewGeneral()) { + Text("General Settings") } NavigationLink(destination: SettingsViewModule()) { Text("Media Player")