Skip to content

Commit 87f8e14

Browse files
author
Zaid Kokaja
committed
Allow packages to be upgraded automatically #4
1 parent 416a86a commit 87f8e14

File tree

3 files changed

+76
-24
lines changed

3 files changed

+76
-24
lines changed

Brewlet/AppDelegate.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, PreferencesDelegate {
5151
os_log("Notification permission error: %s", type: .error, error.debugDescription)
5252
}
5353
}
54-
5554
// Run initial tasks to set status
5655
update_upgrade(sender: nil)
5756
update_info()
@@ -257,13 +256,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, PreferencesDelegate {
257256
}
258257

259258
updateItem.isEnabled = true
259+
260+
os_log("Checked outdated status.", type: .info)
260261

261262
// Update icon in main thread
262263
DispatchQueue.main.async {
263264
self.statusItem.button?.image = NSImage(named: iconName)
265+
266+
// Upgrade packages if configured to do so
267+
let autoUpgrade = self.userDefaults.bool(forKey: "autoUpgrade")
268+
if autoUpgrade && outdatedPackageCount > 0 {
269+
os_log("Auto upgrading packages.", type: .info)
270+
let updateItem = self.statusMenu.item(withTag: self.name2tag["update"]!)!
271+
self.update_upgrade(sender: updateItem)
272+
}
264273
}
265-
266-
os_log("Checked outdated status.", type: .info)
267274
}
268275
}
269276

@@ -430,6 +437,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, PreferencesDelegate {
430437
*/
431438
func sendNotification(title: String, body: String, timeInterval: TimeInterval = 1) {
432439

440+
// Disable notifications based on user's preferences
441+
if userDefaults.bool(forKey: "dontNotify") {
442+
return
443+
}
444+
433445
// Create the notification content
434446
let content = UNMutableNotificationContent()
435447
content.title = title

Brewlet/PreferencesController.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright © 2020 zzada. All rights reserved.
77
//
88

9+
import OSLog
910
import Cocoa
1011

1112
protocol PreferencesDelegate {
@@ -19,6 +20,8 @@ class PreferencesController: NSWindowController {
1920
@IBOutlet weak var includeDependencies: NSButton!
2021
@IBOutlet weak var updateInterval: NSSlider!
2122
@IBOutlet weak var shareAnalytics: NSButton!
23+
@IBOutlet weak var autoUpgrade: NSButton!
24+
@IBOutlet weak var dontNotifyAvailable: NSButton!
2225

2326
var delegate: PreferencesDelegate?
2427

@@ -50,7 +53,10 @@ class PreferencesController: NSWindowController {
5053
shareAnalytics.state = .on
5154
} else {
5255
shareAnalytics.state = .off
53-
}
56+
}
57+
58+
autoUpgrade.state = defaults.bool(forKey: "autoUpgrade") ? .on : .off
59+
dontNotifyAvailable.state = defaults.bool(forKey: "dontNotify") ? .on : .off
5460
}
5561

5662
@IBAction func includeDependenciesPressed(_ sender: NSButton) {
@@ -61,6 +67,16 @@ class PreferencesController: NSWindowController {
6167
delegate?.shareAnalyticsChanged(newState: sender.state)
6268
}
6369

70+
@IBAction func autoUpgradeChanged(_ sender: NSButton) {
71+
os_log("Update auto upgrade: %s", type: .info, sender.state == .on ? "on" : "off")
72+
UserDefaults.standard.set(sender.state == .on, forKey: "autoUpgrade")
73+
}
74+
75+
@IBAction func notifyChanged(_ sender: NSButton) {
76+
os_log("Update don't notify: %s", type: .info, sender.state == .on ? "on" : "off")
77+
UserDefaults.standard.set(sender.state == .on, forKey: "dontNotify")
78+
}
79+
6480
@IBAction func updateIntervalChanged(_ sender: NSSlider) {
6581

6682
var seconds: TimeInterval? = nil

Brewlet/PreferencesController.xib

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15705" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16097" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15705"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16097"/>
66
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
77
</dependencies>
88
<objects>
99
<customObject id="-2" userLabel="File's Owner" customClass="PreferencesController" customModule="Brewlet" customModuleProvider="target">
1010
<connections>
11+
<outlet property="autoUpgrade" destination="vLx-4w-H2K" id="Q9Q-b5-GFk"/>
12+
<outlet property="dontNotifyAvailable" destination="Ed1-vP-MiE" id="tDI-2a-QWm"/>
1113
<outlet property="includeDependencies" destination="064-ff-SAT" id="EcF-bh-YXb"/>
1214
<outlet property="shareAnalytics" destination="Fzh-vX-V3a" id="IWq-vV-CyK"/>
1315
<outlet property="updateInterval" destination="WZd-Rc-dmM" id="PzN-hH-YKX"/>
@@ -19,14 +21,14 @@
1921
<window title="Brewlet Preferences" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="F0z-JX-Cv5">
2022
<windowStyleMask key="styleMask" titled="YES" closable="YES"/>
2123
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
22-
<rect key="contentRect" x="196" y="240" width="496" height="166"/>
24+
<rect key="contentRect" x="196" y="240" width="496" height="207"/>
2325
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
2426
<view key="contentView" id="se5-gp-TjO">
25-
<rect key="frame" x="0.0" y="0.0" width="496" height="166"/>
27+
<rect key="frame" x="0.0" y="0.0" width="496" height="207"/>
2628
<autoresizingMask key="autoresizingMask"/>
2729
<subviews>
2830
<slider verticalHuggingPriority="750" ambiguous="YES" translatesAutoresizingMaskIntoConstraints="NO" id="WZd-Rc-dmM">
29-
<rect key="frame" x="187" y="118" width="291" height="24"/>
31+
<rect key="frame" x="187" y="159" width="291" height="24"/>
3032
<constraints>
3133
<constraint firstAttribute="height" constant="24" id="NaI-IH-328"/>
3234
</constraints>
@@ -36,7 +38,7 @@
3638
</connections>
3739
</slider>
3840
<textField toolTip="Check for outdated packages, and update metadata at the given time period." horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="NmZ-9C-va2">
39-
<rect key="frame" x="18" y="130" width="156" height="16"/>
41+
<rect key="frame" x="18" y="171" width="156" height="16"/>
4042
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
4143
<textFieldCell key="cell" lineBreakMode="clipping" title="Check for updates every:" id="N2S-tz-WUd">
4244
<font key="font" metaFont="system"/>
@@ -45,7 +47,7 @@
4547
</textFieldCell>
4648
</textField>
4749
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="r7G-Rd-0g0">
48-
<rect key="frame" x="449" y="95" width="40" height="16"/>
50+
<rect key="frame" x="449" y="136" width="40" height="16"/>
4951
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
5052
<textFieldCell key="cell" lineBreakMode="clipping" title="Never" id="GOR-Ow-dRi">
5153
<font key="font" metaFont="system"/>
@@ -54,7 +56,7 @@
5456
</textFieldCell>
5557
</textField>
5658
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="bLq-eF-t2c">
57-
<rect key="frame" x="216" y="95" width="27" height="16"/>
59+
<rect key="frame" x="216" y="136" width="27" height="16"/>
5860
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
5961
<textFieldCell key="cell" lineBreakMode="clipping" title="1 hr" id="Lnj-m1-a4v">
6062
<font key="font" metaFont="system"/>
@@ -63,7 +65,7 @@
6365
</textFieldCell>
6466
</textField>
6567
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Tdj-10-P2m">
66-
<rect key="frame" x="332" y="95" width="35" height="16"/>
68+
<rect key="frame" x="332" y="136" width="35" height="16"/>
6769
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
6870
<textFieldCell key="cell" lineBreakMode="clipping" title="3 hrs" id="A5d-kA-hqR">
6971
<font key="font" metaFont="system"/>
@@ -72,7 +74,7 @@
7274
</textFieldCell>
7375
</textField>
7476
<button toolTip="Show and notify user about outdated packages that are dependencies of main packages." verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="064-ff-SAT">
75-
<rect key="frame" x="18" y="53" width="198" height="18"/>
77+
<rect key="frame" x="18" y="45" width="198" height="18"/>
7678
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
7779
<buttonCell key="cell" type="check" title="Include dependency updates" bezelStyle="regularSquare" imagePosition="left" inset="2" id="JV5-ba-lsL">
7880
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
@@ -83,7 +85,7 @@
8385
</connections>
8486
</button>
8587
<button toolTip="Toggle Homebrew's analytics on or off respectively." verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Fzh-vX-V3a">
86-
<rect key="frame" x="18" y="25" width="116" height="18"/>
88+
<rect key="frame" x="18" y="18" width="116" height="18"/>
8789
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
8890
<buttonCell key="cell" type="check" title="Share Analytics" bezelStyle="regularSquare" imagePosition="left" inset="2" id="qE3-XI-fiN">
8991
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
@@ -93,28 +95,50 @@
9395
<action selector="shareAnalyticsPressed:" target="-2" id="n9R-Qz-5PE"/>
9496
</connections>
9597
</button>
96-
<button toolTip="Show and notify user about outdated packages that are dependencies of main packages." horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Gf3-wD-l3z">
97-
<rect key="frame" x="219" y="50" width="21" height="22"/>
98+
<button toolTip="Toggle Homebrew's analytics on or off respectively." horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="PXg-pr-ea1">
99+
<rect key="frame" x="140" y="15" width="21" height="22"/>
98100
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
99-
<buttonCell key="cell" type="help" bezelStyle="helpButton" alignment="center" controlSize="mini" enabled="NO" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Mk7-vH-PLL">
101+
<buttonCell key="cell" type="help" bezelStyle="helpButton" alignment="center" controlSize="mini" enabled="NO" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="5V8-03-j17">
100102
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
101-
<font key="font" metaFont="menu" size="9"/>
103+
<font key="font" metaFont="label" size="9"/>
102104
</buttonCell>
103105
</button>
104-
<button toolTip="Toggle Homebrew's analytics on or off respectively." horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="PXg-pr-ea1">
105-
<rect key="frame" x="140" y="22" width="21" height="22"/>
106+
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ed1-vP-MiE">
107+
<rect key="frame" x="18" y="103" width="283" height="18"/>
106108
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
107-
<buttonCell key="cell" type="help" bezelStyle="helpButton" alignment="center" controlSize="mini" enabled="NO" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="5V8-03-j17">
109+
<buttonCell key="cell" type="check" title="Don't notify me when updates are available" bezelStyle="regularSquare" imagePosition="left" inset="2" id="uaN-ZH-Roy">
110+
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
111+
<font key="font" metaFont="system"/>
112+
</buttonCell>
113+
<connections>
114+
<action selector="notifyChanged:" target="-2" id="w6G-T1-0bq"/>
115+
</connections>
116+
</button>
117+
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="vLx-4w-H2K">
118+
<rect key="frame" x="18" y="75" width="334" height="18"/>
119+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
120+
<buttonCell key="cell" type="check" title="Auto upgrade packages when updates are available" bezelStyle="regularSquare" imagePosition="left" inset="2" id="YPE-Pa-Ear">
121+
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
122+
<font key="font" metaFont="system"/>
123+
</buttonCell>
124+
<connections>
125+
<action selector="autoUpgradeChanged:" target="-2" id="c2B-ra-il7"/>
126+
</connections>
127+
</button>
128+
<button toolTip="Show and notify user about outdated packages that are dependencies of main packages." horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Gf3-wD-l3z">
129+
<rect key="frame" x="220" y="42" width="21" height="22"/>
130+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
131+
<buttonCell key="cell" type="help" bezelStyle="helpButton" alignment="center" controlSize="mini" enabled="NO" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Mk7-vH-PLL">
108132
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
109-
<font key="font" metaFont="menu" size="9"/>
133+
<font key="font" metaFont="label" size="9"/>
110134
</buttonCell>
111135
</button>
112136
</subviews>
113137
</view>
114138
<connections>
115139
<outlet property="delegate" destination="-2" id="0bl-1N-AYu"/>
116140
</connections>
117-
<point key="canvasLocation" x="-3869" y="-99"/>
141+
<point key="canvasLocation" x="-4193" y="-104"/>
118142
</window>
119143
</objects>
120144
</document>

0 commit comments

Comments
 (0)