Skip to content

Commit 354f674

Browse files
committed
feat: added update interval to the connectivity reader (#2570)
1 parent 5e79726 commit 354f674

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

Modules/Net/main.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ public class Network: Module {
205205
self?.connectivityCallback(Network_Connectivity(status: false))
206206
}
207207
}
208+
self.settingsView.setInterval = { [weak self] value in
209+
self?.connectivityReader?.setInterval(value)
210+
}
208211
self.settingsView.publicIPRefreshIntervalCallback = { [weak self] in
209212
self?.setIPUpdater()
210213
}

Modules/Net/readers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ internal class ConnectivityReader: Reader<Network_Connectivity> {
732732
}
733733

734734
override func setup() {
735-
self.interval = 1
735+
self.setInterval(Store.shared.int(key: "Network_updateICMPInterval", defaultValue: 1))
736736
DispatchQueue.global(qos: .background).async {
737737
self.addr = self.resolve()
738738
self.openConn()

Modules/Net/settings.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
6464
private var widgetActivationThreshold: Int = 0
6565
private var widgetActivationThresholdSize: SizeUnit = .MB
6666
private var ICMPHost: String = "1.1.1.1"
67+
private var updateICMPIntervalValue: Int = 1
6768
private var publicIPState: Bool = true
6869
private var publicIPRefreshInterval: String = "never"
6970
private var baseValue: String = "byte"
@@ -73,6 +74,7 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
7374
public var callbackWhenUpdateNumberOfProcesses: (() -> Void) = {}
7475
public var usageResetCallback: (() -> Void) = {}
7576
public var ICMPHostCallback: ((_ newState: Bool) -> Void) = { _ in }
77+
public var setInterval: ((_ value: Int) -> Void) = {_ in }
7678
public var publicIPRefreshIntervalCallback: (() -> Void) = {}
7779

7880
private let title: String
@@ -100,6 +102,7 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
100102
self.widgetActivationThreshold = Store.shared.int(key: "\(self.title)_widgetActivationThreshold", defaultValue: self.widgetActivationThreshold)
101103
self.widgetActivationThresholdSize = SizeUnit.fromString(Store.shared.string(key: "\(self.title)_widgetActivationThresholdSize", defaultValue: self.widgetActivationThresholdSize.key))
102104
self.ICMPHost = Store.shared.string(key: "\(self.title)_ICMPHost", defaultValue: self.ICMPHost)
105+
self.updateICMPIntervalValue = Store.shared.int(key: "\(self.title)_updateICMPInterval", defaultValue: self.updateICMPIntervalValue)
103106
self.publicIPState = Store.shared.bool(key: "\(self.title)_publicIP", defaultValue: self.publicIPState)
104107
self.publicIPRefreshInterval = Store.shared.string(key: "\(self.title)_publicIPRefreshInterval", defaultValue: self.publicIPRefreshInterval)
105108
self.baseValue = Store.shared.string(key: "\(self.title)_base", defaultValue: self.baseValue)
@@ -226,7 +229,12 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
226229
self.addArrangedSubview(PreferencesSection([
227230
PreferencesRow(localizedString("Connectivity host (ICMP)"), component: ICMPField) {
228231
NSWorkspace.shared.open(URL(string: "https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol")!)
229-
}
232+
},
233+
PreferencesRow(localizedString("Update interval"), component: selectView(
234+
action: #selector(self.changeICMPUpdateInterval),
235+
items: ReaderUpdateIntervals,
236+
selected: "\(self.updateICMPIntervalValue)"
237+
))
230238
]))
231239

232240
if widgets.contains(where: { $0 == .text }) {
@@ -321,6 +329,12 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
321329
}
322330
}
323331
}
332+
@objc private func changeICMPUpdateInterval(_ sender: NSMenuItem) {
333+
guard let key = sender.representedObject as? String, let value = Int(key) else { return }
334+
self.updateICMPIntervalValue = value
335+
Store.shared.set(key: "\(self.title)_updateICMPInterval", value: value)
336+
self.setInterval(value)
337+
}
324338

325339
@objc func togglePublicIPState(_ sender: NSControl) {
326340
self.publicIPState = controlState(sender)

0 commit comments

Comments
 (0)