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

Window destroy on android and iOS #3733

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bl1nch
Copy link
Contributor

@bl1nch bl1nch commented Jul 29, 2024

Description

This PR should provide window destroy (app exit) function for Android and IOS platforms

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • I signed the CLA.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing tests pass locally with my changes
  • I have made corresponding changes to the documentation (if applicable)

Summary by Sourcery

This pull request introduces a new feature that allows the application to destroy the window (exit the app) on Android and iOS platforms. The implementation checks the platform and performs the appropriate action to close the app.

  • New Features:
    • Added window destroy functionality for Android and iOS platforms.

Copy link
Contributor

sourcery-ai bot commented Jul 29, 2024

Reviewer's Guide by Sourcery

This pull request introduces a new feature that allows the application to destroy the window (exit the app) on Android and iOS platforms. The implementation checks the platform and calls the appropriate method to exit the app. Significant changes include importing necessary libraries and modifying the 'destroyWindow' function to handle mobile platforms.

File-Level Changes

Files Changes
packages/flet/lib/src/utils/desktop.dart Enhanced the 'destroyWindow' function to support window destruction (app exit) on both Android and iOS platforms by adding platform-specific exit logic.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@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 @bl1nch - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using a more graceful shutdown method for iOS instead of exit(0), which may not allow for proper cleanup.
  • The logic could be simplified by removing the redundant isDesktop() check and potentially adding a default case for unknown platforms.
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 to tell me if it was helpful.

Comment on lines 247 to 250
if (isDesktop()) {
if (isDesktop() || isMobile()) {
debugPrint("destroyWindow()");
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Remove redundant platform check as all cases are handled specifically.

The isDesktop() || isMobile() check is no longer necessary since all platforms are handled in the subsequent conditions. Consider removing this check and keeping only the debug print statement outside of any condition.

Suggested change
if (isDesktop()) {
if (isDesktop() || isMobile()) {
debugPrint("destroyWindow()");
debugPrint("destroyWindow()");

@bl1nch bl1nch marked this pull request as draft July 29, 2024 10:26
@bl1nch bl1nch marked this pull request as ready for review July 29, 2024 10:50
Copy link
Contributor

@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 @bl1nch - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding unit tests to ensure the window destroy functionality works correctly across all platforms.
  • Using exit(0) for iOS might be too abrupt. Consider implementing a more graceful shutdown process that allows for proper cleanup.
  • Research and implement platform-specific best practices for app termination on mobile platforms to improve user experience.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues 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 to tell me if it was helpful.

packages/flet/lib/src/utils/desktop.dart Show resolved Hide resolved
Comment on lines 248 to 254
Future destroyWindow() async {
if (isDesktop()) {
if (isDesktop() || isMobile()) {
debugPrint("destroyWindow()");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Consider renaming the function to reflect its expanded functionality

The destroyWindow() function now handles application exit for both desktop and mobile platforms. Consider renaming it to something more general like exitApplication() or terminateApp() to accurately reflect its broader purpose.

Suggested change
Future destroyWindow() async {
if (isDesktop()) {
if (isDesktop() || isMobile()) {
debugPrint("destroyWindow()");
}
Future terminateApp() async {
if (isDesktop() || isMobile()) {
debugPrint("terminateApp()");
}

@bl1nch bl1nch force-pushed the window-destroy-android-ios branch from d602007 to a42139f Compare November 26, 2024 04:00
@bl1nch bl1nch force-pushed the window-destroy-android-ios branch from a42139f to 598f8c8 Compare November 26, 2024 04:01
@syleishere
Copy link
Contributor

syleishere commented Nov 28, 2024

I have even found SystemNavigator.pop() to not always work on android, especially on google TV, exit(0) seems to be only reliable way to terminate app on android. On IOS I believe there is no way to terminate. exit(0) is next best thing there as well but should background it on that platform. In Python can terminate as well:

    async def terminate1(self, e):
        print("Trying to kill program")
        self.config.page.loop.stop()

    async def terminate2(self, e):
        sys.exit(0)

    async def terminate3(self, e):
        to_cancel = asyncio.all_tasks(self.config.page.loop)
        if not to_cancel:
            return
        for task in to_cancel:
            task.cancel()
        self.config.page.loop.run_until_complete(
            asyncio.gather(*to_cancel, return_exceptions=True)
        )

I find sys.exit(0) the most reliable for android/IOS, for desktop terminate3 works clean.

Tried tonight using android studio with a pure flutter counter example, using my google TV streamer as test device, SystemNavigator.pop() did not work, but exit(0) successfully made android studio loose connection to device. Only other issue I have found on google TV is back button on remote crashes the flet app while home button is fine, so I submitted a PR to add predictive back support where I will call exit(0) on all platforms.

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