From 26d21e901017510e4c52d13a823a0687ef79373e Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Wed, 24 Dec 2025 17:22:30 +0100 Subject: [PATCH] feat: added ip change counter to the ip change notification that prevents notification when ip disappears for 3 reads --- Modules/Net/notifications.swift | 57 +++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/Modules/Net/notifications.swift b/Modules/Net/notifications.swift index 882f2df6..ffe38ca5 100644 --- a/Modules/Net/notifications.swift +++ b/Modules/Net/notifications.swift @@ -35,6 +35,11 @@ class Notifications: NotificationsWrapper { private var publicIP: String? private var wifi: String? + private var localIPCount: Int = 0 + private var localIPThreshold: Int = 3 + private var publicIPCount: Int = 0 + private var publicIPThreshold: Int = 3 + private var connectionInit: Bool = false private var interfaceInit: Bool = false private var localIPInit: Bool = false @@ -119,37 +124,49 @@ class Notifications: NotificationsWrapper { if self.localIPState { let addr = value.laddr.v4 ?? value.laddr.v6 if addr != self.localIP { - var subtitle = "" - if let prev = self.localIP { - subtitle = localizedString("Previous IP", prev) - } - if let new = addr { - if !subtitle.isEmpty { - subtitle += "\n" + self.localIPCount += 1 + if self.localIPCount >= self.localIPThreshold { + var subtitle = "" + if let prev = self.localIP { + subtitle = localizedString("Previous IP", prev) } - subtitle += localizedString("New IP", new) + if let new = addr { + if !subtitle.isEmpty { + subtitle += "\n" + } + subtitle += localizedString("New IP", new) + } + self.newNotification(id: self.localID, title: localizedString("Local IP changed"), subtitle: subtitle) + self.localIP = addr + self.localIPCount = 0 } - self.newNotification(id: self.localID, title: localizedString("Local IP changed"), subtitle: subtitle) + } else { + self.localIPCount = 0 } - self.localIP = addr } if self.publicIPState { let addr = value.raddr.v4 ?? value.raddr.v6 if addr != self.publicIP { - var subtitle = "" - if let prev = self.publicIP { - subtitle = localizedString("Previous IP", prev) - } - if let new = addr { - if !subtitle.isEmpty { - subtitle += "\n" + self.publicIPCount += 1 + if self.publicIPCount >= self.publicIPThreshold { + var subtitle = "" + if let prev = self.publicIP { + subtitle = localizedString("Previous IP", prev) } - subtitle += localizedString("New IP", new) + if let new = addr { + if !subtitle.isEmpty { + subtitle += "\n" + } + subtitle += localizedString("New IP", new) + } + self.newNotification(id: self.publicID, title: localizedString("Public IP changed"), subtitle: subtitle) + self.publicIP = addr + self.publicIPCount = 0 } - self.newNotification(id: self.publicID, title: localizedString("Public IP changed"), subtitle: subtitle) + } else { + self.publicIPCount = 0 } - self.publicIP = addr } if self.wifiState {