From 354f67446ecdb6b3deecac5287389b764ef1029d Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Wed, 9 Jul 2025 20:18:25 +0200 Subject: [PATCH] feat: added update interval to the connectivity reader (#2570) --- Modules/Net/main.swift | 3 +++ Modules/Net/readers.swift | 2 +- Modules/Net/settings.swift | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Modules/Net/main.swift b/Modules/Net/main.swift index 67ae4994..f3b16301 100644 --- a/Modules/Net/main.swift +++ b/Modules/Net/main.swift @@ -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() } diff --git a/Modules/Net/readers.swift b/Modules/Net/readers.swift index 913e4a51..38e6d620 100644 --- a/Modules/Net/readers.swift +++ b/Modules/Net/readers.swift @@ -732,7 +732,7 @@ internal class ConnectivityReader: Reader { } 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() diff --git a/Modules/Net/settings.swift b/Modules/Net/settings.swift index 34d055db..05c4b35b 100644 --- a/Modules/Net/settings.swift +++ b/Modules/Net/settings.swift @@ -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)