Add toggle button to display favorite state

This commit is contained in:
Davide De Rosa 2021-07-25 10:38:09 +02:00
parent 18da02e5a1
commit 9cec70247a
2 changed files with 65 additions and 11 deletions

View File

@ -47,6 +47,8 @@ class ProviderServiceView: NSView {
@IBOutlet private weak var buttonRefreshInfrastructure: NSButton!
@IBOutlet private weak var buttonFavorite: NSButton!
var isEnabled: Bool = true {
didSet {
popupCategory.isEnabled = isEnabled
@ -83,6 +85,9 @@ class ProviderServiceView: NSView {
labelCategoryCaption.stringValue = L10n.App.Service.Cells.Category.caption.asCaption
labelLocationCaption.stringValue = L10n.Core.Service.Cells.Provider.Pool.caption.asCaption
buttonRefreshInfrastructure.image = NSImage(named: NSImage.refreshTemplateName)
buttonFavorite.image = NSImage(named: NSImage.bookmarksTemplateName)
updateFavoriteState()
}
// MARK: Actions
@ -98,6 +103,7 @@ class ProviderServiceView: NSView {
@IBAction private func selectLocation(_ sender: Any?) {
loadAreas()
if let pool = selectedPool() {
updateFavoriteState()
delegate?.providerView(self, didSelectPool: pool)
}
}
@ -113,6 +119,20 @@ class ProviderServiceView: NSView {
delegate?.providerViewDidRequestInfrastructureRefresh(self)
}
@IBAction private func toggleFavorite(_ sender: Any?) {
guard let category = selectedCategory(), let group = selectedGroup() else {
return
}
let groupId = group.uniqueId(in: category)
let isFavorite = buttonFavorite.state == .on
if isFavorite {
profile?.favoriteGroupIds = profile?.favoriteGroupIds ?? []
profile?.favoriteGroupIds?.append(groupId)
} else {
profile?.favoriteGroupIds?.removeAll { $0 == groupId }
}
}
// MARK: Helpers
func reloadData() {
@ -210,6 +230,14 @@ class ProviderServiceView: NSView {
popupArea.isHidden = menu.items.isEmpty
}
private func selectedCategory() -> PoolCategory? {
return popupCategory.selectedItem?.representedObject as? PoolCategory
}
private func selectedGroup() -> PoolGroup? {
return popupLocation.selectedItem?.representedObject as? PoolGroup
}
private func selectedPool() -> Pool? {
guard popupArea.numberOfItems > 0 else {
guard let group = popupLocation.selectedItem?.representedObject as? PoolGroup else {
@ -219,4 +247,13 @@ class ProviderServiceView: NSView {
}
return popupArea.itemArray.first?.representedObject as? Pool
}
private func updateFavoriteState() {
guard let category = selectedCategory(), let group = selectedGroup() else {
return
}
let groupId = group.uniqueId(in: category)
let isFavorite = profile?.favoriteGroupIds?.contains(groupId) ?? false
buttonFavorite.state = isFavorite ? .on : .off
}
}

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="18122" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="18122"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
@ -10,11 +10,11 @@
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="sHl-x0-m0K" customClass="ProviderServiceView" customModule="Passepartout" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="450" height="120"/>
<rect key="frame" x="0.0" y="0.0" width="450" height="117"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="lnT-CF-seE">
<rect key="frame" x="-2" y="101" width="114" height="17"/>
<rect key="frame" x="-2" y="99" width="114" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" title="C:" id="nKl-9a-xar">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@ -22,7 +22,7 @@
</textFieldCell>
</textField>
<popUpButton horizontalHuggingPriority="249" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="VhE-Zb-FmE">
<rect key="frame" x="118" y="96" width="285" height="25"/>
<rect key="frame" x="117" y="93" width="287" height="25"/>
<popUpButtonCell key="cell" type="push" title="Category" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="qEL-KY-U1o" id="caC-KG-3AH">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
@ -37,7 +37,7 @@
</connections>
</popUpButton>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Q5f-wF-CyU">
<rect key="frame" x="118" y="77" width="77" height="14"/>
<rect key="frame" x="118" y="75" width="77" height="14"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Last updated:" id="rXU-3s-USr">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@ -45,7 +45,7 @@
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="kSn-Qv-xSD" userLabel="Label Location Caption">
<rect key="frame" x="-2" y="33" width="114" height="17"/>
<rect key="frame" x="-2" y="32" width="114" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" title="L:" id="9nM-rm-VgR">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@ -53,7 +53,7 @@
</textFieldCell>
</textField>
<popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Ri0-Ls-IYO">
<rect key="frame" x="118" y="28" width="285" height="25"/>
<rect key="frame" x="117" y="26" width="287" height="25"/>
<popUpButtonCell key="cell" type="push" title="Location" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="0Iw-Gz-qU0" id="7cD-BZ-osm">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
@ -70,7 +70,7 @@
</connections>
</popUpButton>
<textField hidden="YES" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="HF2-PC-G6P" userLabel="Label Area Caption">
<rect key="frame" x="-2" y="2" width="114" height="17"/>
<rect key="frame" x="-2" y="2" width="114" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" title="A:" id="UXF-Ej-6Ua">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@ -78,7 +78,7 @@
</textFieldCell>
</textField>
<popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="IsL-OU-zXZ">
<rect key="frame" x="118" y="-3" width="285" height="25"/>
<rect key="frame" x="117" y="-4" width="287" height="25"/>
<popUpButtonCell key="cell" type="push" title="Area" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="glB-Ir-wE8" id="7qV-EM-r3X">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
@ -93,7 +93,7 @@
</connections>
</popUpButton>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="bE9-EU-Xzq">
<rect key="frame" x="404" y="92" width="52" height="32"/>
<rect key="frame" x="403" y="90" width="54" height="32"/>
<constraints>
<constraint firstAttribute="width" constant="40" id="Y6u-np-cEO"/>
</constraints>
@ -105,6 +105,19 @@
<action selector="refreshInfrastructure:" target="sHl-x0-m0K" id="aPQ-BZ-Q9m"/>
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="JVN-JS-Vr7">
<rect key="frame" x="403" y="23" width="54" height="32"/>
<constraints>
<constraint firstAttribute="width" constant="40" id="zrS-eu-hUt"/>
</constraints>
<buttonCell key="cell" type="push" title="FAV" bezelStyle="rounded" alignment="center" borderStyle="border" inset="2" id="IWi-Ee-l6X">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES" changeBackground="YES" changeGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="toggleFavorite:" target="sHl-x0-m0K" id="jDS-u5-bI6"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="VhE-Zb-FmE" firstAttribute="top" secondItem="sHl-x0-m0K" secondAttribute="top" id="3zG-8f-Ij8"/>
@ -112,13 +125,16 @@
<constraint firstAttribute="trailing" secondItem="bE9-EU-Xzq" secondAttribute="trailing" id="Cou-HK-JNt"/>
<constraint firstItem="lnT-CF-seE" firstAttribute="centerY" secondItem="VhE-Zb-FmE" secondAttribute="centerY" id="ElO-QZ-sgv"/>
<constraint firstItem="Q5f-wF-CyU" firstAttribute="leading" secondItem="VhE-Zb-FmE" secondAttribute="leading" id="KFp-CA-AHU"/>
<constraint firstItem="JVN-JS-Vr7" firstAttribute="centerY" secondItem="Ri0-Ls-IYO" secondAttribute="centerY" id="LFQ-32-SgB"/>
<constraint firstItem="HF2-PC-G6P" firstAttribute="centerY" secondItem="IsL-OU-zXZ" secondAttribute="centerY" id="NTF-FX-jMd"/>
<constraint firstAttribute="bottom" secondItem="IsL-OU-zXZ" secondAttribute="bottom" id="Ryi-JT-4C3"/>
<constraint firstItem="HF2-PC-G6P" firstAttribute="leading" secondItem="lnT-CF-seE" secondAttribute="leading" id="Src-yJ-Rgx"/>
<constraint firstItem="Ri0-Ls-IYO" firstAttribute="trailing" secondItem="VhE-Zb-FmE" secondAttribute="trailing" id="TJa-4c-18u"/>
<constraint firstAttribute="trailing" secondItem="JVN-JS-Vr7" secondAttribute="trailing" id="UOH-nd-6bl"/>
<constraint firstItem="IsL-OU-zXZ" firstAttribute="trailing" secondItem="Ri0-Ls-IYO" secondAttribute="trailing" id="YCr-qy-rGi"/>
<constraint firstItem="kSn-Qv-xSD" firstAttribute="trailing" secondItem="lnT-CF-seE" secondAttribute="trailing" id="aP2-Pc-I1b"/>
<constraint firstItem="Q5f-wF-CyU" firstAttribute="top" secondItem="VhE-Zb-FmE" secondAttribute="bottom" constant="8" id="cf6-K8-a1Q"/>
<constraint firstItem="JVN-JS-Vr7" firstAttribute="leading" secondItem="Ri0-Ls-IYO" secondAttribute="trailing" constant="10" id="cqg-c7-ybl"/>
<constraint firstItem="IsL-OU-zXZ" firstAttribute="leading" secondItem="Ri0-Ls-IYO" secondAttribute="leading" id="eVk-E1-11K"/>
<constraint firstItem="Ri0-Ls-IYO" firstAttribute="leading" secondItem="VhE-Zb-FmE" secondAttribute="leading" id="fw7-wV-jJg"/>
<constraint firstItem="VhE-Zb-FmE" firstAttribute="leading" secondItem="sHl-x0-m0K" secondAttribute="leading" constant="120" id="gAw-cm-xhQ"/>
@ -132,6 +148,7 @@
<constraint firstItem="bE9-EU-Xzq" firstAttribute="leading" secondItem="VhE-Zb-FmE" secondAttribute="trailing" constant="10" id="xNf-cQ-LJw"/>
</constraints>
<connections>
<outlet property="buttonFavorite" destination="JVN-JS-Vr7" id="CW9-0i-1uL"/>
<outlet property="buttonRefreshInfrastructure" destination="bE9-EU-Xzq" id="fna-ol-MOT"/>
<outlet property="labelCategoryCaption" destination="lnT-CF-seE" id="QKu-yJ-nkS"/>
<outlet property="labelLastInfrastructureUpdate" destination="Q5f-wF-CyU" id="SpA-o9-pY2"/>