diff --git a/Sora.xcodeproj/project.pbxproj b/Sora.xcodeproj/project.pbxproj index cf26c32..245421c 100644 --- a/Sora.xcodeproj/project.pbxproj +++ b/Sora.xcodeproj/project.pbxproj @@ -92,11 +92,12 @@ 133D7C7B2D2BE2630075467E /* Views */ = { isa = PBXGroup; children = ( - 133D7C812D2BE2630075467E /* SettingsView */, + 133D7C832D2BE2630075467E /* SettingsSubViews */, 133D7C7F2D2BE2630075467E /* MediaInfoView */, - 133D7C7C2D2BE2630075467E /* SearchView.swift */, 133D7C7D2D2BE2630075467E /* HomeView.swift */, 133D7C7E2D2BE2630075467E /* LibraryView.swift */, + 133D7C7C2D2BE2630075467E /* SearchView.swift */, + 133D7C822D2BE2630075467E /* SettingsView.swift */, ); path = Views; sourceTree = ""; @@ -109,21 +110,12 @@ path = MediaInfoView; sourceTree = ""; }; - 133D7C812D2BE2630075467E /* SettingsView */ = { - isa = PBXGroup; - children = ( - 133D7C822D2BE2630075467E /* SettingsView.swift */, - 133D7C832D2BE2630075467E /* SubViews */, - ); - path = SettingsView; - sourceTree = ""; - }; - 133D7C832D2BE2630075467E /* SubViews */ = { + 133D7C832D2BE2630075467E /* SettingsSubViews */ = { isa = PBXGroup; children = ( 133D7C842D2BE2630075467E /* SettingsViewModule.swift */, ); - path = SubViews; + path = SettingsSubViews; sourceTree = ""; }; 133D7C852D2BE2640075467E /* Utils */ = { diff --git a/Sora/Utils/Loaders/JSController.swift b/Sora/Utils/Loaders/JSController.swift index 946d1ed..1115934 100644 --- a/Sora/Utils/Loaders/JSController.swift +++ b/Sora/Utils/Loaders/JSController.swift @@ -28,7 +28,7 @@ class JSController: ObservableObject { context.evaluateScript(script) } - func fetchSearchResults(keyword: String, module: ScrapingModule, completion: @escaping ([MediaItem]) -> Void) { + func fetchSearchResults(keyword: String, module: ScrapingModule, completion: @escaping ([SearchItem]) -> Void) { let searchUrl = module.metadata.searchBaseUrl.replacingOccurrences(of: "%s", with: keyword.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "") guard let url = URL(string: searchUrl) else { @@ -53,55 +53,15 @@ class JSController: ObservableObject { if let parseFunction = self.context.objectForKeyedSubscript("searchResults"), let results = parseFunction.call(withArguments: [html]).toArray() as? [[String: String]] { - let mediaItems = results.map { item in - MediaItem( + let resultItems = results.map { item in + SearchItem( title: item["title"] ?? "", imageUrl: item["image"] ?? "", href: item["href"] ?? "" ) } DispatchQueue.main.async { - completion(mediaItems) - } - } else { - print("Failed to parse results") - DispatchQueue.main.async { completion([]) } - } - }.resume() - } - - func fetchInfoContent(href: String, module: ScrapingModule, completion: @escaping ([MediaItem]) -> Void) { - guard let url = URL(string: href) else { - completion([]) - return - } - - URLSession.custom.dataTask(with: url) { [weak self] data, response, error in - guard let self = self else { return } - - if let error = error { - print("Network error: \(error)") - DispatchQueue.main.async { completion([]) } - return - } - - guard let data = data, let html = String(data: data, encoding: .utf8) else { - print("Failed to decode HTML") - DispatchQueue.main.async { completion([]) } - return - } - - if let parseFunction = self.context.objectForKeyedSubscript("fetchInfo"), - let results = parseFunction.call(withArguments: [html]).toArray() as? [[String: String]] { - let mediaItems = results.map { item in - MediaItem( - title: item["title"] ?? "", - imageUrl: item["image"] ?? "", - href: item["href"] ?? "" - ) - } - DispatchQueue.main.async { - completion(mediaItems) + completion(resultItems) } } else { print("Failed to parse results") diff --git a/Sora/Views/MediaInfoView/MediaInfoView.swift b/Sora/Views/MediaInfoView/MediaInfoView.swift index 9f901ad..6f20a7a 100644 --- a/Sora/Views/MediaInfoView/MediaInfoView.swift +++ b/Sora/Views/MediaInfoView/MediaInfoView.swift @@ -27,15 +27,7 @@ struct MediaInfoView: View { .padding() Button(action: { - var finalHref = href - if !href.starts(with: "http") { - var baseUrl = module.metadata.baseUrl - if !baseUrl.hasSuffix("/") && !href.hasPrefix("/") { - baseUrl += "/" - } - finalHref = baseUrl + href - } - if let url = URL(string: finalHref) { + if let url = URL(string: href) { UIApplication.shared.open(url) } }) { diff --git a/Sora/Views/SearchView.swift b/Sora/Views/SearchView.swift index 7bef4ef..42fd3c4 100644 --- a/Sora/Views/SearchView.swift +++ b/Sora/Views/SearchView.swift @@ -8,7 +8,7 @@ import SwiftUI import Kingfisher -struct MediaItem: Identifiable { +struct SearchItem: Identifiable { let id = UUID() let title: String let imageUrl: String @@ -20,8 +20,8 @@ struct SearchView: View { @StateObject private var jsController = JSController() @EnvironmentObject var moduleManager: ModuleManager - @State private var mediaItems: [MediaItem] = [] - @State private var selectedMediaItem: MediaItem? + @State private var searchItems: [SearchItem] = [] + @State private var selectedSearchItem: SearchItem? @State private var isSearching = false @State private var searchText = "" @@ -58,7 +58,7 @@ struct SearchView: View { } LazyVGrid(columns: [GridItem(.adaptive(minimum: 150))], spacing: 16) { - ForEach(mediaItems) { item in + ForEach(searchItems) { item in VStack { KFImage(URL(string: item.imageUrl)) .resizable() @@ -73,7 +73,7 @@ struct SearchView: View { .lineLimit(1) } .onTapGesture { - selectedMediaItem = item + selectedSearchItem = item } } } @@ -117,7 +117,7 @@ struct SearchView: View { } } } - .sheet(item: $selectedMediaItem) { item in + .sheet(item: $selectedSearchItem) { item in MediaInfoView(title: item.title, imageUrl: item.imageUrl, href: item.href, module: selectedModule!) } } @@ -131,7 +131,7 @@ struct SearchView: View { private func performSearch() { guard !searchText.isEmpty, let module = selectedModule else { - mediaItems = [] + searchItems = [] return } @@ -142,7 +142,7 @@ struct SearchView: View { let jsContent = try moduleManager.getModuleContent(module) jsController.loadScript(jsContent) jsController.fetchSearchResults(keyword: searchText, module: module) { items in - mediaItems = items + searchItems = items isSearching = false } } catch { diff --git a/Sora/Views/SettingsView/SubViews/SettingsViewModule.swift b/Sora/Views/SettingsSubViews/SettingsViewModule.swift similarity index 100% rename from Sora/Views/SettingsView/SubViews/SettingsViewModule.swift rename to Sora/Views/SettingsSubViews/SettingsViewModule.swift diff --git a/Sora/Views/SettingsView/SettingsView.swift b/Sora/Views/SettingsView.swift similarity index 100% rename from Sora/Views/SettingsView/SettingsView.swift rename to Sora/Views/SettingsView.swift