-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
ios/MullvadVPN/View controllers/Tunnel/FeaturesIndicator/ChipFeatures.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// ChipFeatures.swift | ||
// MullvadVPN | ||
// | ||
// Created by Mojgan on 2024-12-06. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
import Foundation | ||
import MullvadSettings | ||
import SwiftUICore | ||
|
||
protocol ChipFeature { | ||
var isEnabled: Bool { get } | ||
func chipName() -> LocalizedStringKey | ||
} | ||
|
||
struct DaitaFeature: ChipFeature { | ||
let settings: LatestTunnelSettings | ||
|
||
var isEnabled: Bool { | ||
settings.daita.daitaState.isEnabled | ||
} | ||
|
||
func chipName() -> LocalizedStringKey { | ||
LocalizedStringKey("DAITA") | ||
} | ||
} | ||
|
||
struct QuantumResistanceFeature: ChipFeature { | ||
let settings: LatestTunnelSettings | ||
var isEnabled: Bool { | ||
settings.tunnelQuantumResistance.isEnabled | ||
} | ||
|
||
func chipName() -> LocalizedStringKey { | ||
LocalizedStringKey("Quantum resistance") | ||
} | ||
} | ||
|
||
struct MultihopFeature: ChipFeature { | ||
let settings: LatestTunnelSettings | ||
var isEnabled: Bool { | ||
settings.tunnelMultihopState.isEnabled | ||
} | ||
|
||
func chipName() -> LocalizedStringKey { | ||
LocalizedStringKey("Multihop") | ||
} | ||
} | ||
|
||
struct ObfuscationFeature: ChipFeature { | ||
let settings: LatestTunnelSettings | ||
|
||
var isEnabled: Bool { | ||
settings.wireGuardObfuscation.state.isEnabled | ||
} | ||
|
||
func chipName() -> LocalizedStringKey { | ||
LocalizedStringKey("Obfuscation") | ||
} | ||
} | ||
|
||
struct DNSFeature: ChipFeature { | ||
let settings: LatestTunnelSettings | ||
|
||
var isEnabled: Bool { | ||
settings.dnsSettings.enableCustomDNS || !settings.dnsSettings.blockingOptions.isEmpty | ||
} | ||
|
||
func chipName() -> LocalizedStringKey { | ||
if !settings.dnsSettings.blockingOptions.isEmpty { | ||
return LocalizedStringKey("DNS content blockers") | ||
} | ||
return LocalizedStringKey("Custom DNS") | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
ios/MullvadVPN/View controllers/Tunnel/FeaturesIndicator/ChipsView/ChipContainerView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// ChipContainerView.swift | ||
// MullvadVPN | ||
// | ||
// Created by Mojgan on 2024-12-05. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
struct ChipContainerView<ViewModel>: View where ViewModel: ChipViewModelProtocol { | ||
@ObservedObject var viewModel: ViewModel | ||
|
||
init(viewModel: ViewModel) { | ||
self.viewModel = viewModel | ||
} | ||
|
||
var body: some View { | ||
GeometryReader { geo in | ||
let containerWidth = geo.size.width | ||
ZStack(alignment: .topLeading) { | ||
var width = CGFloat.zero | ||
var height = CGFloat.zero | ||
|
||
ForEach(viewModel.chips) { data in | ||
ChipView(item: data) | ||
.padding(5) | ||
.alignmentGuide(.leading) { dimension in | ||
if abs(width - dimension.width) > containerWidth { | ||
width = 0 | ||
height -= dimension.height | ||
} | ||
let result = width | ||
if data.id == viewModel.chips.last!.id { | ||
width = 0 | ||
} else { | ||
width -= dimension.width | ||
} | ||
return result | ||
} | ||
.alignmentGuide(.top) { _ in | ||
let result = height | ||
if data.id == viewModel.chips.last!.id { | ||
height = 0 | ||
} | ||
return result | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview("ChipContainerView") { | ||
ChipContainerView(viewModel: MockChipViewModel()) | ||
} | ||
|
||
private class MockChipViewModel: ChipViewModelProtocol { | ||
@Published var chips: [ChipModel] = (5 ..< 20).map { index in | ||
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
return ChipModel(name: LocalizedStringKey(String((0 ..< index).map { _ in letters.randomElement()! }))) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
ios/MullvadVPN/View controllers/Tunnel/FeaturesIndicator/ChipsView/ChipModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// FeatureChipModel.swift | ||
// MullvadVPN | ||
// | ||
// Created by Mojgan on 2024-12-05. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUICore | ||
|
||
struct ChipModel: Identifiable { | ||
let id = UUID() | ||
let name: LocalizedStringKey | ||
} |
33 changes: 33 additions & 0 deletions
33
ios/MullvadVPN/View controllers/Tunnel/FeaturesIndicator/ChipsView/ChipView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// FeatureChipView.swift | ||
// MullvadVPN | ||
// | ||
// Created by Mojgan on 2024-12-05. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ChipView: View { | ||
let item: ChipModel | ||
var body: some View { | ||
HStack(spacing: UIMetrics.padding4) { | ||
Text(item.name) | ||
.font(.body) | ||
.lineLimit(1) | ||
.foregroundStyle(Color(uiColor: .primaryTextColor)) | ||
} | ||
.padding(.horizontal, UIMetrics.padding8) | ||
.padding(.vertical, UIMetrics.padding4) | ||
.background(Color(uiColor: .secondaryColor)) | ||
.foregroundStyle(Color(uiColor: .primaryColor)) | ||
.overlay { | ||
RoundedRectangle(cornerRadius: UIMetrics.controlCornerRadius) | ||
.stroke(Color(uiColor: .primaryColor), style: StrokeStyle(lineWidth: 1.0)) | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
ChipView(item: ChipModel(name: LocalizedStringKey("Example"))) | ||
} |
12 changes: 12 additions & 0 deletions
12
...ullvadVPN/View controllers/Tunnel/FeaturesIndicator/ChipsView/ChipViewModelProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// ChipViewModelProtocol.swift | ||
// MullvadVPN | ||
// | ||
// Created by Mojgan on 2024-12-05. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
protocol ChipViewModelProtocol: ObservableObject { | ||
var chips: [ChipModel] { get } | ||
} |
47 changes: 47 additions & 0 deletions
47
ios/MullvadVPN/View controllers/Tunnel/FeaturesIndicator/FeatureChipViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// FeatureChipViewModel.swift | ||
// MullvadVPN | ||
// | ||
// Created by Mojgan on 2024-12-05. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import MullvadSettings | ||
import SwiftUICore | ||
class FeatureChipViewModel: ChipViewModelProtocol { | ||
@Published var chips: [ChipModel] = [] | ||
|
||
let tunnelManager: TunnelManager | ||
var observer: TunnelObserver? | ||
|
||
init(tunnelManager: TunnelManager) { | ||
self.tunnelManager = tunnelManager | ||
let observer = TunnelBlockObserver( | ||
didLoadConfiguration: { [weak self] tunnelManager in | ||
guard let self else { return } | ||
chips = createChips(tunnelManager.settings) | ||
}, | ||
didUpdateTunnelSettings: { [weak self] _, latestTunnelSettings in | ||
guard let self else { return } | ||
chips = createChips(latestTunnelSettings) | ||
} | ||
) | ||
self.observer = observer | ||
tunnelManager.addObserver(observer) | ||
} | ||
|
||
private func createChips(_ latestTunnelSettings: LatestTunnelSettings) -> [ChipModel] { | ||
let features: [ChipFeature] = [ | ||
DaitaFeature(settings: latestTunnelSettings), | ||
QuantumResistanceFeature(settings: latestTunnelSettings), | ||
MultihopFeature(settings: latestTunnelSettings), | ||
ObfuscationFeature(settings: latestTunnelSettings), | ||
DNSFeature(settings: latestTunnelSettings), | ||
] | ||
|
||
return features | ||
.filter { $0.isEnabled } | ||
.map { ChipModel(name: $0.chipName()) } | ||
} | ||
} |
Oops, something went wrong.