Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ final class VPNDaemon: Daemon, DatabaseAccess, ProvidersAccess {
numberOfAttempts = 0
}

deinit {
NotificationCenter.default.removeObserver(self)
}

func start() {
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(neStatusDidChange(notification:)), name: .NEVPNStatusDidChange, object: nil)
Expand Down Expand Up @@ -96,6 +100,9 @@ final class VPNDaemon: Daemon, DatabaseAccess, ProvidersAccess {
case .connected:
nextStatus = .connected
Client.preferences.timeToConnectVPN = Date().timeIntervalSince1970 - Client.preferences.lastVPNConnectionAttempt
if Client.preferences.lastVPNConnectionSuccess == nil {
Client.preferences.lastVPNConnectionSuccess = Date().timeIntervalSince1970
}
Comment thread
kp-mario-nachbaur marked this conversation as resolved.
Outdated

let previousStatus = accessedDatabase.transient.vpnStatus

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public final class MockVPNProvider: VPNProvider, ConfigurationAccess, DatabaseAc
return nil
}

public var connectedDate: Date? { nil }

/// :nodoc:
public func prepare() {
accessedDatabase.transient.isNetworkReachable = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public final class DefaultVPNProvider: VPNProvider, ConfigurationAccess, Databas
return nil
}

public var connectedDate: Date? { activeProfile?.connectedDate }
Comment thread
kp-mario-nachbaur marked this conversation as resolved.
Outdated

private var vpnLog: String {
return accessedDatabase.transient.vpnLog
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public final class IKEv2Profile: NetworkExtensionProfile {
return currentVPN
}

public var connectedDate: Date? { currentVPN.connection.connectedDate }

/// :nodoc:
public func prepare() {
currentVPN.loadFromPreferences { (_) in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
/// :nodoc:
public var native: Any?

public var connectedDate: Date? {
if let native = native as? NETunnelProviderManager {
return native.connection.connectedDate
}
return nil
Comment thread
kp-mario-nachbaur marked this conversation as resolved.
Outdated
}

/// :nodoc:
public func prepare() {
find(completionHandler: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ import Foundation
/// :nodoc:
public var native: Any?

public var connectedDate: Date? {
if let native = native as? NETunnelProviderManager {
return native.connection.connectedDate
}
return nil
}

/// :nodoc:
Comment thread
kp-mario-nachbaur marked this conversation as resolved.
public func prepare() {
find(completionHandler: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public protocol VPNProfile: AnyObject {
/// The underlying native profile implementation.
var native: Any? { get }

/// The connection date time, if connected. Otherwise nil.
var connectedDate: Date? { get }

/**
Prepares the profile for use, like synchronizing with the current VPN status and making sure that the profile is not corrupt.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public protocol VPNProvider: AnyObject {
/// The `Server` associated with the current profile.
var profileServer: Server? { get }

/// The connection date time, if connected. Otherwise nil.
var connectedDate: Date? { get }

/**
Prepares the provider for VPN operations. Normally invoked when initializing the library.
*/
Expand Down
45 changes: 29 additions & 16 deletions PIA VPN/UI/Dashboard/DashboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,26 @@ final class DashboardViewController: AutolayoutViewController {

private var connectionTimer: Timer?

private var formattedConnectionTime: String? {
guard let startTime = Client.preferences.lastVPNConnectionSuccess else {
private func formattedConnectionTime(isConnected: Bool) -> String? {
let now: TimeInterval = Date.now.timeIntervalSince1970
let connectionTime: TimeInterval

// by default use the time we know we established the connection
if let lastVPNConnectionSuccess = Client.preferences.lastVPNConnectionSuccess {
connectionTime = now - lastVPNConnectionSuccess
// as fallback use system connected time and use that as connection success
} else if let connectedDate = Client.providers.vpnProvider.connectedDate {
let timeInterval = connectedDate.timeIntervalSince1970
Client.preferences.lastVPNConnectionSuccess = timeInterval
connectionTime = now - timeInterval
// last resort store current time because we know status is connected
} else if isConnected {
Client.preferences.lastVPNConnectionSuccess = now
Comment thread
kp-mario-nachbaur marked this conversation as resolved.
connectionTime = 0
} else {
return nil
}

let connectionTime = Date().timeIntervalSince1970 - startTime

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = [.hour, .minute, .second]
Expand Down Expand Up @@ -1050,7 +1063,7 @@ final class DashboardViewController: AutolayoutViewController {

let titleLabelView = UILabel(frame: CGRect.zero)
titleLabelView.style(style: TextStyle.textStyleNavigationBarTitle)
titleLabelView.text = formattedConnectionTime
titleLabelView.text = formattedConnectionTime(isConnected: true)
setNavBarTheme(.green, with: titleLabelView)

AppPreferences.shared.todayWidgetVpnStatus = VPNStatus.connected.rawValue
Expand Down Expand Up @@ -1157,18 +1170,18 @@ final class DashboardViewController: AutolayoutViewController {
private func setNavBar(titleLabel: UILabel) {
navigationTitleLabel = titleLabel
#if targetEnvironment(macCatalyst)
if #available(iOS 16.0, *) {
let title = UIBarButtonItem(customView: titleLabel)
if #available(macCatalyst 26.0, *) {
title.hidesSharedBackground = true
if #available(iOS 16.0, *) {
let title = UIBarButtonItem(customView: titleLabel)
if #available(macCatalyst 26.0, *) {
title.hidesSharedBackground = true
}
let titleGroup = UIBarButtonItemGroup.fixedGroup(items: [title])
navigationItem.centerItemGroups = [titleGroup]
} else {
navigationItem.title = titleLabel.text
}
let titleGroup = UIBarButtonItemGroup.fixedGroup(items: [title])
navigationItem.centerItemGroups = [titleGroup]
} else {
navigationItem.title = titleLabel.text
}
#else
navigationItem.titleView = titleLabel
navigationItem.titleView = titleLabel
#endif
setNeedsStatusBarAppearanceUpdate()
}
Expand Down Expand Up @@ -1220,7 +1233,7 @@ final class DashboardViewController: AutolayoutViewController {
}

@objc private func updateConnectionTime() {
navigationTitleLabel?.text = formattedConnectionTime
navigationTitleLabel?.text = formattedConnectionTime(isConnected: currentStatus == .connected)
}

// MARK: Restylable
Expand Down
Loading