Split VPN status and data count

This commit is contained in:
Davide De Rosa 2022-04-23 09:46:41 +02:00
parent 1083941eeb
commit ad1539023e
3 changed files with 14 additions and 31 deletions

View File

@ -49,15 +49,7 @@ extension PassepartoutError {
}
extension VPNManager.ObservableState {
enum LocalizedStyle {
case statusOnly
case dataCountOrStatus
case statusAndDataCount
}
func localizedStatusDescription(withErrors: Bool, style: LocalizedStyle) -> String {
func localizedStatusDescription(withErrors: Bool, dataCountIfAvailable: Bool) -> String {
guard isEnabled else {
// report application errors even if VPN is disabled
@ -74,25 +66,10 @@ extension VPNManager.ObservableState {
return errorDescription
}
}
let statusDescription = vpnStatus.localizedDescription
var dataCountDescription: String?
if vpnStatus == .connected, let dataCount = dataCount {
dataCountDescription = dataCount.localizedDescription
}
switch style {
case .statusOnly:
return statusDescription
case .dataCountOrStatus:
return dataCountDescription ?? statusDescription
case .statusAndDataCount:
var comps: [String] = [statusDescription]
if let dataCountDescription = dataCountDescription {
comps.append(dataCountDescription)
}
return comps.joined(separator: " ")
if dataCountIfAvailable, vpnStatus == .connected, let dataCount = dataCount {
return dataCount.localizedDescription
}
return vpnStatus.localizedDescription
}
}

View File

@ -95,7 +95,7 @@ extension ProfileView {
Text(L10n.Profile.Items.ConnectionStatus.caption)
.withTrailingText(currentVPNState.localizedStatusDescription(
withErrors: true,
style: .dataCountOrStatus
dataCountIfAvailable: true
))
}
}

View File

@ -35,13 +35,19 @@ struct VPNStatusText: View {
var body: some View {
debugChanges()
return Text(statusDescription)
return HStack {
Text(statusDescription)
Spacer()
currentVPNState.dataCount.map {
Text($0.localizedDescription)
}
}
}
private var statusDescription: String {
return currentVPNState.localizedStatusDescription(
currentVPNState.localizedStatusDescription(
withErrors: false,
style: .statusAndDataCount
dataCountIfAvailable: false
)
}
}