Skip to content

Commit

Permalink
Fix rebase from main
Browse files Browse the repository at this point in the history
  • Loading branch information
buggmagnet authored and Jon Petersson committed Aug 11, 2023
1 parent 33a8655 commit 1b76016
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 101 deletions.
58 changes: 23 additions & 35 deletions ios/MullvadVPN/UI appearance/UIMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
import UIKit

enum UIMetrics {
enum TableView {
/// Height for separators between cells and/or sections.
static let separatorHeight: CGFloat = 0.33
/// Spacing used between distinct sections of views
static let sectionSpacing: CGFloat = 24
/// Common layout margins for row views presentation
/// Similar to `SettingsCell.layoutMargins` however maintains equal horizontal spacing
static let rowViewLayoutMargins = NSDirectionalEdgeInsets(top: 16, leading: 24, bottom: 16, trailing: 24)
/// Common cell indentation width
static let cellIndentationWidth: CGFloat = 16
}

enum CustomAlert {
/// Layout margins for container (main view) in `CustomAlertViewController`
static let containerMargins = NSDirectionalEdgeInsets(
Expand Down Expand Up @@ -51,65 +63,32 @@ enum UIMetrics {
enum SettingsCell {
static let textFieldContentInsets = UIEdgeInsets(top: 8, left: 24, bottom: 8, right: 24)
static let textFieldNonEditingContentInsetLeft: CGFloat = 40
}
}

extension UIMetrics {
enum TableView {
/// Height for separators between cells and/or sections.
static let separatorHeight: CGFloat = 0.33

/// Spacing used between distinct sections of views
static let sectionSpacing: CGFloat = 24

/// Common layout margins for row views presentation
/// Similar to `SettingsCell.layoutMargins` however maintains equal horizontal spacing
static let rowViewLayoutMargins = NSDirectionalEdgeInsets(top: 16, leading: 24, bottom: 16, trailing: 24)

/// Common cell indentation width
static let cellIndentationWidth: CGFloat = 16
}

/// Group of constants related to in-app notifications banner.
enum InAppBannerNotification {
/// Layout margins for contents presented within the banner.
static let layoutMargins = NSDirectionalEdgeInsets(top: 16, leading: 24, bottom: 16, trailing: 24)

/// Size of little round severity indicator.
static let indicatorSize = CGSize(width: 12, height: 12)
}

enum SettingsCell {
/// Common layout margins for settings cell presentation.
static let layoutMargins = NSDirectionalEdgeInsets(top: 16, leading: 24, bottom: 16, trailing: 12)

/// Common layout margins for text field.
static let inputCellTextFieldLayoutMargins = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)

/// Spacing between left view and content view.
static let selectableSettingsCellLeftViewSpacing: CGFloat = 12

/// Spacing between left view and content view.
static let checkableSettingsCellLeftViewSpacing: CGFloat = 20
}

enum FilterView {
// Spacing between chips and label.
static let labelSpacing: CGFloat = 5

// Spacing between chip views.
static let interChipViewSpacing: CGFloat = 8

// Chip view corner radius.
static let chipViewCornerRadius: CGFloat = 8

// Chip view layout margins.
static let chipViewLayoutMargins = UIEdgeInsets(top: 3, left: 8, bottom: 3, right: 8)

// Spacing between chip view label and button.
static let chipViewLabelSpacing: CGFloat = 7
}
}

extension UIMetrics {
/// Common layout margins for content presentation
static let contentLayoutMargins = NSDirectionalEdgeInsets(top: 24, leading: 24, bottom: 24, trailing: 24)

Expand All @@ -119,6 +98,15 @@ extension UIMetrics {
/// Common layout margins for location cell presentation
static let selectLocationCellLayoutMargins = NSDirectionalEdgeInsets(top: 16, leading: 28, bottom: 16, trailing: 12)

/// Group of constants related to in-app notifications banner.
enum InAppBannerNotification {
/// Layout margins for contents presented within the banner.
static let layoutMargins = NSDirectionalEdgeInsets(top: 16, leading: 24, bottom: 16, trailing: 24)

/// Size of little round severity indicator.
static let indicatorSize = CGSize(width: 12, height: 12)
}

/// Spacing used in stack views of buttons
static let interButtonSpacing: CGFloat = 16

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ final class PreferencesDataSource: UITableViewDiffableDataSource<

switch sectionIdentifier {
case .wireGuardObfuscationPort:
return UIMetrics.sectionSpacing
return UIMetrics.TableView.sectionSpacing

default:
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ final class RelayFilterDataSource: UITableViewDiffableDataSource<

viewModel.$relays
.combineLatest(viewModel.$relayFilter)
.sink { [weak self] relays in
self?.updateDataSnapshot()
.sink { [weak self] _, filter in
self?.updateDataSnapshot(filter: filter)
}
.store(in: &disposeBag)
}
Expand Down Expand Up @@ -112,7 +112,7 @@ final class RelayFilterDataSource: UITableViewDiffableDataSource<
applySnapshot(snapshot, animated: false)
}

private func updateDataSnapshot() {
private func updateDataSnapshot(filter: RelayFilter? = nil) {
let oldSnapshot = snapshot()

var newSnapshot = NSDiffableDataSourceSnapshot<Section, Item>()
Expand All @@ -126,17 +126,10 @@ final class RelayFilterDataSource: UITableViewDiffableDataSource<
}
case .providers:
if !oldSnapshot.itemIdentifiers(inSection: section).isEmpty {
var items: [RelayFilterDataSource.Item]

switch viewModel.relayFilter.ownership {
case .owned:
items = viewModel.ownedProviders.map { Item.provider($0) }
case .rented:
items = viewModel.rentedProviders.map { Item.provider($0) }
default: items = viewModel.uniqueProviders.map { Item.provider($0) }
}
let ownership = (filter ?? viewModel.relayFilter).ownership
let items = viewModel.availableProviders(for: ownership).map { Item.provider($0) }

newSnapshot.appendItems([.allProviders])
newSnapshot.appendItems([.allProviders], toSection: .providers)
newSnapshot.appendItems(items, toSection: .providers)
}
}
Expand Down Expand Up @@ -178,7 +171,10 @@ final class RelayFilterDataSource: UITableViewDiffableDataSource<
}

private func updateAllProvidersSelection() {
if viewModel.uniqueProviders.count == getSelectedIndexPaths(in: .providers).count {
let selectedCount = getSelectedIndexPaths(in: .providers).count
let providerCount = viewModel.availableProviders(for: viewModel.relayFilter.ownership).count

if selectedCount == providerCount {
selectRow(true, at: indexPath(for: .allProviders))
}
}
Expand Down Expand Up @@ -343,17 +339,8 @@ extension RelayFilterDataSource: UITableViewDelegate {
let items = snapshot.itemIdentifiers(inSection: .providers)
snapshot.deleteItems(items)
} else {
var items: [RelayFilterDataSource.Item]

switch viewModel.relayFilter.ownership {
case .owned:
items = viewModel.ownedProviders.map { Item.provider($0) }
case .rented:
items = viewModel.rentedProviders.map { Item.provider($0) }
default: items = viewModel.uniqueProviders.map { Item.provider($0) }
}

snapshot.appendItems([.allProviders])
let items = viewModel.availableProviders(for: viewModel.relayFilter.ownership).map { Item.provider($0) }
snapshot.appendItems([.allProviders], toSection: .providers)
snapshot.appendItems(items, toSection: .providers)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ class RelayFilterViewController: UIViewController {
comment: ""
)

navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .cancel, actionHandler: { [weak self] in
self?.didFinish?()
})
navigationItem.rightBarButtonItem = UIBarButtonItem(
systemItem: .cancel,
primaryAction: UIAction(handler: { [weak self] _ in
self?.didFinish?()
})
)

applyButton.addTarget(self, action: #selector(applyFilter), for: .touchUpInside)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@ class RelayFilterViewModel {

func addItemToFilter(_ item: RelayFilterDataSource.Item) {
switch item {
case .ownershipAny:
relayFilter.ownership = .any
relayFilter.providers = .any
case .ownershipOwned:
relayFilter.ownership = .owned
relayFilter.providers = .only(ownedProviders)
case .ownershipRented:
relayFilter.ownership = .rented
relayFilter.providers = .only(rentedProviders)
case .ownershipAny, .ownershipOwned, .ownershipRented:
relayFilter.ownership = ownership(for: item) ?? .any
case .allProviders:
relayFilter.providers = .any
case let .provider(name):
Expand All @@ -52,31 +45,33 @@ class RelayFilterViewModel {
if !providers.contains(name) {
providers.append(name)
providers.caseInsensitiveSort()
relayFilter.providers = .only(providers)

if providers == availableProviders(for: relayFilter.ownership) {
relayFilter.providers = .any
} else {
relayFilter.providers = .only(providers)
}
}
}
}
}

func removeItemFromFilter(_ item: RelayFilterDataSource.Item) {
var dataSourceToRemoveFrom = [String]()
switch relayFilter.ownership {
case .any:
dataSourceToRemoveFrom = uniqueProviders
case .owned:
dataSourceToRemoveFrom = ownedProviders
case .rented:
dataSourceToRemoveFrom = rentedProviders
}

switch item {
case .ownershipAny, .ownershipOwned, .ownershipRented:
break
case .allProviders:
relayFilter.providers = .only([])
case let .provider(name):
dataSourceToRemoveFrom.removeAll { $0 == name }
relayFilter.providers = .only(dataSourceToRemoveFrom)
switch relayFilter.providers {
case .any:
var providers = availableProviders(for: relayFilter.ownership)
providers.removeAll { $0 == name }
relayFilter.providers = .only(providers)
case var .only(providers):
providers.removeAll { $0 == name }
relayFilter.providers = .only(providers)
}
}
}

Expand Down Expand Up @@ -118,4 +113,15 @@ class RelayFilterViewModel {
func providerItem(for providerName: String?) -> RelayFilterDataSource.Item? {
return .provider(providerName ?? "")
}

func availableProviders(for ownership: RelayFilter.Ownership) -> [String] {
switch ownership {
case .any:
return uniqueProviders
case .owned:
return ownedProviders
case .rented:
return rentedProviders
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ final class SelectLocationViewController: UIViewController {
value: "Filter",
comment: ""
),
actionHandler: { [weak self] in
primaryAction: UIAction(handler: { [weak self] _ in
self?.navigateToFilter?()
}
})
)

navigationItem.rightBarButtonItem = UIBarButtonItem(
Expand All @@ -66,7 +66,7 @@ final class SelectLocationViewController: UIViewController {
self?.didFinish?()
})
)

setUpDataSource()
setUpTableView()
setUpTopContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SettingsDNSInfoCell: UITableViewCell {
super.init(style: style, reuseIdentifier: reuseIdentifier)

backgroundColor = .secondaryColor
contentView.directionalLayoutMargins = UIMetrics.settingsCellLayoutMargins
contentView.directionalLayoutMargins = UIMetrics.SettingsCell.layoutMargins

titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.textColor = UIColor.Cell.titleTextColor
Expand Down
9 changes: 5 additions & 4 deletions ios/MullvadVPN/Views/CheckboxView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ class CheckboxView: UIView {
return imageView
}()

var isChecked: Bool { didSet {
checkmarkView.alpha = isChecked ? 1 : 0
}}
var isChecked = false {
didSet {
checkmarkView.alpha = isChecked ? 1 : 0
}
}

init() {
isChecked = false
super.init(frame: .zero)

directionalLayoutMargins = .init(top: 4, leading: 4, bottom: 4, trailing: 4)
Expand Down
1 change: 0 additions & 1 deletion ios/MullvadVPNTests/RelaySelectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ class RelaySelectorTests: XCTestCase {
)

XCTAssertNil(result)
>>>>>>> a61a0cd20 (Add filtering to location selection)
}
}

Expand Down
12 changes: 6 additions & 6 deletions ios/RelaySelector/RelaySelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,9 @@ public enum RelaySelector {
}

/// Determines whether a `REST.ServerRelay` satisfies the given relay filter.
public static func relayMatchesFilter(_ relay: REST.ServerRelay, filter: RelayFilter) -> Bool {
if case let .only(providers) = filter.providers {
if !providers.contains(relay.provider) {
return false
}
public static func relayMatchesFilter(_ relay: AnyRelay, filter: RelayFilter) -> Bool {
if case let .only(providers) = filter.providers, providers.contains(relay.provider) == false {
return false
}

switch filter.ownership {
Expand Down Expand Up @@ -318,9 +316,11 @@ public struct RelaySelectorResult: Codable {
}
}

protocol AnyRelay {
public protocol AnyRelay {
var hostname: String { get }
var owned: Bool { get }
var location: String { get }
var provider: String { get }
var weight: UInt64 { get }
var active: Bool { get }
var includeInCountry: Bool { get }
Expand Down

0 comments on commit 1b76016

Please sign in to comment.