feat: added update interval to the connectivity reader (#2570)

This commit is contained in:
Serhiy Mytrovtsiy
2025-07-09 20:18:25 +02:00
parent 5e79726c47
commit 354f67446e
3 changed files with 19 additions and 2 deletions

View File

@@ -205,6 +205,9 @@ public class Network: Module {
self?.connectivityCallback(Network_Connectivity(status: false))
}
}
self.settingsView.setInterval = { [weak self] value in
self?.connectivityReader?.setInterval(value)
}
self.settingsView.publicIPRefreshIntervalCallback = { [weak self] in
self?.setIPUpdater()
}

View File

@@ -732,7 +732,7 @@ internal class ConnectivityReader: Reader<Network_Connectivity> {
}
override func setup() {
self.interval = 1
self.setInterval(Store.shared.int(key: "Network_updateICMPInterval", defaultValue: 1))
DispatchQueue.global(qos: .background).async {
self.addr = self.resolve()
self.openConn()

View File

@@ -64,6 +64,7 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
private var widgetActivationThreshold: Int = 0
private var widgetActivationThresholdSize: SizeUnit = .MB
private var ICMPHost: String = "1.1.1.1"
private var updateICMPIntervalValue: Int = 1
private var publicIPState: Bool = true
private var publicIPRefreshInterval: String = "never"
private var baseValue: String = "byte"
@@ -73,6 +74,7 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
public var callbackWhenUpdateNumberOfProcesses: (() -> Void) = {}
public var usageResetCallback: (() -> Void) = {}
public var ICMPHostCallback: ((_ newState: Bool) -> Void) = { _ in }
public var setInterval: ((_ value: Int) -> Void) = {_ in }
public var publicIPRefreshIntervalCallback: (() -> Void) = {}
private let title: String
@@ -100,6 +102,7 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
self.widgetActivationThreshold = Store.shared.int(key: "\(self.title)_widgetActivationThreshold", defaultValue: self.widgetActivationThreshold)
self.widgetActivationThresholdSize = SizeUnit.fromString(Store.shared.string(key: "\(self.title)_widgetActivationThresholdSize", defaultValue: self.widgetActivationThresholdSize.key))
self.ICMPHost = Store.shared.string(key: "\(self.title)_ICMPHost", defaultValue: self.ICMPHost)
self.updateICMPIntervalValue = Store.shared.int(key: "\(self.title)_updateICMPInterval", defaultValue: self.updateICMPIntervalValue)
self.publicIPState = Store.shared.bool(key: "\(self.title)_publicIP", defaultValue: self.publicIPState)
self.publicIPRefreshInterval = Store.shared.string(key: "\(self.title)_publicIPRefreshInterval", defaultValue: self.publicIPRefreshInterval)
self.baseValue = Store.shared.string(key: "\(self.title)_base", defaultValue: self.baseValue)
@@ -226,7 +229,12 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
self.addArrangedSubview(PreferencesSection([
PreferencesRow(localizedString("Connectivity host (ICMP)"), component: ICMPField) {
NSWorkspace.shared.open(URL(string: "https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol")!)
}
},
PreferencesRow(localizedString("Update interval"), component: selectView(
action: #selector(self.changeICMPUpdateInterval),
items: ReaderUpdateIntervals,
selected: "\(self.updateICMPIntervalValue)"
))
]))
if widgets.contains(where: { $0 == .text }) {
@@ -321,6 +329,12 @@ internal class Settings: NSStackView, Settings_v, NSTextFieldDelegate {
}
}
}
@objc private func changeICMPUpdateInterval(_ sender: NSMenuItem) {
guard let key = sender.representedObject as? String, let value = Int(key) else { return }
self.updateICMPIntervalValue = value
Store.shared.set(key: "\(self.title)_updateICMPInterval", value: value)
self.setInterval(value)
}
@objc func togglePublicIPState(_ sender: NSControl) {
self.publicIPState = controlState(sender)