From 6996df091b2d0e5728df583daabf7c8dc71056c2 Mon Sep 17 00:00:00 2001 From: Francesco <100066266+cranci1@users.noreply.github.com> Date: Thu, 15 May 2025 18:56:07 +0200 Subject: [PATCH] improvements --- .../Mutations/AniListPushUpdates.swift | 12 +++--- Sora/Utils/Analytics/Analytics.swift | 15 +------- .../DownloadManager/DownloadManager.swift | 7 ---- Sora/Utils/Extensions/UIDevice+Model.swift | 2 +- Sora/Utils/Extensions/URL.swift | 5 +++ Sora/Utils/Extensions/URLSession.swift | 13 ++----- .../CustomPlayer/CustomPlayer.swift | 3 -- Sora/Views/LibraryView/LibraryView.swift | 37 +++++++++---------- Sora/Views/MediaInfoView/MediaInfoView.swift | 3 +- Sora/Views/SearchView.swift | 10 ++--- .../SettingsSubViews/SettingsViewPlayer.swift | 21 ----------- 11 files changed, 39 insertions(+), 89 deletions(-) diff --git a/Sora/Tracking Services/AniList/Mutations/AniListPushUpdates.swift b/Sora/Tracking Services/AniList/Mutations/AniListPushUpdates.swift index 4383e1f..997ab5a 100644 --- a/Sora/Tracking Services/AniList/Mutations/AniListPushUpdates.swift +++ b/Sora/Tracking Services/AniList/Mutations/AniListPushUpdates.swift @@ -118,16 +118,15 @@ class AniListMutation { "variables": variables ] guard let jsonData = try? JSONSerialization.data(withJSONObject: requestBody, options: []) else { - completion(.failure(NSError(domain: "", code: -1, - userInfo: [NSLocalizedDescriptionKey: "Failed to serialize GraphQL request"]))) + completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to serialize GraphQL request"]))) return } - + var request = URLRequest(url: apiURL) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") request.httpBody = jsonData - + URLSession.shared.dataTask(with: request) { data, resp, error in if let e = error { return completion(.failure(e)) @@ -135,9 +134,8 @@ class AniListMutation { guard let data = data, let json = try? JSONDecoder().decode(AniListMediaResponse.self, from: data), let mal = json.data.Media?.idMal else { - return completion(.failure(NSError(domain: "", code: -1, - userInfo: [NSLocalizedDescriptionKey: "Failed to decode AniList response or idMal missing"]))) - } + return completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to decode AniList response or idMal missing"]))) + } completion(.success(mal)) }.resume() } diff --git a/Sora/Utils/Analytics/Analytics.swift b/Sora/Utils/Analytics/Analytics.swift index e8b62ad..5899c96 100644 --- a/Sora/Utils/Analytics/Analytics.swift +++ b/Sora/Utils/Analytics/Analytics.swift @@ -8,7 +8,6 @@ import Foundation import UIKit -// MARK: - Analytics Response Model struct AnalyticsResponse: Codable { let status: String let message: String @@ -16,26 +15,21 @@ struct AnalyticsResponse: Codable { let timestamp: String? } -// MARK: - Analytics Manager class AnalyticsManager { - static let shared = AnalyticsManager() private let analyticsURL = URL(string: "http://151.106.3.14:47474/analytics")! private let moduleManager = ModuleManager() private init() {} - // MARK: - Send Analytics Data func sendEvent(event: String, additionalData: [String: Any] = [:]) { let defaults = UserDefaults.standard - // Ensure the key is set with a default value if missing if defaults.object(forKey: "analyticsEnabled") == nil { defaults.setValue(false, forKey: "analyticsEnabled") } - let analyticsEnabled = UserDefaults.standard.bool(forKey: "analyticsEnabled") guard analyticsEnabled else { @@ -48,10 +42,8 @@ class AnalyticsManager { return } - // Prepare analytics data var safeAdditionalData = additionalData - - // Check and convert NSError if present + if let errorValue = additionalData["error"] as? NSError { safeAdditionalData["error"] = errorValue.localizedDescription } @@ -68,7 +60,6 @@ class AnalyticsManager { sendRequest(with: analyticsData) } - // MARK: - Private Request Method private func sendRequest(with data: [String: Any]) { var request = URLRequest(url: analyticsURL) request.httpMethod = "POST" @@ -105,18 +96,14 @@ class AnalyticsManager { }.resume() } - // MARK: - Get App Version private func getAppVersion() -> String { return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown_version" } - // MARK: - Get Device Model private func getDeviceModel() -> String { return UIDevice.modelName } - - // MARK: - Get Selected Module private func getSelectedModule() -> ScrapingModule? { guard let selectedModuleId = UserDefaults.standard.string(forKey: "selectedModuleId") else { return nil } return moduleManager.modules.first { $0.id.uuidString == selectedModuleId } diff --git a/Sora/Utils/DownloadManager/DownloadManager.swift b/Sora/Utils/DownloadManager/DownloadManager.swift index c533b59..0a09e51 100644 --- a/Sora/Utils/DownloadManager/DownloadManager.swift +++ b/Sora/Utils/DownloadManager/DownloadManager.swift @@ -222,10 +222,3 @@ struct ActiveDownload: Identifiable { var progress: Double let task: URLSessionTask } - -extension URL { - static func isValidHLSURL(string: String) -> Bool { - guard let url = URL(string: string), url.pathExtension == "m3u8" else { return false } - return true - } -} diff --git a/Sora/Utils/Extensions/UIDevice+Model.swift b/Sora/Utils/Extensions/UIDevice+Model.swift index e07970d..b7a2c2a 100644 --- a/Sora/Utils/Extensions/UIDevice+Model.swift +++ b/Sora/Utils/Extensions/UIDevice+Model.swift @@ -18,7 +18,7 @@ public extension UIDevice { return identifier + String(UnicodeScalar(UInt8(value))) } - func mapToDevice(identifier: String) -> String { // swiftlint:disable:this cyclomatic_complexity + func mapToDevice(identifier: String) -> String { #if os(iOS) switch identifier { case "iPod5,1": diff --git a/Sora/Utils/Extensions/URL.swift b/Sora/Utils/Extensions/URL.swift index 3dc88f2..bb02f84 100644 --- a/Sora/Utils/Extensions/URL.swift +++ b/Sora/Utils/Extensions/URL.swift @@ -17,4 +17,9 @@ extension URL { } return params } + + static func isValidHLSURL(string: String) -> Bool { + guard let url = URL(string: string), url.pathExtension == "m3u8" else { return false } + return true + } } diff --git a/Sora/Utils/Extensions/URLSession.swift b/Sora/Utils/Extensions/URLSession.swift index 78b8834..b712ec1 100644 --- a/Sora/Utils/Extensions/URLSession.swift +++ b/Sora/Utils/Extensions/URLSession.swift @@ -7,26 +7,21 @@ import Foundation -class FetchDelegate: NSObject, URLSessionTaskDelegate -{ +class FetchDelegate: NSObject, URLSessionTaskDelegate { private let allowRedirects: Bool init(allowRedirects: Bool) { self.allowRedirects = allowRedirects } func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) { - if(allowRedirects) - { + if(allowRedirects) { completionHandler(request) - } - else - { + } else { completionHandler(nil) } - } - } + extension URLSession { static let userAgents = [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36", diff --git a/Sora/Utils/MediaPlayer/CustomPlayer/CustomPlayer.swift b/Sora/Utils/MediaPlayer/CustomPlayer/CustomPlayer.swift index 4358ff6..5db5a65 100644 --- a/Sora/Utils/MediaPlayer/CustomPlayer/CustomPlayer.swift +++ b/Sora/Utils/MediaPlayer/CustomPlayer/CustomPlayer.swift @@ -301,9 +301,6 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele } #if os(iOS) && !targetEnvironment(macCatalyst) - if #available(iOS 16.0, *) { - playerViewController.allowsVideoFrameAnalysis = false - } #endif if let url = subtitlesURL, !url.isEmpty { diff --git a/Sora/Views/LibraryView/LibraryView.swift b/Sora/Views/LibraryView/LibraryView.swift index 94ea459..32cae84 100644 --- a/Sora/Views/LibraryView/LibraryView.swift +++ b/Sora/Views/LibraryView/LibraryView.swift @@ -102,9 +102,9 @@ struct LibraryView: View { ForEach(libraryManager.bookmarks) { item in if let module = moduleManager.modules.first(where: { $0.id.uuidString == item.moduleId }) { Button(action: { - selectedBookmark = item - isDetailActive = true - }) { + selectedBookmark = item + isDetailActive = true + }) { VStack(alignment: .leading) { ZStack { KFImage(URL(string: item.imageUrl)) @@ -148,21 +148,21 @@ struct LibraryView: View { } .padding(.horizontal, 20) NavigationLink( - destination: Group { - if let bookmark = selectedBookmark, - let module = moduleManager.modules.first(where: { $0.id.uuidString == bookmark.moduleId }) { - MediaInfoView(title: bookmark.title, - imageUrl: bookmark.imageUrl, - href: bookmark.href, - module: module) - } else { - Text("No Data Available") - } - }, - isActive: $isDetailActive - ) { - EmptyView() - } + destination: Group { + if let bookmark = selectedBookmark, + let module = moduleManager.modules.first(where: { $0.id.uuidString == bookmark.moduleId }) { + MediaInfoView(title: bookmark.title, + imageUrl: bookmark.imageUrl, + href: bookmark.href, + module: module) + } else { + Text("No Data Available") + } + }, + isActive: $isDetailActive + ) { + EmptyView() + } .onAppear { updateOrientation() } @@ -360,7 +360,6 @@ struct ContinueWatchingCell: View { if totalTime > 0 { let ratio = lastPlayedTime / totalTime - // Clamp ratio between 0 and 1: currentProgress = max(0, min(ratio, 1)) } else { currentProgress = max(0, min(item.progress, 1)) diff --git a/Sora/Views/MediaInfoView/MediaInfoView.swift b/Sora/Views/MediaInfoView/MediaInfoView.swift index cf8bc54..ec0894f 100644 --- a/Sora/Views/MediaInfoView/MediaInfoView.swift +++ b/Sora/Views/MediaInfoView/MediaInfoView.swift @@ -647,7 +647,6 @@ struct MediaInfoView: View { } else { jsController.fetchStreamUrl(episodeUrl: href, softsub: module.metadata.softsub == true, module: module, completion: completion) } - } catch { self.handleStreamFailure(error: error) DispatchQueue.main.async { @@ -718,7 +717,7 @@ struct MediaInfoView: View { headers = streams[index]["headers"] as? [String:String] ?? [:] index += 1 } - + alert.addAction(UIAlertAction(title: title, style: .default) { _ in self.playStream(url: streamUrl, fullURL: fullURL, subtitles: subtitles,headers: headers) diff --git a/Sora/Views/SearchView.swift b/Sora/Views/SearchView.swift index 0f1a84a..fbd31ce 100644 --- a/Sora/Views/SearchView.swift +++ b/Sora/Views/SearchView.swift @@ -316,13 +316,11 @@ struct SearchBar: View { .background(Color(.systemGray6)) .cornerRadius(8) .onChange(of: text){newValue in - debounceTimer?.invalidate() - // Start a new timer to wait before performing the action + debounceTimer?.invalidate() debounceTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in - // Perform the action after the delay (debouncing) - onSearchButtonClicked() - } - } + onSearchButtonClicked() + } + } .overlay( HStack { Image(systemName: "magnifyingglass") diff --git a/Sora/Views/SettingsView/SettingsSubViews/SettingsViewPlayer.swift b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewPlayer.swift index 7bce329..f62b368 100644 --- a/Sora/Views/SettingsView/SettingsSubViews/SettingsViewPlayer.swift +++ b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewPlayer.swift @@ -74,27 +74,6 @@ struct SettingsViewPlayer: View { } } - Section(header: Text("Progress bar Marker Color")) { - ColorPicker("Segments Color", selection: Binding( - get: { - if let data = UserDefaults.standard.data(forKey: "segmentsColorData"), - let uiColor = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? UIColor { - return Color(uiColor) - } - return .yellow - }, - set: { newColor in - let uiColor = UIColor(newColor) - if let data = try? NSKeyedArchiver.archivedData( - withRootObject: uiColor, - requiringSecureCoding: false - ) { - UserDefaults.standard.set(data, forKey: "segmentsColorData") - } - } - )) - } - Section(header: Text("Skip Settings"), footer : Text("Double tapping the screen on it's sides will skip with the short tap setting.")) { HStack { Text("Tap Skip:")