Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
kinda added observer to the experimental player
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed Sep 2, 2024
1 parent 119ca2d commit edc3fcf
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 21 deletions.
Binary file not shown.
22 changes: 11 additions & 11 deletions Ryu/Swift - Views/Anime Info/AnimeDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ class AnimeDetailViewController: UITableViewController, WKNavigationDelegate, GC
print("No caption URL selected")
return
}
self.openHiAnimeExperimental(url: sourceURL, subURL: selectedCaptionURL)
self.openHiAnimeExperimental(url: sourceURL, subURL: selectedCaptionURL, cell: cell, fullURL: fullURL)
}
}
}
Expand Down Expand Up @@ -669,7 +669,7 @@ class AnimeDetailViewController: UITableViewController, WKNavigationDelegate, GC
case "GoGoAnime":
srcURL = self.extractIframeSourceURL(from: htmlString)
case "ZoroTv":
self.extractIframeAndGetM3U8URL(from: htmlString) { [weak self] result in
self.extractIframeAndGetM3U8URL(from: htmlString, cell: cell, fullURL: fullURL) { [weak self] result in
guard let self = self else { return }
guard let m3u8URL = result else {
print("Error extracting m3u8 URL")
Expand Down Expand Up @@ -718,7 +718,7 @@ class AnimeDetailViewController: UITableViewController, WKNavigationDelegate, GC
}.resume()
}

func extractIframeAndGetM3U8URL(from htmlString: String, completion: @escaping (URL?) -> Void) {
func extractIframeAndGetM3U8URL(from htmlString: String, cell: EpisodeCell, fullURL: String, completion: @escaping (URL?) -> Void) {
do {
let doc = try SwiftSoup.parse(htmlString)

Expand Down Expand Up @@ -756,7 +756,7 @@ class AnimeDetailViewController: UITableViewController, WKNavigationDelegate, GC

if selectedPlayer == "Experimental" {
DispatchQueue.main.async {
self.openVideo(url: m3u8URL)
self.openVideo(url: m3u8URL, cell: cell, fullURL: fullURL)
}
} else {
self.loadQualityOptions(from: m3u8URL) { success, error in
Expand Down Expand Up @@ -791,9 +791,9 @@ class AnimeDetailViewController: UITableViewController, WKNavigationDelegate, GC
}
}

func openVideo(url: URL) {
let videoTitle = animeTitle
let viewController = CustomPlayerView(videoTitle: videoTitle ?? "", videoURL: url)
func openVideo(url: URL, cell: EpisodeCell, fullURL: String) {
let videoTitle = animeTitle!
let viewController = CustomPlayerView(videoTitle: videoTitle, videoURL: url, cell: cell, fullURL: fullURL)
viewController.modalPresentationStyle = .fullScreen
self.present(viewController, animated: true, completion: nil)
}
Expand Down Expand Up @@ -1377,7 +1377,7 @@ class AnimeDetailViewController: UITableViewController, WKNavigationDelegate, GC
openInExternalPlayer(player: player, url: sourceURL)
case "Experimental":
let videoTitle = animeTitle
let viewController = CustomPlayerView(videoTitle: videoTitle ?? "", videoURL: sourceURL)
let viewController = CustomPlayerView(videoTitle: videoTitle ?? "", videoURL: sourceURL, cell: cell, fullURL: fullURL)
viewController.modalPresentationStyle = .fullScreen
self.present(viewController, animated: true, completion: nil)
default:
Expand Down Expand Up @@ -1413,9 +1413,9 @@ class AnimeDetailViewController: UITableViewController, WKNavigationDelegate, GC
}
}

func openHiAnimeExperimental(url: URL, subURL: URL) {
let videoTitle = animeTitle
let viewController = CustomPlayerView(videoTitle: videoTitle ?? "", videoURL: url, subURL: subURL)
func openHiAnimeExperimental(url: URL, subURL: URL, cell: EpisodeCell, fullURL: String) {
let videoTitle = animeTitle!
let viewController = CustomPlayerView(videoTitle: videoTitle, videoURL: url, cell: cell, fullURL: fullURL)
viewController.modalPresentationStyle = .fullScreen
self.present(viewController, animated: true, completion: nil)
}
Expand Down
75 changes: 71 additions & 4 deletions Ryu/Utils/Player/Custom Player/CustomPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class CustomVideoPlayerView: UIView, AVPictureInPictureControllerDelegate {
private var subtitlesURL: URL?
private var originalBrightness: CGFloat = UIScreen.main.brightness
private var isFullBrightness = false
private var cell: EpisodeCell
private var fullURL: String
private var animeDetailsViewController: AnimeDetailViewController?

private var subtitles: [SubtitleCue] = []
private var subtitleTimer: Timer?
Expand Down Expand Up @@ -172,6 +175,8 @@ class CustomVideoPlayerView: UIView, AVPictureInPictureControllerDelegate {
}()

override init(frame: CGRect) {
self.cell = EpisodeCell()
self.fullURL = ""
super.init(frame: frame)
setupPlayer()
setupUI()
Expand All @@ -180,13 +185,19 @@ class CustomVideoPlayerView: UIView, AVPictureInPictureControllerDelegate {
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
self.cell = EpisodeCell()
self.fullURL = ""
super.init(coder: coder)
setupPlayer()
setupUI()
setupGestures()
updateSubtitleAppearance()
}

deinit {
NotificationCenter.default.removeObserver(self)
if let timeObserverToken = timeObserverToken {
player?.removeTimeObserver(timeObserverToken)
if let token = timeObserverToken {
player?.removeTimeObserver(token)
}
}

Expand Down Expand Up @@ -327,7 +338,7 @@ class CustomVideoPlayerView: UIView, AVPictureInPictureControllerDelegate {
playerLayer?.frame = self.bounds
}

func setVideo(url: URL, title: String, subURL: URL? = nil) {
func setVideo(url: URL, title: String, subURL: URL? = nil, cell: EpisodeCell, fullURL: String) {
self.videoTitle = title
titleLabel.text = title
self.baseURL = url.deletingLastPathComponent()
Expand Down Expand Up @@ -360,6 +371,8 @@ class CustomVideoPlayerView: UIView, AVPictureInPictureControllerDelegate {
subtitleTimer?.invalidate()
subtitleTimer = nil
}

addPeriodicTimeObserver()
}

func play() {
Expand Down Expand Up @@ -446,6 +459,60 @@ class CustomVideoPlayerView: UIView, AVPictureInPictureControllerDelegate {
}
}

private func addPeriodicTimeObserver() {
let interval = CMTime(seconds: 1.0, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
timeObserverToken = player?.addPeriodicTimeObserver(forInterval: interval, queue: .main) { [weak self] time in
guard let self = self,
let currentItem = self.player?.currentItem,
currentItem.duration.seconds.isFinite else {
return
}

let currentTime = time.seconds
let duration = currentItem.duration.seconds
let progress = currentTime / duration
let remainingTime = duration - currentTime

self.cell.updatePlaybackProgress(progress: Float(progress), remainingTime: remainingTime)

UserDefaults.standard.set(currentTime, forKey: "lastPlayedTime_\(self.fullURL)")
UserDefaults.standard.set(duration, forKey: "totalTime_\(self.fullURL)")

let episodeNumber = Int(self.cell.episodeNumber) ?? 0
let selectedMediaSource = UserDefaults.standard.string(forKey: "selectedMediaSource") ?? "AnimeWorld"

let continueWatchingItem = ContinueWatchingItem(
animeTitle: self.animeDetailsViewController?.animeTitle ?? "Unknown Anime",
episodeTitle: "Ep. \(episodeNumber)",
episodeNumber: episodeNumber,
imageURL: self.animeDetailsViewController?.imageUrl ?? "",
fullURL: self.fullURL,
lastPlayedTime: currentTime,
totalTime: duration,
source: selectedMediaSource
)
ContinueWatchingManager.shared.saveItem(continueWatchingItem)

if remainingTime < 120 && !(self.animeDetailsViewController!.hasSentUpdate) {
let cleanedTitle = self.animeDetailsViewController?.cleanTitle(self.animeDetailsViewController?.animeTitle ?? "Unknown Anime")

self.animeDetailsViewController?.fetchAnimeID(title: cleanedTitle ?? "Title") { animeID in
let aniListMutation = AniListMutation()
aniListMutation.updateAnimeProgress(animeId: animeID, episodeNumber: Int(self.cell.episodeNumber) ?? 0) { result in
switch result {
case .success():
print("Successfully updated anime progress.")
case .failure(let error):
print("Failed to update anime progress: \(error.localizedDescription)")
}
}

self.animeDetailsViewController?.hasSentUpdate = true
}
}
}
}

private func updatePlayPauseButton() {
let isPlaying = player?.rate != 0
let imageName = isPlaying ? "pause.fill" : "play.fill"
Expand Down
11 changes: 9 additions & 2 deletions Ryu/Utils/Player/Custom Player/CustomPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ class CustomPlayerView: UIViewController {
private var videoTitle: String?
private var videoURL: URL?
private var subURL: URL?
private var cell: EpisodeCell
private var fullURL: String

weak var delegate: CustomPlayerViewDelegate?

init(videoTitle: String, videoURL: URL, subURL: URL? = nil) {
init(videoTitle: String, videoURL: URL, subURL: URL? = nil, cell: EpisodeCell, fullURL: String) {
self.videoTitle = videoTitle
self.videoURL = videoURL
self.subURL = subURL
self.cell = cell
self.fullURL = fullURL
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
self.cell = EpisodeCell()
self.fullURL = ""
super.init(coder: coder)
}

Expand All @@ -47,7 +54,7 @@ class CustomPlayerView: UIViewController {
])

if let videoURL = videoURL {
playerView.setVideo(url: videoURL, title: videoTitle ?? "", subURL: subURL)
playerView.setVideo(url: videoURL, title: videoTitle ?? "", subURL: subURL, cell: cell, fullURL: fullURL)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Ryu/Utils/Player/Sources/ExternalVideoPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class ExternalVideoPlayer: UIViewController, WKNavigationDelegate, WKScriptMessa
self.animeDetailsViewController?.openInExternalPlayer(player: selectedPlayer, url: url)
} else if selectedPlayer == "Experimental" {
let videoTitle = self.animeDetailsViewController?.animeTitle ?? "Anime"
let customPlayerVC = CustomPlayerView(videoTitle: videoTitle, videoURL: url)
let customPlayerVC = CustomPlayerView(videoTitle: videoTitle, videoURL: url, cell: self.cell, fullURL: self.fullURL)
customPlayerVC.modalPresentationStyle = .fullScreen
customPlayerVC.delegate = self
self.present(customPlayerVC, animated: true, completion: nil)
Expand Down
2 changes: 1 addition & 1 deletion Ryu/Utils/Player/Sources/ExternalVideoPlayer3rb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class ExternalVideoPlayer3rb: UIViewController, GCKRemoteMediaClientListener {
self.animeDetailsViewController?.openInExternalPlayer(player: selectedPlayer, url: url)
} else if selectedPlayer == "Experimental" {
let videoTitle = self.animeDetailsViewController?.animeTitle ?? "Anime"
let customPlayerVC = CustomPlayerView(videoTitle: videoTitle, videoURL: url)
let customPlayerVC = CustomPlayerView(videoTitle: videoTitle, videoURL: url, cell: self.cell, fullURL: self.fullURL)
customPlayerVC.modalPresentationStyle = .fullScreen
customPlayerVC.delegate = self
self.present(customPlayerVC, animated: true, completion: nil)
Expand Down
2 changes: 1 addition & 1 deletion Ryu/Utils/Player/Sources/ExternalVideoPlayerJK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class ExternalVideoPlayerJK: UIViewController, WKNavigationDelegate, GCKRemoteMe
self.animeDetailsViewController?.openInExternalPlayer(player: selectedPlayer, url: url)
} else if selectedPlayer == "Experimental" {
let videoTitle = self.animeDetailsViewController?.animeTitle ?? "Anime"
let customPlayerVC = CustomPlayerView(videoTitle: videoTitle, videoURL: url)
let customPlayerVC = CustomPlayerView(videoTitle: videoTitle, videoURL: url, cell: self.cell, fullURL: self.fullURL)
customPlayerVC.modalPresentationStyle = .fullScreen
customPlayerVC.delegate = self
self.present(customPlayerVC, animated: true, completion: nil)
Expand Down
2 changes: 1 addition & 1 deletion Ryu/Utils/Player/Sources/ExternalVideoPlayerKura.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class ExternalVideoPlayerKura: UIViewController, GCKRemoteMediaClientListener {
self.animeDetailsViewController?.openInExternalPlayer(player: selectedPlayer, url: url)
} else if selectedPlayer == "Experimental" {
let videoTitle = self.animeDetailsViewController?.animeTitle ?? "Anime"
let customPlayerVC = CustomPlayerView(videoTitle: videoTitle, videoURL: url)
let customPlayerVC = CustomPlayerView(videoTitle: videoTitle, videoURL: url, cell: self.cell, fullURL: self.fullURL)
customPlayerVC.modalPresentationStyle = .fullScreen
customPlayerVC.delegate = self
self.present(customPlayerVC, animated: true, completion: nil)
Expand Down

0 comments on commit edc3fcf

Please sign in to comment.