Skip to content

Commit

Permalink
Merge branch 'main' into anh/netp-check-entitlement-periodically
Browse files Browse the repository at this point in the history
# Conflicts:
#	DuckDuckGo.xcodeproj/project.pbxproj
#	DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
  • Loading branch information
quanganhdo committed Feb 21, 2024
2 parents 98a348a + be3dd4d commit 029a9dc
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
{
"identity" : "trackerradarkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/TrackerRadarKit.git",
"location" : "https://github.com/duckduckgo/TrackerRadarKit",
"state" : {
"revision" : "a6b7ba151d9dc6684484f3785293875ec01cc1ff",
"version" : "1.2.2"
Expand Down
44 changes: 25 additions & 19 deletions DuckDuckGo/SettingsSubscriptionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,36 @@ struct SettingsSubscriptionView: View {

private var subscriptionDetailsView: some View {
return Group {
SettingsCellView(label: UserText.settingsPProVPNTitle,
subtitle: viewModel.state.networkProtection.status != "" ? viewModel.state.networkProtection.status : nil,
action: { viewModel.presentLegacyView(.netP) },
disclosureIndicator: true,
isButton: true)

if viewModel.shouldShowNetP {
SettingsCellView(label: UserText.settingsPProVPNTitle,
subtitle: viewModel.state.networkProtection.status != "" ? viewModel.state.networkProtection.status : nil,
action: { viewModel.presentLegacyView(.netP) },
disclosureIndicator: true,
isButton: true)
}

SettingsCellView(label: UserText.settingsPProDBPTitle,
subtitle: UserText.settingsPProDBPSubTitle,
action: { isShowingDBP.toggle() }, isButton: true)
.sheet(isPresented: $isShowingDBP) {
SubscriptionPIRView()
if viewModel.shouldShowDBP {
SettingsCellView(label: UserText.settingsPProDBPTitle,
subtitle: UserText.settingsPProDBPSubTitle,
action: { isShowingDBP.toggle() }, isButton: true)
.sheet(isPresented: $isShowingDBP) {
SubscriptionPIRView()
}
}

SettingsCellView(label: UserText.settingsPProITRTitle,
subtitle: UserText.settingsPProITRSubTitle,
action: { isShowingITP.toggle() }, isButton: true)
.sheet(isPresented: $isShowingITP) {
SubscriptionITPView()
if viewModel.shouldShowITP {
SettingsCellView(label: UserText.settingsPProITRTitle,
subtitle: UserText.settingsPProITRSubTitle,
action: { isShowingITP.toggle() }, isButton: true)
.sheet(isPresented: $isShowingITP) {
SubscriptionITPView()
}
}


NavigationLink(destination: SubscriptionSettingsView()) {
SettingsCustomCell(content: { manageSubscriptionView })
if viewModel.shouldShowDBP || viewModel.shouldShowITP || viewModel.shouldShowNetP {
NavigationLink(destination: SubscriptionSettingsView()) {
SettingsCustomCell(content: { manageSubscriptionView })
}
}

}
Expand Down
57 changes: 35 additions & 22 deletions DuckDuckGo/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ final class SettingsViewModel: ObservableObject {
// Add more views as needed here...
@Published var shouldNavigateToDBP = false
@Published var shouldNavigateToITP = false

@Published var shouldShowNetP = false
@Published var shouldShowDBP = false
@Published var shouldShowITP = false

// Our View State
@Published private(set) var state: SettingsState
Expand Down Expand Up @@ -327,32 +331,41 @@ extension SettingsViewModel {
@available(iOS 15.0, *)
@MainActor
private func setupSubscriptionEnvironment() async {

// Active subscription check
if let token = accountManager.accessToken {
guard let token = accountManager.accessToken else {
setupSubscriptionPurchaseOptions()
return
}

// Fetch available subscriptions from the backend (or sign out)
switch await SubscriptionService.getSubscriptionDetails(token: token) {
case .success(let response) where !response.isSubscriptionActive:
AccountManager().signOut()
setupSubscriptionPurchaseOptions()
case .success(let response):
// Cache Subscription state
Self.cachedHasActiveSubscription = self.state.subscription.hasActiveSubscription

// Fetch available subscriptions from the backend (or sign out)
if case .success(let response) = await SubscriptionService.getSubscriptionDetails(token: token) {
if !response.isSubscriptionActive {
AccountManager().signOut()
setupSubscriptionPurchaseOptions()
return
}

// Check for valid entitlements
if case let .success(entitlements) = await AccountManager().fetchEntitlements() {
self.state.subscription.hasActiveSubscription = !entitlements.isEmpty
}

// Cache Subscription state
Self.cachedHasActiveSubscription = self.state.subscription.hasActiveSubscription

// Enable Subscription purchase if there's no active subscription
if self.state.subscription.hasActiveSubscription == false {
setupSubscriptionPurchaseOptions()
// Check entitlements and update UI accordingly
let entitlements: [AccountManager.Entitlement] = [.identityTheftRestoration, .dataBrokerProtection, .networkProtection]
for entitlement in entitlements {
if case .success = await AccountManager().hasEntitlement(for: entitlement) {
switch entitlement {
case .identityTheftRestoration:
self.shouldShowITP = true
case .dataBrokerProtection:
self.shouldShowDBP = true
case .networkProtection:
self.shouldShowNetP = true
}
}
}
} else {

// Enable Subscription purchase if there's no active subscription
if !self.state.subscription.hasActiveSubscription {
setupSubscriptionPurchaseOptions()
}
default:
setupSubscriptionPurchaseOptions()
}
}
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/DuckUI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let package = Package(
targets: ["DuckUI"])
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "109.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "109.0.1"),
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/SyncUI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let package = Package(
],
dependencies: [
.package(path: "../DuckUI"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "109.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "109.0.1"),
.package(url: "https://github.com/duckduckgo/DesignResourcesKit", exact: "2.0.0")
],
targets: [
Expand Down
2 changes: 1 addition & 1 deletion LocalPackages/Waitlist/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let package = Package(
targets: ["Waitlist", "WaitlistMocks"])
],
dependencies: [
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "109.0.0"),
.package(url: "https://github.com/duckduckgo/BrowserServicesKit", exact: "109.0.1"),
.package(url: "https://github.com/duckduckgo/DesignResourcesKit", exact: "2.0.0")
],
targets: [
Expand Down

0 comments on commit 029a9dc

Please sign in to comment.