-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
448 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// CarPlay+NowPlaying.swift | ||
// iOS | ||
// | ||
// Created by Rasmus Krämer on 23.02.24. | ||
// | ||
|
||
import CarPlay | ||
import Defaults | ||
import SPPlayback | ||
|
||
internal extension CarPlayDelegate { | ||
func updateNowPlayingTemplate() -> NowPlayingObserver { | ||
CPNowPlayingTemplate.shared.updateNowPlayingButtons([ | ||
CPNowPlayingPlaybackRateButton() { _ in | ||
var rate = AudioPlayer.shared.playbackRate + Defaults[.playbackSpeedAdjustment] | ||
|
||
if rate > 2 { | ||
rate = 0.25 | ||
} | ||
|
||
AudioPlayer.shared.playbackRate = rate | ||
} | ||
]) | ||
|
||
CPNowPlayingTemplate.shared.upNextTitle = String(localized: "carPlay.chapters") | ||
CPNowPlayingTemplate.shared.isUpNextButtonEnabled = true | ||
|
||
let observer = NowPlayingObserver(interfaceController: interfaceController) | ||
CPNowPlayingTemplate.shared.add(observer) | ||
|
||
return observer | ||
} | ||
|
||
class NowPlayingObserver: NSObject, CPNowPlayingTemplateObserver { | ||
private var interfaceController: CPInterfaceController? | ||
|
||
init(interfaceController: CPInterfaceController?) { | ||
self.interfaceController = interfaceController | ||
} | ||
|
||
func nowPlayingTemplateUpNextButtonTapped(_ nowPlayingTemplate: CPNowPlayingTemplate) { | ||
Task { | ||
if AudioPlayer.shared.chapters.count > 1 { | ||
try await interfaceController?.pushTemplate(CPListTemplate(title: .init(localized: "carPlay.chapters.title"), sections: [ | ||
.init(items: AudioPlayer.shared.chapters.map { chapter in | ||
let item = CPListItem( | ||
text: chapter.title, | ||
detailText: (chapter.end - chapter.start).hoursMinutesSecondsString(includeSeconds: true, includeLabels: false)) | ||
|
||
item.handler = { _, completion in | ||
AudioPlayer.shared.seek(to: chapter.start) | ||
Task { | ||
try await self.interfaceController?.popTemplate(animated: true) | ||
completion() | ||
} | ||
} | ||
|
||
item.isPlaying = AudioPlayer.shared.chapter == chapter | ||
|
||
return item | ||
}) | ||
]), animated: true) | ||
} else { | ||
try await interfaceController?.presentTemplate(CPAlertTemplate(titleVariants: [.init(localized: "carPlay.chapters.empty.short"), .init(localized: "carPlay.chapters.empty")], actions: [ | ||
.init(title: .init(localized: "carPlay.chapters.dismiss"), style: .cancel) { _ in | ||
Task { | ||
try? await self.interfaceController?.dismissTemplate(animated: true) | ||
} | ||
} | ||
]), animated: true) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// CarPlay+Playback.swift | ||
// iOS | ||
// | ||
// Created by Rasmus Krämer on 23.02.24. | ||
// | ||
|
||
import CarPlay | ||
import SPBase | ||
import SPOffline | ||
import SPPlayback | ||
|
||
internal extension CarPlayDelegate { | ||
static func startPlayback(item: CPSelectableListItem, completion: () -> Void) { | ||
(item.userInfo! as! PlayableItem).startPlayback() | ||
NotificationCenter.default.post(name: Self.updateContentNotifications, object: nil) | ||
|
||
completion() | ||
} | ||
|
||
static func updateSections(_ sections: [CPListSection]) -> [CPListSection] { | ||
sections.map { | ||
CPListSection(items: $0.items.map { | ||
let item = $0 as! CPListItem | ||
let playableItem = $0.userInfo as! PlayableItem | ||
|
||
if AudioPlayer.shared.item == playableItem { | ||
item.isPlaying = true | ||
item.playbackProgress = OfflineManager.shared.requireProgressEntity(item: playableItem).progress | ||
} else { | ||
item.isPlaying = false | ||
} | ||
|
||
return item | ||
}, header: $0.header!, headerSubtitle: $0.headerSubtitle, headerImage: $0.headerImage, headerButton: $0.headerButton, sectionIndexTitle: $0.sectionIndexTitle) | ||
} | ||
} | ||
|
||
static let updateContentNotifications = NSNotification.Name("io.rfk.shelfplayer.carplay.update") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// | ||
// CarPlayDelegate.swift | ||
// iOS | ||
// | ||
// Created by Rasmus Krämer on 23.02.24. | ||
// | ||
|
||
import CarPlay | ||
import Defaults | ||
import SPBase | ||
import SPPlayback | ||
import SPOffline | ||
import SPOfflineExtended | ||
|
||
class CarPlayDelegate: UIResponder, CPTemplateApplicationSceneDelegate { | ||
// we need to keep a strong reference to this object | ||
internal var nowPlayingObserver: NowPlayingObserver? | ||
internal var interfaceController: CPInterfaceController? | ||
|
||
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController) { | ||
self.interfaceController = interfaceController | ||
|
||
Task { | ||
// Check if the user is logged in | ||
|
||
if !AudiobookshelfClient.shared.isAuthorized { | ||
try await interfaceController.presentTemplate(CPAlertTemplate(titleVariants: [String(localized: "carPlay.unauthorized.short"), String(localized: "carPlay.unauthorized")], actions: []), animated: true) | ||
|
||
return | ||
} | ||
|
||
nowPlayingObserver = updateNowPlayingTemplate() | ||
|
||
// Try to fetch libraries | ||
|
||
// if false, let libraries = try? await AudiobookshelfClient.shared.getLibraries() { | ||
try await interfaceController.setRootTemplate(try buildOfflineListTemplate(), animated: true) | ||
} | ||
} | ||
|
||
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didDisconnectInterfaceController interfaceController: CPInterfaceController) { | ||
self.interfaceController = nil | ||
nowPlayingObserver = nil | ||
} | ||
} | ||
|
||
extension CarPlayDelegate { | ||
private func buildOfflineListTemplate() async throws -> CPListTemplate { | ||
let template = CPListTemplate(title: String(localized: "carPlay.offline.title"), sections: try getOfflineSections(), assistantCellConfiguration: .init(position: .top, visibility: .always, assistantAction: .playMedia)) | ||
|
||
Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { _ in | ||
Task { @MainActor in | ||
template.updateSections(Self.updateSections(template.sections)) | ||
} | ||
} | ||
NotificationCenter.default.addObserver(forName: Self.updateContentNotifications, object: nil, queue: nil) { _ in | ||
Task { @MainActor in | ||
template.updateSections(Self.updateSections(template.sections)) | ||
} | ||
} | ||
|
||
return template | ||
} | ||
|
||
private func getOfflineSections() throws -> [CPListSection] { | ||
let podcasts = try OfflineManager.shared.getPodcasts() | ||
let audiobooks = try OfflineManager.shared.getAudiobooks() | ||
|
||
var sections: [CPListSection] = [ | ||
.init(items: audiobooks.map { @MainActor in | ||
var image: UIImage? | ||
var detailText = "" | ||
|
||
if let imageUrl = $0.image?.url, let data = try? Data(contentsOf: imageUrl) { | ||
image = UIImage(data: data) | ||
} | ||
|
||
if let author = $0.author { | ||
detailText += author | ||
} | ||
if let narrator = $0.narrator { | ||
if !detailText.isEmpty { | ||
detailText += " • " | ||
} | ||
|
||
detailText += narrator | ||
} | ||
|
||
let item = CPListItem(text: $0.name, detailText: detailText.isEmpty ? nil : detailText, image: image, accessoryImage: nil, accessoryType: .none) | ||
|
||
item.isExplicitContent = $0.explicit | ||
|
||
item.playingIndicatorLocation = .trailing | ||
item.isPlaying = AudioPlayer.shared.item == $0 | ||
|
||
item.userInfo = $0 | ||
item.handler = Self.startPlayback | ||
|
||
item.playbackProgress = OfflineManager.shared.requireProgressEntity(item: $0).progress | ||
|
||
return item | ||
}, header: String(localized: "carPlay.offline.sections.audiobooks"), headerSubtitle: nil, headerImage: UIImage(systemName: "bookmark.fill"), headerButton: nil, sectionIndexTitle: nil), | ||
] | ||
|
||
sections.append(contentsOf: podcasts.map { | ||
CPListSection(items: $0.value.map { @MainActor in | ||
let item = CPListItem(text: $0.name, detailText: $0.descriptionText) | ||
|
||
item.userInfo = $0 | ||
item.handler = Self.startPlayback | ||
|
||
item.playingIndicatorLocation = .trailing | ||
item.isPlaying = AudioPlayer.shared.item == $0 | ||
|
||
item.playbackProgress = OfflineManager.shared.requireProgressEntity(item: $0).progress | ||
|
||
return item | ||
}, header: $0.key.name, headerSubtitle: $0.key.author, headerImage: { | ||
if let imageUrl = $0.image?.url, let data = try? Data(contentsOf: imageUrl) { | ||
return UIImage(data: data) | ||
} | ||
|
||
return nil | ||
}($0.key), headerButton: nil, sectionIndexTitle: nil) | ||
}) | ||
|
||
return sections | ||
} | ||
} |
Oops, something went wrong.