Skip to content

Commit

Permalink
Merge branch 'add-gambling-adult-content-blocking'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej Mihajlov committed Jun 3, 2022
2 parents eaf335f + 282da61 commit b51a6de
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 9 deletions.
3 changes: 3 additions & 0 deletions ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Line wrap the file at 100 chars. Th


## [Unreleased]
### Added
- Add option to block gambling and adult content.

### Fixed
- Improve random port distribution. Should be less biased towards port 53.

Expand Down
2 changes: 2 additions & 0 deletions ios/MullvadVPN/DNSSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct DNSBlockingOptions: OptionSet, Codable {
static let blockAdvertising = DNSBlockingOptions(rawValue: 1 << 0)
static let blockTracking = DNSBlockingOptions(rawValue: 1 << 1)
static let blockMalware = DNSBlockingOptions(rawValue: 1 << 2)
static let blockAdultContent = DNSBlockingOptions(rawValue: 1 << 3)
static let blockGambling = DNSBlockingOptions(rawValue: 1 << 4)

var serverAddress: IPv4Address? {
if isEmpty {
Expand Down
66 changes: 65 additions & 1 deletion ios/MullvadVPN/PreferencesDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class PreferencesDataSource: NSObject, UITableViewDataSource, UITableViewDelegat
case blockAdvertising
case blockTracking
case blockMalware
case blockAdultContent
case blockGambling
case useCustomDNS
case addDNSServer
case dnsServer(_ uniqueID: UUID)
Expand Down Expand Up @@ -309,7 +311,7 @@ class PreferencesDataSource: NSObject, UITableViewDataSource, UITableViewDelegat
private func updateSnapshot() {
var newSnapshot = DataSourceSnapshot<Section, Item>()
newSnapshot.appendSections([.mullvadDNS, .customDNS])
newSnapshot.appendItems([.blockAdvertising, .blockTracking, .blockMalware], in: .mullvadDNS)
newSnapshot.appendItems([.blockAdvertising, .blockTracking, .blockMalware, .blockAdultContent, .blockGambling], in: .mullvadDNS)
newSnapshot.appendItems([.useCustomDNS], in: .customDNS)

let dnsServerItems = viewModel.customDNSDomains.map { entry in
Expand Down Expand Up @@ -377,6 +379,40 @@ class PreferencesDataSource: NSObject, UITableViewDataSource, UITableViewDelegat

return cell

case .blockAdultContent:
let cell = tableView.dequeueReusableCell(withIdentifier: CellReuseIdentifiers.settingSwitch.rawValue, for: indexPath) as! SettingsSwitchCell

cell.titleLabel.text = NSLocalizedString(
"BLOCK_ADULT_CELL_LABEL",
tableName: "Preferences",
value: "Block adult content",
comment: ""
)
cell.accessibilityHint = nil
cell.setOn(viewModel.blockAdultContent, animated: false)
cell.action = { [weak self] isOn in
self?.setBlockAdultContent(isOn)
}

return cell

case .blockGambling:
let cell = tableView.dequeueReusableCell(withIdentifier: CellReuseIdentifiers.settingSwitch.rawValue, for: indexPath) as! SettingsSwitchCell

cell.titleLabel.text = NSLocalizedString(
"BLOCK_GAMBLING_CELL_LABEL",
tableName: "Preferences",
value: "Block gambling",
comment: ""
)
cell.accessibilityHint = nil
cell.setOn(viewModel.blockGambling, animated: false)
cell.action = { [weak self] isOn in
self?.setBlockGambling(isOn)
}

return cell

case .useCustomDNS:
let cell = tableView.dequeueReusableCell(withIdentifier: CellReuseIdentifiers.settingSwitch.rawValue, for: indexPath) as! SettingsSwitchCell

Expand Down Expand Up @@ -476,6 +512,34 @@ class PreferencesDataSource: NSObject, UITableViewDataSource, UITableViewDelegat
}
}

private func setBlockAdultContent(_ isEnabled: Bool) {
let oldViewModel = viewModel

viewModel.setBlockAdultContent(isEnabled)

if oldViewModel.customDNSPrecondition != viewModel.customDNSPrecondition {
reloadCustomDNSFooter()
}

if !isEditing {
delegate?.preferencesDataSource(self, didChangeViewModel: viewModel)
}
}

private func setBlockGambling(_ isEnabled: Bool) {
let oldViewModel = viewModel

viewModel.setBlockGambling(isEnabled)

if oldViewModel.customDNSPrecondition != viewModel.customDNSPrecondition {
reloadCustomDNSFooter()
}

if !isEditing {
delegate?.preferencesDataSource(self, didChangeViewModel: viewModel)
}
}

private func setEnableCustomDNS(_ isEnabled: Bool) {
viewModel.setEnableCustomDNS(isEnabled)

Expand Down
40 changes: 32 additions & 8 deletions ios/MullvadVPN/PreferencesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ enum CustomDNSPrecondition {
case .emptyDNSDomains:
if isEditing {
return NSAttributedString(
markdownString: NSLocalizedString(
string: NSLocalizedString(
"CUSTOM_DNS_NO_DNS_ENTRIES_EDITING_ON_FOOTNOTE",
tableName: "Preferences",
value: "To enable this setting, add at least one server.",
comment: "Foot note displayed if there are no DNS entries and table view is in editing mode."
),
font: preferredFont
attributes: [.font: preferredFont]
)
} else {
return NSAttributedString(
Expand All @@ -57,13 +57,13 @@ enum CustomDNSPrecondition {

case .conflictsWithOtherSettings:
return NSAttributedString(
markdownString: NSLocalizedString(
"CUSTOM_DNS_DISABLE_ADTRACKER_BLOCKING_FOOTNOTE",
string: NSLocalizedString(
"CUSTOM_DNS_DISABLE_CONTENT_BLOCKERS_FOOTNOTE",
tableName: "Preferences",
value: "Disable **Block ads**, **Block trackers** and **Block malware** to activate this setting.",
comment: "Foot note displayed when custom DNS cannot be enabled, because ad/tracker/malware blockers features should be disabled first."
value: "Disable all content blockers (under Preferences) to activate this setting.",
comment: "Foot note displayed when custom DNS cannot be enabled, because content blockers should be disabled first."
),
font: preferredFont
attributes: [.font: preferredFont]
)
}
}
Expand All @@ -78,6 +78,8 @@ struct PreferencesViewModel: Equatable {
private(set) var blockAdvertising: Bool
private(set) var blockTracking: Bool
private(set) var blockMalware: Bool
private(set) var blockAdultContent: Bool
private(set) var blockGambling: Bool
private(set) var enableCustomDNS: Bool
var customDNSDomains: [DNSServerEntry]

Expand All @@ -96,6 +98,16 @@ struct PreferencesViewModel: Equatable {
enableCustomDNS = false
}

mutating func setBlockAdultContent(_ newValue: Bool) {
blockAdultContent = newValue
enableCustomDNS = false
}

mutating func setBlockGambling(_ newValue: Bool) {
blockGambling = newValue
enableCustomDNS = false
}

mutating func setEnableCustomDNS(_ newValue: Bool) {
blockTracking = false
blockAdvertising = false
Expand All @@ -104,7 +116,7 @@ struct PreferencesViewModel: Equatable {

/// Precondition for enabling Custom DNS.
var customDNSPrecondition: CustomDNSPrecondition {
if blockAdvertising || blockTracking || blockMalware {
if blockAdvertising || blockTracking || blockMalware || blockAdultContent || blockGambling {
return .conflictsWithOtherSettings
} else {
let hasValidDNSDomains = customDNSDomains.contains { entry in
Expand All @@ -128,6 +140,8 @@ struct PreferencesViewModel: Equatable {
blockAdvertising = dnsSettings.blockingOptions.contains(.blockAdvertising)
blockTracking = dnsSettings.blockingOptions.contains(.blockTracking)
blockMalware = dnsSettings.blockingOptions.contains(.blockMalware)
blockAdultContent = dnsSettings.blockingOptions.contains(.blockAdultContent)
blockGambling = dnsSettings.blockingOptions.contains(.blockGambling)
enableCustomDNS = dnsSettings.enableCustomDNS
customDNSDomains = dnsSettings.customDNSDomains.map { ipAddress in
return DNSServerEntry(identifier: UUID(), address: "\(ipAddress)")
Expand All @@ -141,6 +155,8 @@ struct PreferencesViewModel: Equatable {
mergedViewModel.blockAdvertising = other.blockAdvertising
mergedViewModel.blockTracking = other.blockTracking
mergedViewModel.blockMalware = other.blockMalware
mergedViewModel.blockAdultContent = other.blockAdultContent
mergedViewModel.blockGambling = other.blockGambling
mergedViewModel.enableCustomDNS = other.enableCustomDNS

var oldDNSDomains = customDNSDomains
Expand Down Expand Up @@ -218,6 +234,14 @@ struct PreferencesViewModel: Equatable {
blockingOptions.insert(.blockMalware)
}

if blockAdultContent {
blockingOptions.insert(.blockAdultContent)
}

if blockGambling {
blockingOptions.insert(.blockGambling)
}

var dnsSettings = DNSSettings()
dnsSettings.blockingOptions = blockingOptions
dnsSettings.enableCustomDNS = enableCustomDNS
Expand Down

0 comments on commit b51a6de

Please sign in to comment.