Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared code for VPN onboarding tips #1024

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ let package = Package(
.library(name: "SpecialErrorPages", targets: ["SpecialErrorPages"]),
.library(name: "DuckPlayer", targets: ["DuckPlayer"]),
.library(name: "PhishingDetection", targets: ["PhishingDetection"]),
.library(name: "Onboarding", targets: ["Onboarding"])
.library(name: "Onboarding", targets: ["Onboarding"]),
.library(name: "TipKitUtils", targets: ["TipKitUtils"])
],
dependencies: [
.package(url: "https://github.com/duckduckgo/duckduckgo-autofill.git", exact: "15.0.0"),
Expand Down Expand Up @@ -429,6 +430,14 @@ let package = Package(
.define("DEBUG", .when(configuration: .debug))
]
),
.target(
name: "TipKitUtils",
dependencies: [
],
swiftSettings: [
.define("DEBUG", .when(configuration: .debug))
]
),

// MARK: - Test Targets
.testTarget(
Expand Down
36 changes: 36 additions & 0 deletions Sources/Common/Combine/CurrentValuePublisher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// The Swift Programming Language

Check failure on line 1 in Sources/Common/Combine/CurrentValuePublisher.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Header comments should be consistent with project patterns (file_header)
// https://docs.swift.org/swift-book

import Combine
import Foundation

public final class CurrentValuePublisher<Output, Failure: Error> {

private(set) public var value: Output
private let wrappedPublisher: AnyPublisher<Output, Failure>
private var cancellable: AnyCancellable?

public init(initialValue: Output, publisher: AnyPublisher<Output, Failure>) {
value = initialValue
wrappedPublisher = publisher

subscribeToPublisherUpdates()
}

private func subscribeToPublisherUpdates() {
cancellable = wrappedPublisher
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in }) { [weak self] value in
self?.value = value
}
}
}

extension CurrentValuePublisher: Publisher {
public func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {

Check failure on line 30 in Sources/Common/Combine/CurrentValuePublisher.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)

wrappedPublisher.receive(subscriber: subscriber)
}

Check failure on line 34 in Sources/Common/Combine/CurrentValuePublisher.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)

Check failure on line 35 in Sources/Common/Combine/CurrentValuePublisher.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
}
94 changes: 94 additions & 0 deletions Sources/TipKitUtils/TipKitController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// TipKitController.swift
//
// 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 os.log
import TipKit

public protocol TipKitControlling {
@available(iOS 17.0, *)
func configureTipKit()

@available(iOS 17.0, *)
func resetTipKitOnNextAppLaunch()
}

typealias TipKitAppEventHandler = TipKitController

public final class TipKitController {

private let logger: Logger
private let userDefaults: UserDefaults

private var resetTipKitOnNextLaunch: Bool {
get {
userDefaults.bool(forKey: "resetTipKitOnNextLaunch")
}

set {
userDefaults.set(newValue, forKey: "resetTipKitOnNextLaunch")
}
}

public init(logger: Logger,
userDefaults: UserDefaults) {

self.logger = logger
self.userDefaults = userDefaults
}

@available(iOS 17.0, macOS 14.0, *)
public func configureTipKit(_ configuration: [Tips.ConfigurationOption] = []) {
do {
if resetTipKitOnNextLaunch {
resetTipKit()
resetTipKitOnNextLaunch = false
}

try Tips.configure(configuration)

logger.debug("TipKit initialized")
} catch {
logger.error("Failed to initialize TipKit: \(error)")
}
}

@available(iOS 17.0, macOS 14.0, *)
private func resetTipKit() {
do {
try Tips.resetDatastore()

logger.debug("TipKit reset")
} catch {
logger.debug("Failed to reset TipKit: \(error)")
}
}

/// Resets TipKit
///
/// One thing that's not documented as of 2024-10-09 is that resetting TipKit must happen before it's configured.
/// When trying to reset it after it's configured we get `TipKit.TipKitError(value: TipKit.TipKitError.Value.tipsDatastoreAlreadyConfigured)`.
/// In order to make things work for us we set a user defaults value that ensures TipKit will be reset on next
/// app launch instead of directly trying to reset it here.
///
@available(iOS 17.0, *)
public func resetTipKitOnNextAppLaunch() {
resetTipKitOnNextLaunch = true
logger.debug("TipKit will reset on next app launch")
}
}
Loading