will it work?

This commit is contained in:
Francesco 2025-06-01 11:57:22 +02:00
parent 8bb221cd36
commit 1d10d542a0

View file

@ -60,6 +60,9 @@ struct MediaInfoView: View {
@State private var customAniListID: Int?
@State private var showStreamLoadingView: Bool = false
@State private var currentStreamTitle: String = ""
private enum StreamAction { case play, download }
@State private var streamAction: StreamAction = .play
@State private var pendingEpisodeLink: EpisodeLink? = nil
@State private var activeFetchID: UUID? = nil
@Environment(\.dismiss) private var dismiss
@ -374,21 +377,23 @@ struct MediaInfoView: View {
}
Button(action: {
if let ep = episodeLinks.first {
let downloadStatus = jsController.isEpisodeDownloadedOrInProgress(
showTitle: title,
episodeNumber: ep.number,
season: 1
)
if downloadStatus == .notDownloaded {
selectedEpisodeNumber = ep.number
fetchStream(href: ep.href)
DropManager.shared.showDrop(title: "Starting Download", subtitle: "", duration: 1.0, icon: UIImage(systemName: "arrow.down.circle"))
} else {
DropManager.shared.showDrop(title: "Already Downloaded", subtitle: "", duration: 1.0, icon: UIImage(systemName: "checkmark.circle"))
}
if let ep = episodeLinks.first {
let downloadStatus = jsController.isEpisodeDownloadedOrInProgress(
showTitle: title,
episodeNumber: ep.number,
season: 1
)
if downloadStatus == .notDownloaded {
streamAction = .download
pendingEpisodeLink = ep
selectedEpisodeNumber = ep.number
fetchStream(href: ep.href)
DropManager.shared.showDrop(title: "Starting Download", subtitle: "", duration: 1.0, icon: UIImage(systemName: "arrow.down.circle"))
} else {
DropManager.shared.showDrop(title: "Already Downloaded", subtitle: "", duration: 1.0, icon: UIImage(systemName: "checkmark.circle"))
}
}
}) {
HStack(spacing: 4) {
Image(systemName: "arrow.down.circle")
@ -1072,7 +1077,12 @@ struct MediaInfoView: View {
self.showStreamSelectionAlert(streams: sources, fullURL: href, subtitles: result.subtitles?.first, fetchID: fetchID)
} else if let streamUrl = sources[0]["streamUrl"] as? String {
let headers = sources[0]["headers"] as? [String: String]
self.playStream(url: streamUrl, fullURL: href, subtitles: result.subtitles?.first, headers: headers, fetchID: fetchID)
if streamAction == .download, let ep = pendingEpisodeLink, let urlObj = URL(string: streamUrl) {
let subtitleURL = result.subtitles?.first.flatMap { URL(string: $0) }
self.startEpisodeDownloadWithProcessedStream(episode: ep, url: urlObj, streamUrl: streamUrl, subtitleURL: subtitleURL)
} else {
self.playStream(url: streamUrl, fullURL: href, subtitles: result.subtitles?.first, headers: headers, fetchID: fetchID)
}
} else {
self.handleStreamFailure(error: nil)
}
@ -1080,7 +1090,13 @@ struct MediaInfoView: View {
if streams.count > 1 {
self.showStreamSelectionAlert(streams: streams, fullURL: href, subtitles: result.subtitles?.first, fetchID: fetchID)
} else {
self.playStream(url: streams[0], fullURL: href, subtitles: result.subtitles?.first, fetchID: fetchID)
let streamUrl = streams[0]
if streamAction == .download, let ep = pendingEpisodeLink, let urlObj = URL(string: streamUrl) {
let subtitleURL = result.subtitles?.first.flatMap { URL(string: $0) }
self.startEpisodeDownloadWithProcessedStream(episode: ep, url: urlObj, streamUrl: streamUrl, subtitleURL: subtitleURL)
} else {
self.playStream(url: streamUrl, fullURL: href, subtitles: result.subtitles?.first, fetchID: fetchID)
}
}
} else {
self.handleStreamFailure(error: nil)
@ -1185,10 +1201,18 @@ struct MediaInfoView: View {
alert.addAction(UIAlertAction(title: title, style: .default) { _ in
guard self.activeFetchID == fetchID else {
return
guard self.activeFetchID == fetchID else { return }
switch self.streamAction {
case .play:
self.playStream(url: streamUrl, fullURL: fullURL, subtitles: subtitles, headers: headers, fetchID: fetchID)
case .download:
if let ep = self.pendingEpisodeLink, let urlObj = URL(string: streamUrl) {
let subtitleURL = subtitles.flatMap { URL(string: $0) }
self.startEpisodeDownloadWithProcessedStream(episode: ep, url: urlObj, streamUrl: streamUrl, subtitleURL: subtitleURL)
}
}
self.playStream(url: streamUrl, fullURL: fullURL, subtitles: subtitles, headers: headers, fetchID: fetchID)
self.streamAction = .play
self.pendingEpisodeLink = nil
})
streamIndex += 1