Skip to content

fix: open nfc settings when switched off #19

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

Merged
merged 4 commits into from
Apr 16, 2025

Conversation

Dhruv1797
Copy link
Contributor

@Dhruv1797 Dhruv1797 commented Apr 11, 2025

What’s implemented:

NFC availability check using FlutterNfcKit.

Toast messages for:

    NFC disabled → prompts user.

    Device doesn’t support NFC.

    Prompt to bring phone near Magic ePaper hardware.

Redirect to settings:

    Android: Uses MethodChannel (com.yourapp.nfc/settings) to launch Settings.ACTION_NFC_SETTINGS via native code in MainActivity.kt.

    iOS: Uses AppSettings.openAppSettings() to open app settings (NFC toggle isn’t directly accessible on iOS).

Files Modified:

main.dart: Core NFC logic + platform checks.

nfc_settings_launcher.dart: New utility for method channel.

MainActivity.kt: Native handler to open NFC settings.

Summary by Sourcery

Improve NFC handling by adding comprehensive NFC availability checks, user notifications, and platform-specific settings navigation

New Features:

  • Implement NFC availability checks using FlutterNfcKit with detailed user feedback

Bug Fixes:

  • Provide user-friendly guidance when NFC is not available or disabled

Enhancements:

  • Add toast messages for different NFC states (disabled, unsupported)
  • Create cross-platform mechanism to open NFC settings

Copy link

sourcery-ai bot commented Apr 11, 2025

Reviewer's Guide by Sourcery

This pull request implements NFC availability checks and provides user guidance, including prompting the user to enable NFC and redirecting them to the appropriate settings on both Android and iOS platforms. It uses FlutterNfcKit for NFC checks and platform-specific methods for opening settings.

Sequence diagram for handling NFC availability

sequenceDiagram
    participant User
    participant Flutter App
    participant FlutterNfcKit
    participant Platform (Android/iOS)

    User->>Flutter App: Triggers NFC action (e.g., writeImages)
    Flutter App->>FlutterNfcKit: Check NFC availability
    FlutterNfcKit->>Platform: Get NFC status
    Platform-->>FlutterNfcKit: Returns NFC status (available, disabled, not_supported)
    FlutterNfcKit-->>Flutter App: Returns NFC availability

    alt NFC is disabled
        Flutter App->>Flutter App: Show toast message "NFC is disabled. Please enable it."
        alt Platform is Android
            Flutter App->>Platform: Open NFC settings
        else Platform is iOS
            Flutter App->>Platform: Open App settings
        end
    else NFC is not supported
        Flutter App->>Flutter App: Show toast message "This device does not support NFC."
    else NFC is available
        Flutter App->>Flutter App: Show toast message "Bring your phone near to the Magic Epaper Hardware"
        Flutter App->>FlutterNfcKit: poll(timeout)
        FlutterNfcKit->>Platform: Poll for NFC tag
        Platform-->>FlutterNfcKit: Returns NFC tag
        FlutterNfcKit-->>Flutter App: Returns NFC tag
    end
Loading

Updated class diagram for NFCSettingsLauncher

classDiagram
    class NFCSettingsLauncher {
        +static const platform
        +static Future<void> openNFCSettings()
    }
    note for NFCSettingsLauncher "Utility class to open NFC settings on Android and iOS"
Loading

File-Level Changes

Change Details Files
Implemented NFC availability check and user prompting using FlutterNfcKit.
  • Added NFC availability check using FlutterNfcKit.
  • Displayed toast messages for NFC disabled, device doesn't support NFC, and prompt to bring phone near hardware.
  • Handled different NFCAvailability states.
  • Added a dependency on app_settings and fluttertoast.
lib/util/protocol.dart
pubspec.yaml
pubspec.lock
Implemented redirection to NFC settings on Android and iOS.
  • On Android, used MethodChannel to launch Settings.ACTION_NFC_SETTINGS via native code.
  • On iOS, used AppSettings.openAppSettings() to open app settings.
  • Created a new utility class NFCSettingsLauncher for method channel calls.
  • Added native handler in MainActivity.kt to open NFC settings.
lib/util/protocol.dart
android/app/src/main/kotlin/org/fossasia/magic_epaper_app/MainActivity.kt
lib/util/nfc_settings_launcher.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Dhruv1797 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding error handling around the AppSettings.openAppSettings() call for iOS, as it can fail if the user has restricted app settings access.
  • The CHANNEL constant in MainActivity.kt should be defined in a more central place (e.g. a shared constants file) to avoid potential mismatches.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +17 to +24
if (call.method == "openNFCSettings") {
val intent = Intent(Settings.ACTION_NFC_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
result.success(null)
} else {
result.notImplemented()
}
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Consider error handling for the platform intent.

In case startActivity fails (for example, if NFC settings are not available), adding try/catch around the intent launch could help avoid unexpected crashes.

Suggested change
if (call.method == "openNFCSettings") {
val intent = Intent(Settings.ACTION_NFC_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
result.success(null)
} else {
result.notImplemented()
}
if (call.method == "openNFCSettings") {
try {
val intent = Intent(Settings.ACTION_NFC_SETTINGS)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
result.success(null)
} catch (e: Exception) {
result.error("UNAVAILABLE", "NFC Settings not available", null)
}
} else {
result.notImplemented()
}

@Dhruv1797
Copy link
Contributor Author

@kienvo please review it once.

@Dhruv1797
Copy link
Contributor Author

nfc-handing-demo.mp4

Here is the demo video for nfc-handling for opening nfc settings-when nfc is disabled or off.
@kienvo please review it once.

@kienvo
Copy link
Member

kienvo commented Apr 16, 2025

The common build failed. Please resolve it.

@Dhruv1797
Copy link
Contributor Author

Hi @kienvo,
I’ve removed the pubspec.lock file (which is auto-generated and might have been causing the common build to fail). Please try running the common build again and let me know if this issue still persists.

@Dhruv1797
Copy link
Contributor Author

Hi @kienvo, The common build and Android build have passed successfully. The only issue remaining is with the iOS build. According to the logs, CocoaPods is reporting a compatibility issue for the pod "flutter_nfc_kit", which is the same as before—I haven’t made any changes to it.

Should i update this dependency and push changes:
flutter_nfc_kit: ^3.5.2 → flutter_nfc_kit: ^3.6.0?

@mariobehling mariobehling changed the title fix: nfc-handling-open-nfc-settings-when-off. fix: open nfc settings when switched off Apr 16, 2025
@mariobehling mariobehling merged commit 1ad1032 into fossasia:main Apr 16, 2025
2 of 3 checks passed
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.

3 participants