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

feat: Add now playing info for media player #1316

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
20 changes: 17 additions & 3 deletions kDrive/UI/View/Files/Preview/AudioCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ final class AudioCollectionViewCell: PreviewCollectionViewCell {
@IBOutlet var playButton: UIButton!
@IBOutlet var landscapePlayButton: UIButton!
@IBOutlet var iconHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var songTitleLabel: UILabel!
@IBOutlet weak var artistNameLabel: UILabel!

var driveFileManager: DriveFileManager!

Expand Down Expand Up @@ -75,14 +77,16 @@ final class AudioCollectionViewCell: PreviewCollectionViewCell {
super.prepareForReuse()
setControls(enabled: false)
singleTrackPlayer.reset()
songTitleLabel.text = "tre"
artistNameLabel.text = "tre"
iconImageView.image = nil
}

override func configureWith(file: File) {
// file should be safe for async work in the player
let frozenFile = file.freezeIfNeeded()
setUpPlayButtons()

Task {
setUpPlayButtons()
Task { @MainActor in
await singleTrackPlayer.setup(with: frozenFile)
setControls(enabled: true)
setupObservation()
Expand Down Expand Up @@ -142,6 +146,16 @@ final class AudioCollectionViewCell: PreviewCollectionViewCell {
self.positionSlider.maximumValue = sliderMaximum
}
.store(in: &cancellables)

singleTrackPlayer
.onCurrentTrackMetadata
.receive(on: DispatchQueue.main)
.sink { metadata in
self.iconImageView.image = metadata.artwork
self.artistNameLabel.text = metadata.artist
self.songTitleLabel.text = metadata.title
}
.store(in: &cancellables)
}

@MainActor func setUpPlayButtons() {
Expand Down
30 changes: 22 additions & 8 deletions kDrive/UI/View/Files/Preview/AudioCollectionViewCell.xib
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="23094" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_3" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22685"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23084"/>
<capability name="Named colors" minToolsVersion="9.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand All @@ -19,10 +19,10 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="JwS-de-Pn7">
<rect key="frame" x="24" y="246.5" width="458" height="334"/>
<rect key="frame" x="24" y="185.33333333333334" width="458" height="456.66666666666663"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="center" spacing="36" translatesAutoresizingMaskIntoConstraints="NO" id="RQC-MS-Xpl">
<rect key="frame" x="0.0" y="0.0" width="458" height="302"/>
<rect key="frame" x="0.0" y="0.0" width="458" height="424.66666666666669"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="music" translatesAutoresizingMaskIntoConstraints="NO" id="W9a-yf-gb4">
<rect key="frame" x="169" y="0.0" width="120" height="120"/>
Expand All @@ -31,8 +31,20 @@
<constraint firstAttribute="height" constant="120" id="SGq-zm-Qis"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Song title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="l45-mr-rI7" customClass="IKLabel" customModule="InfomaniakCoreUI">
<rect key="frame" x="182.33333333333334" y="155.99999999999997" width="93.666666666666657" height="27.666666666666657"/>
<fontDescription key="fontDescription" type="system" pointSize="23"/>
<color key="textColor" name="backgroundCardView"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Artist" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="I2u-ME-J8U" customClass="IKLabel" customModule="InfomaniakCoreUI">
<rect key="frame" x="206.33333333333334" y="219.66666666666666" width="45.666666666666657" height="23"/>
<fontDescription key="fontDescription" type="system" pointSize="19"/>
<color key="textColor" name="backgroundCardView"/>
<nil key="highlightedColor"/>
</label>
<stackView opaque="NO" contentMode="scaleToFill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="Stm-WL-vWh">
<rect key="frame" x="5" y="156" width="448" height="40"/>
<rect key="frame" x="5" y="278.66666666666663" width="448" height="40"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Q1t-cA-X5f">
<rect key="frame" x="0.0" y="0.0" width="40" height="40"/>
Expand Down Expand Up @@ -78,7 +90,7 @@
</subviews>
</stackView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nEX-wd-Ij8">
<rect key="frame" x="194" y="232" width="70" height="70"/>
<rect key="frame" x="194" y="354.66666666666663" width="70" height="70"/>
<constraints>
<constraint firstAttribute="height" constant="70" id="PVE-gP-cBs"/>
<constraint firstAttribute="width" constant="70" id="cfr-Lz-2Q8"/>
Expand Down Expand Up @@ -113,15 +125,17 @@
</constraints>
<size key="customSize" width="506" height="827"/>
<connections>
<outlet property="artistNameLabel" destination="I2u-ME-J8U" id="SAn-0a-1QQ"/>
<outlet property="elapsedTimeLabel" destination="8sR-5A-U2n" id="FNe-ZD-WTw"/>
<outlet property="iconHeightConstraint" destination="SGq-zm-Qis" id="4mQ-eH-uYF"/>
<outlet property="iconImageView" destination="W9a-yf-gb4" id="Pfh-2f-8LB"/>
<outlet property="landscapePlayButton" destination="Q1t-cA-X5f" id="OAx-pH-vod"/>
<outlet property="playButton" destination="nEX-wd-Ij8" id="uAY-1X-0fZ"/>
<outlet property="positionSlider" destination="43x-AW-wg9" id="2gc-jE-Usy"/>
<outlet property="remainingTimeLabel" destination="1Ga-6s-Xxf" id="Pl6-EM-Orh"/>
<outlet property="songTitleLabel" destination="l45-mr-rI7" id="NyL-Lf-eST"/>
</connections>
<point key="canvasLocation" x="421.73913043478262" y="320.42410714285711"/>
<point key="canvasLocation" x="420.8955223880597" y="320.25171624713954"/>
</collectionViewCell>
</objects>
<resources>
Expand Down
10 changes: 6 additions & 4 deletions kDriveCore/AudioPlayer/SingleTrackPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class SingleTrackPlayer {

private var playableFileName: String?

private var trackMetadata: TrackMetadata?
public private(set) var currentTrackMetadata: TrackMetadata?

// MARK: Player Observation

Expand All @@ -58,6 +58,7 @@ public final class SingleTrackPlayer {
public let onRemainingTimeChange = PassthroughSubject<String, Never>()
public let onPositionChange = PassthroughSubject<Float, Never>()
public let onPositionMaximumChange = PassthroughSubject<Float, Never>()
public let onCurrentTrackMetadata = PassthroughSubject<TrackMetadata, Never>()

var player: AVPlayer?

Expand Down Expand Up @@ -127,17 +128,17 @@ public final class SingleTrackPlayer {

if !playableFile.isLocalVersionOlderThanRemote {
let asset = AVAsset(url: playableFile.localUrl)
trackMetadata = extractTrackMetadata(from: asset)
player = AVPlayer(url: playableFile.localUrl)
onCurrentTrackMetadata.send(extractTrackMetadata(from: asset))
setUpObservers()
} else if let token = driveFileManager.apiFetcher.currentToken {
driveFileManager.apiFetcher.performAuthenticatedRequest(token: token) { token, _ in
if let token {
let url = Endpoint.download(file: playableFile).url
let headers = ["Authorization": "Bearer \(token.accessToken)"]
let asset = AVURLAsset(url: url, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
Task {
self.trackMetadata = self.extractTrackMetadata(from: asset)
Task { @MainActor in
self.onCurrentTrackMetadata.send(self.extractTrackMetadata(from: asset))
self.player = AVPlayer(playerItem: AVPlayerItem(asset: asset))
self.setUpObservers()
}
Expand Down Expand Up @@ -274,6 +275,7 @@ public final class SingleTrackPlayer {
self?.setNowPlayingPlaybackInfo()
}


if let currentItem = player.currentItem {
NotificationCenter.default.addObserver(
self,
Expand Down
8 changes: 4 additions & 4 deletions kDriveCore/AudioPlayer/TrackMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import UIKit

struct TrackMetadata {
let title: String
let artist: String
let artwork: UIImage?
public struct TrackMetadata {
public var title: String
public var artist: String
public var artwork: UIImage?
}
Loading