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

FilePickerControl features like new event and fixes upload behavior between page update #3875

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

Conversation

andersou
Copy link
Contributor

@andersou andersou commented Aug 26, 2024

Description

The FilePicker component exhibits strange behaviors when the webpage is refreshed. The internal "files" structure, which is responsible for managing previously selected files, becomes null during the component's update process. This issue affects the file upload functionality, causing it to fail.

Additionally, after refreshing the webpage, the upload process does not execute as expected, leading to further problems with file uploads.

New Feature Added: A new functionality has been introduced: an event for completed uploads. This event should trigger correctly once the upload process is finalized, providing feedback on the status of the upload.


Feel free to adjust the details according to your specific requirements!

Test Code

# Test code for the review of this PR

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

Enhance the FilePicker component by adding a new event for completed uploads and fixing the file upload behavior during page refresh. Refactor the file upload logic to improve the component's reliability.

New Features:

  • Introduce a new event for completed uploads in the FilePicker component, allowing feedback on the status of the upload process.

Bug Fixes:

  • Fix the issue where the FilePicker component's internal 'files' structure becomes null during page refresh, causing file upload failures.

Enhancements:

  • Refactor the file upload logic in the FilePicker component to improve reliability and maintainability.

…and fixed the files control across session (page refresh on web)
Copy link
Contributor

sourcery-ai bot commented Aug 26, 2024

Reviewer's Guide by Sourcery

This pull request implements improvements to the FilePickerControl component, addressing issues with file upload behavior during page refreshes and introducing a new event for completed uploads. The changes primarily affect the file_picker.dart and file_picker.py files, modifying the internal file management structure and adding new functionality for upload status feedback.

File-Level Changes

Change Details Files
Refactored file management in FilePickerControl
  • Changed _files from a nullable List? to a non-nullable global List
  • Updated file selection logic to use the global files list
  • Modified uploadFiles method to accept List as a parameter
packages/flet/lib/src/controls/file_picker.dart
Improved upload process and state management
  • Added a new 'UPLOADING_FILES' state to FilePickerState enum
  • Implemented resetDialogState method to reset the file picker state after upload
  • Modified upload logic to work with the new file management structure
packages/flet/lib/src/controls/file_picker.dart
sdk/python/packages/flet-core/src/flet_core/file_picker.py
Added new event for completed uploads
  • Implemented sendUploadFinishEvent method to trigger the new event
  • Added on_upload_finished event handler in the Python SDK
  • Updated uploadFiles method to call sendUploadFinishEvent after all files are processed
packages/flet/lib/src/controls/file_picker.dart
sdk/python/packages/flet-core/src/flet_core/file_picker.py
Removed dependencies on FletAppServices and FletServer
  • Removed imports for flet_app_services.dart and flet_server.dart
  • Updated methods to no longer rely on FletServer for upload operations
packages/flet/lib/src/controls/file_picker.dart

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

Overall Comments:

  • Consider refactoring the build method into smaller, more focused methods to improve readability and maintainability.
  • The global files variable could lead to issues in concurrent scenarios. Consider keeping this as part of the component's state instead.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 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.

@@ -78,12 +76,12 @@ class FilePickerControl extends StatefulWidget {
State<FilePickerControl> createState() => _FilePickerControlState();
}

List<PlatformFile> files = [];
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 moving the 'files' list into the state class

Global variables can lead to unexpected behavior and make the code harder to maintain. It would be better to keep this list as part of the _FilePickerControlState class.

Suggested change
List<PlatformFile> files = [];
class _FilePickerControlState extends State<FilePickerControl> {
List<PlatformFile> files = [];
// Rest of the class implementation...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the state class, the files variable becomes unstable during page refreshes.

Copy link
Contributor

Choose a reason for hiding this comment

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

It sounds like the instability of the files variable during page refreshes might be due to how the state is being managed or preserved across refreshes. One approach to address this could be to ensure that the state is properly initialized or restored when the component is rebuilt.

You might want to consider using a state management solution or a mechanism to persist the state across refreshes, such as using a StatefulWidget with a State that properly handles initialization and disposal. Additionally, you could explore using local storage or a similar method to save and restore the files list if it needs to persist across page reloads.

If you can provide more details about how the state is currently being managed, I might be able to offer more specific suggestions.

@@ -114,6 +115,7 @@ def __init__(
self,
on_result: Optional[Callable[[FilePickerResultEvent], None]] = None,
on_upload: Optional[Callable[[FilePickerUploadEvent], None]] = None,
on_upload_finished: Optional[Callable[[ControlEvent], None]] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider whether the new state and event handler are essential.

The recent changes have increased the complexity of the FilePicker class. The addition of a new state (UPLOADING_FILES) and event handler (on_upload_finished) adds more logic to manage, which can make the code harder to maintain. The on_upload_finished handler seems redundant as a simple pass-through, potentially adding unnecessary complexity. Consider whether these additions are essential. If they are, aim to integrate them with minimal redundancy and straightforward logic. I've refactored the code to ensure on_upload_finished is only added if provided, and encapsulated event data conversion for better readability. This approach reduces complexity and improves maintainability.

@ndonkoHenri
Copy link
Collaborator

ndonkoHenri commented Aug 26, 2024

Thanks for your contribution.
Will you mind sharing some code for the proper review of your additions?

@FeodorFitsner
Copy link
Contributor

I'm wondering how files selected with FilePicker stay alive between page refreshes? Is it the standard behavior of modern browsers? I mean if I do a simple page with file input control, choose a file and then refresh the page would it work?

@andersou
Copy link
Contributor Author

Hi Feodor, if you try to upload a file, it works for the first time. If you refresh the Page, the file uploader doesnt work correctly. Tomorrow i will make a example to reproduce this behavior.

@syleishere
Copy link
Contributor

syleishere commented Oct 25, 2024

Can anyone test file_picker on google tv streamer? It is no longer working at all on that platform. I cannot figure out if this is a google issue or a recent update to android APIs. Watching adb logcat reveals nothing interesting, although I can post those logs. Ironically it is not just my app, I see exact same logcat messages for wireguard app not working selecting config files to create a tunnel.

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.

4 participants