Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #8371: Update Rewards icon properly when first enabling it #8512

Merged
merged 1 commit into from
Dec 4, 2023
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 @@ -95,14 +95,18 @@ class BraveRewardsViewController: UIViewController, PopoverContentComponent {
rewardsView.publisherView.hostLabel.text = tab.url?.baseDomain
}
}

private let rewardsServiceStartGroup = DispatchGroup()

override func viewDidLoad() {
super.viewDidLoad()

rewardsView.rewardsToggle.isOn = rewards.isEnabled

rewardsServiceStartGroup.enter()
rewards.startRewardsService { [weak self] in
guard let self = self else { return }
defer { self.rewardsServiceStartGroup.leave() }
if let rewardsAPI = self.rewards.rewardsAPI {
let observer = RewardsObserver(rewardsAPI: rewardsAPI)
rewardsAPI.add(observer)
Expand Down Expand Up @@ -139,22 +143,24 @@ class BraveRewardsViewController: UIViewController, PopoverContentComponent {

private var isCreatingWallet: Bool = false
@objc private func rewardsToggleValueChanged() {
rewardsView.rewardsToggle.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.rewardsView.rewardsToggle.isUserInteractionEnabled = true
}
let isOn = rewardsView.rewardsToggle.isOn
rewards.isEnabled = isOn
rewardsView.subtitleLabel.text = isOn ? Strings.Rewards.enabledBody : Strings.Rewards.disabledBody
if rewardsView.rewardsToggle.isOn {
rewardsView.statusView.setVisibleStatus(status: supportedListCount > 0 ? .rewardsOn : .rewardsOnNoCount)
} else {
rewardsView.statusView.setVisibleStatus(status: .rewardsOff)
}
if publisher != nil {
UIView.animate(withDuration: 0.15) {
self.rewardsView.publisherView.isHidden = !self.rewardsView.rewardsToggle.isOn
self.rewardsView.publisherView.alpha = self.rewardsView.rewardsToggle.isOn ? 1.0 : 0.0
rewardsServiceStartGroup.notify(queue: .main) { [self] in
rewardsView.rewardsToggle.isUserInteractionEnabled = false
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.rewardsView.rewardsToggle.isUserInteractionEnabled = true
}
let isOn = rewardsView.rewardsToggle.isOn
rewards.isEnabled = isOn
rewardsView.subtitleLabel.text = isOn ? Strings.Rewards.enabledBody : Strings.Rewards.disabledBody
if rewardsView.rewardsToggle.isOn {
rewardsView.statusView.setVisibleStatus(status: supportedListCount > 0 ? .rewardsOn : .rewardsOnNoCount)
} else {
rewardsView.statusView.setVisibleStatus(status: .rewardsOff)
}
if publisher != nil {
UIView.animate(withDuration: 0.15) {
self.rewardsView.publisherView.isHidden = !self.rewardsView.rewardsToggle.isOn
self.rewardsView.publisherView.alpha = self.rewardsView.rewardsToggle.isOn ? 1.0 : 0.0
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public class BrowserViewController: UIViewController {
})
}

rewardsEnabledObserveration = rewards.observe(\.isEnabled, options: [.new]) { [weak self] _, _ in
rewardsEnabledObserveration = rewards.ads.observe(\.isEnabled, options: [.new]) { [weak self] _, _ in
guard let self = self else { return }
self.updateRewardsButtonState()
self.setupAdsNotificationHandler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension BrowserViewController {
return
}
self.topToolbar.rewardsButton.isHidden = Preferences.Rewards.hideRewardsIcon.value || privateBrowsingManager.isPrivateBrowsing
self.topToolbar.rewardsButton.iconState = Preferences.Rewards.rewardsToggledOnce.value ? (rewards.isEnabled || rewards.isCreatingWallet ? .enabled : .disabled) : .initial
self.topToolbar.rewardsButton.iconState = rewards.isEnabled || rewards.isTurningOnRewards ? .enabled : (Preferences.Rewards.rewardsToggledOnce.value ? .disabled : .initial)
}

func showBraveRewardsPanel() {
Expand All @@ -44,7 +44,7 @@ extension BrowserViewController {

Preferences.FullScreenCallout.rewardsCalloutCompleted.value = true
present(controller, animated: true)
topToolbar.rewardsButton.iconState = Preferences.Rewards.rewardsToggledOnce.value ? (rewards.isEnabled || rewards.isCreatingWallet ? .enabled : .disabled) : .initial
topToolbar.rewardsButton.iconState = Preferences.Rewards.rewardsToggledOnce.value ? (rewards.isEnabled || rewards.isTurningOnRewards ? .enabled : .disabled) : .initial
return
}

Expand Down
17 changes: 9 additions & 8 deletions Sources/Brave/Frontend/Rewards/BraveRewards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ public class BraveRewards: NSObject {
if let toggleAds {
self.ads.isEnabled = toggleAds
}
self.isTurningOnRewards = false
return
}
self.ads.initialize(walletInfo: walletInfo) { success in
if success, let toggleAds {
self.ads.isEnabled = toggleAds
}
self.isTurningOnRewards = false
}
}
}
Expand Down Expand Up @@ -118,10 +120,9 @@ public class BraveRewards: NSObject {
ads.isEnabled
}
set {
willChangeValue(for: \.isEnabled)
Preferences.Rewards.rewardsToggledOnce.value = true
createWalletIfNeeded { [weak self] in
guard let self = self else { return }
Preferences.Rewards.rewardsToggledOnce.value = true
self.rewardsAPI?.setAutoContributeEnabled(newValue)
let wasEnabled = self.ads.isEnabled
if !wasEnabled && newValue {
Expand All @@ -132,25 +133,25 @@ public class BraveRewards: NSObject {
if !newValue {
self.ads.isEnabled = newValue
self.proposeAdsShutdown()
self.isTurningOnRewards = false
} else {
self.fetchWalletAndInitializeAds(toggleAds: true)
}
self.didChangeValue(for: \.isEnabled)
}
}
}

private(set) var isCreatingWallet: Bool = false
private(set) var isTurningOnRewards: Bool = false

private func createWalletIfNeeded(_ completion: (() -> Void)? = nil) {
if isCreatingWallet {
if isTurningOnRewards {
// completion block will be hit by previous call
return
}
isCreatingWallet = true
isTurningOnRewards = true
startRewardsService {
guard let rewardsAPI = self.rewardsAPI else { return }
rewardsAPI.createWalletAndFetchDetails { [weak self] success in
self?.isCreatingWallet = false
rewardsAPI.createWalletAndFetchDetails { success in
completion?()
}
}
Expand Down