diff --git a/Sora.xcodeproj/project.xcworkspace/xcuserdata/Francesco.xcuserdatad/UserInterfaceState.xcuserstate b/Sora.xcodeproj/project.xcworkspace/xcuserdata/Francesco.xcuserdatad/UserInterfaceState.xcuserstate index c41b391..5e46609 100644 Binary files a/Sora.xcodeproj/project.xcworkspace/xcuserdata/Francesco.xcuserdatad/UserInterfaceState.xcuserstate and b/Sora.xcodeproj/project.xcworkspace/xcuserdata/Francesco.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Sora/Utils/Miru/MiruDataStruct.swift b/Sora/Utils/Miru/MiruDataStruct.swift index 8b42d02..c50e75d 100644 --- a/Sora/Utils/Miru/MiruDataStruct.swift +++ b/Sora/Utils/Miru/MiruDataStruct.swift @@ -8,11 +8,11 @@ import Foundation struct MiruDataStruct: Codable { - let likes: [Like] + var likes: [Like] struct Like: Codable { let anilistID: Int - let gogoSlug: String + var gogoSlug: String let title: String let cover: String diff --git a/Sora/Views/LibraryViews/LibraryManager.swift b/Sora/Views/LibraryViews/LibraryManager.swift index 6952a00..4833d69 100644 --- a/Sora/Views/LibraryViews/LibraryManager.swift +++ b/Sora/Views/LibraryViews/LibraryManager.swift @@ -45,7 +45,7 @@ class LibraryManager: ObservableObject { Logger.shared.log("Removed from library: \(item.title)") } - func importFromMiruData(_ miruData: MiruDataStruct) { + func importFromMiruData(_ miruData: MiruDataStruct, module: ModuleStruct) { var newLibraryItems: [LibraryItem] = [] for like in miruData.likes { @@ -54,6 +54,7 @@ class LibraryManager: ObservableObject { title: like.title, image: like.cover, url: like.gogoSlug, + module: module, dateAdded: Date() ) newLibraryItems.append(libraryItem) @@ -61,7 +62,7 @@ class LibraryManager: ObservableObject { } DispatchQueue.main.async { - self.libraryItems = newLibraryItems + self.libraryItems.append(contentsOf: newLibraryItems) self.saveLibrary() Logger.shared.log("Completed importing \(newLibraryItems.count) items") } diff --git a/Sora/Views/LibraryViews/LibraryView.swift b/Sora/Views/LibraryViews/LibraryView.swift index fd45d2c..628d3d6 100644 --- a/Sora/Views/LibraryViews/LibraryView.swift +++ b/Sora/Views/LibraryViews/LibraryView.swift @@ -15,6 +15,7 @@ struct LibraryItem: Identifiable, Codable { let title: String let image: String let url: String + let module: ModuleStruct var dateAdded: Date } @@ -29,7 +30,9 @@ struct LibraryView: View { } else { LazyVGrid(columns: [GridItem(.adaptive(minimum: 150))], spacing: 20) { ForEach(libraryManager.libraryItems.sorted(by: { $0.dateAdded > $1.dateAdded })) { item in - itemView(item) + NavigationLink(destination: MediaView(module: item.module, item: ItemResult(name: item.title, imageUrl: item.image, href: item.url))) { + itemView(item) + } } } .padding() @@ -79,4 +82,4 @@ struct LibraryView: View { } } } -} +} \ No newline at end of file diff --git a/Sora/Views/MediaViews/MediaExtraction.swift b/Sora/Views/MediaViews/MediaExtraction.swift index 1aa6ac8..f72e187 100644 --- a/Sora/Views/MediaViews/MediaExtraction.swift +++ b/Sora/Views/MediaViews/MediaExtraction.swift @@ -12,6 +12,7 @@ extension MediaView { func fetchItemDetails() { guard let url = URL(string: item.href.hasPrefix("https") ? item.href : "\(module.module[0].details.baseURL.hasSuffix("/") ? module.module[0].details.baseURL : "\(module.module[0].details.baseURL)/")\(item.href.hasPrefix("/") ? String(item.href.dropFirst()) : item.href)") else { return } + print(url) URLSession.custom.dataTask(with: url) { data, response, error in defer { isLoading = false } guard let data = data, error == nil else { return } diff --git a/Sora/Views/MediaViews/MediaView.swift b/Sora/Views/MediaViews/MediaView.swift index d0a8e69..1aac813 100644 --- a/Sora/Views/MediaViews/MediaView.swift +++ b/Sora/Views/MediaViews/MediaView.swift @@ -186,6 +186,7 @@ struct MediaView: View { title: item.name, image: item.imageUrl, url: item.href, + module: module, dateAdded: Date() ) libraryManager.addToLibrary(libraryItem) @@ -267,7 +268,7 @@ struct MediaView: View { } private func openSafariViewController(with urlString: String) { - guard let url = URL(string: item.href.hasPrefix("http") ? item.href : "\(module.module[0].details.baseURL)\(item.href)") else { + guard let url = URL(string: item.href.hasPrefix("https") ? item.href : "\(module.module[0].details.baseURL.hasSuffix("/") ? module.module[0].details.baseURL : "\(module.module[0].details.baseURL)/")\(item.href.hasPrefix("/") ? String(item.href.dropFirst()) : item.href)") else { Logger.shared.log("Unable to open the webpage") return } diff --git a/Sora/Views/SettingsViews/SettingView.swift b/Sora/Views/SettingsViews/SettingView.swift index b7febe0..9405081 100644 --- a/Sora/Views/SettingsViews/SettingView.swift +++ b/Sora/Views/SettingsViews/SettingView.swift @@ -15,8 +15,11 @@ struct SettingsView: View { @State private var showImportSuccessAlert = false @State private var showImportFailAlert = false @State private var importErrorMessage = "" + @State private var miruDataToImport: MiruDataStruct? + @State private var selectedModule: ModuleStruct? @StateObject private var libraryManager = LibraryManager.shared - + @StateObject private var modulesManager = ModulesManager() + var body: some View { NavigationView { Form { @@ -52,12 +55,15 @@ struct SettingsView: View { Text("Storage") } } - Button(action: { - isDocumentPickerPresented = true - }) { - HStack { - Image(systemName: "tray.and.arrow.down.fill") - Text("Import Miru Bookmarks") + ForEach(modulesManager.modules.filter { $0.extractor == "dub-sub" }, id: \.name) { module in + Button(action: { + isDocumentPickerPresented = true + selectedModule = module + }) { + HStack { + Image(systemName: "tray.and.arrow.down.fill") + Text("Import Miru Bookmarks into \(module.name)") + } } } } @@ -122,8 +128,12 @@ struct SettingsView: View { .sheet(isPresented: $isDocumentPickerPresented) { DocumentPicker( libraryManager: libraryManager, - onSuccess: { - showImportSuccessAlert = true + onSuccess: { miruData in + miruDataToImport = miruData + if let selectedModule = selectedModule { + libraryManager.importFromMiruData(miruData, module: selectedModule) + showImportSuccessAlert = true + } }, onFailure: { errorMessage in importErrorMessage = errorMessage @@ -148,7 +158,7 @@ struct SettingsView: View { struct DocumentPicker: UIViewControllerRepresentable { var libraryManager: LibraryManager - var onSuccess: () -> Void + var onSuccess: (MiruDataStruct) -> Void var onFailure: (String) -> Void func makeCoordinator() -> Coordinator { @@ -166,10 +176,10 @@ struct DocumentPicker: UIViewControllerRepresentable { class Coordinator: NSObject, UIDocumentPickerDelegate { var parent: DocumentPicker var libraryManager: LibraryManager - var onSuccess: () -> Void + var onSuccess: (MiruDataStruct) -> Void var onFailure: (String) -> Void - init(_ parent: DocumentPicker, libraryManager: LibraryManager, onSuccess: @escaping () -> Void, onFailure: @escaping (String) -> Void) { + init(_ parent: DocumentPicker, libraryManager: LibraryManager, onSuccess: @escaping (MiruDataStruct) -> Void, onFailure: @escaping (String) -> Void) { self.parent = parent self.libraryManager = libraryManager self.onSuccess = onSuccess @@ -199,10 +209,16 @@ struct DocumentPicker: UIViewControllerRepresentable { do { let data = try Data(contentsOf: selectedFileURL) - let miruData = try JSONDecoder().decode(MiruDataStruct.self, from: data) - libraryManager.importFromMiruData(miruData) + var miruData = try JSONDecoder().decode(MiruDataStruct.self, from: data) + + miruData.likes = miruData.likes.map { like in + var updatedLike = like + updatedLike.gogoSlug = "/series/" + like.gogoSlug + return updatedLike + } + Logger.shared.log("Imported Miru data from \(selectedFileURL)") - onSuccess() + onSuccess(miruData) } catch { let errorMessage = "Failed to import Miru data: \(error.localizedDescription)" print(errorMessage) @@ -212,7 +228,7 @@ struct DocumentPicker: UIViewControllerRepresentable { } func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { - let errorMessage = "Document picker was cancelled" + let errorMessage = "Document picker was closed" print(errorMessage) Logger.shared.log(errorMessage) onFailure(errorMessage)