Skip to content

Commit

Permalink
Countdown preferences input (#5)
Browse files Browse the repository at this point in the history
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
jacquealvesb authored and bbrks committed Oct 4, 2019
1 parent 17e4a83 commit 1917aab
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 8 deletions.
13 changes: 13 additions & 0 deletions StatusbarCountdown.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
17E0B24A2344BC4400D83311 /* PreferencesWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E0B2482344BC4400D83311 /* PreferencesWindow.swift */; };
17E0B24B2344BC4400D83311 /* PreferencesWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 17E0B2492344BC4400D83311 /* PreferencesWindow.xib */; };
17E0B24D23457B6500D83311 /* ShakeWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E0B24C23457B6500D83311 /* ShakeWindow.swift */; };
B57B8F4E1ACB590D00BD794F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B57B8F4D1ACB590D00BD794F /* AppDelegate.swift */; };
B57B8F501ACB590D00BD794F /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B57B8F4F1ACB590D00BD794F /* Images.xcassets */; };
B57B8F531ACB590D00BD794F /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = B57B8F511ACB590D00BD794F /* MainMenu.xib */; };
Expand All @@ -24,6 +27,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
17E0B2482344BC4400D83311 /* PreferencesWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesWindow.swift; sourceTree = "<group>"; };
17E0B2492344BC4400D83311 /* PreferencesWindow.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PreferencesWindow.xib; sourceTree = "<group>"; };
17E0B24C23457B6500D83311 /* ShakeWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShakeWindow.swift; sourceTree = "<group>"; };
B57B8F481ACB590D00BD794F /* StatusbarCountdown.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = StatusbarCountdown.app; sourceTree = BUILT_PRODUCTS_DIR; };
B57B8F4C1ACB590D00BD794F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B57B8F4D1ACB590D00BD794F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -76,6 +82,9 @@
B57B8F4D1ACB590D00BD794F /* AppDelegate.swift */,
B57B8F4F1ACB590D00BD794F /* Images.xcassets */,
B57B8F511ACB590D00BD794F /* MainMenu.xib */,
17E0B2482344BC4400D83311 /* PreferencesWindow.swift */,
17E0B2492344BC4400D83311 /* PreferencesWindow.xib */,
17E0B24C23457B6500D83311 /* ShakeWindow.swift */,
B57B8F4B1ACB590D00BD794F /* Supporting Files */,
);
path = StatusbarCountdown;
Expand Down Expand Up @@ -167,6 +176,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand All @@ -188,6 +198,7 @@
files = (
B57B8F501ACB590D00BD794F /* Images.xcassets in Resources */,
B57B8F531ACB590D00BD794F /* MainMenu.xib in Resources */,
17E0B24B2344BC4400D83311 /* PreferencesWindow.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -205,6 +216,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
17E0B24D23457B6500D83311 /* ShakeWindow.swift in Sources */,
17E0B24A2344BC4400D83311 /* PreferencesWindow.swift in Sources */,
B57B8F4E1ACB590D00BD794F /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
40 changes: 34 additions & 6 deletions StatusbarCountdown/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
import Cocoa
import Foundation

protocol PreferencesWindowDelegate {
func preferencesDidUpdate()
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
class AppDelegate: NSObject, NSApplicationDelegate, PreferencesWindowDelegate {

// TODO: Remove these hardcoded values (into a plist)
// TODO: Provide a GUI config to set plist values
let countToDate = NSDate(timeIntervalSince1970: 1597249800) // FIXME: Your countdown date here... - 2020-08-04 16:30:00 UTC
let countdownName = "Countdown Name" // FIXME: Your countdown name here...
let DEFAULT_NAME = "Countdown Name"
let DEFAULT_DATE = NSDate(timeIntervalSince1970: 1597249800)

var countToDate = NSDate(timeIntervalSince1970: 1597249800)
var countdownName = "Countdown Name"

var showName = true
var showSeconds = true
var zeroPad = false
Expand All @@ -25,8 +30,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {

let statusItem = NSStatusBar.system.statusItem(withLength: -1)
@IBOutlet weak var statusMenu: NSMenu!

var preferencesWindow: PreferencesWindow!

func applicationDidFinishLaunching(_ notification: Notification) {
preferencesWindow = PreferencesWindow()
preferencesWindow.delegate = self

statusItem.title = ""
statusItem.menu = statusMenu
formatter.minimumIntegerDigits = zeroPad ? 2 : 1
Expand All @@ -36,6 +45,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
selector: #selector(tick),
userInfo: nil,
repeats: true)

updatePreferences()
}

func preferencesDidUpdate() { // Delegate when setting values are updated
updatePreferences()
}

func updatePreferences() {
let defaults = UserDefaults.standard

// Gets the saved values in user defaults
countdownName = defaults.string(forKey: "name") ?? DEFAULT_NAME
countToDate = defaults.value(forKey: "date") as? NSDate ?? DEFAULT_DATE
}

// Calculates the difference in time from now to the specified date and sets the statusItem title
Expand Down Expand Up @@ -107,6 +130,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
formatter.minimumIntegerDigits = zeroPad ? 2 : 1
}

// MenuItem click event to open preferences popover
@IBAction func configurePreferences(_ sender: Any) {
preferencesWindow.showWindow(nil)
}

// MenuItem click event to quit application
@IBAction func quitApplication(sender: NSMenuItem) {
NSApplication.shared.terminate(self);
Expand Down
10 changes: 8 additions & 2 deletions StatusbarCountdown/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14868" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14313.18"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14868"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
Expand Down Expand Up @@ -38,6 +38,12 @@
<action selector="toggleZeroPadWithSender:" target="Voe-Tx-rLC" id="Ipf-z6-8Z2"/>
</connections>
</menuItem>
<menuItem title="Preferences" id="gEw-R4-QZI">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="configurePreferences:" target="Voe-Tx-rLC" id="15g-C7-0ee"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="DWl-xP-vrW"/>
<menuItem title="Quit" id="SeC-Z9-3ew">
<modifierMask key="keyEquivalentModifierMask"/>
Expand Down
81 changes: 81 additions & 0 deletions StatusbarCountdown/PreferencesWindow.swift
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
}
}
Loading

0 comments on commit 1917aab

Please sign in to comment.