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

[url_launcher] migrating objc plugin to swift #4753

Merged
merged 38 commits into from
Oct 26, 2023

Conversation

chrisdlangham
Copy link
Contributor

@chrisdlangham chrisdlangham commented Aug 22, 2023

This PR converts the iOS portion of the url_launcher plugin from objc to swift.

Fixes flutter/flutter#119102

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [relevant style guides] and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the [CLA].
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the [pub versioning philosophy], or this PR is [exempt from version changes].
  • I updated CHANGELOG.md to add a description of the change, [following repository CHANGELOG style].
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Could you split out a new PR that contains just the migration of the tests to Swift, so we can land that first in a separate PR? Rewriting tests and implementation in the same PR is much, much riskier than doing each one at a time.

@hellohuanlin
Copy link
Contributor

@chrisdlangham do we plan to rebase and update this PR?

@chrisdlangham
Copy link
Contributor Author

@hellohuanlin Yes this is ready now

* @param url The invalid URL string
* @return The error to return
*/
func invalidURLError(for url: String) -> LaunchResultDetails {
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be private?

private var currentSession: URLLaunchSession?
private let launcher: Launcher

var topViewController: UIViewController? {
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be private?

Copy link
Contributor

Choose a reason for hiding this comment

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

hmmm, what's the diff between this func, and the one in UIViewController extension below?

@chrisdlangham
Copy link
Contributor Author

I think I have addressed everything so far. Thanks for the feedback.
@stuartmorgan @hellohuanlin

XCTAssertEqual(error?.code, "argument_error")
XCTAssertEqual(error?.message, "Unable to parse URL")
XCTAssertEqual(error?.details as? String, "Provided URL: urls can't have spaces")
XCTAssertTrue(details == .failedToLoad || details == .invalidUrl)
Copy link
Contributor

Choose a reason for hiding this comment

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

which case will it actually be?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It depends on the iOS version the tests are being run on. I think @stuartmorgan can explain it better, but on iOS 17 we should get .failedToLoad, and on older versions, we should get .invalidUrl

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you do "if iOS 17 else".

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately Swift apparently has no equivalent to Obj-C SDK preprocessor checks, and that's the determining factor. We don't want to switch on whether it's running on iOS 17, we want to switch on whether it was linked against the iOS 17 SDK, and that's apparently not expressible in Swift. (The best I can find is people suggesting mapping from the SDK to the version of Xcode that introduced that SDK, and then from there to the version of Swift introduced by that version of Xcode, but that's extremely gross since this has nothing to do with Swift language version.)

Copy link
Contributor

Choose a reason for hiding this comment

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

I realized there's a better option; I've added a utility method in the test file that checks the NSURL behavior directly, and then switches based on that in the tests.

}

/// Default implementation of Launcher, using UIApplication.
final class UIApplicationLauncher: Launcher {
Copy link
Contributor

Choose a reason for hiding this comment

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

can this simply be an empty extension?

extension UIApplication: Launcher {}

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Done.

}

func launchUrl(
url: String, universalLinksOnly: Bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

line break

}

func openUrlInSafariViewController(
url: String, completion: @escaping (Result<LaunchResult, Error>) -> Void
Copy link
Contributor

Choose a reason for hiding this comment

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

line break. please go over all signatures and make sure comma separated items are in separate lines.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've swift-formated the whole plugin.

topViewController?.present(session.safariViewController, animated: true, completion: nil)
}

func closeSafariViewController() throws {
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this throwing?

/// The URL could not be launched
failedToLoad,

/// The URL was not launched because it is not invalid URL
Copy link
Contributor

Choose a reason for hiding this comment

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

"because the URL is invalid."

/// The URL was successfully launched.
success,

/// The URL could not be launched
Copy link
Contributor

Choose a reason for hiding this comment

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

This is missing a period.

);
expect(
await launcher.launch(
'invalid://u r l',
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't use the URL that's used for invalid URL tests for a case where the return value isn't invalidUrl. (You could just use the same URL in every test now, but if you're going to keep the URLs that used to be special, please be consistent with them.)

required String url,
}) {
// Replace this in https://github.com/flutter/flutter/issues/127665
// This is temporary since FlutterError is not a NSError.
Copy link
Contributor

Choose a reason for hiding this comment

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

This line can be removed; it's not temporary due to FlutterError. What's temporary is throwing a PlatformException at all.

}) {
// Replace this in https://github.com/flutter/flutter/issues/127665
// This is temporary since FlutterError is not a NSError.
// The PlatformExceptions thrown here are for compatibility with the
Copy link
Contributor

Choose a reason for hiding this comment

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

"is for"

// Replace this in https://github.com/flutter/flutter/issues/127665
// This is temporary since FlutterError is not a NSError.
// The PlatformExceptions thrown here are for compatibility with the
// previous Objective-C implementation.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove "Objective-C" since that's not true of the previous implementation.

required final LaunchResult results,
required String url,
}) {
// Replace this in https://github.com/flutter/flutter/issues/127665
Copy link
Contributor

Choose a reason for hiding this comment

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

// TODO(stuartmorgan): Remove this as part of standardizing error handling. See
// flutter/flutter#127665

required final LaunchResult results,
required String url,
}) {
// Replace this in https://github.com/flutter/flutter/issues/127665
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this entire comment block to be on the throw PlatformException rather than here, since that's the only part it applies to in this version.

@chrisdlangham
Copy link
Contributor Author

I've noticed quite a few merge conflicts. I've been contemplating whether to continue this, and this has helped me make a decision. Unfortunately, I'm now facing a shortage of both time and energy for this. Please feel free to either close this or relocate it as needed.

@stuartmorgan
Copy link
Contributor

@chrisdlangham Thanks for letting us know. This is basically done, and I definitely don't want to lose all the great work you've done on this, so I'll resolve the conflicts with my recent PR, and do any last adjustments to get it landed. We really appreciate the contribution!

@stuartmorgan stuartmorgan dismissed their stale review October 26, 2023 16:06

Taking over PR; dismissing previous review

@stuartmorgan
Copy link
Contributor

I think I've addressed the previous comments, so this should be ready for another review @hellohuanlin

Copy link
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

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

LGTM!

@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 26, 2023
@auto-submit auto-submit bot merged commit 3aff029 into flutter:main Oct 26, 2023
79 checks passed
@chrisdlangham chrisdlangham deleted the coverting-objc-to-swift branch October 26, 2023 21:20
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 27, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Oct 27, 2023
flutter/packages@fea24c5...2af6954

2023-10-26 katelovett@google.com [two_dimensional_scrollables] Add TableSpanPadding (flutter/packages#5039)
2023-10-26 engine-flutter-autoroll@skia.org Roll Flutter (stable) from 6c4930c to d211f42 (1 revision) (flutter/packages#5238)
2023-10-26 chrislangham@rightnow.org [url_launcher] migrating objc plugin to swift (flutter/packages#4753)
2023-10-26 ybz975218925@live.com [go_router_builder]Avoid losing NullabilitySuffix for typeArguments (flutter/packages#5215)
2023-10-26 engine-flutter-autoroll@skia.org Roll Flutter from 5dd2a4e to c555599 (14 revisions) (flutter/packages#5237)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
HugoOlthof pushed a commit to moneybird/packages that referenced this pull request Dec 13, 2023
This PR converts the iOS portion of the url_launcher plugin from objc to swift. 

*List which issues are fixed by this PR. You must list at least one issue.*
flutter/flutter#119102
@reidbaker reidbaker mentioned this pull request Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: url_launcher platform-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[url_launcher] iOS Swift migration
3 participants