Skip to content

Instantly share code, notes, and snippets.

View vialyx's full-sized avatar
🎯
Focusing

Maxim Vialyx vialyx

🎯
Focusing
View GitHub Profile
// MARK: - INUIAddVoiceShortcutViewControllerDelegate
extension TransportListViewController: INUIAddVoiceShortcutViewControllerDelegate {
@available(iOS 12.0, *)
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
print("addVoiceShortcutViewController didFinishWith voiceShortcut: \(String(describing: voiceShortcut)) error: \(String(describing: error))")
setupIntents()
controller.dismiss(animated: true, completion: nil)
}
import IntentsUI
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
// Call setup once at view did load
addObservers()
setupIntents()
}
enum Shortcuts: String {
case refreshList = "com.vialyx.MVD.refresh_list"
var identifier: String {
return rawValue
}
var title: String {
switch self {
case .refreshList:
<key>NSUserActivityTypes</key>
<dict>
<key>Refresh</key>
<string>com.vialyx.MVD.refresh_list</string>
</dict>
extension Date {
func get(_ unit: Calendar.Component) -> Int {
return Calendar.current.component(unit, from: self)
}
var hour: Int {
return get(.hour)
}
extension Date {
func add(_ unit: Calendar.Component, value: Int) -> Date {
return Calendar.current.date(byAdding: unit, value: value, to: self)!
}
func add(year: Int) -> Date {
return add(.year, value: year)
}
extension Date {
var time: Date? {
let components = Calendar.current.dateComponents([.hour, .minute], from: self)
return Calendar.current.date(from: components)
}
}
let today = Date() // "Sep 17, 2018 at 7:49 PM"
let startOfDay = Calendar.current.startOfDay(for: Date())
let dateInterval = DateInterval(start: startOfDay, duration: 8 * 60 * 60)
let meetingDate = startOfDay + 13 * 60 * 60
print("Are we have a meeting today?: \(dateInterval.contains(meetingDate))")
// Are we have a meeting today?: false
// Yeah! Today a good day for productive work without meetings!
let startOfDay = Calendar.current.startOfDay(for: Date())
let endOfDay = startOfDay + 8 * 60 * 60
let dateInterval = DateInterval(start: startOfDay, end: endOfDay)
let startLunch = startOfDay + 4 * 60 * 60
let lunchInterval = DateInterval(start: startLunch, duration: 60 * 60)
print("intersection: \(String(describing: dateInterval.intersection(with: lunchInterval)))")
// intersection: Optional(2018-09-17 01:00:00 +0000 to 2018-09-17 02:00:00 +0000)
print("intersects: \(dateInterval.intersects(lunchInterval))")
extension Date {
func compareTime(with date: Date) -> ComparisonResult {
var components = DateComponents()
components.hour = calendar.component(.hour, from: self)
components.minute = calendar.component(.minute, from: self)
var withComponents = DateComponents()
withComponents.hour = calendar.component(.hour, from: date)
withComponents.minute = calendar.component(.minute, from: date)