Mulberry/App/Model/PlayStats.swift
astar76 8e6f8a86a4 Mulberry for iOS: initial SwiftUI port of the Android app
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 19:30:49 -05:00

105 lines
3.3 KiB
Swift

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
/// Play counts, last played, favourites and 0–5 ratings (Android PlayStats plus desktop ratings).
@MainActor
final class PlayStats: ObservableObject {
static let shared = PlayStats()
struct Entry: Codable {
var count = 0
var last: Double = 0
var fav = false
var rating = 0
var skips = 0
}
@Published private(set) var map: [String: Entry] = [:]
private var file: URL { Library.support.appendingPathComponent("stats.json") }
private var saveScheduled = false
private init() {
if let d = try? Data(contentsOf: file), let m = try? JSONDecoder().decode([String: Entry].self, from: d) { map = m }
}
private func key(_ s: Song) -> String { s.id }
func played(_ s: Song) {
var e = map[key(s)] ?? Entry()
e.count += 1
e.last = Date().timeIntervalSince1970
map[key(s)] = e
saveLater()
}
func skipped(_ s: Song) {
var e = map[key(s)] ?? Entry()
e.skips += 1
map[key(s)] = e
saveLater()
}
func count(_ s: Song) -> Int { map[key(s)]?.count ?? 0 }
func fav(_ s: Song) -> Bool { map[key(s)]?.fav ?? false }
func rating(_ s: Song) -> Int { map[key(s)]?.rating ?? 0 }
func setFav(_ s: Song, _ on: Bool) {
var e = map[key(s)] ?? Entry()
e.fav = on
map[key(s)] = e
saveLater()
Scrobbler.shared.love(s, on)
Player.shared.publishWidget()
}
func setRating(_ s: Song, _ r: Int) {
var e = map[key(s)] ?? Entry()
e.rating = max(0, min(5, r))
map[key(s)] = e
saveLater()
}
private func saveLater() {
guard !saveScheduled else { return }
saveScheduled = true
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in
guard let self else { return }
self.saveScheduled = false
if let d = try? JSONEncoder().encode(self.map) { try? d.write(to: self.file, options: .atomic) }
}
}
// Smart lists (Android Playlists tab)
func mostPlayed(_ all: [Song], _ n: Int = 100) -> [Song] {
all.filter { count($0) > 0 }.sorted { count($0) > count($1) }.prefix(n).map { $0 }
}
func recentlyPlayed(_ all: [Song], _ n: Int = 100) -> [Song] {
all.filter { (map[key($0)]?.last ?? 0) > 0 }
.sorted { (map[key($0)]?.last ?? 0) > (map[key($1)]?.last ?? 0) }.prefix(n).map { $0 }
}
func recentlyAdded(_ all: [Song], _ n: Int = 100) -> [Song] {
all.sorted { $0.dateAdded > $1.dateAdded }.prefix(n).map { $0 }
}
func favourites(_ all: [Song]) -> [Song] { all.filter { fav($0) } }
func neverPlayed(_ all: [Song]) -> [Song] { all.filter { count($0) == 0 } }
func topRated(_ all: [Song]) -> [Song] { all.filter { rating($0) > 0 }.sorted { rating($0) > rating($1) } }
}
/// ReplayGain values read lazily from tags.
final class ReplayGainCache: @unchecked Sendable {
static let shared = ReplayGainCache()
private var cache: [String: Double] = [:]
private let lock = NSLock()
func gain(_ s: Song) -> Double {
lock.lock()
if let g = cache[s.path] { lock.unlock(); return g }
lock.unlock()
let t = TagReader.read(s.url)
let g = t.albumGain ?? t.trackGain ?? 0
lock.lock(); cache[s.path] = g; lock.unlock()
return g
}
}