Created
April 23, 2024 11:14
-
-
Save abhijithvijayan/52777175ad3a9c21f42442443cc775a6 to your computer and use it in GitHub Desktop.
swiftui-dateformat-fix.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run any SwiftUI view as a Mac app. | |
import Cocoa | |
import SwiftUI | |
import Foundation | |
public extension Date | |
{ | |
static let shortDateFormatter: DateFormatter = { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateStyle = .short | |
dateFormatter.timeStyle = .none | |
return dateFormatter | |
}() | |
static let mediumDateFormatter: DateFormatter = { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateStyle = .medium | |
dateFormatter.timeStyle = .none | |
return dateFormatter | |
}() | |
func numberOfCalendarDays(since date: Date) -> Int | |
{ | |
let today = Calendar.current.startOfDay(for: self) | |
let previousDay = Calendar.current.startOfDay(for: date) | |
let components = Calendar.current.dateComponents([.day], from: previousDay, to: today) | |
return components.day! | |
} | |
func relativeDateString(since date: Date, dateFormatter: DateFormatter? = nil) -> String | |
{ | |
let dateFormatter = dateFormatter ?? Date.mediumDateFormatter | |
let numberOfDays = self.numberOfCalendarDays(since: date) | |
switch numberOfDays | |
{ | |
case 0: return NSLocalizedString("Today", comment: "") | |
case 1: return NSLocalizedString("Yesterday", comment: "") | |
default: return dateFormatter.string(from: date) | |
} | |
} | |
} | |
let twoDaysAgo = Date.now.addingTimeInterval(-1 * 2 * 24 * 60 * 60) | |
let currentDate = Date() | |
let numberOfDays = max(twoDaysAgo.numberOfCalendarDays(since: currentDate), 0) | |
let numberOfDaysText: String | |
if numberOfDays == 1 | |
{ | |
numberOfDaysText = NSLocalizedString("1 day", comment: "") | |
} | |
else | |
{ | |
numberOfDaysText = String(format: NSLocalizedString("%@ days", comment: ""), NSNumber(value: numberOfDays)) | |
} | |
NSApplication.shared.run { | |
VStack { | |
Text(numberOfDaysText) | |
.padding() | |
.background(Capsule().fill(Color.blue)) | |
.padding() | |
} | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
} | |
extension NSApplication { | |
public func run<V: View>(@ViewBuilder view: () -> V) { | |
let appDelegate = AppDelegate(view()) | |
NSApp.setActivationPolicy(.regular) | |
mainMenu = customMenu | |
delegate = appDelegate | |
run() | |
} | |
} | |
// Inspired by https://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html | |
extension NSApplication { | |
var customMenu: NSMenu { | |
let appMenu = NSMenuItem() | |
appMenu.submenu = NSMenu() | |
let appName = ProcessInfo.processInfo.processName | |
appMenu.submenu?.addItem(NSMenuItem(title: "About \(appName)", action: #selector(NSApplication.orderFrontStandardAboutPanel(_:)), keyEquivalent: "")) | |
appMenu.submenu?.addItem(NSMenuItem.separator()) | |
let services = NSMenuItem(title: "Services", action: nil, keyEquivalent: "") | |
self.servicesMenu = NSMenu() | |
services.submenu = self.servicesMenu | |
appMenu.submenu?.addItem(services) | |
appMenu.submenu?.addItem(NSMenuItem.separator()) | |
appMenu.submenu?.addItem(NSMenuItem(title: "Hide \(appName)", action: #selector(NSApplication.hide(_:)), keyEquivalent: "h")) | |
let hideOthers = NSMenuItem(title: "Hide Others", action: #selector(NSApplication.hideOtherApplications(_:)), keyEquivalent: "h") | |
hideOthers.keyEquivalentModifierMask = [.command, .option] | |
appMenu.submenu?.addItem(hideOthers) | |
appMenu.submenu?.addItem(NSMenuItem(title: "Show All", action: #selector(NSApplication.unhideAllApplications(_:)), keyEquivalent: "")) | |
appMenu.submenu?.addItem(NSMenuItem.separator()) | |
appMenu.submenu?.addItem(NSMenuItem(title: "Quit \(appName)", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")) | |
let windowMenu = NSMenuItem() | |
windowMenu.submenu = NSMenu(title: "Window") | |
windowMenu.submenu?.addItem(NSMenuItem(title: "Minmize", action: #selector(NSWindow.miniaturize(_:)), keyEquivalent: "m")) | |
windowMenu.submenu?.addItem(NSMenuItem(title: "Zoom", action: #selector(NSWindow.performZoom(_:)), keyEquivalent: "")) | |
windowMenu.submenu?.addItem(NSMenuItem.separator()) | |
windowMenu.submenu?.addItem(NSMenuItem(title: "Show All", action: #selector(NSApplication.arrangeInFront(_:)), keyEquivalent: "m")) | |
let mainMenu = NSMenu(title: "Main Menu") | |
mainMenu.addItem(appMenu) | |
mainMenu.addItem(windowMenu) | |
return mainMenu | |
} | |
} | |
class AppDelegate<V: View>: NSObject, NSApplicationDelegate, NSWindowDelegate { | |
init(_ contentView: V) { | |
self.contentView = contentView | |
} | |
var window: NSWindow! | |
var hostingView: NSView? | |
var contentView: V | |
func applicationDidFinishLaunching(_ notification: Notification) { | |
window = NSWindow( | |
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), | |
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], | |
backing: .buffered, defer: false) | |
window.center() | |
window.setFrameAutosaveName("Main Window") | |
hostingView = NSHostingView(rootView: contentView) | |
window.contentView = hostingView | |
window.makeKeyAndOrderFront(nil) | |
window.delegate = self | |
NSApp.activate(ignoringOtherApps: true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment