mirror of
https://github.com/cranci1/Sora.git
synced 2026-04-13 04:50:41 +00:00
idk what i did lol
This commit is contained in:
parent
acfca87d61
commit
4922ddd628
7 changed files with 45 additions and 23 deletions
Binary file not shown.
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue