-
Notifications
You must be signed in to change notification settings - Fork 445
Fix #627 Add brave://search URL scheme #3582
Conversation
e2dce19
to
2cf8ab0
Compare
bdc9f1f
to
219c74b
Compare
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.
Thanks for your PR and sorry for late reply, we had an important release to handle before jumping to contributor's code
I left some review, please check it out when you have time
if let urlString = urlItem?.absoluteString { | ||
if let braveUrl = urlString.addingPercentEncoding(withAllowedCharacters: .alphanumerics).flatMap(self.urlScheme) { | ||
self.handleUrl(braveUrl) | ||
} | ||
} else if let text = nonUrlText { | ||
if let braveUrl = text.addingPercentEncoding(withAllowedCharacters: .alphanumerics).flatMap(self.searchScheme) { | ||
self.handleUrl(braveUrl) |
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.
This part is too duplicated, what do you think about adding new var urlOrText: String?
then assign to it either url absolute string or the nonUrlText
, and call rest of the code.
This way you won't need to call addingPercentEncoding
two times
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.
Good idea, I'll solve it this way
@@ -386,7 +386,7 @@ class TopToolbarView: UIView, ToolbarProtocol { | |||
if search { | |||
locationTextField?.text = text | |||
// Not notifying when empty agrees with AutocompleteTextField.textDidChange. | |||
delegate?.topToolbar(self, didEnterText: text) | |||
delegate?.topToolbar(self, didSubmitText: text) |
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.
What is this change needed for?
Currently it seems to break other part of the app when you are on google.com for example, and tap on url bar, it reloads the app instead of focusing on url bar
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.
I noticed that without this change, view is stuck with empty url bar and doesn't submit searching. Thanks for your comment, I haven't realized it breaks other part of the app, will try to solve that in different way.
self.handleUrl(braveUrl) | ||
} | ||
if let braveUrl = urlItem?.absoluteString.addingPercentEncoding(withAllowedCharacters: .alphanumerics).flatMap(self.urlScheme) ?? nonUrlText?.addingPercentEncoding(withAllowedCharacters: .alphanumerics).flatMap(self.searchScheme) { | ||
self.handleUrl(braveUrl) | ||
} |
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.
I didn't remove one of the .addingPercentEncoding parts since later there is a .flatMap part which is different in both cases, but I managed to make it little simpler
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.
Functionality wise code works for me now, good job
we can make more improvements to our code architecture in ShareToBraveViewController
class
@@ -35,21 +39,24 @@ class ShareToBraveViewController: SLComposeServiceViewController { | |||
|
|||
provider.loadItem(of: provider.isUrl ? kUTTypeURL : kUTTypeText) { item, error in | |||
var urlItem: URL? | |||
var nonUrlText: String? |
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.
This work, but I think we can make the code cleaner now, if you look at logic below that, there is a lot of nested ifs and the code is not super readable,
For us differences between text and url are small, we can remove code duplication a little and improve architecture
What do you think if we create a new structure to ShareToBraveViewController
class holding either URL or text and handling the scheme properly,
Sample code
private struct Scheme {
private enum SchemeType {
case url, query
}
private let type: SchemeType
private let urlOrQuery: String
init?(item: NSSecureCoding) {
if let text = item as? String {
urlOrQuery = text
type = .query
} else if let url = (item as? URL)?.absoluteString.firstURL?.absoluteString {
urlOrQuery = url
type = .url
} else {
return nil
}
}
var schemeUrl: URL? {
guard let percentEncodedUrlOrQuery = urlOrQuery
.addingPercentEncoding(withAllowedCharacters: .alphanumerics) else {
return nil
}
// Can improve even further with adding param using `URLComponents`
switch type {
case .url:
return URL(string: "brave://open-url?url=\(percentEncodedUrlOrQuery)")
case .query:
return URL(string: "brave://search?q=\(percentEncodedUrlOrQuery)")
}
}
}
Then calling it from provider.loadItem
is much cleaner, you can simply do:
guard let item = item, let schemeUrl = Scheme(item: item)?.schemeUrl else {
self.cancel()
return
}
self.handleUrl(schemeUrl)
what do you think about it?
return | ||
} | ||
|
||
locationTextField?.text = text |
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.
You can move it to first line in this function,
locationTextField?.text = text
is set here and in early return, we can simplify it and call once at the beginning of this function
@@ -42,6 +42,14 @@ class NavigationRouterTests: XCTestCase { | |||
XCTAssertEqual(badNav, NavigationPath.url(webURL: URL(string: "blah"), isPrivate: false)) | |||
} | |||
|
|||
func testSearchScheme() { |
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.
Thank you for adding unit test for it
|
||
components.queryItems = [queryItem] | ||
return components.url | ||
} |
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.
Tried to added URLComponents logic you've mentioned, what do you think?
I also deleted ".addingPercentEncoding" part since encoding is solved by URLComponents type.
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.
that's even better, nice find
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.
Looks great, I will run this code for a bit and if all is good we will merge it
Thanks for this PR, merging it, should be live in version 1.28 early July |
Summary of Changes
This pull request fixes part of #627.
It fix handling nonUrl text when sharing to Brave. Before, app opened blank tab. With this fix, app search for the text using default search engine in the browser.
Submitter Checklist:
NSLocalizableString()
Test Plan:
Reviewer Checklist:
QA/(Yes|No)
release-notes/(include|exclude)
bug
/enhancement