Skip to content

Commit

Permalink
Fix icons
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Dec 24, 2021
1 parent fe71b12 commit 2e9411c
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 80 deletions.
2 changes: 0 additions & 2 deletions Work Hours/AppInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by Janez Troha on 24/12/2021.
//

import Foundation
import Foundation
import os.log
import SwiftUI
Expand All @@ -19,5 +18,4 @@ enum AppInfo {
static var isRunningTests: Bool {
ProcessInfo.processInfo.arguments.contains("isRunningTests") || ProcessInfo.processInfo.environment["CI"] ?? "false" != "false"
}

}
68 changes: 68 additions & 0 deletions Work Hours/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"images" : [
{
"filename" : "icon_16x16.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"filename" : "[email protected]",
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"filename" : "icon_32x32.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"filename" : "[email protected]",
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"filename" : "icon_128x128.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"filename" : "[email protected]",
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"filename" : "icon_256x256.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"filename" : "icon_512x512-1.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"filename" : "icon_512x512.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"filename" : "[email protected]",
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
File renamed without changes
File renamed without changes
21 changes: 21 additions & 0 deletions Work Hours/Defaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Defaults.swift
// Work Hours
//
// Created by Janez Troha on 24/12/2021.
//

import Defaults
import Foundation

enum StatusBarIcon: String {
case deskclock
case deskclockFill = "deskclock.fill"
case lanyardcardFill = "lanyardcard.fill"
case clockArrowCirclepath = "clock.arrow.circlepath"
}

extension Defaults.Keys {
static let statusBarIcon = Key<String>("statusBarIcon", default: StatusBarIcon.deskclock.rawValue)
static let stopOnSleep = Key<Bool>("stopOnSleep", default: true)
}
2 changes: 2 additions & 0 deletions Work Hours/Extensions/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ extension Calendar {
static func isSameYear(_ lcp: Date, _ rcp: Date) -> Bool {
return Calendar.current.compare(lcp, to: rcp, toGranularity: .year) == .orderedSame
}

static func isSameMonth(_ lcp: Date, _ rcp: Date) -> Bool {
return Calendar.current.compare(lcp, to: rcp, toGranularity: .month) == .orderedSame
}

static func isSameDay(_ lcp: Date, _ rcp: Date) -> Bool {
return Calendar.current.compare(lcp, to: rcp, toGranularity: .day) == .orderedSame
}
Expand Down
35 changes: 17 additions & 18 deletions Work Hours/Extensions/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,43 @@ extension Date {
static func - (lhs: Date, rhs: Date) -> TimeInterval {
return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
}


init(dateString:String) {
self = Date.iso8601Formatter.date(from: dateString)!
}
static let iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withFullDate,
.withTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime]
return formatter
}()
init(dateString: String) {
self = Date.iso8601Formatter.date(from: dateString)!
}

static let iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withFullDate,
.withTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime]
return formatter
}()

static let RFC3339DateFormatter: DateFormatter = {
let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(identifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return RFC3339DateFormatter
}()

static let YearMonthFormatter: DateFormatter = {
let YearMonthFormatter = DateFormatter()
YearMonthFormatter.locale = Locale(identifier: "en_US_POSIX")
YearMonthFormatter.dateFormat = "yyyy-MM"
YearMonthFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return YearMonthFormatter
}()

static let YearMonthDayFormatter: DateFormatter = {
let YearMonthFormatter = DateFormatter()
YearMonthFormatter.locale = Locale(identifier: "en_US_POSIX")
YearMonthFormatter.dateFormat = "yyyy-MM-dd"
YearMonthFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return YearMonthFormatter
}()

}

extension TimeInterval {
Expand All @@ -58,11 +57,11 @@ extension TimeInterval {

return String(format: "%0.2d:%0.2d", hours, minutes)
}

static func hoursAndMinutes(_ diff: Int) -> String {
let minutes = (diff / 60) % 60
let hours = (diff / 3600)

return String(format: "%0.2dh %0.2dm", hours, minutes)
}
}
52 changes: 24 additions & 28 deletions Work Hours/ReportsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ enum Action: String {
struct Report {
let timestamp: String
let amount: String
static func fromData(_ data: [String:Int])->[Report]{

static func fromData(_ data: [String: Int]) -> [Report] {
var reports = [Report]()
for (ts, duration) in data {
reports.append(Report(timestamp: ts, amount: TimeInterval.hoursAndMinutes(duration)))
Expand All @@ -28,7 +28,6 @@ struct Report {
}
}


enum Events {
static var logFile: URL? {
let docURL = URL(string: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!)!
Expand All @@ -42,14 +41,14 @@ enum Events {
}
return FileManager.documentDirectoryURL.appendingPathComponent("MyWorkHours").appendingPathComponent("log.csv")
}

static func write(_ action: Action, _ timestamp: Date) {
guard let logFile = logFile else {
return
}

let data = "\(action.rawValue),\(timestamp.ISO8601Format())\n"

if FileManager.default.fileExists(atPath: logFile.path) {
os_log("Appending %s to %s", data, logFile.path)
if let fileHandle = try? FileHandle(forWritingTo: logFile.absoluteURL) {
Expand All @@ -62,15 +61,15 @@ enum Events {
try? "action,timestamp\n\(data)".write(to: logFile.absoluteURL, atomically: true, encoding: .utf8)
}
}

// Restore last state from event source
static func isRunning() -> Date? {
guard let logFile = logFile else {
return nil
}

if FileManager.default.fileExists(atPath: logFile.path) {
guard let csvFile: CSV = try? CSV(url: logFile, loadColumns:false) else {
guard let csvFile: CSV = try? CSV(url: logFile, loadColumns: false) else {
return nil
}
guard let lastLine = csvFile.enumeratedRows.last else {
Expand All @@ -81,14 +80,12 @@ enum Events {
return Date(dateString: timestamp)
}
return nil

}
return nil

return nil
}

static func generateReport(formatter: DateFormatter) -> [Report]? {
var data:[String:Int] = [:]
var data: [String: Int] = [:]
guard let logFile = logFile else {
return nil
}
Expand All @@ -108,28 +105,27 @@ enum Events {
os_log("Unknown line %s", line)
return nil
}

// corrupted data
if startTimestamp != nil, endTimestamp != nil {
// same day, TODO: remove once we are ok with current state of parsing
//if formatter.string(from: startTimestamp!) == formatter.string(from: endTimestamp!) {
let elapsed = Int(endTimestamp!.timeIntervalSince(startTimestamp!))
let key = formatter.string(from: startTimestamp!)
if let val = data[key] {
data[key] = elapsed + val
}else{
data[key] = elapsed
}
startTimestamp = nil
endTimestamp = nil
//}else {
// if formatter.string(from: startTimestamp!) == formatter.string(from: endTimestamp!) {
let elapsed = Int(endTimestamp!.timeIntervalSince(startTimestamp!))
let key = formatter.string(from: startTimestamp!)
if let val = data[key] {
data[key] = elapsed + val
} else {
data[key] = elapsed
}
startTimestamp = nil
endTimestamp = nil
// }else {
// return nil
//}
// }
}
}
return Report.fromData(data)
}
return nil

}
}
29 changes: 12 additions & 17 deletions Work Hours/StatusBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,27 @@ import SwiftUI

typealias Scheduler = NSBackgroundActivityScheduler


class StatusBarController: NSObject, NSMenuDelegate {
var statusItem: NSStatusItem!
var statusItemMenu: NSMenu!
var timerModel = TimerModel()

func addSubmenu(withTitle: String, action: Selector?) -> NSMenuItem {
func addSubmenu(withTitle: String, action: Selector?) -> NSMenuItem {
let item = NSMenuItem(title: withTitle, action: action, keyEquivalent: "")
item.target = self
return item
}
func fromReports(_ reports: [Report])-> NSMenu{

func fromReports(_ reports: [Report]) -> NSMenu {
let submenu = NSMenu()
for report in reports {
submenu.addItem(addSubmenu(withTitle: "\(report.timestamp) worked \(report.amount)", action: #selector(copyToPasteboard)))
}
return submenu
}

@objc func copyToPasteboard() {

}


@objc func copyToPasteboard() {}

override
init() {
super.init()
Expand Down Expand Up @@ -78,9 +76,7 @@ class StatusBarController: NSObject, NSMenuDelegate {
addApplicationItems()
}

func menuDidClose(_: NSMenu) {

}
func menuDidClose(_: NSMenu) {}

func menuWillOpen(_: NSMenu) {
updateMenu()
Expand All @@ -94,7 +90,6 @@ class StatusBarController: NSObject, NSMenuDelegate {
}
}


func addApplicationItems() {
if !timerModel.isRunning {
let startItem = NSMenuItem(title: "Start Work", action: #selector(toggle), keyEquivalent: "s")
Expand All @@ -104,7 +99,7 @@ class StatusBarController: NSObject, NSMenuDelegate {
} else {
let changeStart = NSMenuItem(title: "Change Start Time", action: #selector(toggle), keyEquivalent: "c")
changeStart.target = self
//statusItemMenu.addItem(changeStart)
// statusItemMenu.addItem(changeStart)

let stopItem = NSMenuItem(title: "Stop Work", action: #selector(toggle), keyEquivalent: "s")
stopItem.target = self
Expand All @@ -114,14 +109,14 @@ class StatusBarController: NSObject, NSMenuDelegate {
statusItemMenu.addItem(NSMenuItem.separator())

let dailyItem = NSMenuItem(title: "Daily Reports", action: nil, keyEquivalent: "")
if let dailyReports = Events.generateReport(formatter: Date.YearMonthDayFormatter){
dailyItem.submenu = fromReports(dailyReports)
if let dailyReports = Events.generateReport(formatter: Date.YearMonthDayFormatter) {
dailyItem.submenu = fromReports(dailyReports.sorted(by: { $0.timestamp < $1.timestamp }).suffix(7))
}
statusItemMenu.addItem(dailyItem)

let monthlyItem = NSMenuItem(title: "Monthly Reports", action: nil, keyEquivalent: "")
if let monthlyReports = Events.generateReport(formatter: Date.YearMonthFormatter){
monthlyItem.submenu = fromReports(monthlyReports)
if let monthlyReports = Events.generateReport(formatter: Date.YearMonthFormatter) {
monthlyItem.submenu = fromReports(monthlyReports.sorted(by: { $0.timestamp < $1.timestamp }))
}
statusItemMenu.addItem(monthlyItem)

Expand Down
3 changes: 0 additions & 3 deletions Work Hours/TimerModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import Foundation
import os.log
import SwiftCSV




class TimerModel: ObservableObject {
@Published var display: String = "00:00"
@Published var isRunning: Bool
Expand Down
Loading

0 comments on commit 2e9411c

Please sign in to comment.