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

Fix/communication between app and overlay #96

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

serhiisdev
Copy link

@serhiisdev serhiisdev commented Feb 5, 2024

Fix communication between app and overlay.
Cache and reuse FlutterEngineGroup and FlutterEngine.
Fix crash when closing overlay.

Now there is no need to use ReceivePort or SendPort to communicate with the overlay. Just [FlutterOverlayWindow.overlayListener] and [FlutterOverlayWindow.shareData] will be enough.

for example:

FlutterOverlayWindow.overlayListener.listen((event) {
  print('Event: $event');
});

FlutterOverlayWindow.shareData('Message');

@serhiisdev serhiisdev force-pushed the fix/communication_between_app_and_overlay branch 4 times, most recently from 5225d5a to 7d55a19 Compare February 5, 2024 18:56
@X-SLAYER
Copy link
Owner

X-SLAYER commented Feb 5, 2024

i will verify it and try to merge it ASAP

@serhiisdev serhiisdev force-pushed the fix/communication_between_app_and_overlay branch 2 times, most recently from c5f99e5 to 81bba40 Compare February 5, 2024 19:31
@serhiisdev
Copy link
Author

I force-pushed a fix for the last commit. Now, it should work as intended. If the overlay is being shown, and [showOverlay] is called, the overlay will reopen. Before the force-pushed fix, it would have just been closed.

@serhiisdev serhiisdev force-pushed the fix/communication_between_app_and_overlay branch 2 times, most recently from a7c4254 to 7fff772 Compare February 6, 2024 17:25
@serhiisdev
Copy link
Author

serhiisdev commented Feb 6, 2024

I have updated implementation of messaging between the app and the overlay.

It has the following main improvements:

  1. Message channel is registered at app start, now [shareData] can be called in [overlayMain] function:

@pragma("vm:entry-point")
void overlayMain() {
WidgetsFlutterBinding.ensureInitialized();
FlutterOverlayWindow.shareData('Message from overlay');
...
}

  1. Now [shareData] returns non-nullable boolean.

And now, the overlay can receive and react to events from the main app even when it's not attached to the view (closed from the user's perspective).

For example, consider this situation:

  1. Overlay has a variable [counter] and has registered such listener :
    FlutterOverlayWindow.overlayListener.listen((event) {
      counter += 1;
    });
  1. Overlay is closed.

  2. Main app sends event while overlay is closed:

FlutterOverlayWindow.shareData('Message from main app');

After this action [counter] will be increased by 1.

This change in behavior can be considered a "fix", but theoretically, it could unexpectedly alter plugin behavior for some users.

Update:
I have added option to subscribe to the overlay status: overlay showing or overlay closed.
So user can appropriately react to these changes if needed.

This change in behavior can be considered a "fix", but theoretically, it could unexpectedly alter plugin behavior for some users.
I have tested plugin implementation before my pull request, and messages from overlay could be emitted via SendPort when overlay is closed, so there is no change in this behavior.

@serhiisdev serhiisdev force-pushed the fix/communication_between_app_and_overlay branch 5 times, most recently from 4174ffe to a861816 Compare February 7, 2024 01:42
@irfansadiqdevops
Copy link

I'm unable to get data from overlay to flutter UI. I can send data from ui to overlay

@serhiisdev
Copy link
Author

serhiisdev commented Feb 11, 2024

I'm unable to get data from overlay to flutter UI. I can send data from ui to overlay

@irfansadiqdevops

Can you test this sample app and tell if it works?
https://github.com/serhiisdev/flutter_overlay_window_sample_app

If the sample app works, the following information can be helpful:

  1. Do you send the same type of data from the overlay to the Flutter UI and from the Flutter UI to the overlay?
  2. Is main Flutter app subscribed to [FlutterOverlayWindow.overlayListener]?

If the sample app does not work, the following information can be helpful:

  1. What version of Flutter are you using to run the app?
  2. What version of Android are you using?
  3. Are you running this app on an emulator or a real Android device? If you are using a real device, information about the device model can be helpful.

Providing a minimal reproducible example would be helpful for further investigation.

@irfansadiqdevops
Copy link

@serhiisdev Thanks a lot for your response. I use example of
I'll definitely use example you provide and will give you feedback

@irfansadiqdevops
Copy link

@serhiisdev I tried example you ask but I'm only getting data on overlay side from flutter side. I've never get print MainAppScreen Event from overlay:
I'm using 3.16.1 flutter version
android sdk verison 34.0.0
and running on physical device infinix s5

@serhiisdev
Copy link
Author

serhiisdev commented Feb 11, 2024

@irfansadiqdevops
Thank you for your response.

I have tested app on both Android 14 (sdk 34) and Android 9 (sdk 28) using Android emulator and Flutter 3.16.1, and communication worked both ways.

Can you provide following information:

  1. When you downloaded sample app and tried to get packages (flutter pub get), did you receive following error ?

"
The current Dart SDK version is 3.2.1.

Because flutter_overlay_window_sample_app requires SDK version >=3.2.6 <4.0.0, version solving failed.
"

  1. And have you resolved it by updating SDK constraint to the following ?
sdk: '>=3.2.1 <4.0.0'
  1. You mentioned you're using Android SDK 34. Are you referring to the Android SDK set in the app's build.gradle file or to the Android SDK of your device? I checked the Android version of infinix s5 on a website with phone specifications, and according to this information, the device has Android 9. And, do you have the original Android from the device manufacturer installed, or do you have a custom Android ROM?

  2. When you open overlay and press the "+" button on overlay screen, does the counter increment on both the overlay screen and the Flutter app screen, or only on the overlay screen?

  3. When you press the "+" button on the main Flutter app screen, does the counter increment on both the main app screen and the overlay screen?

  4. Can you test app with the latest Flutter stable version 3.16.9 ?

  5. Can you test app on another device (it can be an Android emulator) ?

  6. Can you update this part of overlay screen widget:

    await FlutterOverlayWindow.shareData("Hello Flutter app from overlay");

to this:

    final isSent =
        await FlutterOverlayWindow.shareData("Hello Flutter app from overlay");
    print('isSent: $isSent');

And tell what is the output to console: 'isSent: true' or 'isSent: false' ?

@irfansadiqdevops
Copy link

@serhiisdev I've update flutter sdk to 3.16.9 and your code, It works. Thanks a lot

@serhiisdev serhiisdev force-pushed the fix/communication_between_app_and_overlay branch from be99a7b to e825131 Compare February 27, 2024 17:29
@X-SLAYER
Copy link
Owner

can you rebase from the latest version so I could verify the communication overlay and merge it thanks

@serhiisdev serhiisdev force-pushed the fix/communication_between_app_and_overlay branch 3 times, most recently from 73f852b to e6ec0e6 Compare April 10, 2024 07:26
cache and reuse flutter engine group
cleanup resources on detaching from activity

ensure FlutterEngine is created before attempting to launch overlay
…sent using the [shareData] method

doc improve documentation for [shareData] method

feat return the original Future from the [disposeOverlayListener] method
…thod

feat return non-nullable value from [shareData] method

doc update documentation for [shareData] method
@serhiisdev serhiisdev force-pushed the fix/communication_between_app_and_overlay branch from e6ec0e6 to fb0a426 Compare April 10, 2024 07:31
@serhiisdev
Copy link
Author

can you rebase from the latest version so I could verify the communication overlay and merge it thanks

@X-SLAYER

Done

@HaloWang
Copy link
Contributor

Has there been any progress on this pull request?

@X-SLAYER
Copy link
Owner

Has there been any progress on this pull request?

Sorry I didn't verify it yet I need to test it otherwise you can use this MR directly

@bugrevealingbme
Copy link

bugrevealingbme commented Dec 10, 2024

I can't get the data I sent from Overlay

@NoahFlatfish
Copy link

NoahFlatfish commented Jan 15, 2025

flutter --version Flutter 3.24.3 • channel stable • https://github.com/flutter/flutter.git Framework • revision 2663184aa7 (4 months ago) • 2024-09-11 16:27:48 -0500 Engine • revision 36335019a8 Tools • Dart 3.5.3 • DevTools 2.37.3

when i build with cable works fine but when i build with apk or appbundle
FlutterOverlayWindow.overlayListener and FlutterOverlayWindow.shareData(data) doesn't work

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.

6 participants