Skip to content

Commit

Permalink
Fixed some minor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Auties00 committed Sep 28, 2024
1 parent 9152743 commit 282a8b6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 99 deletions.
4 changes: 2 additions & 2 deletions Artemis.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 15;
CURRENT_PROJECT_VERSION = 16;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = 25U5GYGLQC;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -939,7 +939,7 @@
CODE_SIGN_ENTITLEMENTS = "";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 15;
CURRENT_PROJECT_VERSION = 16;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = 25U5GYGLQC;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "EpisodePlayer.saveProgress(last:):EpisodePlayer.swift">
<PersistentStrings>
<PersistentString
value = "self.player?.currentTime().seconds ">
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "AnimeController.getFairplayContentIdentifier(episodeId:):AnimeController.swift">
<PersistentStrings>
Expand Down Expand Up @@ -77,12 +85,7 @@
</PersistentStrings>
</ContextState>
<ContextState
contextName = "ApiController.sendRequest&lt;&#x3c4;_0_0&gt;(method:url:path:data:log:):ApiController.swift">
<PersistentStrings>
<PersistentString
value = "error">
</PersistentString>
</PersistentStrings>
contextName = "closure #1 in closure #1 in PlayerViewController.contentKeySession(_:didProvide:):EpisodePlayerView.swift">
</ContextState>
<ContextState
contextName = "closure #2 in PlayerViewController.contentKeySession(_:didProvide:):EpisodePlayerView.swift">
Expand Down Expand Up @@ -127,15 +130,20 @@
contextName = "closure #1 in closure #1 in TabNavigationView.body.getter:NavigationView.swift">
</ContextState>
<ContextState
contextName = "closure #1 in FairplayContentKeySessionHandler.handleOnlineFairplay(contentIdentifier:loadingRequest:):Fairplay.swift">
contextName = "ApiController.sendRequest&lt;&#x3c4;_0_0&gt;(method:url:path:data:log:):ApiController.swift">
<PersistentStrings>
<PersistentString
value = "String(data: certificateDecodedData, encoding: .utf8)">
value = "error">
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "closure #1 in closure #1 in PlayerViewController.contentKeySession(_:didProvide:):EpisodePlayerView.swift">
contextName = "closure #1 in FairplayContentKeySessionHandler.handleOnlineFairplay(contentIdentifier:loadingRequest:):Fairplay.swift">
<PersistentStrings>
<PersistentString
value = "String(data: certificateDecodedData, encoding: .utf8)">
</PersistentString>
</PersistentStrings>
</ContextState>
<ContextState
contextName = "closure #1 in closure #1 in LibraryController.downloadEpisode(episode:handler:) [inlined]:LibraryController.swift">
Expand Down Expand Up @@ -235,13 +243,13 @@
contextName = "DRMResponse.init(from:):DRMResponse.swift">
<PersistentStrings>
<PersistentString
value = "Data(base64Encoded: base64Data)">
value = "String(data: base64Data, encoding: .ascii)">
</PersistentString>
<PersistentString
value = "String(data: base64Data, encoding: .utf8)">
</PersistentString>
<PersistentString
value = "String(data: base64Data, encoding: .ascii)">
value = "Data(base64Encoded: base64Data)">
</PersistentString>
</PersistentStrings>
</ContextState>
Expand Down
10 changes: 9 additions & 1 deletion Artemis/Controller/AccountController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,15 @@ class AccountController {
}

withMutation(keyPath: \.dashboard) {
continueWatchingBucket.contentList.removeAll(where: { $0.id == episode.id || $0.parentId == episode.parentId } )
continueWatchingBucket.contentList.removeAll(where: {
guard let continueWatchingEpisode = $0.wrappedValue as? Episode else {
return false
}

return continueWatchingEpisode.id == episode.id
|| continueWatchingEpisode.episodeInformation?.seasonId == episode.episodeInformation?.seasonId
|| continueWatchingEpisode.episodeInformation?.season?.series?.id == episode.episodeInformation?.season?.series?.id
})
continueWatchingBucket.contentList.insert(.episode(episode), at: 0)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Artemis/Controller/LibraryController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class LibraryController {
downloadTask.resume()
}

// POSSIBLE IMPROVEMENT: Find a way to determine if updaring the entry is necessary
// POSSIBLE IMPROVEMENT: Find a way to determine if updating the entry is necessary
private func saveDownload(downloadEntry: DownloadableEntry) async throws {
guard let downloadedEntry = try await createDownload(downloadEntry: downloadEntry) else {
return
Expand Down
56 changes: 25 additions & 31 deletions Artemis/View/Player/EpisodePlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import AVKit
import UIKit

class EpisodePlayer: AVPlayerViewController {
private static let heartbeatInterval = 15_000
private static let heartbeatInterval: Double = 15

private let playerId: String
private var episodable: (any Episodable)!
Expand All @@ -32,7 +32,6 @@ class EpisodePlayer: AVPlayerViewController {
private var statusObserver: NSObject?
private var languageObserver: (any NSObjectProtocol)?
private var watchProgressObserver: Any?
private var watchProgressLastUpdate: Int64?

// Couldn't find any other way to present the AVVideoPlayer full screen as the Apple TV app does
// fullScreenCover doesn't have the same effect
Expand Down Expand Up @@ -76,7 +75,6 @@ class EpisodePlayer: AVPlayerViewController {
return
}

self.watchProgressLastUpdate = nil
if(self.nextEpisodes.isEmpty) {
if let response = try? await self.animeController.getAdjacentEpisodes(id: self.episode.id) {
if let nextEpisodes = response.following {
Expand All @@ -100,29 +98,30 @@ class EpisodePlayer: AVPlayerViewController {
await beautifyPlayerItem(playerItem: playerItem)

if(self.player == nil) {
let player = AVPlayer(playerItem: playerItem)
let interval = CMTime(seconds: 15, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
self.watchProgressObserver = player.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main) { time in
Task { [weak self] in
await self?.saveProgress(last: false)
}
}

let player = AVQueuePlayer(playerItem: playerItem)
player.actionAtItemEnd = .none
player.appliesMediaSelectionCriteriaAutomatically = true
await MainActor.run {
self.player = player
}
NotificationCenter.default.addObserver(self, selector: #selector(onNext), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
}else {
nextButton?.removeFromSuperview()
self.player?.replaceCurrentItem(with: playerItem)
}

if restoreProgress, let watchProgress = episode.watchProgress {
await self.player?.seek(to: CMTime(value: Int64(watchProgress), timescale: 1))
}

if(watchProgressObserver == nil) {
let interval = CMTime(seconds: EpisodePlayer.heartbeatInterval, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
self.watchProgressObserver = self.player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main) { time in
Task { [weak self] in
await self?.saveProgress(last: false)
}
}
}

let audioSession = AVAudioSession.sharedInstance()
try? audioSession.setCategory(.playback)
try? audioSession.setActive(true, options: [])
Expand Down Expand Up @@ -260,7 +259,6 @@ class EpisodePlayer: AVPlayerViewController {
seriesItem.value = self.episodable.parentTitle as NSString
seriesItem.locale = Locale.current
playerItem.externalMetadata.append(seriesItem)

}

private func configurePlayerItemLocale(playerItem: AVPlayerItem) async {
Expand Down Expand Up @@ -424,8 +422,15 @@ class EpisodePlayer: AVPlayerViewController {
return
}

nextButton?.removeFromSuperview()
if let queuePlayer = self.player as? AVQueuePlayer {
queuePlayer.removeAllItems()
} else {
self.player?.pause()
self.player = nil
}

Task { [weak self] in
self?.player?.pause()
await MainActor.run {
if let nextEpisode = self?.nextEpisodes.removeFirst() {
self?.episode = nextEpisode
Expand Down Expand Up @@ -462,26 +467,15 @@ class EpisodePlayer: AVPlayerViewController {
return nil
}

private nonisolated func saveProgress(last: Bool) async {
let now = Date.now.millisecondsSince1970
if !last, let watchProgressLastUpdate = await self.watchProgressLastUpdate, now - watchProgressLastUpdate < EpisodePlayer.heartbeatInterval {
return
}

await MainActor.run {
self.watchProgressLastUpdate = now
}

guard let playerItem = await self.player?.currentItem else {
private func saveProgress(last: Bool) async {
guard let currentTime = self.player?.currentTime().seconds, currentTime.isFinite else {
return
}

let progress = Int(await self.player?.currentTime().seconds ?? 0)
await self.episode.watchProgress = Int(progress)
await self.episode.watchedAt = Date.now.millisecondsSince1970

let episodeId = await self.episode.id
try? await animeController.saveWatchProgress(cid: playerId, id: episodeId, progress: progress, last: last)
self.episode.watchProgress = Int(currentTime)
self.episode.watchedAt = Date.now.millisecondsSince1970
let episodeId = self.episode.id
try? await animeController.saveWatchProgress(cid: playerId, id: episodeId, progress: Int(currentTime), last: last)
}

private func showError(error: String) {
Expand Down
53 changes: 0 additions & 53 deletions artemis_source.json

This file was deleted.

0 comments on commit 282a8b6

Please sign in to comment.