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.
LeCountdown/LeCountdown/Model/Model+Extensions.swift

85 lines
1.8 KiB

//
// Countdown+Extension.swift
// LeCountdown
//
// Created by Laurent Morvillier on 25/01/2023.
//
import Foundation
import SwiftUI
import CoreData
extension Countdown {
var displayName: String {
return self.name ?? self.coolpic.emoji
}
var name: String? {
return self.activity?.name
}
var url: URL {
if let url = URL(string: self.stringId) {
return url
} else {
return URL(fileURLWithPath: self.stringId) // stupid fallthrough
}
}
var coolpic: CoolPic {
if let image, let coolpic = CoolPic(rawValue: image) {
return coolpic
}
return CoolPic.allCases[0]
}
var imageName: String {
return self.coolpic.rawValue
}
var coolSound: Sound {
return Sound.allCases[Int(self.sound)]
}
var soundName: String {
coolSound.soundName
}
// var soundFile: SoundFile {
// return Sound.allCases[Int(self.sound)].soundFile
// }
static func fake(context: NSManagedObjectContext) -> Countdown {
let cd = Countdown(context: context)
cd.duration = 4 * 60.0
let activity = Activity(context: context)
activity.name = "Tea"
cd.activity = activity
return cd
}
}
extension Record {
var details: String {
if let start, let end {
return "\(start) - \(end)"
} 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)) ?? "--"
}
}