Let's test Trakt Updates

This commit is contained in:
cranci1 2025-06-11 10:36:48 +02:00
parent 1a4b78e64e
commit 175f011d01
4 changed files with 99 additions and 36 deletions

View file

@ -31,13 +31,10 @@ class TraktMutation {
} }
enum ExternalIDType { enum ExternalIDType {
case imdb(String)
case tmdb(Int) case tmdb(Int)
var dictionary: [String: Any] { var dictionary: [String: Any] {
switch self { switch self {
case .imdb(let id):
return ["imdb": id]
case .tmdb(let id): case .tmdb(let id):
return ["tmdb": id] return ["tmdb": id]
} }

View file

@ -24,6 +24,9 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
let onWatchNext: () -> Void let onWatchNext: () -> Void
let aniListID: Int let aniListID: Int
var headers: [String:String]? = nil var headers: [String:String]? = nil
var tmdbID: Int? = nil
var isMovie: Bool = false
var seasonNumber: Int = 1
private var aniListUpdatedSuccessfully = false private var aniListUpdatedSuccessfully = false
private var aniListUpdateImpossible: Bool = false private var aniListUpdateImpossible: Bool = false
@ -1644,14 +1647,41 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
let remainingPercentage = (self.duration - self.currentTimeVal) / self.duration let remainingPercentage = (self.duration - self.currentTimeVal) / self.duration
if remainingPercentage < 0.1 && if remainingPercentage < 0.1 {
self.aniListID != 0 && if self.aniListID != 0 && !self.aniListUpdatedSuccessfully && !self.aniListUpdateImpossible {
!self.aniListUpdatedSuccessfully &&
!self.aniListUpdateImpossible
{
self.tryAniListUpdate() self.tryAniListUpdate()
} }
if let tmdbId = self.tmdbID {
let traktMutation = TraktMutation()
if self.isMovie {
traktMutation.markAsWatched(type: "movie", externalID: .tmdb(tmdbId)) { result in
switch result {
case .success:
Logger.shared.log("Successfully updated Trakt progress for movie", type: "General")
case .failure(let error):
Logger.shared.log("Failed to update Trakt progress: \(error.localizedDescription)", type: "Error")
}
}
} else {
traktMutation.markAsWatched(
type: "episode",
externalID: .tmdb(tmdbId),
episodeNumber: self.episodeNumber,
seasonNumber: self.seasonNumber
) { result in
switch result {
case .success:
Logger.shared.log("Successfully updated Trakt progress for episode \(self.episodeNumber)", type: "General")
case .failure(let error):
Logger.shared.log("Failed to update Trakt progress: \(error.localizedDescription)", type: "Error")
}
}
}
}
}
self.sliderHostingController?.rootView = MusicProgressSlider( self.sliderHostingController?.rootView = MusicProgressSlider(
value: Binding( value: Binding(
get: { max(0, min(self.sliderViewModel.sliderValue, self.duration)) }, get: { max(0, min(self.sliderViewModel.sliderValue, self.duration)) },
@ -1796,6 +1826,7 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
@objc func seekBackward() { @objc func seekBackward() {
let skipValue = UserDefaults.standard.double(forKey: "skipIncrement") let skipValue = UserDefaults.standard.double(forKey: "skipIncrement")
let finalSkip = skipValue > 0 ? skipValue : 10 let finalSkip = skipValue > 0 ? skipValue : 10
currentTimeVal = max(currentTimeVal - finalSkip, 0) currentTimeVal = max(currentTimeVal - finalSkip, 0)
player.seek(to: CMTime(seconds: currentTimeVal, preferredTimescale: 600)) { [weak self] finished in player.seek(to: CMTime(seconds: currentTimeVal, preferredTimescale: 600)) { [weak self] finished in
guard self != nil else { return } guard self != nil else { return }

View file

@ -20,7 +20,9 @@ class VideoPlayerViewController: UIViewController {
var aniListID: Int = 0 var aniListID: Int = 0
var headers: [String:String]? = nil var headers: [String:String]? = nil
var totalEpisodes: Int = 0 var totalEpisodes: Int = 0
var tmdbID: Int? = nil
var isMovie: Bool = false
var seasonNumber: Int = 1
var episodeNumber: Int = 0 var episodeNumber: Int = 0
var episodeImageUrl: String = "" var episodeImageUrl: String = ""
var mediaTitle: String = "" var mediaTitle: String = ""
@ -200,7 +202,8 @@ class VideoPlayerViewController: UIViewController {
let remainingPercentage = (duration - currentTime) / duration let remainingPercentage = (duration - currentTime) / duration
if remainingPercentage < 0.1 && self.aniListID != 0 { if remainingPercentage < 0.1 {
if self.aniListID != 0 {
let aniListMutation = AniListMutation() let aniListMutation = AniListMutation()
aniListMutation.updateAnimeProgress(animeId: self.aniListID, episodeNumber: self.episodeNumber) { result in aniListMutation.updateAnimeProgress(animeId: self.aniListID, episodeNumber: self.episodeNumber) { result in
switch result { switch result {
@ -211,6 +214,36 @@ class VideoPlayerViewController: UIViewController {
} }
} }
} }
if let tmdbId = self.tmdbID {
let traktMutation = TraktMutation()
if self.isMovie {
traktMutation.markAsWatched(type: "movie", externalID: .tmdb(tmdbId)) { result in
switch result {
case .success:
Logger.shared.log("Successfully updated Trakt progress for movie", type: "General")
case .failure(let error):
Logger.shared.log("Failed to update Trakt progress: \(error.localizedDescription)", type: "Error")
}
}
} else {
traktMutation.markAsWatched(
type: "episode",
externalID: .tmdb(tmdbId),
episodeNumber: self.episodeNumber,
seasonNumber: self.seasonNumber
) { result in
switch result {
case .success:
Logger.shared.log("Successfully updated Trakt progress for episode \(self.episodeNumber)", type: "General")
case .failure(let error):
Logger.shared.log("Failed to update Trakt progress: \(error.localizedDescription)", type: "Error")
}
}
}
}
}
} }
} }

View file

@ -1302,6 +1302,7 @@ struct MediaInfoView: View {
videoPlayerViewController.streamUrl = url videoPlayerViewController.streamUrl = url
videoPlayerViewController.fullUrl = fullURL videoPlayerViewController.fullUrl = fullURL
videoPlayerViewController.episodeNumber = selectedEpisodeNumber videoPlayerViewController.episodeNumber = selectedEpisodeNumber
videoPlayerViewController.seasonNumber = selectedSeason + 1
videoPlayerViewController.episodeImageUrl = selectedEpisodeImage videoPlayerViewController.episodeImageUrl = selectedEpisodeImage
videoPlayerViewController.mediaTitle = title videoPlayerViewController.mediaTitle = title
videoPlayerViewController.subtitles = subtitles ?? "" videoPlayerViewController.subtitles = subtitles ?? ""
@ -1350,6 +1351,7 @@ struct MediaInfoView: View {
episodeImageUrl: selectedEpisodeImage, episodeImageUrl: selectedEpisodeImage,
headers: headers ?? nil headers: headers ?? nil
) )
customMediaPlayer.seasonNumber = selectedSeason + 1
customMediaPlayer.modalPresentationStyle = .fullScreen customMediaPlayer.modalPresentationStyle = .fullScreen
Logger.shared.log("Opening custom media player with url: \(url)") Logger.shared.log("Opening custom media player with url: \(url)")