Skip to content

Commit

Permalink
DI #2 + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
federicocappelli committed May 10, 2024
1 parent 24d28b1 commit ffc80df
Show file tree
Hide file tree
Showing 23 changed files with 223 additions and 157 deletions.
27 changes: 0 additions & 27 deletions Core/AccountManager+AppGroup.swift

This file was deleted.

16 changes: 4 additions & 12 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,6 @@
D6FEB8B12B7498A300C3615F /* HeadlessWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6FEB8B02B7498A300C3615F /* HeadlessWebView.swift */; };
D6FEB8B32B74990D00C3615F /* HeadlessWebViewNavCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6FEB8B22B74990D00C3615F /* HeadlessWebViewNavCoordinator.swift */; };
D6FEB8B52B74994000C3615F /* HeadlessWebViewCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6FEB8B42B74994000C3615F /* HeadlessWebViewCoordinator.swift */; };
D6FF22482BC95F0B008E7BCC /* AccountManager+AppGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6FF22472BC95F0B008E7BCC /* AccountManager+AppGroup.swift */; };
EA39B7E2268A1A35000C62CD /* privacy-reference-tests in Resources */ = {isa = PBXBuildFile; fileRef = EA39B7E1268A1A35000C62CD /* privacy-reference-tests */; };
EAB19EDA268963510015D3EA /* DomainMatchingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAB19ED9268963510015D3EA /* DomainMatchingTests.swift */; };
EE0153E12A6EABE0002A8B26 /* NetworkProtectionConvenienceInitialisers.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE0153E02A6EABE0002A8B26 /* NetworkProtectionConvenienceInitialisers.swift */; };
Expand Down Expand Up @@ -866,6 +865,7 @@
F159BDA41F0BDB5A00B4A01D /* TabViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F159BDA31F0BDB5A00B4A01D /* TabViewController.swift */; };
F15D43201E706CC500BF2CDC /* AutocompleteViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F15D431F1E706CC500BF2CDC /* AutocompleteViewController.swift */; };
F15E9F3C2BECFCFD00DEFDDE /* SubscriptionTestingUtilities in Frameworks */ = {isa = PBXBuildFile; productRef = F15E9F3B2BECFCFD00DEFDDE /* SubscriptionTestingUtilities */; };
F15E9F3E2BEE128200DEFDDE /* SubscriptionManageriOS14.swift in Sources */ = {isa = PBXBuildFile; fileRef = F15E9F3D2BEE128200DEFDDE /* SubscriptionManageriOS14.swift */; };
F1617C131E572E0300DEDCAF /* TabSwitcherViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1617C121E572E0300DEDCAF /* TabSwitcherViewController.swift */; };
F1617C151E57336D00DEDCAF /* TabManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1617C141E57336D00DEDCAF /* TabManager.swift */; };
F1617C191E573EA800DEDCAF /* TabSwitcherDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1617C181E573EA800DEDCAF /* TabSwitcherDelegate.swift */; };
Expand Down Expand Up @@ -2415,7 +2415,6 @@
D6FEB8B02B7498A300C3615F /* HeadlessWebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeadlessWebView.swift; sourceTree = "<group>"; };
D6FEB8B22B74990D00C3615F /* HeadlessWebViewNavCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeadlessWebViewNavCoordinator.swift; sourceTree = "<group>"; };
D6FEB8B42B74994000C3615F /* HeadlessWebViewCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeadlessWebViewCoordinator.swift; sourceTree = "<group>"; };
D6FF22472BC95F0B008E7BCC /* AccountManager+AppGroup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AccountManager+AppGroup.swift"; sourceTree = "<group>"; };
EA39B7E1268A1A35000C62CD /* privacy-reference-tests */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = "privacy-reference-tests"; path = "submodules/privacy-reference-tests"; sourceTree = SOURCE_ROOT; };
EAB19ED9268963510015D3EA /* DomainMatchingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DomainMatchingTests.swift; sourceTree = "<group>"; };
EE0153E02A6EABE0002A8B26 /* NetworkProtectionConvenienceInitialisers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkProtectionConvenienceInitialisers.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2519,6 +2518,7 @@
F1564F022B7B915F00D454A6 /* AppDelegate+SKAD4.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AppDelegate+SKAD4.swift"; sourceTree = "<group>"; };
F159BDA31F0BDB5A00B4A01D /* TabViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TabViewController.swift; sourceTree = "<group>"; };
F15D431F1E706CC500BF2CDC /* AutocompleteViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutocompleteViewController.swift; sourceTree = "<group>"; };
F15E9F3D2BEE128200DEFDDE /* SubscriptionManageriOS14.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionManageriOS14.swift; sourceTree = "<group>"; };
F1617C121E572E0300DEDCAF /* TabSwitcherViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TabSwitcherViewController.swift; sourceTree = "<group>"; };
F1617C141E57336D00DEDCAF /* TabManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TabManager.swift; sourceTree = "<group>"; };
F1617C181E573EA800DEDCAF /* TabSwitcherDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TabSwitcherDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4339,6 +4339,7 @@
isa = PBXGroup;
children = (
D60170BB2BA32DD6001911B5 /* Subscription.swift */,
F15E9F3D2BEE128200DEFDDE /* SubscriptionManageriOS14.swift */,
D6D95CE42B6DA3F200960317 /* AsyncHeadlessWebview */,
D664C7952B289AA000CBFA76 /* Subscription.storekit */,
D664C7932B289AA000CBFA76 /* ViewModel */,
Expand Down Expand Up @@ -4452,14 +4453,6 @@
name = Model;
sourceTree = "<group>";
};
D6FF22462BC95EF9008E7BCC /* Subscription */ = {
isa = PBXGroup;
children = (
D6FF22472BC95F0B008E7BCC /* AccountManager+AppGroup.swift */,
);
name = Subscription;
sourceTree = "<group>";
};
EA7EFE662677F5BD0075464E /* PrivacyReferenceTests */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -4848,7 +4841,6 @@
F143C2E51E4A4CD400CFDE3A /* Core */ = {
isa = PBXGroup;
children = (
D6FF22462BC95EF9008E7BCC /* Subscription */,
F1CE42A71ECA0A520074A8DF /* Bookmarks */,
837774491F8E1ECE00E17A29 /* ContentBlocker */,
F143C2E61E4A4CD400CFDE3A /* Core.h */,
Expand Down Expand Up @@ -6256,6 +6248,7 @@
3132FA2627A0784600DD7A12 /* FilePreviewHelper.swift in Sources */,
9820FF502244FECC008D4782 /* UIScrollViewExtension.swift in Sources */,
8540BD5423D8D5080057FDD2 /* PreserveLoginsAlert.swift in Sources */,
F15E9F3E2BEE128200DEFDDE /* SubscriptionManageriOS14.swift in Sources */,
1E87615928A1517200C7C5CE /* PrivacyDashboardViewController.swift in Sources */,
EE9D68D12AE00CF300B55EF4 /* NetworkProtectionVPNSettingsView.swift in Sources */,
319A371028299A850079FBCE /* PasswordHider.swift in Sources */,
Expand Down Expand Up @@ -6984,7 +6977,6 @@
854858E32937BC550063610B /* CollectionExtension.swift in Sources */,
1E6A4D692984208800A371D3 /* LocaleExtension.swift in Sources */,
98F6EA472863124100720957 /* ContentBlockerRulesLists.swift in Sources */,
D6FF22482BC95F0B008E7BCC /* AccountManager+AppGroup.swift in Sources */,
F1134EB01F40AC6300B73467 /* AtbParser.swift in Sources */,
EE50052E29C369D300AE0773 /* FeatureFlag.swift in Sources */,
BD15DB852B959CFD00821457 /* BundleExtension.swift in Sources */,
Expand Down
20 changes: 8 additions & 12 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

@UserDefaultsWrapper(key: .privacyConfigCustomURL, defaultValue: nil)
private var privacyConfigCustomURL: String?
public let subscriptionManager: SubscriptionManaging?
public let subscriptionManager: SubscriptionManaging
private var accountManager: AccountManaging? {
subscriptionManager?.accountManager
subscriptionManager.accountManager
}

override init() {
Expand All @@ -122,6 +122,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
subscriptionService: subscriptionService,
authService: authService,
subscriptionEnvironment: subscriptionEnvironment)
} else {
// This is used just for iOS <15, it's a sort of mocked environment that will not be used.
subscriptionManager = SubscriptionManageriOS14(accountManager: accountManager)
}

vpnFeatureVisibility = DefaultNetworkProtectionVisibility(accountManager: accountManager)
Expand Down Expand Up @@ -352,8 +355,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
NetworkProtectionAccessController().refreshNetworkProtectionAccess()
#endif

// setupSubscriptionsEnvironment()

if vpnFeatureVisibility.shouldKeepVPNAccessViaWaitlist() {
clearDebugWaitlistState()
}
Expand All @@ -365,11 +366,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}

@available(iOS 15.0, *)
public func getSubscriptionManager() -> SubscriptionManaging {
subscriptionManager!
}

private func prepareTabsModel(previewsSource: TabPreviewsSource = TabPreviewsSource(),
appSettings: AppSettings = AppDependencyProvider.shared.appSettings,
isDesktop: Bool = UIDevice.current.userInterfaceIdiom == .pad) -> TabsModel {
Expand Down Expand Up @@ -591,9 +587,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func updateSubscriptionStatus() {
Task {
guard let token = accountManager?.accessToken else { return }

if case .success(let subscription) = await subscriptionManager?.subscriptionService.getSubscription(accessToken: token,
cachePolicy: .reloadIgnoringLocalCacheData) {
var subscriptionService: SubscriptionService { subscriptionManager.subscriptionService }
if case .success(let subscription) = await subscriptionService.getSubscription(accessToken: token,
cachePolicy: .reloadIgnoringLocalCacheData) {
if subscription.isActive {
DailyPixel.fire(pixel: .privacyProSubscriptionActive)
}
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/DefaultNetworkProtectionVisibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct DefaultNetworkProtectionVisibility: NetworkProtectionFeatureVisibility {
static func forTokenStore() -> DefaultNetworkProtectionVisibility {
DefaultNetworkProtectionVisibility(networkProtectionTokenStore: nil,
networkProtectionAccessManager: nil,
accountManager: AppDelegate.appDelegate().getSubscriptionManager().accountManager)
accountManager: AppDelegate.appDelegate().subscriptionManager.accountManager)
}

func isWaitlistBetaActive() -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Feedback/VPNMetadataCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ final class DefaultVPNMetadataCollector: VPNMetadataCollector {
enableSource: .init(from: accessManager.networkProtectionAccessType()),
betaParticipant: accessType == .waitlistInvited,
hasToken: hasToken,
subscriptionActive: AppDelegate.accountManager.isUserAuthenticated
subscriptionActive: AppDelegate.appDelegate().subscriptionManager.accountManager.isUserAuthenticated
)
}
}
Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,8 @@ class MainViewController: UIViewController {
@objc
private func onEntitlementsChange(_ notification: Notification) {
Task {
guard case .success(false) = await AppDelegate.appDelegate().subscriptionManager?.accountManager.hasEntitlement(for: .networkProtection) else { return }
let accountManager = AppDelegate.appDelegate().subscriptionManager.accountManager
guard case .success(false) = await accountManager.hasEntitlement(for: .networkProtection) else { return }

let controller = NetworkProtectionTunnelController()

Expand Down
9 changes: 6 additions & 3 deletions DuckDuckGo/NetworkProtectionConvenienceInitialisers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension NetworkProtectionKeychainTokenStore {
let isSubscriptionEnabled = featureVisibility.isPrivacyProLaunched()
let accessTokenProvider: () -> String? = {
if featureVisibility.shouldMonitorEntitlement() {
return { AppDelegate.accountManager.accessToken }
return { AppDelegate.appDelegate().subscriptionManager.accountManager.accessToken }
}
return { nil }
}()
Expand All @@ -79,12 +79,14 @@ extension NetworkProtectionKeychainTokenStore {
extension NetworkProtectionCodeRedemptionCoordinator {
convenience init(isManualCodeRedemptionFlow: Bool = false) {
let settings = VPNSettings(defaults: .networkProtectionGroupDefaults)
var subscriptionManager: SubscriptionManaging { AppDelegate.appDelegate().subscriptionManager }
let networkProtectionVisibility = DefaultNetworkProtectionVisibility(accountManager: subscriptionManager.accountManager)
self.init(
environment: settings.selectedEnvironment,
tokenStore: NetworkProtectionKeychainTokenStore(),
isManualCodeRedemptionFlow: isManualCodeRedemptionFlow,
errorEvents: .networkProtectionAppDebugEvents,
isSubscriptionEnabled: DefaultNetworkProtectionVisibility(accountManager: AppDelegate.accountManager).isPrivacyProLaunched()
isSubscriptionEnabled: networkProtectionVisibility.isPrivacyProLaunched()
)
}
}
Expand All @@ -101,11 +103,12 @@ extension NetworkProtectionVPNSettingsViewModel {
extension NetworkProtectionLocationListCompositeRepository {
convenience init() {
let settings = VPNSettings(defaults: .networkProtectionGroupDefaults)
var subscriptionManager: SubscriptionManaging { AppDelegate.appDelegate().subscriptionManager }
self.init(
environment: settings.selectedEnvironment,
tokenStore: NetworkProtectionKeychainTokenStore(),
errorEvents: .networkProtectionAppDebugEvents,
isSubscriptionEnabled: DefaultNetworkProtectionVisibility(accountManager: AppDelegate.accountManager).isPrivacyProLaunched()
isSubscriptionEnabled: DefaultNetworkProtectionVisibility(accountManager: subscriptionManager.accountManager).isPrivacyProLaunched()
)
}
}
Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGo/NetworkProtectionDebugViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ final class NetworkProtectionDebugViewController: UITableViewController {
}

required convenience init?(coder: NSCoder) {
self.init(coder: coder, tokenStore: NetworkProtectionKeychainTokenStore(), accountManager: AppDelegate.accountManager)
self.init(coder: coder, tokenStore: NetworkProtectionKeychainTokenStore(),
accountManager: AppDelegate.appDelegate().subscriptionManager.accountManager)
}

override func viewWillAppear(_ animated: Bool) {
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/NetworkProtectionRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct NetworkProtectionRootView: View {
redemptionCoordinator: NetworkProtectionCodeRedemptionCoordinator(isManualCodeRedemptionFlow: true),
completion: inviteCompletion
)
if DefaultNetworkProtectionVisibility(accountManager: AppDelegate.appDelegate().subscriptionManager?.accountManager).isPrivacyProLaunched() {
if DefaultNetworkProtectionVisibility(accountManager: AppDelegate.appDelegate().subscriptionManager.accountManager).isPrivacyProLaunched() {
NetworkProtectionStatusView(
statusModel: NetworkProtectionStatusViewModel()
)
Expand Down
16 changes: 5 additions & 11 deletions DuckDuckGo/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class SettingsViewModel: ObservableObject {
var emailManager: EmailManager { EmailManager() }

// Subscription Dependencies
private let subscriptionManager: SubscriptionManaging?
private let subscriptionManager: SubscriptionManaging
private var subscriptionSignOutObserver: Any?

private enum UserDefaultsCacheKey: String, UserDefaultsCacheKeyStore {
Expand Down Expand Up @@ -390,7 +390,7 @@ final class SettingsViewModel: ObservableObject {
// MARK: Default Init
init(state: SettingsState? = nil,
legacyViewProvider: SettingsLegacyViewProvider,
subscriptionManager: SubscriptionManaging?,
subscriptionManager: SubscriptionManaging,
voiceSearchHelper: VoiceSearchHelperProtocol = AppDependencyProvider.shared.voiceSearchHelper,
variantManager: VariantManager = AppDependencyProvider.shared.variantManager,
deepLink: SettingsDeepLinkSection? = nil) {
Expand Down Expand Up @@ -451,7 +451,7 @@ extension SettingsViewModel {
private func getNetworkProtectionState() -> SettingsState.NetworkProtection {
var enabled = false
#if NETWORK_PROTECTION
if #available(iOS 15, *), let subscriptionManager {
if #available(iOS 15, *) {
enabled = DefaultNetworkProtectionVisibility(accountManager: subscriptionManager.accountManager).shouldKeepVPNAccessViaWaitlist()
}
#endif
Expand Down Expand Up @@ -491,8 +491,7 @@ extension SettingsViewModel {

#if NETWORK_PROTECTION
private func updateNetPStatus(connectionStatus: ConnectionStatus) {
if let subscriptionManager,
DefaultNetworkProtectionVisibility(accountManager: subscriptionManager.accountManager).isPrivacyProLaunched() {
if DefaultNetworkProtectionVisibility(accountManager: subscriptionManager.accountManager).isPrivacyProLaunched() {
switch connectionStatus {
case .connected:
self.state.networkProtection.status = UserText.netPCellConnected
Expand Down Expand Up @@ -744,11 +743,6 @@ extension SettingsViewModel {

@MainActor
private func setupSubscriptionEnvironment() async {

guard let subscriptionManager else {
return
}

// If there's cached data use it by default
if let cachedSubscription = subscriptionStateCache.get() {
state.subscription = cachedSubscription
Expand Down Expand Up @@ -783,7 +777,7 @@ extension SettingsViewModel {
// Check entitlements and update state
let entitlements: [Entitlement.ProductName] = [.networkProtection, .dataBrokerProtection, .identityTheftRestoration]
for entitlement in entitlements {
if case .success = await AccountManager().hasEntitlement(for: entitlement) {
if case .success = await subscriptionManager.accountManager.hasEntitlement(for: entitlement) {
switch entitlement {
case .identityTheftRestoration:
self.state.subscription.entitlements.append(.identityTheftRestoration)
Expand Down
Loading

0 comments on commit ffc80df

Please sign in to comment.