diff options
| author | Bug Magnet <marco.nikic@mullvad.net> | 2023-09-21 16:31:36 +0200 |
|---|---|---|
| committer | Bug Magnet <marco.nikic@mullvad.net> | 2023-09-21 16:31:36 +0200 |
| commit | 2b2dbded60ba1e03112781dc6a04705e08286805 (patch) | |
| tree | 8c001e19acdbcd37b5cc30d4856d728022a38f46 | |
| parent | c8ef994c5330a72478af098b913dccc256e98f78 (diff) | |
| parent | 8bda51f880e96202d66a358ec2fd8c1b342cc82f (diff) | |
| download | mullvadvpn-2b2dbded60ba1e03112781dc6a04705e08286805.tar.xz mullvadvpn-2b2dbded60ba1e03112781dc6a04705e08286805.zip | |
Merge branch 'add-toggle-for-social-media-block-list-ios-318'
4 files changed, 109 insertions, 62 deletions
diff --git a/ios/MullvadSettings/DNSSettings.swift b/ios/MullvadSettings/DNSSettings.swift index cae9de2928..3885b01502 100644 --- a/ios/MullvadSettings/DNSSettings.swift +++ b/ios/MullvadSettings/DNSSettings.swift @@ -19,6 +19,7 @@ public struct DNSBlockingOptions: OptionSet, Codable { public static let blockMalware = DNSBlockingOptions(rawValue: 1 << 2) public static let blockAdultContent = DNSBlockingOptions(rawValue: 1 << 3) public static let blockGambling = DNSBlockingOptions(rawValue: 1 << 4) + public static let blockSocialMedia = DNSBlockingOptions(rawValue: 1 << 5) public var serverAddress: IPv4Address? { if isEmpty { diff --git a/ios/MullvadVPN/View controllers/Preferences/PreferencesCellFactory.swift b/ios/MullvadVPN/View controllers/Preferences/PreferencesCellFactory.swift index f1f7e64ab2..de07f7a304 100644 --- a/ios/MullvadVPN/View controllers/Preferences/PreferencesCellFactory.swift +++ b/ios/MullvadVPN/View controllers/Preferences/PreferencesCellFactory.swift @@ -11,8 +11,8 @@ import UIKit protocol PreferencesCellEventHandler { func addDNSEntry() func didChangeDNSEntry(with identifier: UUID, inputString: String) -> Bool - func didChangeState(for item: PreferencesDataSource.Item, isOn: Bool) - func showInfo(for item: PreferencesDataSource.InfoButtonItem) + func didChangeState(for preference: PreferencesDataSource.Item, isOn: Bool) + func showInfo(for button: PreferencesDataSource.InfoButtonItem) func addCustomPort(_ port: UInt16) func selectCustomPortEntry(_ port: UInt16) -> Bool } @@ -36,104 +36,119 @@ final class PreferencesCellFactory: CellFactoryProtocol { return cell } + func configure( + _ cell: UITableViewCell, + toggleSetting: Bool, + title: String, + for preference: PreferencesDataSource.Item + ) { + guard let cell = cell as? SettingsSwitchCell else { return } + + cell.titleLabel.text = title + cell.accessibilityHint = nil + cell.applySubCellStyling() + cell.setOn(toggleSetting, animated: false) + cell.action = { [weak self] isOn in + self?.delegate?.didChangeState( + for: preference, + isOn: isOn + ) + } + } + // swiftlint:disable:next cyclomatic_complexity function_body_length func configureCell(_ cell: UITableViewCell, item: PreferencesDataSource.Item, indexPath: IndexPath) { switch item { case .blockAdvertising: - guard let cell = cell as? SettingsSwitchCell else { return } - - cell.titleLabel.text = NSLocalizedString( + let localizedString = NSLocalizedString( "BLOCK_ADS_CELL_LABEL", tableName: "Preferences", value: "Block ads", comment: "" ) - cell.accessibilityHint = nil - cell.applySubCellStyling() - cell.setOn(viewModel.blockAdvertising, animated: false) - cell.action = { [weak self] isOn in - self?.delegate?.didChangeState( - for: .blockAdvertising, - isOn: isOn - ) - } - case .blockTracking: - guard let cell = cell as? SettingsSwitchCell else { return } + configure( + cell, + toggleSetting: viewModel.blockAdvertising, + title: localizedString, + for: .blockAdvertising + ) - cell.titleLabel.text = NSLocalizedString( + case .blockTracking: + let localizedString = NSLocalizedString( "BLOCK_TRACKERS_CELL_LABEL", tableName: "Preferences", value: "Block trackers", comment: "" ) - cell.accessibilityHint = nil - cell.applySubCellStyling() - cell.setOn(viewModel.blockTracking, animated: false) - cell.action = { [weak self] isOn in - self?.delegate?.didChangeState( - for: .blockTracking, - isOn: isOn - ) - } + configure( + cell, + toggleSetting: viewModel.blockTracking, + title: localizedString, + for: .blockTracking + ) case .blockMalware: guard let cell = cell as? SettingsSwitchCell else { return } - cell.titleLabel.text = NSLocalizedString( + let localizedString = NSLocalizedString( "BLOCK_MALWARE_CELL_LABEL", tableName: "Preferences", value: "Block malware", comment: "" ) - cell.accessibilityHint = nil - cell.applySubCellStyling() + configure( + cell, + toggleSetting: viewModel.blockMalware, + title: localizedString, + for: .blockMalware + ) cell.setInfoButtonIsVisible(true) - cell.setOn(viewModel.blockMalware, animated: false) cell.infoButtonHandler = { [weak self] in self?.delegate?.showInfo(for: .blockMalware) } - cell.action = { [weak self] isOn in - self?.delegate?.didChangeState(for: .blockMalware, isOn: isOn) - } case .blockAdultContent: - guard let cell = cell as? SettingsSwitchCell else { return } - - cell.titleLabel.text = NSLocalizedString( + let localizedString = NSLocalizedString( "BLOCK_ADULT_CELL_LABEL", tableName: "Preferences", value: "Block adult content", comment: "" ) - cell.accessibilityHint = nil - cell.applySubCellStyling() - cell.setOn(viewModel.blockAdultContent, animated: false) - cell.action = { [weak self] isOn in - self?.delegate?.didChangeState( - for: .blockAdultContent, - isOn: isOn - ) - } + configure( + cell, + toggleSetting: viewModel.blockAdultContent, + title: localizedString, + for: .blockAdultContent + ) case .blockGambling: - guard let cell = cell as? SettingsSwitchCell else { return } - - cell.titleLabel.text = NSLocalizedString( + let localizedString = NSLocalizedString( "BLOCK_GAMBLING_CELL_LABEL", tableName: "Preferences", value: "Block gambling", comment: "" ) - cell.accessibilityHint = nil - cell.applySubCellStyling() - cell.setOn(viewModel.blockGambling, animated: false) - cell.action = { [weak self] isOn in - self?.delegate?.didChangeState( - for: .blockGambling, - isOn: isOn - ) - } + configure( + cell, + toggleSetting: viewModel.blockGambling, + title: localizedString, + for: .blockGambling + ) + + case .blockSocialMedia: + let localizedString = NSLocalizedString( + "BLOCK_SOCIAL_MEDIA_CELL_LABEL", + tableName: "Preferences", + value: "Block social media", + comment: "" + ) + configure( + cell, + toggleSetting: viewModel.blockSocialMedia, + title: localizedString, + for: .blockSocialMedia + ) case let .wireGuardPort(port): guard let cell = cell as? SelectableSettingsCell else { return } diff --git a/ios/MullvadVPN/View controllers/Preferences/PreferencesDataSource.swift b/ios/MullvadVPN/View controllers/Preferences/PreferencesDataSource.swift index a0bf09647d..354c8370ca 100644 --- a/ios/MullvadVPN/View controllers/Preferences/PreferencesDataSource.swift +++ b/ios/MullvadVPN/View controllers/Preferences/PreferencesDataSource.swift @@ -94,6 +94,7 @@ final class PreferencesDataSource: UITableViewDiffableDataSource< case blockMalware case blockAdultContent case blockGambling + case blockSocialMedia case wireGuardPort(_ port: UInt16?) case wireGuardCustomPort case useCustomDNS @@ -108,7 +109,7 @@ final class PreferencesDataSource: UITableViewDiffableDataSource< #endif static var contentBlockers: [Item] { - [.blockAdvertising, .blockTracking, .blockMalware, .blockAdultContent, .blockGambling] + [.blockAdvertising, .blockTracking, .blockMalware, .blockAdultContent, .blockGambling, .blockSocialMedia] } static var wireGuardPorts: [Item] { @@ -139,6 +140,8 @@ final class PreferencesDataSource: UITableViewDiffableDataSource< return "blockGambling" case .blockAdultContent: return "blockAdultContent" + case .blockSocialMedia: + return "blockSocialMedias" case let .wireGuardPort(port): if let port { return "wireGuardPort(\(port))" @@ -720,6 +723,20 @@ final class PreferencesDataSource: UITableViewDiffableDataSource< } } + private func setBlockSocialMedia(_ isEnabled: Bool) { + let oldViewModel = viewModel + + viewModel.setBlockSocialMedia(isEnabled) + + if oldViewModel.customDNSPrecondition != viewModel.customDNSPrecondition { + reloadDnsServerInfo() + } + + if !isEditing { + delegate?.preferencesDataSource(self, didChangeViewModel: viewModel) + } + } + private func setEnableCustomDNS(_ isEnabled: Bool) { let oldViewModel = viewModel @@ -956,8 +973,8 @@ final class PreferencesDataSource: UITableViewDiffableDataSource< } extension PreferencesDataSource: PreferencesCellEventHandler { - func didChangeState(for item: Item, isOn: Bool) { - switch item { + func didChangeState(for preference: Item, isOn: Bool) { + switch preference { case .blockAdvertising: setBlockAdvertising(isOn) @@ -973,6 +990,9 @@ extension PreferencesDataSource: PreferencesCellEventHandler { case .blockGambling: setBlockGambling(isOn) + case .blockSocialMedia: + setBlockSocialMedia(isOn) + case .useCustomDNS: setEnableCustomDNS(isOn) @@ -992,8 +1012,8 @@ extension PreferencesDataSource: PreferencesCellEventHandler { handleDNSEntryChange(with: identifier, inputString: inputString) } - func showInfo(for item: InfoButtonItem) { - delegate?.preferencesDataSource(self, showInfo: item) + func showInfo(for button: InfoButtonItem) { + delegate?.preferencesDataSource(self, showInfo: button) } func addCustomPort(_ port: UInt16) { diff --git a/ios/MullvadVPN/View controllers/Preferences/PreferencesViewModel.swift b/ios/MullvadVPN/View controllers/Preferences/PreferencesViewModel.swift index 850a84b666..fbfd5f53df 100644 --- a/ios/MullvadVPN/View controllers/Preferences/PreferencesViewModel.swift +++ b/ios/MullvadVPN/View controllers/Preferences/PreferencesViewModel.swift @@ -89,6 +89,7 @@ struct PreferencesViewModel: Equatable { private(set) var blockMalware: Bool private(set) var blockAdultContent: Bool private(set) var blockGambling: Bool + private(set) var blockSocialMedia: Bool private(set) var enableCustomDNS: Bool private(set) var wireGuardPort: UInt16? var customDNSDomains: [DNSServerEntry] @@ -121,6 +122,11 @@ struct PreferencesViewModel: Equatable { enableCustomDNS = false } + mutating func setBlockSocialMedia(_ newValue: Bool) { + blockSocialMedia = newValue + enableCustomDNS = false + } + mutating func setEnableCustomDNS(_ newValue: Bool) { blockTracking = false blockAdvertising = false @@ -166,6 +172,7 @@ struct PreferencesViewModel: Equatable { blockMalware = dnsSettings.blockingOptions.contains(.blockMalware) blockAdultContent = dnsSettings.blockingOptions.contains(.blockAdultContent) blockGambling = dnsSettings.blockingOptions.contains(.blockGambling) + blockSocialMedia = dnsSettings.blockingOptions.contains(.blockSocialMedia) enableCustomDNS = dnsSettings.enableCustomDNS customDNSDomains = dnsSettings.customDNSDomains.map { ipAddress in DNSServerEntry(identifier: UUID(), address: "\(ipAddress)") @@ -245,6 +252,10 @@ struct PreferencesViewModel: Equatable { blockingOptions.insert(.blockGambling) } + if blockSocialMedia { + blockingOptions.insert(.blockSocialMedia) + } + var dnsSettings = DNSSettings() dnsSettings.blockingOptions = blockingOptions dnsSettings.enableCustomDNS = enableCustomDNS |
