Initial Commit
This commit is contained in:
@@ -0,0 +1,379 @@
|
||||
import SwiftUI
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
enum EntityKind: String, Identifiable {
|
||||
case singer, venue
|
||||
var id: String { rawValue }
|
||||
var title: String { self == .singer ? "Singer" : "Venue" }
|
||||
var table: String { self == .singer ? "singers" : "venues" }
|
||||
}
|
||||
|
||||
@MainActor final class AppModel: ObservableObject {
|
||||
@Published var songs = [Song]()
|
||||
@Published var singers = [PersonPlace]()
|
||||
@Published var venues = [PersonPlace]()
|
||||
@Published var error: String?
|
||||
@Published var creating: EntityKind?
|
||||
let player = Player()
|
||||
private(set) var library: Library?
|
||||
let root: URL
|
||||
init(storageURL: URL? = nil) {
|
||||
root = storageURL ?? FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].appendingPathComponent("KJamsClone")
|
||||
perform {
|
||||
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
|
||||
library = try Library(url: root.appendingPathComponent("Library.sqlite")); try refresh()
|
||||
}
|
||||
}
|
||||
func perform(_ work: () throws -> Void) { do { try work() } catch { self.error = error.localizedDescription } }
|
||||
func refresh() throws { guard let library else { return }; songs = try library.songs(); singers = try library.entities("singers"); venues = try library.entities("venues") }
|
||||
@discardableResult func createEntity(_ kind: EntityKind, name: String) throws -> Int64 {
|
||||
guard let library else { throw LibraryError.message("Library unavailable") }
|
||||
let trimmed = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
try library.addEntity(kind.table, name: trimmed)
|
||||
try refresh()
|
||||
let entities = kind == .singer ? singers : venues
|
||||
guard let entity = entities.first(where: { $0.name == trimmed }) else {
|
||||
throw LibraryError.message("The new \(kind.title.lowercased()) could not be loaded")
|
||||
}
|
||||
return entity.id
|
||||
}
|
||||
func importFiles(_ urls: [URL]) { perform { guard let library else { throw LibraryError.message("Library unavailable") }; try Importer(media: root.appendingPathComponent("Media")).run(urls, library: library); try refresh() } }
|
||||
}
|
||||
@main struct KJamsCloneApp: App {
|
||||
@StateObject private var model = AppModel()
|
||||
var body: some Scene {
|
||||
Window("KJamsClone", id: "main") {
|
||||
LibraryView(model: model).frame(minWidth: 1060, minHeight: 680).preferredColorScheme(.dark)
|
||||
}.defaultSize(width: 1260, height: 840)
|
||||
.commands {
|
||||
CommandGroup(replacing: .newItem) {
|
||||
Button("New Singer…") { model.creating = .singer }.keyboardShortcut("n", modifiers: [.command, .shift])
|
||||
Button("New Venue…") { model.creating = .venue }.keyboardShortcut("n", modifiers: [.command, .option])
|
||||
}
|
||||
}
|
||||
Window("Video", id: "lyrics") {
|
||||
LyricsView(player: model.player).frame(minWidth: 480, minHeight: 320).preferredColorScheme(.dark)
|
||||
}.defaultSize(width: 600, height: 400)
|
||||
}
|
||||
}
|
||||
private enum Classic {
|
||||
static let frame = Color(red: 0.15, green: 0.15, blue: 0.15)
|
||||
static let navy = Color(red: 0.008, green: 0.065, blue: 0.16)
|
||||
static let blue = Color(red: 0.015, green: 0.17, blue: 0.42)
|
||||
static let header = Color(white: 0.115)
|
||||
static let line = Color(white: 0.42)
|
||||
}
|
||||
struct LyricsView: View {
|
||||
@ObservedObject var player: Player
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Color.black
|
||||
if let image = player.image {
|
||||
Image(decorative: image, scale: 1).interpolation(.none).resizable().aspectRatio(1.5, contentMode: .fit)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private struct SilverSquareStyle: ButtonStyle {
|
||||
func makeBody(configuration: Configuration) -> some View {
|
||||
configuration.label.foregroundStyle(Color(white: 0.25)).padding(.horizontal, 6).padding(.vertical, 3)
|
||||
.background(LinearGradient(colors: [Color(white: 0.95), Color(white: 0.64)], startPoint: .top, endPoint: .bottom), in: RoundedRectangle(cornerRadius: 4))
|
||||
.overlay(RoundedRectangle(cornerRadius: 4).strokeBorder(Color(white: 0.45), lineWidth: 1.5))
|
||||
.overlay(RoundedRectangle(cornerRadius: 3).strokeBorder(.white.opacity(0.7)).padding(3))
|
||||
.opacity(configuration.isPressed ? 0.7 : 1)
|
||||
}
|
||||
}
|
||||
private struct SilverButton: View {
|
||||
let symbol: String
|
||||
let label: String
|
||||
var size: CGFloat = 40
|
||||
var active = false
|
||||
var enabled = true
|
||||
let action: () -> Void
|
||||
var body: some View {
|
||||
Button(action: action) {
|
||||
Image(systemName: symbol).font(.system(size: size * 0.41, weight: .semibold))
|
||||
.foregroundStyle(active ? Color.blue : Color(white: 0.24))
|
||||
.frame(width: size, height: size)
|
||||
.background(LinearGradient(colors: [.white, Color(white: 0.85), Color(white: 0.63)], startPoint: .top, endPoint: .bottom), in: Circle())
|
||||
.overlay(Circle().strokeBorder(Color(white: 0.45), lineWidth: 1.5))
|
||||
.overlay(Circle().strokeBorder(.white.opacity(0.65), lineWidth: 1).padding(3))
|
||||
.shadow(color: .black.opacity(0.7), radius: 1, y: 1)
|
||||
}.buttonStyle(.plain).disabled(!enabled).opacity(enabled ? 1 : 0.72)
|
||||
.help(label).accessibilityLabel(label)
|
||||
}
|
||||
}
|
||||
struct LibraryView: View {
|
||||
@ObservedObject var model: AppModel
|
||||
@State private var selected: Int64?
|
||||
@State private var singer: Int64?
|
||||
@State private var venue: Int64?
|
||||
@State private var search = ""
|
||||
@State private var name = ""
|
||||
@State private var importing = false
|
||||
@State private var source = "Library"
|
||||
@Environment(\.openWindow) private var openWindow
|
||||
private var visibleSongs: [Song] { model.songs.filter { search.isEmpty || $0.title.localizedCaseInsensitiveContains(search) } }
|
||||
private var selectedSong: Song? { model.songs.first { $0.id == selected } }
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
TransportView(player: model.player, search: $search, hasSelection: selectedSong != nil,
|
||||
play: playSelected, next: playNext, display: { openWindow(id: "lyrics") })
|
||||
.padding(.horizontal, 18).padding(.top, 18).padding(.bottom, 22)
|
||||
HSplitView {
|
||||
sidebar.frame(minWidth: 190, idealWidth: 220, maxWidth: 240)
|
||||
VStack(spacing: 12) {
|
||||
if source == "Library" { songTable } else { directory }
|
||||
detailPanel.frame(height: 84)
|
||||
}.padding(.leading, 12).frame(minWidth: 780)
|
||||
}.padding(.horizontal, 18)
|
||||
footer.padding(.horizontal, 18).padding(.vertical, 12)
|
||||
}
|
||||
.background(Classic.frame)
|
||||
.sheet(item: $model.creating) { kind in
|
||||
AddEntitySheet(model: model, kind: kind) { id in
|
||||
if kind == .singer { singer = id } else { venue = id }
|
||||
}
|
||||
}
|
||||
.fileImporter(isPresented: $importing, allowedContentTypes: [.audio, .zip, UTType(filenameExtension: "cdg") ?? .data], allowsMultipleSelection: true) { result in
|
||||
switch result {
|
||||
case .success(let urls): model.importFiles(urls)
|
||||
case .failure(let error): model.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
.alert("KJamsClone", isPresented: Binding(get: { model.error != nil }, set: { if !$0 { model.error = nil } })) {
|
||||
Button("OK") { model.error = nil }
|
||||
} message: { Text(model.error ?? "") }
|
||||
}
|
||||
private var sidebar: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Text("Source").font(.system(size: 15, weight: .semibold)).padding(.horizontal, 10).frame(height: 31)
|
||||
.frame(maxWidth: .infinity, alignment: .leading).background(Classic.header)
|
||||
ForEach([("Library", "music.note.house.fill", Color.yellow), ("Singers", "person.2.fill", Color.green), ("Venues", "building.2.fill", Color.cyan)], id: \.0) { item in
|
||||
Button { source = item.0 } label: {
|
||||
HStack(spacing: 10) {
|
||||
Image(systemName: item.1).foregroundStyle(item.2).frame(width: 22)
|
||||
Text(item.0).font(.system(size: 16, weight: .medium))
|
||||
Spacer()
|
||||
Text("\(item.0 == "Library" ? model.songs.count : item.0 == "Singers" ? model.singers.count : model.venues.count)")
|
||||
.font(.system(size: 11)).foregroundStyle(.white.opacity(0.6))
|
||||
}.padding(.horizontal, 12).frame(height: 34).contentShape(Rectangle())
|
||||
.background(source == item.0 ? Color(red: 0, green: 0.36, blue: 1) : .clear)
|
||||
}.buttonStyle(.plain)
|
||||
}
|
||||
Spacer()
|
||||
}.foregroundStyle(.white).background(Classic.navy).overlay(Rectangle().stroke(Classic.line, lineWidth: 1))
|
||||
}
|
||||
private var songTable: some View {
|
||||
GeometryReader { geometry in
|
||||
ScrollView(.horizontal) {
|
||||
VStack(spacing: 0) {
|
||||
tableHeader(width: max(geometry.size.width, 790))
|
||||
ScrollView(.vertical) {
|
||||
LazyVStack(spacing: 0) {
|
||||
ForEach(visibleSongs) { song in songRow(song, width: max(geometry.size.width, 790)) }
|
||||
}.frame(maxWidth: .infinity, alignment: .topLeading)
|
||||
}
|
||||
}.frame(width: max(geometry.size.width, 790), height: geometry.size.height)
|
||||
}
|
||||
}.background(Classic.navy).overlay(Rectangle().stroke(Classic.line, lineWidth: 1))
|
||||
}
|
||||
private func widths(_ total: CGFloat) -> [CGFloat] {
|
||||
[42, max(200, total - 598), 100, 100, 76, 86, 72, 122]
|
||||
}
|
||||
private func tableHeader(width: CGFloat) -> some View {
|
||||
let sizes = widths(width)
|
||||
return HStack(spacing: 0) {
|
||||
ForEach(Array(["PLI", "Song Name", "Artist", "Album", "Label", "Key Adjust", "Tempo", "Rating"].enumerated()), id: \.offset) { index, title in
|
||||
Text(title).font(.system(size: 13, weight: .medium)).lineLimit(1)
|
||||
.padding(.leading, 7).frame(width: sizes[index], height: 30, alignment: .leading)
|
||||
.overlay(alignment: .trailing) { Rectangle().fill(Classic.line).frame(width: 1).padding(.vertical, 4) }
|
||||
}
|
||||
}.foregroundStyle(.white).background(Classic.header)
|
||||
.overlay(alignment: .bottom) { Rectangle().fill(Color(white: 0.65)).frame(height: 1) }
|
||||
}
|
||||
private func songRow(_ song: Song, width: CGFloat) -> some View {
|
||||
let sizes = widths(width)
|
||||
return HStack(spacing: 0) {
|
||||
Image(systemName: song.cdg == nil ? "music.note" : "music.note.tv")
|
||||
.foregroundStyle(.white.opacity(0.75)).frame(width: sizes[0])
|
||||
Text(song.title).lineLimit(1).padding(.leading, 7).frame(width: sizes[1], alignment: .leading)
|
||||
ForEach(2..<7) { index in
|
||||
Text(index == 4 ? (song.cdg == nil ? "Audio" : "MP3+G") : "—")
|
||||
.foregroundStyle(.white.opacity(0.5)).padding(.leading, 7).frame(width: sizes[index], alignment: .leading)
|
||||
}
|
||||
Menu {
|
||||
ForEach(0..<6) { value in
|
||||
Button(value == 0 ? "Unrated" : String(repeating: "★", count: value)) {
|
||||
model.perform { try model.library?.rate(song.id, value); try model.refresh() }
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Text(song.rating == 0 ? "☆☆☆☆☆" : String(repeating: "★", count: song.rating) + String(repeating: "☆", count: 5 - song.rating))
|
||||
.font(.system(size: 13)).foregroundStyle(song.rating == 0 ? Color.gray : Color.yellow)
|
||||
}.menuStyle(.borderlessButton).frame(width: sizes[7] - 14).padding(.horizontal, 7).help("Set song rating")
|
||||
}.font(.system(size: 13)).foregroundStyle(.white).frame(height: 30)
|
||||
.background(selected == song.id ? Color(red: 0, green: 0.28, blue: 0.72) : .clear)
|
||||
.contentShape(Rectangle()).onTapGesture(count: 2) { selected = song.id; playSelected() }
|
||||
.onTapGesture { selected = song.id }
|
||||
}
|
||||
private var directory: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
HStack {
|
||||
Text(source).font(.system(size: 14, weight: .semibold))
|
||||
Spacer()
|
||||
Button(source == "Singers" ? "New Singer…" : "New Venue…") { model.creating = source == "Singers" ? .singer : .venue }
|
||||
}.padding(.horizontal, 12).frame(height: 36).background(Classic.header)
|
||||
ScrollView {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
ForEach(source == "Singers" ? model.singers : model.venues) { entity in
|
||||
Button {
|
||||
if source == "Singers" { singer = entity.id } else { venue = entity.id }
|
||||
} label: {
|
||||
HStack { Image(systemName: source == "Singers" ? "person.fill" : "building.2.fill"); Text(entity.name); Spacer()
|
||||
if entity.id == (source == "Singers" ? singer : venue) { Image(systemName: "checkmark") }
|
||||
}.padding(12).contentShape(Rectangle())
|
||||
}.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
}
|
||||
HStack {
|
||||
TextField(source == "Singers" ? "Singer name" : "Venue name", text: $name).textFieldStyle(.roundedBorder).onSubmit(addEntity)
|
||||
Button("Add \(source == "Singers" ? "Singer" : "Venue")", action: addEntity).disabled(name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
||||
}.padding(12)
|
||||
}.foregroundStyle(.white).background(Classic.navy).overlay(Rectangle().stroke(Classic.line, lineWidth: 1))
|
||||
}
|
||||
private var detailPanel: some View {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
HStack(spacing: 16) {
|
||||
Picker("Singer", selection: $singer) { Text("None").tag(nil as Int64?); ForEach(model.singers) { Text($0.name).tag(Optional($0.id)) } }
|
||||
Button { model.creating = .singer } label: { Image(systemName: "plus.circle.fill") }
|
||||
.buttonStyle(.plain).help("Add singer").accessibilityLabel("Add singer")
|
||||
Picker("Venue", selection: $venue) { Text("None").tag(nil as Int64?); ForEach(model.venues) { Text($0.name).tag(Optional($0.id)) } }
|
||||
Button { model.creating = .venue } label: { Image(systemName: "plus.circle.fill") }
|
||||
.buttonStyle(.plain).help("Add venue").accessibilityLabel("Add venue")
|
||||
Text(selectedSong.map { "\($0.plays) plays" } ?? "No song selected").foregroundStyle(.white.opacity(0.55)).font(.system(size: 11))
|
||||
}
|
||||
SeekBar(player: model.player)
|
||||
}.padding(12).frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.background(Classic.navy).overlay(Rectangle().stroke(Classic.line, lineWidth: 1))
|
||||
}
|
||||
private var footer: some View {
|
||||
HStack {
|
||||
Button { importing = true } label: { Image(systemName: "plus").font(.system(size: 20, weight: .bold)).frame(width: 32, height: 24) }
|
||||
.help("Import audio, MP3+G or ZIP").accessibilityLabel("Import songs")
|
||||
RepeatButton(player: model.player).padding(.leading, 12)
|
||||
Spacer()
|
||||
Text("\(visibleSongs.count) \(visibleSongs.count == 1 ? "Song" : "Songs")").font(.system(size: 14, weight: .medium))
|
||||
Spacer()
|
||||
Button { openWindow(id: "lyrics") } label: { Image(systemName: "rectangle.on.rectangle").frame(width: 32, height: 24) }
|
||||
.help("Open video window").accessibilityLabel("Open video window")
|
||||
}.buttonStyle(SilverSquareStyle())
|
||||
}
|
||||
private func playSelected() {
|
||||
guard let song = selectedSong else { return }
|
||||
model.perform { try model.player.load(song); try model.library?.record(song.id, singer: singer, venue: venue); try model.refresh() }
|
||||
}
|
||||
private func playNext() {
|
||||
guard !visibleSongs.isEmpty else { return }
|
||||
let index = visibleSongs.firstIndex { $0.id == selected } ?? -1
|
||||
selected = visibleSongs[(index + 1) % visibleSongs.count].id; playSelected()
|
||||
}
|
||||
private func addEntity() {
|
||||
model.perform {
|
||||
let kind: EntityKind = source == "Singers" ? .singer : .venue
|
||||
let id = try model.createEntity(kind, name: name)
|
||||
if kind == .singer { singer = id } else { venue = id }
|
||||
name = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
private struct AddEntitySheet: View {
|
||||
@ObservedObject var model: AppModel
|
||||
let kind: EntityKind
|
||||
let saved: (Int64) -> Void
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@State private var name = ""
|
||||
@State private var error: String?
|
||||
@FocusState private var focused: Bool
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
Text("New \(kind.title)").font(.title2.bold())
|
||||
TextField("\(kind.title) name", text: $name).textFieldStyle(.roundedBorder)
|
||||
.focused($focused).onSubmit(save)
|
||||
if let error { Text(error).foregroundStyle(.red).font(.callout) }
|
||||
HStack {
|
||||
Spacer()
|
||||
Button("Cancel") { dismiss() }.keyboardShortcut(.cancelAction)
|
||||
Button("Add \(kind.title)", action: save).keyboardShortcut(.defaultAction)
|
||||
.disabled(name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
|
||||
}
|
||||
}.padding(24).frame(width: 360).onAppear { focused = true }
|
||||
}
|
||||
private func save() {
|
||||
do { let id = try model.createEntity(kind, name: name); saved(id); dismiss() }
|
||||
catch { self.error = error.localizedDescription }
|
||||
}
|
||||
}
|
||||
private struct TransportView: View {
|
||||
@ObservedObject var player: Player
|
||||
@Binding var search: String
|
||||
let hasSelection: Bool
|
||||
let play: () -> Void
|
||||
let next: () -> Void
|
||||
let display: () -> Void
|
||||
var body: some View {
|
||||
HStack(alignment: .top, spacing: 28) {
|
||||
VStack(spacing: 15) {
|
||||
HStack(spacing: 7) {
|
||||
SilverButton(symbol: "backward.fill", label: "Back 10 seconds", size: 35, enabled: player.duration > 0) { player.seek(player.time - 10) }
|
||||
SilverButton(symbol: player.playing ? "pause.fill" : "play.fill", label: player.playing ? "Pause" : "Play", size: 49, enabled: hasSelection || player.duration > 0) {
|
||||
if player.duration > 0 { player.toggle() } else { play() }
|
||||
}
|
||||
SilverButton(symbol: "stop", label: "Stop", size: 46, enabled: player.duration > 0) { player.stop() }
|
||||
SilverButton(symbol: "forward.fill", label: "Forward 10 seconds", size: 35, enabled: player.duration > 0) { player.seek(player.time + 10) }
|
||||
SilverButton(symbol: "forward.end.fill", label: "Play next song", size: 26, enabled: hasSelection, action: next)
|
||||
}
|
||||
HStack(spacing: 9) {
|
||||
Image(systemName: "speaker.fill").font(.system(size: 11))
|
||||
Slider(value: $player.volume, in: 0...1).controlSize(.small).tint(.gray).accessibilityLabel("Volume")
|
||||
Image(systemName: "speaker.wave.3.fill").font(.system(size: 12))
|
||||
}.frame(width: 180)
|
||||
}.frame(width: 220)
|
||||
VStack(spacing: 0) {
|
||||
Text(player.duration == 0 ? "Ready!" : player.title).font(.system(size: 15, weight: .medium)).lineLimit(1).padding(.horizontal, 20)
|
||||
if player.duration > 0 { Text(player.playing ? "Playing" : "Paused").font(.system(size: 10)).foregroundStyle(.white.opacity(0.65)).padding(.top, 4) }
|
||||
}.frame(maxWidth: .infinity).frame(height: 64)
|
||||
.background(LinearGradient(colors: [Color(red: 0, green: 0.10, blue: 0.30), Classic.blue], startPoint: .top, endPoint: .bottom), in: Capsule())
|
||||
.overlay(Capsule().strokeBorder(LinearGradient(colors: [.black, .white], startPoint: .top, endPoint: .bottom), lineWidth: 1.5))
|
||||
.padding(.top, 14)
|
||||
HStack(spacing: 12) {
|
||||
TextField("Search", text: $search).textFieldStyle(.plain).font(.system(size: 16))
|
||||
.padding(.horizontal, 8).frame(width: 145, height: 28).background(Color(white: 0.07), in: RoundedRectangle(cornerRadius: 5))
|
||||
.overlay(RoundedRectangle(cornerRadius: 5).stroke(.white.opacity(0.1)))
|
||||
SilverButton(symbol: "eye", label: "Open video window", size: 48, action: display)
|
||||
}.padding(.top, 15)
|
||||
}.foregroundStyle(.white).frame(height: 96)
|
||||
}
|
||||
}
|
||||
private struct SeekBar: View {
|
||||
@ObservedObject var player: Player
|
||||
private func clock(_ seconds: Double) -> String { let n = Int(seconds); return String(format: "%d:%02d", n / 60, n % 60) }
|
||||
var body: some View {
|
||||
HStack(spacing: 10) {
|
||||
Text(clock(player.time))
|
||||
Slider(value: Binding(get: { player.time }, set: { player.seek($0) }), in: 0...max(1, player.duration))
|
||||
.controlSize(.mini).tint(.gray).disabled(player.duration == 0).accessibilityLabel("Playback position")
|
||||
Text(clock(player.duration))
|
||||
}.font(.system(size: 10, design: .monospaced)).foregroundStyle(.white.opacity(0.65))
|
||||
}
|
||||
}
|
||||
private struct RepeatButton: View {
|
||||
@ObservedObject var player: Player
|
||||
var body: some View {
|
||||
Button { player.repeating.toggle() } label: {
|
||||
Image(systemName: "repeat").font(.system(size: 18, weight: .semibold))
|
||||
.foregroundStyle(player.repeating ? Color.blue : Color(white: 0.25)).frame(width: 32, height: 24)
|
||||
}.help(player.repeating ? "Repeat song: on" : "Repeat song: off").accessibilityLabel("Repeat song").accessibilityValue(player.repeating ? "On" : "Off")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user