-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-Enable Broken Site Toast on iOS (#3244)
<!-- Note: This checklist is a reminder of our shared engineering expectations. Feel free to change it, although assigning a GitHub reviewer and the items in bold are required.⚠️ If you're an external contributor, please file an issue first before working on a PR, as we can't guarantee that we will accept your changes if they haven't been discussed ahead of time. Thanks! --> Task/Issue URL: https://app.asana.com/0/1163321984198618/1207889813347127/f Tech Design URL: CC: **Description**: <!-- If at any point it isn't actively being worked on/ready for review/otherwise moving forward strongly consider closing it (or not opening it in the first place). If you decide not to close it, use Draft PR while work is still in progress or use `DO NOT MERGE` label to clarify the PRs state and comment with more information. --> **Steps to test this PR**: 1. Got to a page 2. Refresh twice quickly, you should see the toast prompt. 3. If you do it again it should not show (7 day cool down) 4. By resetting the last shown date user default you can test that dismissing 3 times will stop showing the prompt for 30 days. <!-- Before submitting a PR, please ensure you have tested the combinations you expect the reviewer to test, then delete configurations you *know* do not need explicit testing. Using a simulator where a physical device is unavailable is acceptable. --> **Definition of Done (Internal Only)**: * [ ] Does this PR satisfy our [Definition of Done](https://app.asana.com/0/1202500774821704/1207634633537039/f)? **Copy Testing**: * [ ] Use of correct apostrophes in new copy, ie `’` rather than `'` **Orientation Testing**: * [ ] Portrait * [ ] Landscape **Device Testing**: * [ ] iPhone SE (1st Gen) * [ ] iPhone 8 * [ ] iPhone X * [ ] iPhone 14 Pro * [ ] iPad **OS Testing**: * [ ] iOS 15 * [ ] iOS 16 * [ ] iOS 17 **Theme Testing**: * [ ] Light theme * [ ] Dark theme --- ###### Internal references: [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943)
- Loading branch information
1 parent
3f030f0
commit bc43533
Showing
39 changed files
with
829 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// BrokenSitePromptLimiter.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 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 Core | ||
import BrowserServicesKit | ||
|
||
protocol BrokenSitePromptLimiterStoring { | ||
var lastToastShownDate: Date { get set } | ||
var toastDismissStreakCounter: Int { get set } | ||
} | ||
|
||
final class BrokenSitePromptLimiterStore: BrokenSitePromptLimiterStoring { | ||
@UserDefaultsWrapper(key: .lastBrokenSiteToastShownDate, defaultValue: .distantPast) | ||
var lastToastShownDate: Date | ||
|
||
@UserDefaultsWrapper(key: .toastDismissStreakCounter, defaultValue: 0) | ||
var toastDismissStreakCounter: Int | ||
} | ||
|
||
final class BrokenSitePromptLimiter { | ||
|
||
struct BrokenSitePromptLimiterSettings: Codable { | ||
let maxDismissStreak: Int | ||
let dismissStreakResetDays: Int | ||
let coolDownDays: Int | ||
} | ||
|
||
private var lastToastShownDate: Date { | ||
get { store.lastToastShownDate } | ||
set { store.lastToastShownDate = newValue } | ||
} | ||
|
||
private var toastDismissStreakCounter: Int { | ||
get { store.toastDismissStreakCounter } | ||
set { store.toastDismissStreakCounter = newValue } | ||
} | ||
|
||
private var privacyConfigManager: PrivacyConfigurationManaging | ||
private var store: BrokenSitePromptLimiterStoring | ||
|
||
init(privacyConfigManager: PrivacyConfigurationManaging = ContentBlocking.shared.privacyConfigurationManager, | ||
store: BrokenSitePromptLimiterStoring = BrokenSitePromptLimiterStore()) { | ||
self.privacyConfigManager = privacyConfigManager | ||
self.store = store | ||
} | ||
|
||
private func getSettingsFromConfig() -> BrokenSitePromptLimiterSettings { | ||
let settings = privacyConfigManager.privacyConfig.settings(for: .brokenSitePrompt) | ||
|
||
// Get settings from config or fallback to standard defaults | ||
return BrokenSitePromptLimiterSettings( | ||
maxDismissStreak: settings["maxDismissStreak"] as? Int ?? 3, | ||
dismissStreakResetDays: settings["dismissStreakResetDays"] as? Int ?? 30, | ||
coolDownDays: settings["coolDownDays"] as? Int ?? 7 | ||
) | ||
} | ||
|
||
/// If it has been `dismissStreakResetDays` or more since the last time we showed the prompt, reset the dismiss counter to 0 | ||
private func resetDismissStreakIfNeeded(dismissStreakResetDays: Int) { | ||
if !lastToastShownDate.isLessThan(daysAgo: dismissStreakResetDays) { | ||
toastDismissStreakCounter = 0 | ||
} | ||
} | ||
|
||
public func shouldShowToast() -> Bool { | ||
guard privacyConfigManager.privacyConfig.isEnabled(featureKey: .brokenSitePrompt) else { return false } | ||
|
||
let settings = getSettingsFromConfig() | ||
|
||
resetDismissStreakIfNeeded(dismissStreakResetDays: settings.dismissStreakResetDays) | ||
guard toastDismissStreakCounter < settings.maxDismissStreak else { return false } // Don't show the toast if the user dismissed it more than `maxDismissStreak` times in a row | ||
guard !lastToastShownDate.isLessThan(daysAgo: settings.coolDownDays) else { return false } // Only show the toast once per `coolDownDays` days | ||
|
||
return true | ||
} | ||
|
||
public func didShowToast() { | ||
lastToastShownDate = Date() | ||
} | ||
|
||
public func didDismissToast() { | ||
toastDismissStreakCounter += 1 | ||
} | ||
|
||
public func didOpenReport() { | ||
toastDismissStreakCounter = 0 | ||
} | ||
|
||
} |
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,61 @@ | ||
// | ||
// BrokenSitePromptView.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 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 SwiftUI | ||
import DuckUI | ||
import DesignResourcesKit | ||
|
||
struct BrokenSitePromptView: View { | ||
|
||
let viewModel: BrokenSitePromptViewModel | ||
|
||
var body: some View { | ||
VStack { | ||
VStack(alignment: .leading, spacing: 8) { | ||
VStack(alignment: .leading) { | ||
Text(UserText.siteNotWorkingTitle) | ||
.font(Font(uiFont: .daxSubheadSemibold())) | ||
Text(UserText.siteNotWorkingSubtitle) | ||
.font(Font(uiFont: .daxSubheadRegular())) | ||
} | ||
HStack { | ||
Spacer() | ||
Button(UserText.siteNotWorkingDismiss, action: viewModel.onDidDismiss) | ||
.buttonStyle(GhostButtonStyle()) | ||
.fixedSize() | ||
Button(UserText.siteNotWorkingWebsiteIsBroken, action: viewModel.onDidSubmit) | ||
.buttonStyle(PrimaryButtonStyle(compact: true)) | ||
.fixedSize() | ||
} | ||
} | ||
.padding(EdgeInsets(top: 12, leading: 16, bottom: 4, trailing: 16)) | ||
Color(designSystemColor: .lines).frame(height: 1 / UIScreen.main.scale) | ||
} | ||
.background(Color(designSystemColor: .panel)) | ||
} | ||
|
||
} | ||
|
||
#Preview { | ||
|
||
let viewModel = BrokenSitePromptViewModel(onDidDismiss: {}, | ||
onDidSubmit: {}) | ||
return BrokenSitePromptView(viewModel: viewModel) | ||
|
||
} |
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,32 @@ | ||
// | ||
// BrokenSitePromptViewModel.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 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 | ||
|
||
final class BrokenSitePromptViewModel { | ||
|
||
let onDidDismiss: () -> Void | ||
let onDidSubmit: () -> Void | ||
|
||
init(onDidDismiss: @escaping () -> Void, onDidSubmit: @escaping () -> Void) { | ||
self.onDidDismiss = onDidDismiss | ||
self.onDidSubmit = onDidSubmit | ||
} | ||
|
||
} |
Oops, something went wrong.