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

Refactor Permission Handling for Improved Readability #1164

Merged
merged 9 commits into from
Oct 4, 2023

Conversation

michaelnabil230
Copy link
Contributor

@michaelnabil230 michaelnabil230 commented Sep 23, 2023

This PR introduces a new style for checking permission status.

Before:

Future<bool> askPermission() async {
  PermissionStatus status = await Permission.contacts.request();
  if (status.isDenied) {
    // Your code
  } else if (status.isGranted) {
    // Your code
  } else if (status.isRestricted) {
    // Your code
  }
}

After:

Future<bool> askPermission() async {
  await Permission.contacts
    .onDeniedCallback(() {
      // Your code
    })
    .onGrantedCallback(() {
      // Your code
    })
    .onRestrictedCallback(() {
      // Your code
    })
    .request();
}

In this updated code, the permission status checks have been replaced with a more concise style that leverages the PermissionCallbacks extension. This change simplifies the code and makes it more readable.

Pre-launch Checklist

  • I made sure the project builds.
  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is does not need version changes.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I rebased onto main.
  • I added new tests to check the change I am making, or this PR does not need tests.
  • I made sure all existing and new tests are passing.
  • I ran dart format . and committed any changes.
  • I ran flutter analyze and fixed any errors.

@michaelnabil230 michaelnabil230 changed the title Add PermissionCallbacks Refactor Permission Handling for Improved Readability Sep 23, 2023
@codecov
Copy link

codecov bot commented Sep 23, 2023

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Files Coverage Δ
permission_handler/lib/permission_handler.dart 71.42% <47.36%> (-28.58%) ⬇️

📢 Thoughts on this report? Let us know!.

Copy link
Member

@mvanbeusekom mvanbeusekom left a comment

Choose a reason for hiding this comment

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

Hi @michaelnabil230,

This is a really interesting addition that we would love to support. I have two requests that I hope you could help update:

  1. The different callbacks currently accept a function of type VoidCallback, I can imagine that some developers would like to run asynchronous tasks in side the callback (e.g. fetching a location once location permission is granted). I think it would be beneficial to update these callbacks to support a Future<void> (or possible a FutureOr<void>) method.
  2. With the current changes there are two methods to start the permission request, request and ask. I think this might lead to confusion as it will be possible to register callbacks and then use request which doesn't use the registered callbacks. Since this is not a breaking change, why not add the callbacks to the current PermissionAction extension and call them from the request method?

permission_handler/CHANGELOG.md Outdated Show resolved Hide resolved
permission_handler/lib/permission_handler.dart Outdated Show resolved Hide resolved
@michaelnabil230
Copy link
Contributor Author

Hi @mvanbeusekom

Thank you for your instruction.
Now, you have the opportunity to merge the PR so that everyone can reap the benefits of this feature.

Copy link
Member

@mvanbeusekom mvanbeusekom left a comment

Choose a reason for hiding this comment

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

Hi @michaelnabil230,

I have noticed one more issue that I feel should be addressed.

permission_handler/lib/permission_handler.dart Outdated Show resolved Hide resolved
@michaelnabil230
Copy link
Contributor Author

Hi @mvanbeusekom

Thanks for the update! It's awesome to hear that the code is ready to merge. Let's proceed with the merging process.

Copy link
Member

@mvanbeusekom mvanbeusekom left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for this contribution.

P.S. maybe you could create another PR updating the documentation and explain this alternative route, so people are informed about this new feature.

@mvanbeusekom mvanbeusekom merged commit a3bacb4 into Baseflow:main Oct 4, 2023
1 of 3 checks passed
@michaelnabil230 michaelnabil230 deleted the patch-1 branch October 4, 2023 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants