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

task: Add a separate consent checkbox for firebase analytics #2022 #2072

Merged
merged 1 commit into from
Jul 2, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public final class RuuviAnalyticsImpl: RuuviAnalytics {
self.ruuviStorage = ruuviStorage
self.settings = settings
self.alertService = alertService
self.setConsentSettings()
}

public func update() {
Expand Down Expand Up @@ -187,6 +186,10 @@ public final class RuuviAnalyticsImpl: RuuviAnalytics {
set(.alertRSSI(0))
}

public func setConsent(allowed: Bool) {
setConsentSettings(allowed: allowed)
}

private func set(_ property: Properties) {
let value: String = switch property {
case let .loggedIn(isLoggedIn):
Expand Down Expand Up @@ -276,12 +279,12 @@ public final class RuuviAnalyticsImpl: RuuviAnalytics {
return (temperatureAlertCount, humidityAlertCount, pressureAlertCount, movementAlertCount)
}

private func setConsentSettings() {
private func setConsentSettings(allowed: Bool) {
let consentSettings: [ConsentType: ConsentStatus] = [
.adStorage: .denied,
.adUserData: .denied,
.adPersonalization: .denied,
.analyticsStorage: .granted,
.analyticsStorage: allowed ? .granted : .denied,
]
#if DEBUG || ALPHA
// skip using analytics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import UIKit
#if (DEBUG || ALPHA) && canImport(FLEX)
import FLEX
#endif
import RuuviAnalytics
import RuuviContext
import RuuviCore
import RuuviLocal
Expand Down Expand Up @@ -80,6 +81,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
window = UIWindow(frame: UIScreen.main.bounds)
let appRouter = AppRouter()
appRouter.settings = r.resolve(RuuviLocalSettings.self)
appRouter.ruuviAnalytics = r.resolve(RuuviAnalytics.self)
window?.rootViewController = appRouter.viewController
window?.makeKeyAndVisible()
self.appRouter = appRouter
Expand Down
14 changes: 13 additions & 1 deletion Apps/RuuviStation/Sources/Classes/Routers/AppRouter.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import LightRoute
import RuuviAnalytics
import RuuviLocal
import RuuviOntology
import RuuviUser
Expand All @@ -10,14 +11,15 @@ final class AppRouter {
}

var settings: RuuviLocalSettings!
var ruuviAnalytics: RuuviAnalytics!

// navigation controller
private var navigationController: UINavigationController {
if let navigationController = weakNavigationController {
return navigationController
} else {
let rootViewController: UIViewController
if settings.welcomeShown && settings.tosAccepted {
if settings.welcomeShown && settings.tosAccepted && settings.analyticsConsentGiven {
let controller = dashboardViewController()
rootViewController = controller
} else {
Expand Down Expand Up @@ -121,9 +123,19 @@ extension AppRouter: OnboardRouterDelegate {
})
}

func ruuviOnboardDidProvideAnalyticsConsent(
_ router: OnboardRouter,
consentGiven: Bool
) {
ruuviAnalytics.setConsent(
allowed: consentGiven
)
}

private func presentDashboard() {
settings.welcomeShown = true
settings.tosAccepted = true
settings.analyticsConsentGiven = true
AppUtility.lockOrientation(.all)
let controller = dashboardViewController()
navigationController.setNavigationBarHidden(false, animated: false)
Expand Down
22 changes: 21 additions & 1 deletion Apps/RuuviStation/Sources/Classes/Routers/OnboardRouter.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import RuuviLocal
import RuuviOnboard
import RuuviUser
import UIKit
Expand All @@ -13,6 +14,10 @@ protocol OnboardRouterDelegate: AnyObject {
_ router: OnboardRouter,
output: SignInBenefitsModuleOutput
)
func ruuviOnboardDidProvideAnalyticsConsent(
_ router: OnboardRouter,
consentGiven: Bool
)
}

final class OnboardRouter {
Expand All @@ -28,7 +33,12 @@ final class OnboardRouter {
return onboard
} else {
let ruuviUser = r.resolve(RuuviUser.self)!
let onboard = RuuviOnboardPages(ruuviUser: ruuviUser)
let settings = r.resolve(RuuviLocalSettings.self)!
let onboard = RuuviOnboardPages(
ruuviUser: ruuviUser,
tosAccepted: settings.tosAccepted,
analyticsConsentGiven: settings.analyticsConsentGiven
)
onboard.router = self
onboard.output = self
weakOnboard = onboard
Expand All @@ -47,6 +57,16 @@ extension OnboardRouter: RuuviOnboardOutput {
func ruuviOnboardDidShowSignIn(_: RuuviOnboard) {
delegate?.onboardRouterDidShowSignIn(self, output: self)
}

func ruuviOnboardDidProvideAnalyticsConsent(
_ ruuviOnboard: RuuviOnboard,
consentGiven: Bool
) {
delegate?.ruuviOnboardDidProvideAnalyticsConsent(
self,
consentGiven: consentGiven
)
}
}

extension OnboardRouter: SignInBenefitsModuleOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ public final class RuuviOnboardPages: RuuviOnboard {
let view = RuuviOnboardViewController()
view.output = self
view.ruuviUser = ruuviUser
view.tosAccepted = tosAccepted
view.analyticsConsentGiven = analyticsConsentGiven
weakView = view
return view
}
}

private let ruuviUser: RuuviUser
private var tosAccepted: Bool
private var analyticsConsentGiven: Bool

public init(ruuviUser: RuuviUser) {
public init(
ruuviUser: RuuviUser,
tosAccepted: Bool,
analyticsConsentGiven: Bool
) {
self.ruuviUser = ruuviUser
self.tosAccepted = tosAccepted
self.analyticsConsentGiven = analyticsConsentGiven
}

private weak var weakView: UIViewController?
Expand All @@ -40,4 +50,15 @@ extension RuuviOnboardPages: RuuviOnboardViewControllerOutput {
) {
output?.ruuviOnboardDidShowSignIn(self)
}

func ruuviOnboardAnalytics(
_ viewController: RuuviOnboardViewController,
didProvideAnalyticsConsent isConsentGiven: Bool,
sender: Any?
) {
output?.ruuviOnboardDidProvideAnalyticsConsent(
self,
consentGiven: isConsentGiven
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import UIKit

protocol RuuviOnboardSignInCellDelegate: NSObjectProtocol {
func didTapContinueButton(sender: RuuviOnboardSignInCell)
func didProvideAnalyticsConsent(
isConsentGiven: Bool,
sender: RuuviOnboardSignInCell
)
}

class RuuviOnboardSignInCell: UICollectionViewCell {
Expand Down Expand Up @@ -41,6 +45,12 @@ class RuuviOnboardSignInCell: UICollectionViewCell {
return provider
}()

private lazy var analyticsCheckbox: RuuviOnboardCheckboxProvider = {
let provider = RuuviOnboardCheckboxProvider()
provider.delegate = self
return provider
}()

private lazy var continueButton: UIButton = {
let button = UIButton(color: RuuviColor.tintColor.color, cornerRadius: 22)
button.setTitle(
Expand Down Expand Up @@ -118,9 +128,30 @@ private extension RuuviOnboardSignInCell {
)
)

let analyticsCheckboxVC = analyticsCheckbox.makeViewController(
title: RuuviLocalization.onboardingStartAnonymousDataCollectionTitle,
titleMarkupString: "",
titleLink: ""
)
analyticsCheckboxVC.view.backgroundColor = .clear
container.addSubview(analyticsCheckboxVC.view)

analyticsCheckboxVC.view.anchor(
top: tosCheckboxVC.view.bottomAnchor,
leading: container.safeLeadingAnchor,
bottom: nil,
trailing: container.safeTrailingAnchor,
padding: .init(
top: 8,
left: 16,
bottom: 0,
right: 16
)
)

container.addSubview(continueButton)
continueButton.anchor(
top: tosCheckboxVC.view.bottomAnchor,
top: analyticsCheckboxVC.view.bottomAnchor,
leading: nil,
bottom: nil,
trailing: nil,
Expand Down Expand Up @@ -173,10 +204,16 @@ private extension RuuviOnboardSignInCell {

// MARK: - Public
extension RuuviOnboardSignInCell {
func configure(with viewModel: OnboardViewModel) {
func configure(
with viewModel: OnboardViewModel,
tosAccepted: Bool,
analyticsConsentGiven: Bool
) {
titleLabel.text = viewModel.title
subtitleLabel.text = viewModel.subtitle
setContinueButtonEnabled(false)
tosCheckbox.setChecked(tosAccepted)
analyticsCheckbox.setChecked(analyticsConsentGiven)
setContinueButtonEnabled(tosAccepted)
}
}

Expand All @@ -186,6 +223,16 @@ extension RuuviOnboardSignInCell: RuuviOnboardCheckboxViewDelegate {
isChecked: Bool,
sender: RuuviOnboardCheckboxProvider
) {
setContinueButtonEnabled(isChecked, animated: true)
if sender == tosCheckbox {
setContinueButtonEnabled(
isChecked,
animated: true
)
} else if sender == analyticsCheckbox {
delegate?.didProvideAnalyticsConsent(
isConsentGiven: isChecked,
sender: self
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class RuuviOnboardCheckboxProvider: NSObject {
)
}

public func setChecked(_ isChecked: Bool) {
stateHolder.isChecked = isChecked
}

// MARK: - Private
private func setupSubscriptions() {
stateHolder.$isChecked
Expand Down Expand Up @@ -92,7 +96,7 @@ struct RuuviOnboardViewCheckboxView: View {
}
}
.toggleStyle(CheckboxToggleStyle())
.padding(.vertical)
.padding(.vertical, 4)
}

struct TitleView: UIViewRepresentable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ protocol RuuviOnboardViewControllerOutput: AnyObject {
_ viewController: RuuviOnboardViewController,
didPresentSignIn sender: Any?
)
func ruuviOnboardAnalytics(
_ viewController: RuuviOnboardViewController,
didProvideAnalyticsConsent isConsentGiven: Bool,
sender: Any?
)
}

enum OnboardPageType: Int {
Expand All @@ -30,14 +35,16 @@ enum OnboardPageType: Int {
struct OnboardViewModel {
var pageType: OnboardPageType
var title: String
var subtitle: String
var subtitle: String?
var sub_subtitle: String?
var image: UIImage?
}

class RuuviOnboardViewController: UIViewController {
var output: RuuviOnboardViewControllerOutput?
var ruuviUser: RuuviUser?
var tosAccepted: Bool = false
var analyticsConsentGiven: Bool = false

init() {
super.init(nibName: nil, bundle: nil)
Expand Down Expand Up @@ -102,6 +109,9 @@ class RuuviOnboardViewController: UIViewController {
didSet {
pageControl.numberOfPages = viewModels.count
collectionView.reloadData()
if tosAccepted && !analyticsConsentGiven {
scrollToLast()
}
}
}

Expand Down Expand Up @@ -141,6 +151,8 @@ extension RuuviOnboardViewController {
}

private func scrollToLast() {
collectionView.layoutIfNeeded()
guard collectionView.numberOfItems(inSection: 0) > 0 else { return }
let lastPageIndex = viewModels.count - 1
currentPage = lastPageIndex
pageControl.currentPage = lastPageIndex
Expand Down Expand Up @@ -257,7 +269,11 @@ extension RuuviOnboardViewController {
for: indexPath
) as? RuuviOnboardSignInCell
cell?.delegate = self
cell?.configure(with: viewModel)
cell?.configure(
with: viewModel,
tosAccepted: tosAccepted,
analyticsConsentGiven: analyticsConsentGiven
)
return cell
}
}
Expand All @@ -271,6 +287,17 @@ extension RuuviOnboardViewController: RuuviOnboardSignInCellDelegate {
output?.ruuviOnboardCloudSignIn(self, didPresentSignIn: nil)
}
}

func didProvideAnalyticsConsent(
isConsentGiven: Bool,
sender: RuuviOnboardSignInCell
) {
output?.ruuviOnboardAnalytics(
self,
didProvideAnalyticsConsent: isConsentGiven,
sender: nil
)
}
}

extension RuuviOnboardViewController {
Expand Down Expand Up @@ -428,9 +455,7 @@ private extension RuuviOnboardViewController {
title: isUserAuthorized() ?
RuuviLocalization.onboardingThatsItAlreadySignedIn :
RuuviLocalization.onboardingThatsIt,
subtitle: isUserAuthorized() ?
RuuviLocalization.onboardingGoToSignInAlreadySignedIn :
RuuviLocalization.onboardingGoToSignIn
subtitle: nil
)

return [
Expand Down
4 changes: 4 additions & 0 deletions Modules/RuuviOnboard/Sources/RuuviOnboard/RuuviOnboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public protocol RuuviOnboard: AnyObject {
public protocol RuuviOnboardOutput: AnyObject {
func ruuviOnboardDidFinish(_ ruuviOnboard: RuuviOnboard)
func ruuviOnboardDidShowSignIn(_ ruuviOnboard: RuuviOnboard)
func ruuviOnboardDidProvideAnalyticsConsent(
_ ruuviOnboard: RuuviOnboard,
consentGiven: Bool
)
}
Loading
Loading