Skip to content

Commit

Permalink
Add store URL button
Browse files Browse the repository at this point in the history
Awaiting a behind-the-scenes rework (see spotlightishere#9). Currently inoperable. Do not merge in current state.
Also requires spotlightishere/SwordRPC#3
  • Loading branch information
MCMi460 committed Jul 17, 2024
1 parent 089a4d7 commit 8e7ccf5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 17 deletions.
62 changes: 51 additions & 11 deletions Ongaku/Ongaku.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
//

import Combine
import MacControlCenterUI
import os.log
import SwiftUI
import MacControlCenterUI

private let log: Logger = .init(subsystem: "io.github.spotlightishere.Ongaku", category: "main-app")

@main
struct Ongaku: App {
@State var isMenuPresented: Bool = false
let blurple: Color = Color(red: 0.34375, green: 0.39453125, blue: 0.9453125)
let lastFmColor: Color = Color(red: 0.83203125, green: 0.06640625, blue: 0.02734375)
let blurple: Color = .init(red: 0.34375, green: 0.39453125, blue: 0.9453125)
let lastFmColor: Color = .init(red: 0.83203125, green: 0.06640625, blue: 0.02734375)

init() {
do {
player = try MusicPlayer()
Expand All @@ -39,20 +39,21 @@ struct Ongaku: App {
var playerSink: AnyCancellable?
@StateObject var scrobbler: ScrobblerController
@StateObject var rpc: RPCController
@State var showStore: Bool = true
@State var joinRequests: Bool = false

var body: some Scene {
// Designed to match the style of the default
// Storyboard-based menu items.
MenuBarExtra("Ongaku", image: "status_icon") {

MacControlCenterMenu(isPresented: $isMenuPresented) {
MenuHeader("Ongaku") {
Text(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String)
.foregroundColor(.secondary)
}

MenuSection("Active")

HStack {
MenuCircleToggle(
isOn: $rpc.enabled,
Expand All @@ -77,7 +78,7 @@ struct Ongaku: App {
image: Image("last_fm"),
color: lastFmColor
)
) { Text("Last.fm") } onClick: { toggle in
) { Text("Last.fm") } onClick: { _ in
if scrobbler.session == nil {
scrobbler.enabled = false
Task(priority: .userInitiated) {
Expand All @@ -88,7 +89,46 @@ struct Ongaku: App {
}
}
.frame(height: 80)


MenuDisclosureSection("Discord", initiallyExpanded: false) {
MenuToggle(
isOn: $showStore,
style: .init(
image: Image(systemName: "music.note.list"),
color: blurple
)
) {
Text("Store button")
} onClick: { toggle in
rpc.showStore = toggle
if toggle {
joinRequests = false
rpc.joinRequests = false
}
Task(priority: .userInitiated) {
rpc.updatePresence()
}
}
MenuToggle(
isOn: $joinRequests,
style: .init(
image: Image(systemName: "tray.and.arrow.down.fill"),
color: blurple
)
) {
Text("Join requests")
} onClick: { toggle in
rpc.joinRequests = toggle
if toggle {
showStore = false
rpc.showStore = false
}
Task(priority: .userInitiated) {
rpc.updatePresence()
}
}
}

MenuDisclosureSection("Last.fm", initiallyExpanded: false) {
if let session = scrobbler.session {
MenuCommand {
Expand All @@ -103,7 +143,7 @@ struct Ongaku: App {
Spacer()
}
}

MenuCommand {
do {
try scrobbler.clearSession()
Expand Down Expand Up @@ -139,7 +179,7 @@ struct Ongaku: App {
}
}
}

Divider()

MenuCommand {
Expand Down
4 changes: 2 additions & 2 deletions Ongaku/Player/MusicPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// Copyright © 2022 Spotlight Deveaux. All rights reserved.
//

import AppKit
import Combine
import Foundation
import MusicKit
import os.log
import ScriptingBridge
import AppKit

// Adapted from:
// https://gist.github.com/pvieito/3aee709b97602bfc44961df575e2b696
Expand Down Expand Up @@ -46,7 +46,7 @@ private var log: Logger = .init(subsystem: "io.github.spotlightishere.Ongaku", c

private func fetchPlayerState() throws -> PlayerState {
if NSRunningApplication.runningApplications(withBundleIdentifier: musicBundleId).count == 0 { return .closed }

guard let music: AnyObject = SBApplication(bundleIdentifier: musicBundleId),
let playerState = music.playerState,
let track = music.currentTrack
Expand Down
2 changes: 1 addition & 1 deletion Ongaku/Player/PlayerState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum PlayerState {

/// Indicates that the player is not playing anything.
case stopped

/// Indicates that no presence should be shown
case closed
}
Expand Down
34 changes: 31 additions & 3 deletions Ongaku/RPCController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class RPCController: SwordRPCDelegate, ObservableObject {
var player: Player
var playerSink: AnyCancellable?
@Published var enabled: Bool = true
var showStore: Bool = true
var joinRequests: Bool = false
var Presence: RichPresence?
var currentTrack: Track?

init(player: Player) {
self.player = player
Expand All @@ -50,13 +54,14 @@ class RPCController: SwordRPCDelegate, ObservableObject {

func updateRichPresence(playerState state: PlayerState) async {
if !enabled { return }

var presence = RichPresence()

func updateActive(_ active: PlayerState.Active, paused: Bool = false) async {
log.info("Player is active, populating rich presence state accordingly")

let track = active.track
currentTrack = track
presence.details = track.title
presence.state = "\(track.artist ?? "Unknown") \u{2014} \(track.album ?? "Unknown")"

Expand Down Expand Up @@ -103,7 +108,6 @@ class RPCController: SwordRPCDelegate, ObservableObject {
presence.assets.largeText = "There's nothing here!"
presence.assets.smallImage = "stop"
presence.assets.smallText = "Currently stopped"

// If the player is active (i.e. has a track and position), then update
// the rich presence accordingly.
case let .playing(active):
Expand All @@ -117,7 +121,31 @@ class RPCController: SwordRPCDelegate, ObservableObject {

log.info("Sending presence: \(String(describing: presence))")

rpc.setPresence(presence)
Presence = presence
updatePresence()
}

func updatePresence() {
if Presence != nil {
if let track = currentTrack {
if showStore, track.url != nil {
Presence?.buttons = [
RichPresence.Button(
label: "Open in Apple Music",
url: track.url!
),
]
} else {
Presence?.buttons = nil
}
if joinRequests {
// TO BE IMPLEMENTED
} else {
Presence?.secrets = nil
}
}
rpc.setPresence(Presence!)
}
}
}

Expand Down

0 comments on commit 8e7ccf5

Please sign in to comment.