-
Notifications
You must be signed in to change notification settings - Fork 895
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -12,7 +13,7 @@ import UIKit | |
class RecentSearchQRCodeScannerController: UIViewController { | ||
|
||
private let scannerView = ScannerView() | ||
private var didScan: Bool = false | ||
private var didScan = false | ||
private var onDidScan: (_ string: String) -> Void | ||
|
||
public static var hasCameraSupport: Bool { | ||
|
@@ -62,10 +63,20 @@ 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.scannerView.scannedDisplayButton.isHidden = true | ||
self.onDidScan(string) | ||
self.dismiss(animated: true, completion: nil) | ||
}), | ||
for: .touchUpInside | ||
) | ||
} | ||
} | ||
|
||
|
@@ -142,12 +153,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 { | ||
$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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
$0.backgroundColor = UIColor(braveSystemName: .primitivePurple60) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
@@ -164,6 +232,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() | ||
|
There was a problem hiding this comment.
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