From 099e138eb50bec270cebbf29a5f244392e9b90b8 Mon Sep 17 00:00:00 2001 From: Stephen Byatt <47413006+stephenb10@users.noreply.github.com> Date: Wed, 23 Jun 2021 12:11:33 +1000 Subject: [PATCH] Refactored some force unwraps --- .../VideoPlayer/AudioView.swift | 10 --- .../InfoTabBarViewController.swift | 9 --- .../VideoPlayer/MediaInfoView.swift | 5 +- .../VideoPlayer/SubtitlesView.swift | 81 ------------------- .../VideoPlayerViewController.swift | 41 +++++----- 5 files changed, 26 insertions(+), 120 deletions(-) diff --git a/JellyfinPlayer tvOS/VideoPlayer/AudioView.swift b/JellyfinPlayer tvOS/VideoPlayer/AudioView.swift index c3e8d2aa..66e3a035 100644 --- a/JellyfinPlayer tvOS/VideoPlayer/AudioView.swift +++ b/JellyfinPlayer tvOS/VideoPlayer/AudioView.swift @@ -33,16 +33,6 @@ class AudioViewController: UIViewController { } - /* - // MARK: - Navigation - - // In a storyboard-based application, you will often want to do a little preparation before navigation - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - // Get the new view controller using segue.destination. - // Pass the selected object to the new view controller. - } - */ - } struct AudioView: View { diff --git a/JellyfinPlayer tvOS/VideoPlayer/InfoTabBarViewController.swift b/JellyfinPlayer tvOS/VideoPlayer/InfoTabBarViewController.swift index 3701521a..689ed895 100644 --- a/JellyfinPlayer tvOS/VideoPlayer/InfoTabBarViewController.swift +++ b/JellyfinPlayer tvOS/VideoPlayer/InfoTabBarViewController.swift @@ -72,15 +72,10 @@ class InfoTabBarViewController: UITabBarController, UIGestureRecognizerDelegate return } - print(item.title!) - switch item.title { case "Audio": if var height = audioViewController?.height { - print(height) - height += tabBarHeight - UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseOut) { [self] in videoPlayer?.infoViewContainer.frame = CGRect(x: pos.minX, y: pos.minY, width: pos.width, height: height) @@ -91,8 +86,6 @@ class InfoTabBarViewController: UITabBarController, UIGestureRecognizerDelegate break case "Info": if var height = mediaInfoController?.height { - print(height) - height += tabBarHeight UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseOut) { [self] in videoPlayer?.infoViewContainer.frame = CGRect(x: pos.minX, y: pos.minY, width: pos.width, height: height) @@ -103,8 +96,6 @@ class InfoTabBarViewController: UITabBarController, UIGestureRecognizerDelegate break case "Subtitles": if var height = subtitleViewController?.height{ - print(height) - height += tabBarHeight UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseOut) { [self] in videoPlayer?.infoViewContainer.frame = CGRect(x: pos.minX, y: pos.minY, width: pos.width, height: height) diff --git a/JellyfinPlayer tvOS/VideoPlayer/MediaInfoView.swift b/JellyfinPlayer tvOS/VideoPlayer/MediaInfoView.swift index 364a90eb..b210c572 100644 --- a/JellyfinPlayer tvOS/VideoPlayer/MediaInfoView.swift +++ b/JellyfinPlayer tvOS/VideoPlayer/MediaInfoView.swift @@ -45,7 +45,8 @@ struct MediaInfoView: View { HStack(spacing: 30) { VStack { - ImageView(src: item.type == "Episode" ? item.getSeriesPrimaryImage(maxWidth: 200) : item.getPrimaryImage(maxWidth: 200), bh: item.type == "Episode" ? item.getSeriesPrimaryImageBlurHash() : item.getPrimaryImageBlurHash()) .frame(width: 200, height: 300) + ImageView(src: item.type == "Episode" ? item.getSeriesPrimaryImage(maxWidth: 200) : item.getPrimaryImage(maxWidth: 200), bh: item.type == "Episode" ? item.getSeriesPrimaryImageBlurHash() : item.getPrimaryImageBlurHash()) + .frame(width: 200, height: 300) .cornerRadius(10) Spacer() } @@ -68,7 +69,7 @@ struct MediaInfoView: View { if item.type == "Episode" { Text("S\(item.parentIndexNumber ?? 0) • E\(item.indexNumber ?? 0)") - if let date = item.premiereDate! { + if let date = item.premiereDate { Text("•") Text(formatDate(date: date)) } diff --git a/JellyfinPlayer tvOS/VideoPlayer/SubtitlesView.swift b/JellyfinPlayer tvOS/VideoPlayer/SubtitlesView.swift index e2ddedbd..1ea20c2c 100644 --- a/JellyfinPlayer tvOS/VideoPlayer/SubtitlesView.swift +++ b/JellyfinPlayer tvOS/VideoPlayer/SubtitlesView.swift @@ -21,7 +21,6 @@ class SubtitlesViewController: UIViewController { } - func prepareSubtitleView(subtitleTracks: [Subtitle], selectedTrack: Int32, delegate: VideoPlayerSettingsDelegate) { let contentView = UIHostingController(rootView: SubtitleView(selectedTrack: selectedTrack, subtitleTrackArray: subtitleTracks, delegate: delegate)) @@ -33,86 +32,6 @@ class SubtitlesViewController: UIViewController { contentView.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true } - - // - // - // func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - // return subtitleTrackArray.count - // } - // - // func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - // let cell = UITableViewCell() - // let subtitle = subtitleTrackArray[indexPath.row] - // cell.textLabel?.text = subtitle.name - // - // let image = UIImage(systemName: "checkmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: (27), weight: .bold))?.withRenderingMode(.alwaysOriginal).withTintColor(.white) - // cell.imageView?.image = image - // - // if selectedTrack != subtitle.id { - // cell.imageView?.isHidden = true - // } - // else { - // selectedTrackCellRow = indexPath.row - // } - // - // return cell - // } - // - // func tableView(_ tableView: UITableView, didUpdateFocusIn context: UITableViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) { - // - // if let path = context.nextFocusedIndexPath { - // if path.row == selectedTrackCellRow { - // let cell : UITableViewCell = tableView.cellForRow(at: path)! - // cell.imageView?.image = cell.imageView?.image?.withTintColor(.black) - // } - // } - // - // if let path = context.previouslyFocusedIndexPath { - // if path.row == selectedTrackCellRow { - // let cell : UITableViewCell = tableView.cellForRow(at: path)! - // cell.imageView?.image = cell.imageView?.image?.withTintColor(.white) - // } - // } - // - // } - // - // - // func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - // let oldPath = IndexPath(row: selectedTrackCellRow, section: 0) - // if let oldCell : UITableViewCell = tableView.cellForRow(at: oldPath) { - // oldCell.imageView?.isHidden = true - // } - // - // let cell : UITableViewCell = tableView.cellForRow(at: indexPath)! - // cell.imageView?.isHidden = false - // cell.imageView?.image = cell.imageView?.image?.withTintColor(.black) - // - // selectedTrack = Int32(subtitleTrackArray[indexPath.row].id) - // selectedTrackCellRow = indexPath.row - //// infoTabBar?.videoPlayer?.subtitleTrackChanged(newTrackID: selectedTrack) - // print("setting new subtitle") - // tableView.deselectRow(at: indexPath, animated: false) - // - // } - // - // func numberOfSections(in tableView: UITableView) -> Int { - // return 1 - // } - // - // - - - - /* - // MARK: - Navigation - - // In a storyboard-based application, you will often want to do a little preparation before navigation - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - // Get the new view controller using segue.destination. - // Pass the selected object to the new view controller. - } - */ - } struct SubtitleView: View { diff --git a/JellyfinPlayer tvOS/VideoPlayer/VideoPlayerViewController.swift b/JellyfinPlayer tvOS/VideoPlayer/VideoPlayerViewController.swift index 146958df..3e6bcab2 100644 --- a/JellyfinPlayer tvOS/VideoPlayer/VideoPlayerViewController.swift +++ b/JellyfinPlayer tvOS/VideoPlayer/VideoPlayerViewController.swift @@ -140,6 +140,7 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, setupNowPlayingCC() + // Adjust subtitle size mediaPlayer.perform(Selector(("setTextRendererFontSize:")), with: 16) } @@ -154,10 +155,14 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, builder.setMaxBitrate(bitrate: maxBitrate) let profile = builder.buildProfile() - let playbackInfo = PlaybackInfoDto(userId: SessionManager.current.user.user_id!, maxStreamingBitrate: Int(maxBitrate), startTimeTicks: manifest.userData?.playbackPositionTicks ?? 0, deviceProfile: profile, autoOpenLiveStream: true) + guard let currentUser = SessionManager.current.user else { + return + } + + let playbackInfo = PlaybackInfoDto(userId: currentUser.user_id ?? "", maxStreamingBitrate: Int(maxBitrate), startTimeTicks: manifest.userData?.playbackPositionTicks ?? 0, deviceProfile: profile, autoOpenLiveStream: true) DispatchQueue.global(qos: .userInitiated).async { [self] in - MediaInfoAPI.getPostedPlaybackInfo(itemId: manifest.id!, userId: SessionManager.current.user.user_id!, maxStreamingBitrate: Int(maxBitrate), startTimeTicks: manifest.userData?.playbackPositionTicks ?? 0, autoOpenLiveStream: true, playbackInfoDto: playbackInfo) + MediaInfoAPI.getPostedPlaybackInfo(itemId: manifest.id!, userId: currentUser.user_id ?? "", maxStreamingBitrate: Int(maxBitrate), startTimeTicks: manifest.userData?.playbackPositionTicks ?? 0, autoOpenLiveStream: true, playbackInfoDto: playbackInfo) .sink(receiveCompletion: { result in print(result) }, receiveValue: { [self] response in @@ -167,16 +172,17 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, playSessionId = response.playSessionId ?? "" - let mediaSource = response.mediaSources!.first.self! + guard let mediaSource = response.mediaSources?.first.self else { + return + } let item = PlaybackItem() - let streamURL : URL? + let streamURL : URL // Item is being transcoded by request of server - if mediaSource.transcodingUrl != nil - { + if let transcodiungUrl = mediaSource.transcodingUrl { item.videoType = .transcode - streamURL = URL(string: "\(ServerEnvironment.current.server.baseURI!)\(mediaSource.transcodingUrl!)") + streamURL = URL(string: "\(ServerEnvironment.current.server.baseURI!)\(transcodiungUrl)")! } // Item will be directly played by the client else @@ -185,7 +191,7 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, streamURL = URL(string: "\(ServerEnvironment.current.server.baseURI!)/Videos/\(manifest.id!)/stream?Static=true&mediaSourceId=\(manifest.id!)&deviceId=\(SessionManager.current.deviceID)&api_key=\(SessionManager.current.accessToken)&Tag=\(mediaSource.eTag!)")! } - item.videoUrl = streamURL! + item.videoUrl = streamURL let disableSubtitleTrack = Subtitle(name: "None", id: -1, url: nil, delivery: .embed, codec: "") subtitleTrackArray.append(disableSubtitleTrack) @@ -223,7 +229,7 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, } // If no default audio tracks select the first one - if selectedAudioTrack == -1 && !audioTrackArray.isEmpty{ + if selectedAudioTrack == -1 && !audioTrackArray.isEmpty { selectedAudioTrack = audioTrackArray.first!.id } @@ -349,8 +355,8 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, } var nowPlayingInfo = [String: Any]() - - nowPlayingInfo[MPMediaItemPropertyTitle] = manifest.name! + + nowPlayingInfo[MPMediaItemPropertyTitle] = manifest.name ?? "Jellyfin Video" nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 0.0 nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = AVMediaType.video nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = runTicks @@ -359,10 +365,9 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, if let imageData = NSData(contentsOf: manifest.getPrimaryImage(maxWidth: 200)) { if let artworkImage = UIImage(data: imageData as Data) { let artwork = MPMediaItemArtwork.init(boundsSize: artworkImage.size, requestHandler: { (size) -> UIImage in - return artworkImage + return artworkImage }) - nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork - print("set artwork") + nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork } } @@ -571,14 +576,14 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, switch swipe.direction { case .left: print("swiped left") - mediaPlayer.pause() +// mediaPlayer.pause() // player.seek(to: CMTime(value: Int64(self.currentSeconds) + 10, timescale: 1)) - mediaPlayer.play() +// mediaPlayer.play() case .right: print("swiped right") - mediaPlayer.pause() +// mediaPlayer.pause() // player.seek(to: CMTime(value: Int64(self.currentSeconds) + 10, timescale: 1)) - mediaPlayer.play() +// mediaPlayer.play() case .up: break case .down: