Skip to content

Commit

Permalink
Move waitlist code into an AppDelegate extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
samsymons committed Nov 16, 2023
1 parent 44c65c9 commit a1c66d1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 51 deletions.
4 changes: 4 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
4BC6DD1C2A60E6AD001EC129 /* ReportBrokenSiteView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BC6DD1B2A60E6AD001EC129 /* ReportBrokenSiteView.swift */; };
4BCD14632B05AF2B000B1E4C /* NetworkProtectionAccessController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BCD14622B05AF2B000B1E4C /* NetworkProtectionAccessController.swift */; };
4BCD14672B05B682000B1E4C /* NetworkProtectionTermsAndConditionsStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BCD14662B05B682000B1E4C /* NetworkProtectionTermsAndConditionsStore.swift */; };
4BCD14692B05BDD5000B1E4C /* AppDelegate+Waitlists.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BCD14682B05BDD5000B1E4C /* AppDelegate+Waitlists.swift */; };
4BE2756827304F57006B20B0 /* URLRequestExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BE27566272F878F006B20B0 /* URLRequestExtension.swift */; };
4BEF65692989C2FC00B650CB /* AdapterSocketEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 021D307A2989C0C400918636 /* AdapterSocketEvent.swift */; };
4BEF656A2989C2FC00B650CB /* ProxyServerEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 021D307C2989C0C600918636 /* ProxyServerEvent.swift */; };
Expand Down Expand Up @@ -1329,6 +1330,7 @@
4BC6DD1B2A60E6AD001EC129 /* ReportBrokenSiteView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReportBrokenSiteView.swift; sourceTree = "<group>"; };
4BCD14622B05AF2B000B1E4C /* NetworkProtectionAccessController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkProtectionAccessController.swift; sourceTree = "<group>"; };
4BCD14662B05B682000B1E4C /* NetworkProtectionTermsAndConditionsStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkProtectionTermsAndConditionsStore.swift; sourceTree = "<group>"; };
4BCD14682B05BDD5000B1E4C /* AppDelegate+Waitlists.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+Waitlists.swift"; sourceTree = "<group>"; };
4BE27566272F878F006B20B0 /* URLRequestExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = URLRequestExtension.swift; path = ../DuckDuckGo/URLRequestExtension.swift; sourceTree = "<group>"; };
4BFB911A29B7D9530014D4B7 /* AppTrackingProtectionStoringModelPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppTrackingProtectionStoringModelPerformanceTests.swift; sourceTree = "<group>"; };
56244C1C2A137B1900EDF259 /* WaitlistViews.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WaitlistViews.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5122,6 +5124,7 @@
CB24F70E29A3EB15006DCC58 /* AppConfigurationURLProvider.swift */,
84E341951E2F7EFB00BDBA6F /* AppDelegate.swift */,
85DB12EC2A1FED0C000A4A72 /* AppDelegate+AppDeepLinks.swift */,
4BCD14682B05BDD5000B1E4C /* AppDelegate+Waitlists.swift */,
98B31291218CCB8C00E54DE1 /* AppDependencyProvider.swift */,
85BA58591F3506AE00C6E8CA /* AppSettings.swift */,
85BA58541F34F49E00C6E8CA /* AppUserDefaults.swift */,
Expand Down Expand Up @@ -6471,6 +6474,7 @@
31C70B5B2804C61000FB6AD1 /* SaveAutofillLoginManager.swift in Sources */,
85449EFD23FDA71F00512AAF /* KeyboardSettings.swift in Sources */,
980891A222369ADB00313A70 /* FeedbackUserText.swift in Sources */,
4BCD14692B05BDD5000B1E4C /* AppDelegate+Waitlists.swift in Sources */,
988F3DD3237DE8D900AEE34C /* ForgetDataAlert.swift in Sources */,
850ABD012AC3961100A733DF /* MainViewController+Segues.swift in Sources */,
9817C9C321EF594700884F65 /* AutoClear.swift in Sources */,
Expand Down
89 changes: 89 additions & 0 deletions DuckDuckGo/AppDelegate+Waitlists.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// AppDelegate+Waitlists.swift
// DuckDuckGo
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import BackgroundTasks
import NetworkProtection

extension AppDelegate {

func checkWaitlists() {
checkWindowsWaitlist()
checkNetworkProtectionWaitlist()
checkWaitlistBackgroundTasks()
}

private func checkWindowsWaitlist() {
WindowsBrowserWaitlist.shared.fetchInviteCodeIfAvailable { error in
guard error == nil else { return }
WindowsBrowserWaitlist.shared.sendInviteCodeAvailableNotification()
}
}

private func checkNetworkProtectionWaitlist() {
VPNWaitlist.shared.fetchInviteCodeIfAvailable { [weak self] error in
guard error == nil else {
#if !DEBUG
// If the user already has an invite code but their auth token has gone missing, attempt to redeem it again.
let tokenStore = NetworkProtectionKeychainTokenStore()
let waitlistStorage = VPNWaitlist.shared.waitlistStorage
if error == .alreadyHasInviteCode,
let inviteCode = waitlistStorage.getWaitlistInviteCode(),
!tokenStore.isFeatureActivated {
self?.fetchVPNWaitlistAuthToken(inviteCode: inviteCode)
}
#endif
return

}

guard let inviteCode = VPNWaitlist.shared.waitlistStorage.getWaitlistInviteCode() else {
return
}

self?.fetchVPNWaitlistAuthToken(inviteCode: inviteCode)
}
}

private func checkWaitlistBackgroundTasks() {
BGTaskScheduler.shared.getPendingTaskRequests { tasks in
let hasWindowsBrowserWaitlistTask = tasks.contains { $0.identifier == WindowsBrowserWaitlist.backgroundRefreshTaskIdentifier }
if !hasWindowsBrowserWaitlistTask {
WindowsBrowserWaitlist.shared.scheduleBackgroundRefreshTask()
}

let hasVPNWaitlistTask = tasks.contains { $0.identifier == VPNWaitlist.backgroundRefreshTaskIdentifier }
if !hasVPNWaitlistTask {
VPNWaitlist.shared.scheduleBackgroundRefreshTask()
}
}
}

#if NETWORK_PROTECTION
func fetchVPNWaitlistAuthToken(inviteCode: String) {
Task {
do {
try await NetworkProtectionCodeRedemptionCoordinator().redeem(inviteCode)
VPNWaitlist.shared.sendInviteCodeAvailableNotification()
} catch {}
}
}
#endif

}
52 changes: 1 addition & 51 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,46 +408,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}

WindowsBrowserWaitlist.shared.fetchInviteCodeIfAvailable { error in
guard error == nil else { return }
WindowsBrowserWaitlist.shared.sendInviteCodeAvailableNotification()
}

VPNWaitlist.shared.fetchInviteCodeIfAvailable { [weak self] error in
guard error == nil else {
#if !DEBUG
// If the user already has an invite code but their auth token has gone missing, attempt to redeem it again.
let tokenStore = NetworkProtectionKeychainTokenStore()
let waitlistStorage = VPNWaitlist.shared.waitlistStorage
if error == .alreadyHasInviteCode,
let inviteCode = waitlistStorage.getWaitlistInviteCode(),
!tokenStore.isFeatureActivated {
self?.fetchVPNWaitlistAuthToken(inviteCode: inviteCode)
}
#endif
return

}

guard let inviteCode = VPNWaitlist.shared.waitlistStorage.getWaitlistInviteCode() else {
return
}

self?.fetchVPNWaitlistAuthToken(inviteCode: inviteCode)
}

BGTaskScheduler.shared.getPendingTaskRequests { tasks in
let hasWindowsBrowserWaitlistTask = tasks.contains { $0.identifier == WindowsBrowserWaitlist.backgroundRefreshTaskIdentifier }
if !hasWindowsBrowserWaitlistTask {
WindowsBrowserWaitlist.shared.scheduleBackgroundRefreshTask()
}

let hasVPNWaitlistTask = tasks.contains { $0.identifier == VPNWaitlist.backgroundRefreshTaskIdentifier }
if !hasVPNWaitlistTask {
VPNWaitlist.shared.scheduleBackgroundRefreshTask()
}
}

checkWaitlists()
syncService.scheduler.notifyAppLifecycleEvent()
fireFailedCompilationsPixelIfNeeded()
refreshShortcuts()
Expand Down Expand Up @@ -789,17 +750,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
#endif
}

#if NETWORK_PROTECTION
func fetchVPNWaitlistAuthToken(inviteCode: String) {
Task {
do {
try await NetworkProtectionCodeRedemptionCoordinator().redeem(inviteCode)
VPNWaitlist.shared.sendInviteCodeAvailableNotification()
} catch {}
}
}
#endif

}

extension AppDelegate: BlankSnapshotViewRecoveringDelegate {
Expand Down

0 comments on commit a1c66d1

Please sign in to comment.