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

Adding Background/Foreground Services to GeoLocator UPDATE #3803

Merged
merged 6 commits into from
Aug 27, 2024

Conversation

syleishere
Copy link
Contributor

@syleishere syleishere commented Aug 13, 2024

Description

Adds background/foreground service to geolocator, bug fix, as well as client/android updates to more closely match flet-build-template 0.24.0.

Test Code

import flet as ft

async def main(page: ft.Page):
    page.window.always_on_top = True
    page.on_error = lambda e: print(f"Page Error: {e.data}")
    page.scroll = ft.ScrollMode.ADAPTIVE
    page.appbar = ft.AppBar(title=ft.Text("Geolocator Tests"))

    def handle_location(e):
        page.add(ft.Text(f"Location: {e.latitude} {e.longitude}"))
        print(f"Location: {e.latitude} {e.longitude}")


    gl = ft.Geolocator(on_position=handle_location)
    page.overlay.append(gl)

    settings_dlg = lambda handler: ft.AlertDialog(
        adaptive=True,
        title=ft.Text("Opening Location Settings..."),
        content=ft.Text(
            "You are about to be redirected to the location/app settings. "
            "Please locate this app and grant it location permissions."
        ),
        actions=[
            ft.TextButton(
                text="OK",
                on_click=handler,
            ),
        ],
        actions_alignment=ft.MainAxisAlignment.CENTER,
    )

    async def handle_permission_request(e):
        p = await gl.request_permission_async()
        page.add(ft.Text(f"request_permission: {p}"))

    async def handle_get_permission_status(e):
        p = await gl.get_permission_status_async()
        page.add(ft.Text(f"get_permission_status: {p}"))

    async def handle_get_current_position(e):
        p = await gl.get_current_position_async()
        page.add(ft.Text(f"get_current_position: ({p.latitude}, {p.longitude})"))

    async def handle_get_last_known_position(e):
        p = await gl.get_last_known_position_async()
        page.add(ft.Text(f"get_last_known_position: ({p.latitude}, {p.longitude})"))

    async def handle_location_service_enabled(e):
        p = await gl.is_location_service_enabled_async()
        page.add(
            ft.Text(f"is_location_service_enabled: {p}")
        )

    async def handle_open_location_settings(e):
        p = await gl.open_location_settings_async()
        page.close_dialog()
        page.add(ft.Text(f"open_location_settings: {p}"))

    async def handle_open_app_settings(e):
        p = await gl.open_app_settings_async()
        page.close_dialog()
        page.add(ft.Text(f"open_app_settings: {p}"))

    async def service_enable(e):
        p = await gl.service_enable_async()
        page.close_dialog()
        page.add(ft.Text(f"enable service: {p}"))

    async def service_disable(e):
        p = await gl.service_disable_async()
        page.close_dialog()
        page.add(ft.Text(f"disable service: {p}"))

    page.add(
        ft.Row(
            [
                ft.OutlinedButton(
                    "request_permission",
                    on_click=handle_permission_request,
                ),
                ft.OutlinedButton(
                    "get_permission_status",
                    on_click=handle_get_permission_status,
                ),
                ft.OutlinedButton(
                    "get_current_position",
                    on_click=handle_get_current_position,
                ),
                ft.OutlinedButton(
                    "get_last_known_position",
                    visible=False if page.web else True,
                    on_click=handle_get_last_known_position,
                ),
                ft.OutlinedButton(
                    "is_location_service_enabled",
                    on_click=handle_location_service_enabled,
                ),
                ft.OutlinedButton(
                    "open_location_settings",
                    visible=False if page.web else True,
                    on_click=lambda e: page.show_dialog(
                        settings_dlg(handle_open_location_settings)
                    ),
                ),
                ft.OutlinedButton(
                    "open_app_settings",
                    visible=False if page.web else True,
                    on_click=lambda e: page.show_dialog(
                        settings_dlg(handle_open_app_settings)
                    ),
                ),
                ft.OutlinedButton(
                    "Enable Service",
                    on_click=service_enable,
                ),
                ft.OutlinedButton(
                    "Disable Service",
                    on_click=service_disable,
                ),
            ],
            wrap=True,
        )
    )

ft.app(main)

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)

Additional details

Merging this PR along with one for flet-build-template 0.24.0 should be all that is needed.

Summary by Sourcery

Add background and foreground services to the geolocator for improved location tracking capabilities. Fix permission handling issues and update dependencies to align with the flet-build-template 0.24.0. Introduce new methods for enabling and disabling geolocation services, enhancing the control over location tracking.

New Features:

  • Introduce background and foreground services for geolocation tracking, allowing the application to receive location updates even when running in the background.

Bug Fixes:

  • Fix an issue with the geolocation service to ensure it correctly requests and checks for location permissions.

Enhancements:

  • Update the geolocator to include new methods for enabling and disabling location services, improving control over geolocation tracking.

Build:

  • Add dependency overrides for 'geolocator' and 'file_picker' packages in the pubspec.yaml to ensure compatibility with the latest versions.

Copy link
Contributor

sourcery-ai bot commented Aug 13, 2024

Reviewer's Guide by Sourcery

This pull request adds background/foreground service functionality to the GeoLocator control, implements bug fixes, and updates the client and Android components to align with flet-build-template 0.24.0. The changes primarily focus on enhancing location tracking capabilities, improving error handling, and updating dependencies.

File-Level Changes

Files Changes
packages/flet_geolocator/lib/src/geolocator.dart Added background/foreground service functionality to GeoLocator
packages/flet_geolocator/lib/src/geolocator.dart
sdk/python/packages/flet-core/src/flet_core/geolocator.py
Implemented new methods for enabling and disabling location services
packages/flet_geolocator/lib/src/geolocator.dart
sdk/python/packages/flet-core/src/flet_core/geolocator.py
Added position stream handling and callback functionality
client/pubspec.yaml Updated client dependencies and overrides
packages/flet_geolocator/lib/src/geolocator.dart Improved error handling and debugging for location services
packages/flet_geolocator/lib/src/geolocator.dart Added platform-specific location settings for Android, iOS, macOS, and web

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

Overall Comments:

  • Consider adding a configurable update interval for location tracking to allow users to balance between location accuracy and battery life, especially for mobile devices.
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.

} else {
debugPrint('Geolocator: Position is null.');
}
},
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Implement more robust error handling in _enableLocationService

The current error handling only logs the error. Consider implementing a more comprehensive strategy, such as propagating the error to the user or implementing a retry mechanism.

onError: (e) {
  debugPrint('Geolocator: Error getting stream position: $e');
  // Propagate error to user
  _showErrorToUser('Unable to get location. Please try again.');
  // Implement retry mechanism
  _retryLocationService();
},

@syleishere
Copy link
Contributor Author

syleishere commented Aug 13, 2024

Ran tests on windows, web and android. Nothing fails, although this should only be used on android and IOS phones, it does work on other platforms for whatever reason from flutter package.

To run example and have it work on android run app once, then go to permissions and enable location and notification permissions on phone for app. Restart app and should display a foreground service notification background service is enabled for updated website example when they click start service.

Users should enable those permissions on android("LOCATION and NOTIFICATIONS") in their Flet app with permissions package before attempting to enable the service in a real app.

IOS is untested, but I did look at info.plist file and that permission is already there that is needed, so if someone with an iphone can test, wonderful.

client/pubspec.yaml Outdated Show resolved Hide resolved
@FeodorFitsner
Copy link
Contributor

Is the PR ready for review?

@syleishere
Copy link
Contributor Author

Yeah I removed filepicker from pubspec.yaml in client area, should be good to go.

client/pubspec.yaml Outdated Show resolved Hide resolved
@FeodorFitsner
Copy link
Contributor

@syleishere
Copy link
Contributor Author

Took a look seems related to this PR which fixes it: Baseflow/flutter-permission-handler@817ae00

@syleishere
Copy link
Contributor Author

syleishere commented Aug 23, 2024

If you set version to 0.1.3+2 for permission_handler_html should resolve the PR commit from 3 days ago from them. I'll see if it works if i can modify it in flet_permission_handler/pubspec.yaml

@syleishere
Copy link
Contributor Author

Appveyor builds successfully applying permission_handler_html pubspec.yaml fix

@FeodorFitsner FeodorFitsner merged commit 5cbbdff into flet-dev:main Aug 27, 2024
2 checks passed
@ndonkoHenri
Copy link
Contributor

ndonkoHenri commented Aug 28, 2024

@syleishere can you please make a PR to the template repo to update the necessary files (using the 0.24.0 branch as base-branch)?

@syleishere
Copy link
Contributor Author

I have a PR there already for 0.24.0 branch for additions needed to AndroidManifest.xml

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