This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No Bug: Add Brave Today debug menu/environment switcher (#3182)
- Loading branch information
1 parent
827b27b
commit f596798
Showing
6 changed files
with
208 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
145 changes: 145 additions & 0 deletions
145
Client/Frontend/Settings/BraveTodayDebugSettingsController.swift
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,145 @@ | ||
// Copyright 2020 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import UIKit | ||
import Static | ||
|
||
extension FeedDataSource.Environment { | ||
fileprivate var name: String { | ||
switch self { | ||
case .dev: return "Dev" | ||
case .staging: return "Staging" | ||
case .production: return "Production" | ||
} | ||
} | ||
} | ||
|
||
extension FeedDataSource { | ||
fileprivate func description(of state: State) -> String { | ||
switch state { | ||
case .initial: | ||
return "—" | ||
case .loading: | ||
return "Loading" | ||
case .success: | ||
return "Success" | ||
case .failure(let error): | ||
return "Error: \(error.localizedDescription)" | ||
} | ||
} | ||
fileprivate func detailRows(for state: State) -> [Row] { | ||
switch state { | ||
case .initial: | ||
return [] | ||
case .loading(let previousState): | ||
return [Row(text: "Previous State", detailText: description(of: previousState))] | ||
case .success(let cards): | ||
return [ | ||
Row(text: "Sources", detailText: "\(sources.count)"), | ||
Row(text: "Cards Generated", detailText: "\(cards.count)") | ||
] | ||
case .failure(let error): | ||
return [ | ||
Row(text: "Error Details", detailText: error.localizedDescription, cellClass: MultilineSubtitleCell.self) | ||
] | ||
} | ||
} | ||
} | ||
|
||
class BraveTodayDebugSettingsController: TableViewController { | ||
private let feedDataSource: FeedDataSource | ||
|
||
init(dataSource: FeedDataSource) { | ||
feedDataSource = dataSource | ||
if #available(iOS 13.0, *) { | ||
super.init(style: .insetGrouped) | ||
} else { | ||
super.init(style: .grouped) | ||
} | ||
} | ||
|
||
@available(*, unavailable) | ||
required init(coder: NSCoder) { | ||
fatalError() | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
title = "Brave Today QA Settings" | ||
|
||
reloadData() | ||
|
||
if navigationController?.viewControllers.first === self { | ||
navigationItem.rightBarButtonItem = .init(barButtonSystemItem: .done, target: self, action: #selector(tappedDone)) | ||
} | ||
} | ||
|
||
func reloadData() { | ||
dataSource.sections = [ | ||
.init( | ||
rows: [ | ||
Row(text: "Environment", detailText: feedDataSource.environment.name, selection: { [unowned self] in | ||
let picker = TodayEnvironmentPicker(selectedEnvironment: feedDataSource.environment) { [unowned self] newEnvironment in | ||
feedDataSource.environment = newEnvironment | ||
self.reloadData() | ||
self.navigationController?.popViewController(animated: true) | ||
} | ||
navigationController?.pushViewController(picker, animated: true) | ||
}, accessory: .disclosureIndicator) | ||
], | ||
footer: .title("Changing the environment will purge all cached resources immediately.") | ||
), | ||
.init( | ||
rows: [ | ||
Row(text: "State", detailText: feedDataSource.description(of: feedDataSource.state)), | ||
] + feedDataSource.detailRows(for: feedDataSource.state) | ||
) | ||
] | ||
} | ||
|
||
@objc private func tappedDone() { | ||
dismiss(animated: true) | ||
} | ||
} | ||
|
||
private class TodayEnvironmentPicker: TableViewController { | ||
let selectedEnvironment: FeedDataSource.Environment | ||
let environmentUpdated: (FeedDataSource.Environment) -> Void | ||
init(selectedEnvironment: FeedDataSource.Environment, | ||
environmentUpdated: @escaping (FeedDataSource.Environment) -> Void) { | ||
self.selectedEnvironment = selectedEnvironment | ||
self.environmentUpdated = environmentUpdated | ||
if #available(iOS 13.0, *) { | ||
super.init(style: .insetGrouped) | ||
} else { | ||
super.init(style: .grouped) | ||
} | ||
} | ||
|
||
@available(*, unavailable) | ||
required init(coder: NSCoder) { | ||
fatalError() | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
title = "Environment" | ||
navigationItem.leftBarButtonItem = .init(barButtonSystemItem: .cancel, target: self, action: #selector(cancelPicker)) | ||
|
||
dataSource.sections = [ | ||
.init(rows: FeedDataSource.Environment.allCases.map { environment in | ||
Row(text: environment.name, selection: { [unowned self] in | ||
self.environmentUpdated(environment) | ||
}, accessory: environment == selectedEnvironment ? .checkmark : .none) | ||
}) | ||
] | ||
} | ||
|
||
@objc private func cancelPicker() { | ||
navigationController?.popViewController(animated: true) | ||
} | ||
} |
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