feat: added ip change counter to the ip change notification that prevents notification when ip disappears for 3 reads

This commit is contained in:
Serhiy Mytrovtsiy
2025-12-24 17:22:30 +01:00
parent bc851d00f5
commit 26d21e9010

View File

@@ -35,6 +35,11 @@ class Notifications: NotificationsWrapper {
private var publicIP: String? private var publicIP: String?
private var wifi: 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 connectionInit: Bool = false
private var interfaceInit: Bool = false private var interfaceInit: Bool = false
private var localIPInit: Bool = false private var localIPInit: Bool = false
@@ -119,37 +124,49 @@ class Notifications: NotificationsWrapper {
if self.localIPState { if self.localIPState {
let addr = value.laddr.v4 ?? value.laddr.v6 let addr = value.laddr.v4 ?? value.laddr.v6
if addr != self.localIP { if addr != self.localIP {
var subtitle = "" self.localIPCount += 1
if let prev = self.localIP { if self.localIPCount >= self.localIPThreshold {
subtitle = localizedString("Previous IP", prev) var subtitle = ""
} if let prev = self.localIP {
if let new = addr { subtitle = localizedString("Previous IP", prev)
if !subtitle.isEmpty {
subtitle += "\n"
} }
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 { if self.publicIPState {
let addr = value.raddr.v4 ?? value.raddr.v6 let addr = value.raddr.v4 ?? value.raddr.v6
if addr != self.publicIP { if addr != self.publicIP {
var subtitle = "" self.publicIPCount += 1
if let prev = self.publicIP { if self.publicIPCount >= self.publicIPThreshold {
subtitle = localizedString("Previous IP", prev) var subtitle = ""
} if let prev = self.publicIP {
if let new = addr { subtitle = localizedString("Previous IP", prev)
if !subtitle.isEmpty {
subtitle += "\n"
} }
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 { if self.wifiState {