-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #2 and closes #3 * Create settings view * Show settings menu * Saving name and date settings * Handle bad input * Add comments * Rename settings to preferences
- Loading branch information
1 parent
17e4a83
commit 1917aab
Showing
6 changed files
with
306 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// PreferencesWindow.swift | ||
// StatusbarCountdown | ||
// | ||
// Created by Jacqueline Alves on 02/10/19. | ||
// Copyright © 2019 Ben Brooks. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
class PreferencesWindow: NSWindowController, NSWindowDelegate { | ||
|
||
@IBOutlet weak var nameTextField: NSTextField! | ||
@IBOutlet weak var datePicker: NSDatePicker! | ||
@IBOutlet weak var nameBadInputFeedback: NSTextField! | ||
@IBOutlet weak var dateBadInputFeedback: NSTextField! | ||
|
||
var delegate: PreferencesWindowDelegate? | ||
|
||
override func windowDidLoad() { | ||
super.windowDidLoad() | ||
|
||
self.window?.center() // Center the popover | ||
self.window?.makeKeyAndOrderFront(nil) // Make popover appear on top of anything else | ||
|
||
NSApp.activate(ignoringOtherApps: true) // Activate popover | ||
|
||
nameTextField.delegate = self | ||
} | ||
|
||
override var windowNibName : String! { | ||
return "PreferencesWindow" | ||
} | ||
|
||
func save() { | ||
let defaults = UserDefaults.standard | ||
|
||
defaults.set(nameTextField.stringValue, forKey: "name") | ||
defaults.set(datePicker.dateValue, forKey: "date") | ||
|
||
delegate?.preferencesDidUpdate() | ||
} | ||
|
||
@IBAction func validate(_ sender: Any) { | ||
var validation = true | ||
|
||
if nameTextField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines) == "" { // Check if name is empty | ||
nameBadInputFeedback.isHidden = false | ||
window?.shakeWindow() | ||
|
||
validation = false | ||
|
||
} | ||
|
||
if datePicker.dateValue < Date() { // Check if date is after today | ||
dateBadInputFeedback.isHidden = false | ||
window?.shakeWindow() | ||
|
||
validation = false | ||
} | ||
|
||
if validation { // Everything is ok, save values to user defaults and close popover | ||
save() | ||
close() | ||
} | ||
} | ||
|
||
@IBAction func changeDatePicker(_ sender: Any) { | ||
dateBadInputFeedback.isHidden = true // Hide bad input feedback when change the date | ||
} | ||
|
||
@IBAction func closePopover(_ sender: Any) { | ||
close() | ||
} | ||
} | ||
|
||
extension PreferencesWindow: NSTextFieldDelegate { | ||
func controlTextDidChange(_ obj: Notification) { | ||
nameBadInputFeedback.isHidden = true // Hide bad input feedback when something is entered on textfield | ||
} | ||
} |
Oops, something went wrong.