|
1 | 1 | import 'dart:async'; |
2 | 2 | import 'dart:developer'; |
3 | 3 |
|
| 4 | +import 'package:flutter/material.dart'; |
4 | 5 | import 'package:flutter/services.dart'; |
5 | 6 | import 'package:flutter_overlay_window/src/overlay_config.dart'; |
6 | 7 |
|
@@ -81,9 +82,33 @@ class FlutterOverlayWindow { |
81 | 82 | return _res; |
82 | 83 | } |
83 | 84 |
|
84 | | - /// Broadcast data to and from overlay app |
85 | | - static Future shareData(dynamic data) async { |
86 | | - return await _overlayMessageChannel.send(data); |
| 85 | + /// Broadcast [data] to and from overlay app. |
| 86 | + /// |
| 87 | + /// If `true` is returned, it indicates that the [data] was sent. However, it does not guarantee |
| 88 | + /// that the [data] has arrived at its destination, |
| 89 | + /// i.e. to the listeners of the [overlayListener] stream. |
| 90 | + /// Typically, success in sending should imply successful delivery. |
| 91 | + /// |
| 92 | + /// If `false` is returned, it means that the [data] was not sent. |
| 93 | + /// |
| 94 | + /// This method may return `false` under the following conditions: |
| 95 | + /// - The overlay is closed. |
| 96 | + /// - The application is detached from the activity, i.e. the application is closed. |
| 97 | + /// |
| 98 | + /// Returns `true` if the [data] was sent successfully, otherwise `false`. |
| 99 | + /// May return `null` if an unexpected condition occurs, e.g. |
| 100 | + /// the overlay is visible but the overlay's message channel is not registered, |
| 101 | + /// resulting in the [data] not being sent. |
| 102 | + static Future<bool?> shareData(dynamic data) async { |
| 103 | + final isSent = await _overlayMessageChannel.send(data); |
| 104 | + if (isSent == null) { |
| 105 | + debugPrintStack( |
| 106 | + stackTrace: StackTrace.current, |
| 107 | + label: '[FlutterOverlayWindow] ERROR: ' |
| 108 | + 'Failed to send the [data] using the [shareData] method.'); |
| 109 | + return null; |
| 110 | + } |
| 111 | + return isSent as bool; |
87 | 112 | } |
88 | 113 |
|
89 | 114 | /// Streams message shared between overlay and main app |
@@ -123,7 +148,7 @@ class FlutterOverlayWindow { |
123 | 148 | /// Dispose overlay stream. |
124 | 149 | /// |
125 | 150 | /// Once disposed, only a complete restart of the application will re-initialize the listener. |
126 | | - static void disposeOverlayListener() { |
127 | | - _controller.close(); |
| 151 | + static Future<dynamic> disposeOverlayListener() { |
| 152 | + return _controller.close(); |
128 | 153 | } |
129 | 154 | } |
0 commit comments