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

[iOS] - Improve QRCode Scanning Visibility #27229

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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 @@ -4,6 +4,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import AVFoundation
import BraveCore
import BraveShared
import Foundation
import Shared
Expand All @@ -12,9 +13,11 @@ import UIKit
class RecentSearchQRCodeScannerController: UIViewController {

private let scannerView = ScannerView()
private var didScan: Bool = false
private var didScan = false
private var onDidScan: (_ string: String) -> Void

private var didProcessScanReusltsTask: Task<Void, Error>?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this used anywhere or any Task's created, I assume can be deleted?


public static var hasCameraSupport: Bool {
if ProcessInfo.processInfo.isiOSAppOnVisionOS {
// Apps on VisionOS can't access the main camera
Expand Down Expand Up @@ -62,16 +65,28 @@ class RecentSearchQRCodeScannerController: UIViewController {
// Feedback indicating code scan is finalized
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
UIImpactFeedbackGenerator(style: .medium).vibrate()

self.didScan = true
self.onDidScan(string)
self.dismiss(animated: true, completion: nil)

self.scannerView.scannedText = string
self.scannerView.scannedDisplayButton.isHidden = false
self.scannerView.scannedDisplayButton.addAction(
UIAction(handler: { [weak self] _ in
guard let self = self else { return }

self.didProcessScanReusltsTask?.cancel()
self.scannerView.scannedDisplayButton.isHidden = true
self.onDidScan(string)
self.dismiss(animated: true, completion: nil)
}),
for: .touchUpInside
)
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

didProcessScanReusltsTask?.cancel()
scannerView.cameraView.stopRunning()
}

Expand Down Expand Up @@ -142,12 +157,69 @@ extension RecentSearchQRCodeScannerController {
$0.textColor = .braveLabel
}

var scannedText: String? {
didSet {
let processedScannedResult = processScannedText()
let truncationMode = processedScannedResult.truncationMode

if let scannedText = processedScannedResult.string {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = truncationMode
paragraphStyle.baseWritingDirection = .leftToRight

let title = NSAttributedString(
string: scannedText,
attributes: [
.font: UIFont.preferredFont(forTextStyle: .body),
.paragraphStyle: paragraphStyle,
]
)

scannedDisplayButton.titleLabel?.lineBreakMode = truncationMode
scannedDisplayButton.setAttributedTitle(title, for: .normal)
} else {
scannedDisplayButton.setTitle(nil, for: .normal)
}
}
}

private(set) lazy var scannedDisplayButton = UIButton().then {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont see any reason for this to be lazy

$0.contentEdgeInsets = UIEdgeInsets(top: 12, left: 16, bottom: 12, right: 16)
$0.titleLabel?.font = UIFont.preferredFont(forTextStyle: .body)
$0.titleLabel?.lineBreakMode = .byTruncatingTail
$0.setTitleColor(.white, for: .normal)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be schemesOnPrimary

$0.backgroundColor = UIColor(braveSystemName: .primitivePurple60)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be buttonBackground

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this button isnt themed for some reason you can ignore this and the comment below

$0.layer.cornerRadius = 10
$0.layer.cornerCurve = .continuous
$0.layer.masksToBounds = true
$0.isHidden = true
}

private func processScannedText() -> (string: String?, truncationMode: NSLineBreakMode) {
if let text = scannedText, let url = URIFixup.getURL(text) {
let scannedText = URLFormatter.formatURL(
URLOrigin(url: url).url?.absoluteString ?? url.absoluteString,
formatTypes: [
.omitDefaults, .trimAfterHost, .omitHTTPS, .omitTrivialSubdomains,
],
unescapeOptions: .normal
)

let truncationMode: NSLineBreakMode =
["http", "https"].contains(url.scheme ?? "") ? .byTruncatingHead : .byTruncatingTail
return (scannedText, truncationMode)
}

return (scannedText, .byTruncatingTail)
}

override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = .secondaryBraveBackground

addSubview(cameraView)
addSubview(scannedDisplayButton)
addSubview(scrollView)
scrollView.addSubview(stackView)
stackView.addStackViewItems(
Expand All @@ -164,6 +236,13 @@ extension RecentSearchQRCodeScannerController {
$0.width.lessThanOrEqualTo(375)
}

scannedDisplayButton.snp.makeConstraints {
$0.centerX.equalTo(cameraView)
$0.bottom.equalTo(cameraView).inset(10)
$0.leading.greaterThanOrEqualTo(cameraView).inset(40)
$0.trailing.lessThanOrEqualTo(cameraView).inset(40)
}

scrollView.snp.makeConstraints {
$0.top.equalTo(cameraView.snp.bottom).offset(10)
$0.leading.trailing.bottom.equalToSuperview()
Expand Down
Loading