Browse Source

Combining Timezones and Locations.

pull/92/head
Abhishek 5 years ago
parent
commit
333d39b548
  1. 149
      Clocker/Preferences/General/PreferencesViewController.swift
  2. 74
      Clocker/Preferences/Preferences.storyboard

149
Clocker/Preferences/General/PreferencesViewController.swift

@ -64,8 +64,6 @@ class PreferencesViewController: ParentViewController {
@IBOutlet private var searchField: NSSearchField!
@IBOutlet private var messageLabel: NSTextField!
@IBOutlet private var searchCriteria: NSSegmentedControl!
@IBOutlet private var abbreviation: NSTableColumn!
@IBOutlet private var headerView: NSView!
@IBOutlet private var tableview: NSView!
@ -99,6 +97,9 @@ class PreferencesViewController: ParentViewController {
themeDidChangeNotification = NotificationCenter.default.addObserver(forName: .themeDidChangeNotification, object: nil, queue: OperationQueue.main) { _ in
self.setup()
}
searchField.placeholderString = "Enter city, state, country or timezone name"
setupTimezoneDatasource()
}
deinit {
@ -326,6 +327,15 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate
return numberOfRows
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if let message = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "resultCell"), owner: self) as? SearchResultTableViewCell {
message.sourceName.stringValue = "Nicaragua"
return message
}
return nil;
}
func tableView(_: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
var dataSource: TimezoneData?
@ -344,7 +354,13 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate
}
if tableColumn?.identifier.rawValue == PreferencesConstants.availableTimezoneIdentifier {
return handleAvailableTimezoneColumn(for: row, dataSource)
if filteredArray.isEmpty {
return timezoneArray[row]
}
return dataSource != nil ?
handleAvailableTimezoneColumn(for: row, dataSource) :
filteredArray[row] as? String
}
if tableColumn?.identifier.rawValue == PreferencesConstants.customLabelIdentifier {
@ -375,33 +391,13 @@ extension PreferencesViewController: NSTableViewDataSource, NSTableViewDelegate
}
private func handleAvailableTimezoneColumn(for row: Int, _ dataSource: TimezoneData?) -> Any? {
let criteria = searchCriteria.selectedSegment
if criteria == 0 {
if row < filteredArray.count {
return dataSource?.formattedAddress
}
} else {
if searchField.stringValue.isEmpty == false, row < timezoneFilteredArray.count {
return timezoneFilteredArray[row]
}
return timezoneArray[row]
if row < filteredArray.count {
return dataSource?.formattedAddress
}
return nil
}
private func handleAbbreviationColumn(for row: Int) -> Any? {
if searchField.stringValue.isEmpty == false, row < timezoneFilteredArray.count {
let currentSelection = timezoneFilteredArray[row]
if currentSelection == "UTC" {
return "UTC"
} else if currentSelection == "Anywhere on Earth" {
return "GMT+12"
}
return NSTimeZone(name: timezoneFilteredArray[row])?.abbreviation ?? "Error"
}
if timezoneArray.count > row {
// Special return for manually inserted 'UTC'
if timezoneArray[row] == "UTC" {
@ -713,12 +709,20 @@ extension PreferencesViewController {
}
self.appendResultsToFilteredArray(searchResults!.results)
self.findLocalSearchResultsForTimezones()
self.prepareUIForPresentingResults()
}
})
}
}
private func findLocalSearchResultsForTimezones() {
timezoneFilteredArray = []
let lowercasedSearchString = searchField.stringValue.lowercased()
timezoneFilteredArray = timezoneArray.filter { $0.lowercased().contains(lowercasedSearchString) }
filteredArray.append(contentsOf: timezoneFilteredArray)
}
private func generateSearchURL() -> String {
let userPreferredLanguage = Locale.preferredLanguages.first ?? "en-US"
@ -759,6 +763,7 @@ extension PreferencesViewController {
self.filteredArray.append(TimezoneData(with: totalPackage))
}
}
private func prepareUIForPresentingResults() {
@ -920,33 +925,16 @@ extension PreferencesViewController {
availableTimezoneTableView.isHidden = false
isActivityInProgress = false
}
@IBAction func searchOptions(_: Any) {
placeholderLabel.placeholderString = CLEmptyString
placeholderLabel.isHidden = true
if searchCriteria.selectedSegment == 0 {
searchField.placeholderString = "Enter a city, state or country name"
columnName = "Place(s)"
abbreviation.isHidden = true
} else {
searchField.placeholderString = "Enter a timezone name"
columnName = "Timezone(s)"
abbreviation.isHidden = false
timezoneArray = []
timezoneArray.append("UTC")
timezoneArray.append("Anywhere on Earth")
timezoneArray.append(contentsOf: NSTimeZone.knownTimeZoneNames)
}
searchField.stringValue = CLEmptyString
availableTimezoneTableView.reloadData()
private func setupTimezoneDatasource() {
timezoneArray = []
timezoneArray.append("UTC")
timezoneArray.append("Anywhere on Earth")
timezoneArray.append(contentsOf: NSTimeZone.knownTimeZoneNames)
}
@IBAction func addTimeZone(_: NSButton) {
abbreviation.isHidden = true
filteredArray = []
searchCriteria.selectedSegment = 0
view.window?.beginSheet(timezonePanel,
completionHandler: nil)
}
@ -981,25 +969,25 @@ extension PreferencesViewController {
return
}
if searchCriteria.selectedSegment == 0 {
guard let dataObject = filteredArray[availableTimezoneTableView.selectedRow] as? TimezoneData else {
if let dataObject = filteredArray[availableTimezoneTableView.selectedRow] as? String, dataObject != nil {
cleanupAfterInstallingTimezone()
return
}
guard let dataObject = filteredArray[availableTimezoneTableView.selectedRow] as? TimezoneData else {
assertionFailure("Data was unexpectedly nil")
return
}
if messageLabel.stringValue.isEmpty {
searchField.stringValue = CLEmptyString
guard let latitude = dataObject.latitude, let longitude = dataObject.longitude else {
assertionFailure("Data was unexpectedly nil")
return
}
if messageLabel.stringValue.isEmpty {
searchField.stringValue = CLEmptyString
guard let latitude = dataObject.latitude, let longitude = dataObject.longitude else {
assertionFailure("Data was unexpectedly nil")
return
}
getTimezone(for: latitude, and: longitude)
}
} else {
cleanupAfterInstallingTimezone()
getTimezone(for: latitude, and: longitude)
}
}
@ -1008,13 +996,18 @@ extension PreferencesViewController {
data.setLabel(CLEmptyString)
if searchField.stringValue.isEmpty == false {
if timezoneFilteredArray.count <= availableTimezoneTableView.selectedRow {
if filteredArray.count <= availableTimezoneTableView.selectedRow {
return
}
let currentSelection = timezoneFilteredArray[availableTimezoneTableView.selectedRow]
let currentSelection = filteredArray[availableTimezoneTableView.selectedRow]
guard let selection = currentSelection as? String else {
assertionFailure()
return
}
let metaInfo = metadata(for: currentSelection)
let metaInfo = metadata(for: selection)
data.timezoneID = metaInfo.0
data.formattedAddress = metaInfo.1
@ -1031,10 +1024,9 @@ extension PreferencesViewController {
let operationObject = TimezoneDataOperations(with: data)
operationObject.saveObject()
filteredArray = []
timezoneFilteredArray = []
timezoneArray = []
availableTimezoneTableView.reloadData()
refreshTimezoneTableView()
@ -1067,10 +1059,6 @@ extension PreferencesViewController {
@IBAction func closePanel(_: NSButton) {
filteredArray = []
timezoneArray = []
searchCriteria.setSelected(true, forSegment: 0)
columnName = "Place(s)"
availableTimezoneTableView.reloadData()
@ -1181,11 +1169,6 @@ extension PreferencesViewController {
}
@IBAction func filterArray(_ sender: Any?) {
if searchCriteria.selectedSegment == 1 {
filterTimezoneArray(sender)
return
}
messageLabel.stringValue = CLEmptyString
filteredArray = []
@ -1335,12 +1318,8 @@ extension PreferencesViewController: SRRecorderControlDelegate {}
// Helpers
extension PreferencesViewController {
private func numberOfSearchResults() -> Int {
if searchCriteria.selectedSegment == 0 {
return filteredArray.count
}
if searchField.stringValue.isEmpty == false {
return timezoneFilteredArray.count
return filteredArray.count
}
return timezoneArray.count
@ -1353,3 +1332,7 @@ extension PreferencesViewController {
DataStore.shared().setTimezones(newDefaults)
}
}
class SearchResultTableViewCell: NSTableCellView {
@IBOutlet var sourceName: NSTextField!
}

74
Clocker/Preferences/Preferences.storyboard

@ -1620,7 +1620,6 @@ CA
</constraints>
</view>
<connections>
<outlet property="abbreviation" destination="Uln-qb-hZH" id="Il2-Md-pRf"/>
<outlet property="addButton" destination="2Cm-Ys-lwo" id="yCf-T8-d17"/>
<outlet property="addTimezoneButton" destination="1FY-Nb-ZXb" id="mzu-MA-c0E"/>
<outlet property="additionalSortOptions" destination="J2K-hR-0jv" id="oXh-9H-RRO"/>
@ -1633,7 +1632,6 @@ CA
<outlet property="placeholderLabel" destination="xgb-wU-8RU" id="6Ys-l8-FZT"/>
<outlet property="progressIndicator" destination="0A5-gp-lay" id="Ssa-ht-IvM"/>
<outlet property="recorderControl" destination="PbD-dt-goz" id="ZH4-DH-X0L"/>
<outlet property="searchCriteria" destination="4q8-Io-BxM" id="YX2-ws-aMk"/>
<outlet property="searchField" destination="Dha-h9-Nd0" id="SNI-zE-aNT"/>
<outlet property="sortToggle" destination="VGu-Z8-ktt" id="7dH-BI-HrP"/>
<outlet property="stackView" destination="1sT-pO-jK4" id="VKO-mK-cJO"/>
@ -1715,31 +1713,14 @@ DQ
<action selector="addToFavorites:" target="ZT5-cA-xLj" id="GAu-wJ-aEf"/>
</connections>
</button>
<segmentedControl verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="4q8-Io-BxM">
<rect key="frame" x="6" y="253" width="294" height="24"/>
<constraints>
<constraint firstAttribute="width" constant="290" id="g7h-e7-gVN"/>
<constraint firstAttribute="height" constant="21" id="v1u-e6-ebc"/>
</constraints>
<segmentedCell key="cell" borderStyle="border" alignment="left" style="rounded" trackingMode="selectOne" id="2NQ-1E-dBz">
<font key="font" size="12" name="Avenir-Book"/>
<segments>
<segment label="Search By City" width="144" selected="YES"/>
<segment label="Search by Timezone(s)" width="143" tag="1"/>
</segments>
</segmentedCell>
<connections>
<action selector="searchOptions:" target="ZT5-cA-xLj" id="Ct6-bF-UH0"/>
</connections>
</segmentedControl>
<scrollView focusRingType="none" borderType="none" autohidesScrollers="YES" horizontalLineScroll="27" horizontalPageScroll="10" verticalLineScroll="27" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0wY-ff-FLW">
<rect key="frame" x="8" y="30" width="329" height="217"/>
<scrollView focusRingType="none" borderType="none" autohidesScrollers="YES" horizontalLineScroll="32" horizontalPageScroll="10" verticalLineScroll="32" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0wY-ff-FLW">
<rect key="frame" x="8" y="30" width="329" height="244"/>
<clipView key="contentView" drawsBackground="NO" id="rGc-3M-cCq">
<rect key="frame" x="0.0" y="0.0" width="329" height="217"/>
<autoresizingMask key="autoresizingMask"/>
<rect key="frame" x="0.0" y="0.0" width="329" height="244"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView focusRingType="none" verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" multipleSelection="NO" emptySelection="NO" autosaveColumns="NO" rowHeight="25" headerView="mSc-7G-W5K" id="xkl-2X-ZCb">
<rect key="frame" x="0.0" y="0.0" width="329" height="192"/>
<tableView focusRingType="none" verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" multipleSelection="NO" emptySelection="NO" autosaveColumns="NO" rowHeight="30" rowSizeStyle="automatic" headerView="mSc-7G-W5K" viewBased="YES" id="xkl-2X-ZCb">
<rect key="frame" x="0.0" y="0.0" width="329" height="219"/>
<autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
@ -1757,19 +1738,26 @@ DQ
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
</tableColumn>
<tableColumn identifier="abbreviation" editable="NO" width="90" minWidth="90" maxWidth="100" id="Uln-qb-hZH">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" alignment="left" title="Abbreviation">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</tableHeaderCell>
<textFieldCell key="dataCell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" alignment="left" title="Text Cell" id="o6C-hG-elS">
<font key="font" size="13" name="Avenir-Light"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView identifier="resultCell" id="JKD-C2-vcz" customClass="SearchResultTableViewCell" customModule="Clocker" customModuleProvider="target">
<rect key="frame" x="1" y="1" width="225" height="30"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="eZk-tq-PuC">
<rect key="frame" x="0.0" y="1" width="225" height="29"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="aam-iP-2ne">
<font key="font" size="13" name="Avenir-Book"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<connections>
<outlet property="sourceName" destination="eZk-tq-PuC" id="yez-5P-ZB1"/>
</connections>
</tableCellView>
</prototypeCellViews>
</tableColumn>
</tableColumns>
<connections>
@ -1799,14 +1787,14 @@ DQ
</tableHeaderView>
</scrollView>
<progressIndicator wantsLayer="YES" focusRingType="none" horizontalHuggingPriority="750" verticalHuggingPriority="750" maxValue="100" displayedWhenStopped="NO" bezeled="NO" indeterminate="YES" controlSize="small" style="spinning" translatesAutoresizingMaskIntoConstraints="NO" id="0A5-gp-lay">
<rect key="frame" x="165" y="145" width="16" height="16"/>
<rect key="frame" x="165" y="159" width="16" height="16"/>
<constraints>
<constraint firstAttribute="height" constant="16" id="fgE-77-Vda"/>
<constraint firstAttribute="width" constant="16" id="pwe-em-e0a"/>
</constraints>
</progressIndicator>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="xgb-wU-8RU">
<rect key="frame" x="18" y="115" width="309" height="22"/>
<rect key="frame" x="18" y="129" width="309" height="22"/>
<constraints>
<constraint firstAttribute="height" constant="22" id="zqt-d8-yas"/>
</constraints>
@ -1819,11 +1807,11 @@ DQ
</textField>
</subviews>
<constraints>
<constraint firstItem="0wY-ff-FLW" firstAttribute="top" secondItem="Dha-h9-Nd0" secondAttribute="bottom" constant="10" id="4pP-iK-ocx"/>
<constraint firstAttribute="trailing" secondItem="Dha-h9-Nd0" secondAttribute="trailing" constant="8" id="58h-kG-FMP"/>
<constraint firstItem="Dha-h9-Nd0" firstAttribute="leading" secondItem="MAe-t5-3A2" secondAttribute="leading" constant="8" id="7pJ-se-0dL"/>
<constraint firstItem="AnV-LM-kjm" firstAttribute="leading" secondItem="woG-LO-TxX" secondAttribute="trailing" constant="4" id="8s4-Ky-rWp"/>
<constraint firstItem="2Cm-Ys-lwo" firstAttribute="leading" secondItem="MAe-t5-3A2" secondAttribute="leading" constant="8" id="8vH-dk-XjK"/>
<constraint firstItem="0wY-ff-FLW" firstAttribute="top" secondItem="4q8-Io-BxM" secondAttribute="bottom" constant="8" id="Eas-R1-5LS"/>
<constraint firstItem="xgb-wU-8RU" firstAttribute="centerY" secondItem="xkl-2X-ZCb" secondAttribute="centerY" id="FHB-BT-5VB"/>
<constraint firstItem="2Cm-Ys-lwo" firstAttribute="top" secondItem="0wY-ff-FLW" secondAttribute="bottom" constant="5" id="GuG-mS-JAm"/>
<constraint firstItem="xgb-wU-8RU" firstAttribute="top" secondItem="0A5-gp-lay" secondAttribute="bottom" constant="8" id="HRl-Hn-8d4"/>
@ -1834,8 +1822,6 @@ DQ
<constraint firstItem="woG-LO-TxX" firstAttribute="leading" secondItem="2Cm-Ys-lwo" secondAttribute="trailing" constant="2" id="UBG-88-psb"/>
<constraint firstAttribute="trailing" secondItem="xgb-wU-8RU" secondAttribute="trailing" constant="20" id="dUM-1R-ATc"/>
<constraint firstAttribute="trailing" secondItem="0wY-ff-FLW" secondAttribute="trailing" constant="8" id="f6z-6j-u7B"/>
<constraint firstItem="4q8-Io-BxM" firstAttribute="top" secondItem="Dha-h9-Nd0" secondAttribute="bottom" constant="8" id="f8C-DU-Cqx"/>
<constraint firstItem="4q8-Io-BxM" firstAttribute="leading" secondItem="MAe-t5-3A2" secondAttribute="leading" constant="8" id="fXK-Yn-UFx"/>
<constraint firstAttribute="bottom" secondItem="2Cm-Ys-lwo" secondAttribute="bottom" constant="5" id="kds-2H-RO1"/>
<constraint firstAttribute="bottom" secondItem="woG-LO-TxX" secondAttribute="bottom" constant="5" id="lBw-of-9z2"/>
<constraint firstAttribute="bottom" secondItem="AnV-LM-kjm" secondAttribute="bottom" constant="8" id="rFS-Yj-u70"/>
@ -1853,7 +1839,7 @@ DQ
<image name="AdditionalPreferencesHighlighted" width="700" height="700"/>
<image name="Appearance Dynamic" width="350" height="350"/>
<image name="Calendar Tab Dynamic" width="350" height="350"/>
<image name="ClockerIcon-512" width="512" height="512"/>
<image name="ClockerIcon-512" width="1024" height="1024"/>
<image name="NSDescendingSortIndicator" width="9" height="9"/>
<image name="NSFontPanel" width="32" height="32"/>
<image name="NSInfo" width="32" height="32"/>

Loading…
Cancel
Save