diff --git a/apps/mobile/app/(app)/player.tsx b/apps/mobile/app/(app)/player.tsx index 99bb042..a06b216 100644 --- a/apps/mobile/app/(app)/player.tsx +++ b/apps/mobile/app/(app)/player.tsx @@ -44,6 +44,7 @@ import { NowPlayingQueue, QUEUE_HORIZONTAL_INSET, } from "@/components/now-playing-queue" +import { PlayerArtistMenu } from "@/components/player-artist-menu" import { PlayerSlider } from "@/components/player-slider" import { PlayerSymbol } from "@/components/player-symbol" import { useFavorites } from "@/contexts/favorites" @@ -544,28 +545,15 @@ function SongMeta({ { color: colors.foreground }, ]} /> - - - - - + + + void toggleSongFavorite(song.id)} diff --git a/apps/mobile/components/player-artist-menu.tsx b/apps/mobile/components/player-artist-menu.tsx new file mode 100644 index 0000000..1faf302 --- /dev/null +++ b/apps/mobile/components/player-artist-menu.tsx @@ -0,0 +1,79 @@ +import { requireNativeView, requireOptionalNativeModule } from "expo" +import { useState, type ReactNode } from "react" +import { Platform, StyleSheet, View, type ViewProps } from "react-native" + +import { + MediaActionMenu, + type MediaAction, +} from "@/components/media-context-menu" +import type { BrowseSong } from "@otunes/convex/types" + +const NativeMenu = + Platform.OS === "ios" && requireOptionalNativeModule("PlayerNavigationMenu") + ? requireNativeView< + ViewProps & { + artist: string + album: string + fontSize: number + onAction: (event: { nativeEvent: { action: MediaAction } }) => void + } + >("PlayerNavigationMenu") + : null + +export function PlayerArtistMenu({ + song, + compact, + onAction, + children, +}: { + song: BrowseSong + compact: boolean + onAction: (action: MediaAction) => void + children: ReactNode +}) { + const [size, setSize] = useState({ width: 0, height: 0 }) + + return ( + + setSize({ width: layout.width, height: layout.height }) + } + > + + {children} + + {NativeMenu ? ( + onAction(nativeEvent.action)} + /> + ) : size.width > 0 ? ( + + + + ) : null} + + ) +} + +const styles = StyleSheet.create({ + container: { overflow: "hidden" }, +}) diff --git a/apps/mobile/modules/player-navigation-menu/expo-module.config.json b/apps/mobile/modules/player-navigation-menu/expo-module.config.json new file mode 100644 index 0000000..c46913b --- /dev/null +++ b/apps/mobile/modules/player-navigation-menu/expo-module.config.json @@ -0,0 +1,4 @@ +{ + "platforms": ["apple"], + "apple": { "modules": ["PlayerNavigationMenuModule"] } +} diff --git a/apps/mobile/modules/player-navigation-menu/ios/PlayerNavigationMenu.podspec b/apps/mobile/modules/player-navigation-menu/ios/PlayerNavigationMenu.podspec new file mode 100644 index 0000000..7587309 --- /dev/null +++ b/apps/mobile/modules/player-navigation-menu/ios/PlayerNavigationMenu.podspec @@ -0,0 +1,17 @@ +Pod::Spec.new do |s| + s.name = 'PlayerNavigationMenu' + s.version = '1.0.0' + s.summary = 'Native player navigation menu for oTunes' + s.description = s.summary + s.license = { :type => 'MIT' } + s.author = 'oTunes' + s.homepage = 'https://otunes.abunchofknowitalls.com' + s.platforms = { :ios => '16.4' } + s.swift_version = '5.9' + s.source = { :git => '' } + s.static_framework = true + s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' } + s.dependency 'ExpoModulesCore' + s.frameworks = 'UIKit' + s.source_files = '**/*.swift' +end diff --git a/apps/mobile/modules/player-navigation-menu/ios/PlayerNavigationMenuModule.swift b/apps/mobile/modules/player-navigation-menu/ios/PlayerNavigationMenuModule.swift new file mode 100644 index 0000000..58675d3 --- /dev/null +++ b/apps/mobile/modules/player-navigation-menu/ios/PlayerNavigationMenuModule.swift @@ -0,0 +1,56 @@ +import ExpoModulesCore +import UIKit + +public class PlayerNavigationMenuModule: Module { + public func definition() -> ModuleDefinition { + Name("PlayerNavigationMenu") + + View(PlayerNavigationMenuView.self) { + Events("onAction") + Prop("artist") { (view: PlayerNavigationMenuView, value: String) in + view.artist = value + } + Prop("album") { (view: PlayerNavigationMenuView, value: String) in + view.album = value + } + Prop("fontSize") { (view: PlayerNavigationMenuView, value: Double) in + view.fontSize = CGFloat(value) + } + } + } +} + +final class PlayerNavigationMenuView: ExpoView { + let onAction = EventDispatcher() + var artist = "" { didSet { updateMenu(); setNeedsLayout() } } + var album = "" { didSet { updateMenu() } } + var fontSize: CGFloat = 20 { didSet { setNeedsLayout() } } + private let button = UIButton(type: .custom) + + required init(appContext: AppContext? = nil) { + super.init(appContext: appContext) + button.showsMenuAsPrimaryAction = true + button.preferredMenuElementOrder = .fixed + button.accessibilityHint = "Opens album and artist options" + addSubview(button) + } + + private func updateMenu() { + button.accessibilityLabel = artist + button.menu = UIMenu(children: [ + UIAction(title: "Go to Album", subtitle: album, image: UIImage(systemName: "square.stack")) { [weak self] _ in + self?.onAction(["action": "go-album"]) + }, + UIAction(title: "Go to Artist", subtitle: artist, image: UIImage(systemName: "music.mic")) { [weak self] _ in + self?.onAction(["action": "go-artist"]) + }, + ]) + } + + override func layoutSubviews() { + super.layoutSubviews() + // Anchor to the visible artist text, not the marquee's offscreen track. + let textWidth = (artist as NSString).size(withAttributes: [.font: UIFont.systemFont(ofSize: fontSize)]).width + button.frame = CGRect(x: 0, y: 0, width: min(ceil(textWidth), bounds.width), height: bounds.height) + } +}