Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/memory leak #269

Merged
merged 82 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
5370d1d
chore: add videos downloading bar view
eyatsenkoperpetio Dec 15, 2023
9e16130
chore: add view and logic
eyatsenkoperpetio Dec 15, 2023
32ad2ea
chore: add progress to download data
eyatsenkoperpetio Dec 18, 2023
f88fe44
chore: downloads view
eyatsenkoperpetio Dec 19, 2023
82bb2ff
chore: add strings and fix delete video notif
eyatsenkoperpetio Dec 20, 2023
f1d9872
chore: add total progress for bar
eyatsenkoperpetio Dec 20, 2023
f191a36
chore: changes for no nested list flow
eyatsenkoperpetio Dec 20, 2023
fdca041
fix: showing all downloaded item if open from current course
eyatsenkoperpetio Dec 21, 2023
e6a448a
fix: bar progress
eyatsenkoperpetio Dec 21, 2023
d2258ef
chore: add large file alert and show all downloads in download view
eyatsenkoperpetio Dec 26, 2023
68b6e17
chore: change logic and remove extra code
eyatsenkoperpetio Dec 27, 2023
4030157
refactor: remove extra code
eyatsenkoperpetio Dec 27, 2023
ae7db32
chore: remove extra code
eyatsenkoperpetio Dec 27, 2023
02ccce8
chore: add course storage, refactor
eyatsenkoperpetio Dec 28, 2023
1f40b96
chore: add new video formats
eyatsenkoperpetio Dec 28, 2023
ebc81cd
chore: and video to course block
eyatsenkoperpetio Dec 29, 2023
0b1a83e
chore: add new core data models
eyatsenkoperpetio Dec 29, 2023
451ee45
chore: download manager to async await
eyatsenkoperpetio Dec 29, 2023
4b5ea44
chore: add calculate total size download bar
eyatsenkoperpetio Jan 2, 2024
0f8a930
chore: hide total if zero
eyatsenkoperpetio Jan 3, 2024
16b956f
chore: clean up and fix transparent downloads bar when scroll
eyatsenkoperpetio Jan 3, 2024
930b1c8
chore: add new logic check is downloadable video
eyatsenkoperpetio Jan 3, 2024
7e6c218
chore: add download quality view
eyatsenkoperpetio Jan 4, 2024
24983d2
chore: add select quality for download
eyatsenkoperpetio Jan 5, 2024
6d9e3cb
chore: show alert about change download quality when downloading all …
eyatsenkoperpetio Jan 5, 2024
a868a69
chore: remove extra and add strings
eyatsenkoperpetio Jan 8, 2024
fcdb31d
Merge branch 'feat/166_progress_downloads' into feat/video_download_q…
eyatsenkoperpetio Jan 8, 2024
348b1bc
chore: remove empty line
eyatsenkoperpetio Jan 8, 2024
5257f37
chore: add cancel all download when move to background
eyatsenkoperpetio Jan 8, 2024
23b01a4
Merge branch 'develop' into feat/course_download_quality_bars_dev
eyatsenkoperpetio Jan 9, 2024
269a084
Merge branch 'develop' into feat/course_download_quality_bars_dev
rnr Jan 9, 2024
a1f9f1d
chore: add resume downloading
eyatsenkoperpetio Jan 9, 2024
f288e68
fix: remaining count in bar
eyatsenkoperpetio Jan 9, 2024
d50e4d1
chore: add new logic get quality video
eyatsenkoperpetio Jan 15, 2024
acfc9c8
chore: remove extra code
eyatsenkoperpetio Jan 15, 2024
c1d65e4
chore: improve download quality names
eyatsenkoperpetio Jan 15, 2024
2f8709a
Merge branch 'feat/course_download_quality_bars' into feat/course_dow…
eyatsenkoperpetio Jan 15, 2024
b5761cc
chore: new logic
eyatsenkoperpetio Jan 15, 2024
bb69379
chore: update logic
eyatsenkoperpetio Jan 15, 2024
a3972ad
chore: remove extra code
eyatsenkoperpetio Jan 15, 2024
02a59d0
chore: remove extra
eyatsenkoperpetio Jan 15, 2024
71460e1
chore: change strings
eyatsenkoperpetio Jan 15, 2024
209c1a0
chore: add string
eyatsenkoperpetio Jan 15, 2024
6298260
chore: show remaining files in download bar
eyatsenkoperpetio Jan 15, 2024
475328e
chore: verticals blocks downloadable count nested list
eyatsenkoperpetio Jan 15, 2024
7b30bf0
chore: count of files
eyatsenkoperpetio Jan 15, 2024
8b7723b
chore: add disable download when offline
eyatsenkoperpetio Jan 16, 2024
86c9d48
chore: remove empty line
eyatsenkoperpetio Jan 16, 2024
5f698fe
chore: show delete file when offline
eyatsenkoperpetio Jan 16, 2024
e18a7f1
chore: add Untitled title to download cell view
eyatsenkoperpetio Jan 17, 2024
320daf6
Merge branch 'develop' into feat/course_download_quality_bars_dev_alp…
eyatsenkoperpetio Jan 17, 2024
63512f7
Merge branch 'develop' into feat/course-download-quality-bars
eyatsenkoperpetio Jan 17, 2024
0c3a656
fix: tests
eyatsenkoperpetio Jan 17, 2024
19c3564
chore: PR issues and add accessibility labels
eyatsenkoperpetio Jan 18, 2024
6125d9a
chore: add accessibility identifiers
eyatsenkoperpetio Jan 18, 2024
6b635c2
chore: move video download quality view and refactor
eyatsenkoperpetio Jan 19, 2024
0b16dc6
chore: add cancel all for course
eyatsenkoperpetio Jan 19, 2024
8dc472b
chore: show large alert
eyatsenkoperpetio Jan 19, 2024
50b1649
chore: resolve PR comments
eyatsenkoperpetio Jan 19, 2024
ab1792a
chore: resolve PR comments
eyatsenkoperpetio Jan 19, 2024
e7c3b61
chore: resolve PR comments
eyatsenkoperpetio Jan 19, 2024
ccca6a7
fix: tests
eyatsenkoperpetio Jan 20, 2024
fb20cae
chore: resolve PR comments
eyatsenkoperpetio Jan 29, 2024
d5f904b
chore: resolve PR commnets
eyatsenkoperpetio Jan 29, 2024
318b1ab
chore: rename DownloadData to DownloadDataTask
eyatsenkoperpetio Jan 29, 2024
d4c88cf
chore: use app alert and tests
eyatsenkoperpetio Jan 29, 2024
d67ef53
chore: add confirmation alert when deleting video
eyatsenkoperpetio Jan 30, 2024
d705e59
chore: remove empty line
eyatsenkoperpetio Jan 30, 2024
57aad7f
chore: add strings
eyatsenkoperpetio Jan 30, 2024
baca08c
fix: line length
eyatsenkoperpetio Jan 31, 2024
3c099bf
Merge branch 'develop' into feat/course-download-quality-bars
eyatsenkoperpetio Jan 31, 2024
376368d
Merge branch 'develop' into feat/course-download-quality-bars
eyatsenkoperpetio Jan 31, 2024
81a4512
fix: repeat download
eyatsenkoperpetio Jan 31, 2024
22fa31f
chore: add alert when disable downloading
eyatsenkoperpetio Jan 31, 2024
f7f1ad5
chore: resolve PR commnets
eyatsenkoperpetio Jan 31, 2024
38679d4
fix: memory leak
forgotvas Feb 1, 2024
7c0163b
fix: controller setup
forgotvas Feb 1, 2024
add7f63
chore: refactor
forgotvas Feb 1, 2024
2729c3a
Merge remote-tracking branch 'upstream/develop' into fix/memory-leak
forgotvas Feb 1, 2024
6026d15
chore: refactor
forgotvas Feb 1, 2024
75c956a
chore: removed useless code
forgotvas Feb 5, 2024
cc1aad4
Merge remote-tracking branch 'upstream/develop' into fix/memory-leak
forgotvas Feb 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 61 additions & 40 deletions Course/Course/Presentation/Video/PlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct PlayerViewController: UIViewControllerRepresentable {
var seconds: ((Double) -> Void)

init(
videoURL: URL?,
videoURL: URL?,
controller: AVPlayerViewController,
bitrate: CGSize,
progress: @escaping ((Float) -> Void),
Expand All @@ -33,15 +33,12 @@ struct PlayerViewController: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> AVPlayerViewController {
controller.modalPresentationStyle = .fullScreen
controller.allowsPictureInPicturePlayback = true
controller.player = AVPlayer()

addPeriodicTimeObserver(
controller,
currentProgress: { progress, seconds in
self.progress(progress)
self.seconds(seconds)
}
)
let player = AVPlayer()
controller.player = player
context.coordinator.setPlayer(player) { progress, seconds in
self.progress(progress)
self.seconds(seconds)
}

do {
try AVAudioSession.sharedInstance().setCategory(.playback)
Expand All @@ -52,40 +49,64 @@ struct PlayerViewController: UIViewControllerRepresentable {
return controller
}

private func addPeriodicTimeObserver(
_ controller: AVPlayerViewController,
currentProgress: @escaping ((Float, Double) -> Void)
) {
let interval = CMTime(
seconds: 0.1,
preferredTimescale: CMTimeScale(NSEC_PER_SEC)
)

self.controller.player?.addPeriodicTimeObserver(forInterval: interval, queue: .main) { time in
var progress: Float = .zero
let currentSeconds = CMTimeGetSeconds(time)
guard let duration = controller.player?.currentItem?.duration else { return }
let totalSeconds = CMTimeGetSeconds(duration)
progress = Float(currentSeconds / totalSeconds)
currentProgress(progress, currentSeconds)
func updateUIViewController(_ playerController: AVPlayerViewController, context: Context) {
let asset = playerController.player?.currentItem?.asset as? AVURLAsset
if asset?.url.absoluteString != videoURL?.absoluteString {
let player = context.coordinator.player(from: playerController)
player?.replaceCurrentItem(with: AVPlayerItem(url: videoURL!))
player?.currentItem?.preferredMaximumResolution = videoResolution

context.coordinator.setPlayer(player) { progress, seconds in
self.progress(progress)
self.seconds(seconds)
}
}
}

func updateUIViewController(_ playerController: AVPlayerViewController, context: Context) {
DispatchQueue.main.async {
let asset = playerController.player?.currentItem?.asset as? AVURLAsset
if asset?.url.absoluteString != videoURL?.absoluteString {
if playerController.player == nil {
playerController.player = AVPlayer()
playerController.player?.allowsExternalPlayback = true
}
playerController.player?.replaceCurrentItem(with: AVPlayerItem(url: videoURL!))
playerController.player?.currentItem?.preferredMaximumResolution = videoResolution
addPeriodicTimeObserver(playerController, currentProgress: { progress, seconds in
self.progress(progress)
self.seconds(seconds)
})
func makeCoordinator() -> Coordinator {
Coordinator()
}

static func dismantleUIViewController(_ uiViewController: AVPlayerViewController, coordinator: Coordinator) {
coordinator.setPlayer(nil) { _, _ in }
}

class Coordinator {
var currentPlayer: AVPlayer?
var observer: Any?

func player(from playerController: AVPlayerViewController) -> AVPlayer? {
var player = playerController.player
if player == nil {
player = AVPlayer()
player?.allowsExternalPlayback = true
playerController.player = player
}
return player
}

func setPlayer(_ player: AVPlayer?, currentProgress: @escaping ((Float, Double) -> Void)) {
if let observer = observer {
currentPlayer?.removeTimeObserver(observer)
currentPlayer?.pause()
}

let interval = CMTime(
seconds: 0.1,
preferredTimescale: CMTimeScale(NSEC_PER_SEC)
)

observer = player?.addPeriodicTimeObserver(forInterval: interval, queue: .main) {[weak player] time in
var progress: Float = .zero
let currentSeconds = CMTimeGetSeconds(time)
guard let duration = player?.currentItem?.duration else { return }
let totalSeconds = CMTimeGetSeconds(duration)
progress = Float(currentSeconds / totalSeconds)
currentProgress(progress, currentSeconds)
}

currentPlayer = player

}
}
}
Loading