everything changed
This commit is contained in:
@@ -18,8 +18,6 @@ struct GroupUserDefaults {
|
||||
}
|
||||
}
|
||||
|
||||
typealias MoodGroupingMetrics = (mood: Mood, total: Int, percent: Float)
|
||||
|
||||
class Random {
|
||||
static var tomorrowMidnightThirty: Date {
|
||||
let components = DateComponents(hour: 0, minute: 30, second: 0)
|
||||
@@ -52,61 +50,29 @@ class Random {
|
||||
return formatter.string(from: NSNumber(integerLiteral: day)) ?? ""
|
||||
}
|
||||
|
||||
static func createTotalPerc(fromEntries entries: [MoodEntry]) -> [MoodGroupingMetrics] {
|
||||
var returnData = [MoodGroupingMetrics]()
|
||||
static func createTotalPerc(fromEntries entries: [MoodEntry]) -> [MoodMetrics] {
|
||||
let filteredEntries = entries.filter({
|
||||
return ![.missing, .placeholder].contains($0.mood)
|
||||
})
|
||||
var returnData = [MoodMetrics]()
|
||||
|
||||
for (_, mood) in Mood.allValues.enumerated() {
|
||||
let moodEntries = entries.filter({
|
||||
let moodEntries = filteredEntries.filter({
|
||||
Int($0.moodValue) == mood.rawValue
|
||||
})
|
||||
let total = moodEntries.count
|
||||
let perc = (Float(total) / Float(entries.count)) * 100
|
||||
returnData.append((mood, total, perc))
|
||||
let perc = (Float(total) / Float(filteredEntries.count)) * 100
|
||||
returnData.append(MoodMetrics(mood: mood, total: total, percent: perc))
|
||||
}
|
||||
|
||||
returnData = returnData.sorted(by: {
|
||||
$0.0.rawValue > $1.0.rawValue
|
||||
$0.mood.rawValue > $1.mood.rawValue
|
||||
})
|
||||
|
||||
return returnData
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: RawRepresentable {
|
||||
public var rawValue: String {
|
||||
self.timeIntervalSinceReferenceDate.description
|
||||
}
|
||||
|
||||
public init?(rawValue: String) {
|
||||
self = Date(timeIntervalSinceReferenceDate: Double(rawValue) ?? 0.0)
|
||||
}
|
||||
|
||||
func startOfMonth() -> Date {
|
||||
let interval = Calendar.current.dateInterval(of: .month, for: self)
|
||||
return (interval?.start.toLocalTime())! // Without toLocalTime it give last months last date
|
||||
}
|
||||
|
||||
func endOfMonth() -> Date {
|
||||
let interval = Calendar.current.dateInterval(of: .month, for: self)
|
||||
return interval!.end
|
||||
}
|
||||
|
||||
func toLocalTime() -> Date {
|
||||
let timezone = TimeZone.current
|
||||
let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
|
||||
return Date(timeInterval: seconds, since: self)
|
||||
}
|
||||
|
||||
var weekday: Int {
|
||||
Calendar.current.component(.weekday, from: self)
|
||||
}
|
||||
|
||||
var firstDayOfTheMonth: Date {
|
||||
Calendar.current.dateComponents([.calendar, .year,.month], from: self).date!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct RoundedCorner: Shape {
|
||||
|
||||
var radius: CGFloat = .infinity
|
||||
@@ -153,20 +119,6 @@ extension UIView {
|
||||
}
|
||||
}
|
||||
|
||||
extension Date {
|
||||
static func dates(from fromDate: Date, to toDate: Date) -> [Date] {
|
||||
var dates: [Date] = []
|
||||
var date = fromDate
|
||||
|
||||
while date <= toDate {
|
||||
dates.append(date)
|
||||
guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: date) else { break }
|
||||
date = newDate
|
||||
}
|
||||
return dates
|
||||
}
|
||||
}
|
||||
|
||||
extension Color {
|
||||
static func random() -> Self {
|
||||
Self(
|
||||
@@ -175,4 +127,47 @@ extension Color {
|
||||
blue: .random(in: 0...1)
|
||||
)
|
||||
}
|
||||
|
||||
init(hex: String) {
|
||||
let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
|
||||
var int: UInt64 = 0
|
||||
Scanner(string: hex).scanHexInt64(&int)
|
||||
let a, r, g, b: UInt64
|
||||
switch hex.count {
|
||||
case 3: // RGB (12-bit)
|
||||
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
|
||||
case 6: // RGB (24-bit)
|
||||
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
|
||||
case 8: // ARGB (32-bit)
|
||||
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
|
||||
default:
|
||||
(a, r, g, b) = (1, 1, 1, 0)
|
||||
}
|
||||
|
||||
self.init(
|
||||
.sRGB,
|
||||
red: Double(r) / 255,
|
||||
green: Double(g) / 255,
|
||||
blue: Double(b) / 255,
|
||||
opacity: Double(a) / 255
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
func textToImage() -> UIImage? {
|
||||
let nsString = (self as NSString)
|
||||
let font = UIFont.systemFont(ofSize: 100) // you can change your font size here
|
||||
let stringAttributes = [NSAttributedString.Key.font: font]
|
||||
let imageSize = nsString.size(withAttributes: stringAttributes)
|
||||
|
||||
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) // begin image context
|
||||
UIColor.clear.set() // clear background
|
||||
UIRectFill(CGRect(origin: CGPoint(), size: imageSize)) // set rect size
|
||||
nsString.draw(at: CGPoint.zero, withAttributes: stringAttributes) // draw text within rect
|
||||
let image = UIGraphicsGetImageFromCurrentImageContext() // create image from context
|
||||
UIGraphicsEndImageContext() // end image context
|
||||
|
||||
return image ?? UIImage()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user