Skip to content

Commit

Permalink
Simplify View again
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangarelick committed May 7, 2024
1 parent 2c6c2d8 commit 5790fba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
2 changes: 2 additions & 0 deletions SoundSeer/API/MusicAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Alamofire
import OSLog
import SwiftJWT

// TODO: get song id and album id functions for initial loading

class MusicAPI: PlayerAPI {
private static let baseURL = "https://api.music.apple.com/v1"
private static var token: String?
Expand Down
62 changes: 27 additions & 35 deletions SoundSeer/SoundSeerApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,8 @@ import SwiftUI
struct SoundSeerApp: App {
@StateObject var viewModel = SoundSeerViewModel()
@State var isOpenAtLoginEnabled: Bool = SMAppService.mainApp.status == .enabled

private struct LabelContent: View {
@ObservedObject var playerViewModel: SoundSeerViewModel
@State private var window: NSWindow?

var body: some View {
Group {
if playerViewModel.playerState?.playbackState != .playing || playerViewModel.nowPlaying.isEmpty {
Image(systemName: "ear")
} else {
Text(playerViewModel.nowPlaying)
}
}
.onReceive(NotificationCenter.default.publisher(for: NSWindow.didChangeOcclusionStateNotification)) { notification in
Logger.view.debug("Received didChangeOcclusionState notification")

guard let window = notification.object as? NSWindow else { return }
playerViewModel.isAppVisibleInMenuBar = window.occlusionState.contains(.visible)
}
.background(WindowAccessor(window: $window))
}
}

@State var window: NSWindow?

var body: some Scene {
MenuBarExtra {
if viewModel.model != nil {
Expand All @@ -37,39 +16,39 @@ struct SoundSeerApp: App {
.labelStyle(.titleAndIcon)
.disabled(true)
}

Button("Next Track", systemImage: "forward.end", action: viewModel.nextTrack)
.labelStyle(.titleAndIcon)
.disabled(viewModel.playerState?.playbackState != .paused && viewModel.playerState?.playbackState != .playing)

Divider()

Button(!viewModel.currentSong.isEmpty
? (viewModel.prefixLength > 0
? viewModel.currentSong.truncate(length: Int(Double(viewModel.prefixLength) * 1.5))
: viewModel.currentSong.truncate(length: 60))
: "Song unknown", systemImage: "music.note", action: viewModel.openCurrentSong)
.labelStyle(.titleAndIcon)
.disabled(viewModel.currentSongId.isEmpty)

Button(!viewModel.currentArtist.isEmpty
? (viewModel.prefixLength > 0
? viewModel.currentArtist.truncate(length: Int(Double(viewModel.prefixLength) * 1.5))
: viewModel.currentArtist.truncate(length: 60))
: "Artist unknown", systemImage: "person", action: viewModel.openCurrentArtist)
.labelStyle(.titleAndIcon)
.disabled(viewModel.currentSongId.isEmpty)

Button(!viewModel.currentAlbum.isEmpty
? (viewModel.prefixLength > 0
? viewModel.currentAlbum.truncate(length: Int(Double(viewModel.prefixLength) * 1.5))
: viewModel.currentAlbum.truncate(length: 60))
: "Album unknown", systemImage: "opticaldisc", action: viewModel.openCurrentAlbum)
.labelStyle(.titleAndIcon)
.disabled(viewModel.currentSongId.isEmpty)

Divider()

Button("Copy Song URL", systemImage: "doc.on.doc", action: viewModel.copySongExternalURL)
.labelStyle(.titleAndIcon)
.disabled(viewModel.currentSongId.isEmpty)
Expand All @@ -78,15 +57,15 @@ struct SoundSeerApp: App {
.labelStyle(.titleAndIcon)
.disabled(true)
}

Divider()

Button {
// This is a bit of a hack. First, toggle the @State variable to immediately update the UI.
// Perform the state modification, then re-update the @State variable with the source of truth
do {
isOpenAtLoginEnabled.toggle()

if SMAppService.mainApp.status == .enabled {
try SMAppService.mainApp.unregister()
} else {
Expand All @@ -102,13 +81,26 @@ struct SoundSeerApp: App {
}
Text("Open at Login")
}

Button("Quit") {
NSApplication.shared.terminate(nil)
}
} label: {
if viewModel.model != nil {
LabelContent(playerViewModel: viewModel)
Group {
if viewModel.playerState?.playbackState != .playing || viewModel.nowPlaying.isEmpty {
Image(systemName: "ear")
} else {
Text(viewModel.nowPlaying)
}
}
.onReceive(NotificationCenter.default.publisher(for: NSWindow.didChangeOcclusionStateNotification)) { notification in
Logger.view.debug("Received didChangeOcclusionState notification")

guard let window = notification.object as? NSWindow else { return }
viewModel.isAppVisibleInMenuBar = window.occlusionState.contains(.visible)
}
.background(WindowAccessor(window: $window))
} else {
Image(systemName: "ear")
}
Expand Down

0 comments on commit 5790fba

Please sign in to comment.