You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
3.7 KiB
149 lines
3.7 KiB
//
|
|
// Countdown+Extension.swift
|
|
// LeCountdown
|
|
//
|
|
// Created by Laurent Morvillier on 25/01/2023.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
import CoreData
|
|
|
|
extension AbstractSoundTimer {
|
|
|
|
var playables: [any Playable] {
|
|
if let playableIds {
|
|
var playables: [any Playable] = []
|
|
let ids: [String] = playableIds.components(separatedBy: idSeparator)
|
|
for id in ids {
|
|
if let intId = numberFormatter.number(from: id)?.intValue,
|
|
let sound = Sound(rawValue: intId) {
|
|
playables.append(sound)
|
|
} else if let playlist = Playlist(rawValue: id) {
|
|
playables.append(playlist)
|
|
}
|
|
}
|
|
return playables
|
|
}
|
|
return []
|
|
}
|
|
|
|
var allSounds: Set<Sound> {
|
|
return self.playables.reduce(Set<Sound>()) { $0.union($1.soundList) }
|
|
}
|
|
|
|
// func setSounds(_ sounds: Set<Sound>) {
|
|
// self.soundList = sounds.stringRepresentation
|
|
// }
|
|
|
|
var confirmationSounds: Set<Sound> {
|
|
if let confirmationSoundList {
|
|
return Set(confirmationSoundList.enumItems())
|
|
}
|
|
return []
|
|
}
|
|
|
|
func setConfirmationSounds(_ sounds: Set<Sound>) {
|
|
self.confirmationSoundList = sounds.stringRepresentation
|
|
}
|
|
|
|
var someSound: Sound {
|
|
var sounds: Set<Sound> = self.allSounds
|
|
if !AppGuard.main.isSubscriber {
|
|
sounds = sounds.filter { !$0.isRestricted }
|
|
}
|
|
|
|
// remove last played sound if the playlist has at least 3 sounds
|
|
if sounds.count > 2,
|
|
let lastSoundId = Preferences.lastSelectedSound[self.stringId],
|
|
let lastSound = Sound(rawValue: lastSoundId) {
|
|
sounds.remove(lastSound)
|
|
}
|
|
|
|
if let random = sounds.randomElement() {
|
|
Preferences.lastSelectedSound[self.stringId] = random.id
|
|
return random
|
|
}
|
|
|
|
return Sound.default
|
|
}
|
|
|
|
}
|
|
|
|
extension Stopwatch {
|
|
|
|
var coolSound: Sound? {
|
|
return Sound(rawValue: Int(self.sound)) ?? nil
|
|
}
|
|
|
|
static func fake(context: NSManagedObjectContext) -> Stopwatch {
|
|
let stopwatch = Stopwatch(context: context)
|
|
let activity = Activity(context: context)
|
|
activity.name = "Running"
|
|
stopwatch.activity = activity
|
|
return stopwatch
|
|
}
|
|
|
|
}
|
|
|
|
extension Record {
|
|
|
|
var count: Double { return 1.0 }
|
|
|
|
public override func didChangeValue(forKey key: String) {
|
|
super.didChangeValue(forKey: key)
|
|
|
|
switch key {
|
|
case "start", "end":
|
|
self.preCompute()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
func preCompute() {
|
|
if let start {
|
|
self.year = Int16(start.year)
|
|
self.month = Int16(start.month)
|
|
if let end {
|
|
self.duration = end.timeIntervalSince(start)
|
|
}
|
|
}
|
|
}
|
|
|
|
var formattedDay: String {
|
|
if let start {
|
|
return start.formatted(date: .abbreviated, time: .omitted)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
var details: String {
|
|
if let start, let end {
|
|
return "\(start.formatted()) - \(end.formatted())"
|
|
} else {
|
|
return "no details"
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension Activity {
|
|
|
|
fileprivate static var formatter: NumberFormatter = NumberFormatter()
|
|
|
|
var recordCount: String {
|
|
let count: Int = self.records?.count ?? 0
|
|
return Activity.formatter.string(from: NSNumber(value: count)) ?? "--"
|
|
}
|
|
|
|
}
|
|
|
|
extension CustomSound : Localized {
|
|
|
|
var localizedString: String {
|
|
return self.text ?? ""
|
|
}
|
|
|
|
}
|
|
|
|
|