forked from wordpress-mobile/WordPress-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
CompliancePopoverCoordinator
CompliancePopoverViewModel
& `Co…
…mpliancePopoverViewController`
- Loading branch information
1 parent
b4b45b4
commit cec1679
Showing
11 changed files
with
321 additions
and
23 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
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
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
14 changes: 14 additions & 0 deletions
14
WordPress/Classes/ViewRelated/EEUUSCompliance/ComplianceLocationService.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,14 @@ | ||
import WordPressKit | ||
|
||
final class ComplianceLocationService { | ||
func getIPCountryCode(completion: @escaping (Result<String, Error>) -> Void) { | ||
IPLocationRemote().fetchIPCountryCode { result in | ||
switch result { | ||
case .success(let countryCode): | ||
completion(.success(countryCode)) | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
} |
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
89 changes: 89 additions & 0 deletions
89
WordPress/Classes/ViewRelated/EEUUSCompliance/CompliancePopoverCoordinator.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,89 @@ | ||
import UIKit | ||
|
||
final class CompliancePopoverCoordinator { | ||
fileprivate enum Constants { | ||
static let hasSavedPrivacyBannerSettingsKey = "hasSavedPrivacyBannerSettings" | ||
} | ||
|
||
private let viewController: UIViewController | ||
private let complianceService = ComplianceLocationService() | ||
private let defaults: UserDefaults | ||
|
||
init(viewController: UIViewController, defaults: UserDefaults = UserDefaults.standard) { | ||
self.viewController = viewController | ||
self.defaults = defaults | ||
} | ||
|
||
func presentIfNeeded() { | ||
guard FeatureFlag.compliancePopover.enabled else { | ||
return | ||
} | ||
complianceService.getIPCountryCode { [weak self] result in | ||
switch result { | ||
case .success(let countryCode): | ||
DispatchQueue.main.async { | ||
guard let self, self.shouldShowPrivacyBanner(countryCode: countryCode) else { | ||
return | ||
} | ||
let complianceViewModel = CompliancePopoverViewModel() | ||
let complianceViewController = CompliancePopoverViewController(viewModel: complianceViewModel) | ||
let bottomSheetViewController = BottomSheetViewController(childViewController: complianceViewController, customHeaderSpacing: 0) | ||
bottomSheetViewController.show(from: self.viewController) | ||
} | ||
case .failure(let error): | ||
() | ||
} | ||
} | ||
} | ||
|
||
func shouldShowPrivacyBanner(countryCode: String) -> Bool { | ||
let isCountryInEU = Self.gdprCountryCodes.contains(countryCode) | ||
let hasSavedPrivacySettings = defaults.hasSavedPrivacyBannerSettings | ||
return isCountryInEU && !hasSavedPrivacySettings | ||
} | ||
} | ||
|
||
private extension CompliancePopoverCoordinator { | ||
static let gdprCountryCodes: Set<String> = [ | ||
"AT", "AUT", // Austria | ||
"BE", "BEL", // Belgium | ||
"BG", "BGR", // Bulgaria | ||
"HR", "HRV", // Croatia | ||
"CY", "CYP", // Cyprus | ||
"CZ", "CZE", // Czech Republic | ||
"DK", "DNK", // Denmark | ||
"EE", "EST", // Estonia | ||
"FI", "FIN", // Finland | ||
"FR", "FRA", // France | ||
"DE", "DEU", // Germany | ||
"GR", "GRC", // Greece | ||
"HU", "HUN", // Hungary | ||
"IE", "IRL", // Ireland | ||
"IT", "ITA", // Italy | ||
"LV", "LVA", // Latvia | ||
"LT", "LTU", // Lithuania | ||
"LU", "LUX", // Luxembourg | ||
"MT", "MLT", // Malta | ||
"NL", "NLD", // Netherlands | ||
"NO", "NOR", // Norway | ||
"PL", "POL", // Poland | ||
"PT", "PRT", // Portugal | ||
"RO", "ROU", // Romania | ||
"SK", "SVK", // Slovakia | ||
"SI", "SVN", // Slovenia | ||
"ES", "ESP", // Spain | ||
"SE", "SWE", // Sweden | ||
"CH", "CHE", // Switzerland | ||
"IS", | ||
"LI", | ||
"GB", | ||
// *Although the UK has departed from the EU as of January 2021, | ||
// the GDPR was enacted before its withdrawal and is therefore considered a valid UK law.* | ||
] | ||
} | ||
|
||
private extension UserDefaults { | ||
@objc dynamic var hasSavedPrivacyBannerSettings: Bool { | ||
bool(forKey: CompliancePopoverCoordinator.Constants.hasSavedPrivacyBannerSettingsKey) | ||
} | ||
} |
Oops, something went wrong.