Exemple #1
0
        public static Future <R> compute <Q, R>(ComputeCallback <Q, R> callback, Q message, string debugLabel = null)
        {
            var completer = Completer.create();
            var isolate   = Isolate.current;

            var backgroundWorker = new BackgroundWorker();

            backgroundWorker.DoWork             += (sender, args) => { args.Result = callback((Q)args.Argument); };
            backgroundWorker.RunWorkerCompleted += (o, a) => {
                if (!isolate.isValid)
                {
                    return;
                }

                using (Isolate.getScope(isolate)) {
                    if (a.Error != null)
                    {
                        completer.completeError(a.Error);
                    }
                    else
                    {
                        completer.complete(FutureOr.value((R)a.Result));
                    }
                }
            };
            backgroundWorker.RunWorkerAsync(message);
            return(completer.future.to <R>());
        }
        public override Future <byte[]> load(string key)
        {
            var completer = Completer.create();
            var isolate   = Isolate.current;
            var panel     = UIWidgetsPanelWrapper.current.window;

            if (panel.isActive())
            {
                panel.startCoroutine(_loadCoroutine(key, completer, isolate));
            }
            return(completer.future.to <byte[]>());
        }
Exemple #3
0
        void _show()
        {
            D.assert(_mode != _RefreshIndicatorMode.refresh);
            D.assert(_mode != _RefreshIndicatorMode.snap);
            Completer completer = Completer.create();

            _pendingRefreshFuture = completer.future;
            _mode = _RefreshIndicatorMode.snap;
            _positionController
            .animateTo(1.0f / RefreshIndicatorUtils._kDragSizeFactorLimit,
                       duration: RefreshIndicatorUtils._kIndicatorSnapDuration)
            .then((value) => {
                if (mounted && _mode == _RefreshIndicatorMode.snap)
                {
                    D.assert(widget.onRefresh != null);
                    setState(() => { _mode = _RefreshIndicatorMode.refresh; });

                    Future refreshResult = widget.onRefresh();
                    D.assert(() => {
                        if (refreshResult == null)
                        {
                            UIWidgetsError.reportError(new UIWidgetsErrorDetails(
                                                           exception: new UIWidgetsError(
                                                               "The onRefresh callback returned null.\n" +
                                                               "The RefreshIndicator onRefresh callback must return a Promise."
                                                               ),
                                                           context: new ErrorDescription("when calling onRefresh"),
                                                           library: "material library"
                                                           ));
                        }

                        return(true);
                    });
                    if (refreshResult == null)
                    {
                        return;
                    }

                    refreshResult.whenComplete(() => {
                        if (mounted && _mode == _RefreshIndicatorMode.refresh)
                        {
                            completer.complete();
                            _dismiss(_RefreshIndicatorMode.done);
                        }
                    });
                }
            });
        }
Exemple #4
0
        private Future _handleRefresh()
        {
            Completer completer = Completer.create();

            Timer.create(new TimeSpan(0, 0, 0, 3), () => { completer.complete(); });
            return(completer.future.then((_) =>
            {
                _scaffoldKey.currentState?.showSnackBar(new SnackBar(
                                                            content: new Text("Refresh complete"),
                                                            action: new SnackBarAction(
                                                                label: "RETRY",
                                                                onPressed: () => { _refreshIndicatorKey.currentState.show(); }
                                                                )
                                                            ));
            }));
        }
Exemple #5
0
        Future <byte[]> _sendPlatformMessage(string channel, byte[] message)
        {
            Completer completer = Completer.create();

            Window.instance.sendPlatformMessage(channel, message, (reply) => {
                try {
                    completer.complete(FutureOr.value(reply));
                }
                catch (Exception exception) {
                    UIWidgetsError.reportError(new UIWidgetsErrorDetails(
                                                   exception: exception,
                                                   library: "services library",
                                                   context: new ErrorDescription("during a platform message response callback")
                                                   ));
                }
            });

            return(completer.future.to <byte[]>());
        }
Exemple #6
0
        Future <Codec> _loadAsync(NetworkImage key, DecoderCallback decode)
        {
            var completer = Completer.create();
            var isolate   = Isolate.current;
            var panel     = UIWidgetsPanelWrapper.current.window;

            if (panel.isActive())
            {
                panel.startCoroutine(_loadCoroutine(key.url, completer, isolate));
                return(completer.future.to <byte[]>().then_ <byte[]>(data => {
                    if (data != null && data.Length > 0)
                    {
                        return decode(data);
                    }

                    throw new Exception("not loaded");
                }).to <Codec>());
            }

            return(new Future <Codec>(Future.create(() => FutureOr.value(null))));
        }
        public override Future <T> loadStructuredData <T>(string key, Func <string, Future <T> > parser)
        {
            D.assert(key != null);
            D.assert(parser != null);
            if (_structuredDataCache.ContainsKey(key))
            {
                return(_structuredDataCache[key].to <T>());
            }

            Completer  completer = null;
            Future <T> result    = null;

            loadString(key, cache: false).then_ <T>(value => parser(value)).then_ <object>((T value) => {
                result = new SynchronousFuture <T>(value);
                _structuredDataCache[key] = result;
                if (completer != null)
                {
                    // We already returned from the loadStructuredData function, which means
                    // we are in the asynchronous mode. Pass the value to the completer. The
                    // completer's future is what we returned.
                    completer.complete(FutureOr.value(value));
                }

                return(FutureOr.nil);
            });

            if (result != null)
            {
                // The code above ran synchronously, and came up with an answer.
                // Return the SynchronousFuture that we created above.
                return(result);
            }

            // The code above hasn't yet run its "then" handler yet. Let's prepare a
            // completer for it to use when it does run.
            completer = Completer.create();
            _structuredDataCache[key] = result = completer.future.to <T>();
            return(result);
        }
Exemple #8
0
        public static Future precacheImage(
            ImageProvider provider,
            BuildContext context,
            Size size = null,
            ImageErrorListener onError = null
            )
        {
            var config    = createLocalImageConfiguration(context: context, size: size);
            var completer = Completer.create();
            var stream    = provider.resolve(configuration: config);
            ImageStreamListener listener = null;

            listener = new ImageStreamListener(
                (image, sync) => {
                if (!completer.isCompleted)
                {
                    completer.complete();
                }

                SchedulerBinding.instance.addPostFrameCallback(timeStamp => {
                    stream.removeListener(listener: listener);
                });
            },
                onError: error => {
                if (!completer.isCompleted)
                {
                    completer.complete();
                }

                stream.removeListener(listener: listener);
                UIWidgetsError.reportError(new UIWidgetsErrorDetails(
                                               context: new ErrorDescription("image failed to precache"),
                                               library: "image resource service",
                                               silent: true));
            }
                );
            stream.addListener(listener: listener);
            return(completer.future);
        }
Exemple #9
0
        public DrivenScrollActivity(
            ScrollActivityDelegate del,
            float from,
            float to,
            TimeSpan duration,
            Curve curve,
            TickerProvider vsync
            ) : base(del)
        {
            D.assert(duration > TimeSpan.Zero);
            D.assert(curve != null);

            _completer  = Completer.create();
            _controller = AnimationController.unbounded(
                value: from,
                debugLabel: GetType().ToString(),
                vsync: vsync
                );
            _controller.addListener(_tick);
            _controller.animateTo(to, duration: duration, curve: curve)
            .then(o => _end());
        }
Exemple #10
0
        public override Future <_SizeAwareCacheKey> obtainKey(ImageConfiguration configuration)
        {
            Completer completer = null;
            SynchronousFuture <_SizeAwareCacheKey> result = null;

            imageProvider.obtainKey(configuration).then((object key) => {
                // TODO: completer is always null?
                if (completer == null)
                {
                    result = new SynchronousFuture <_SizeAwareCacheKey>(new _SizeAwareCacheKey(key, width, height));
                }
                else
                {
                    completer.complete(FutureOr.value(new _SizeAwareCacheKey(key, width, height)));
                }
            });
            if (result != null)
            {
                return(result);
            }

            completer = Completer.create();
            return(completer.future.to <_SizeAwareCacheKey>());
        }
Exemple #11
0
            public override Widget build(BuildContext context)
            {
                Future <byte[]> f         = null;
                var             completer = Completer.create();
                var             isolate   = Isolate.current;
                var             panel     = UIWidgetsPanelWrapper.current.window;

                if (panel.isActive())
                {
                    panel.startCoroutine(_loadCoroutine("https://buljan.rcsdk8.org/sites/main/files/main-images/camera_lense_0.jpeg", completer, isolate));
                    f = completer.future.to <byte[]>().then_ <byte[]>(data => {
                        if (data != null && data.Length > 0)
                        {
                            return(data);
                        }

                        throw new Exception("not loaded");
                    });
                }
                var futureBuilder = new FutureBuilder <byte[]>(
                    future: f,
                    builder: (ctx, snapshot) =>
                {
                    int width   = 200;
                    int height  = 200;
                    Color color = Colors.blue;
                    if (snapshot.connectionState == ConnectionState.done)
                    {
                        return(new Container(alignment: Alignment.center, width: width, height: height, color: color, child: Image.memory(snapshot.data)));
                    }
                    else if (snapshot.connectionState == ConnectionState.waiting)
                    {
                        return(new Container(alignment: Alignment.center, width: width, height: height, color: color, child: new Text("waiting")));
                    }
                    else
                    {
                        return(new Container(alignment: Alignment.center, width: width, height: height, color: color, child: new Text("else")));
                    }
                }
                    );
                var streamBuilder = new StreamBuilder <int>(
                    stream: myStream,
                    initialData: 1,
                    builder: (ctx, snapshot) =>
                {
                    var data = snapshot.data;
                    return(new Container(child: new Text($"stream data: {data}")));
                }
                    );

                return(new Container(
                           color: Colors.blueGrey,
                           child: new Column(
                               children: new List <Widget>
                {
                    streamBuilder,
                    futureBuilder
                }
                               )
                           ));
            }
Exemple #12
0
        // @override
        //     Stream<T> asStream() {
        //     final StreamController<T> controller = StreamController<T>();
        //     controller.add(_value);
        //     controller.close();
        //     return controller.stream;
        // }

        public override Future catchError(Func <Exception, FutureOr> onError, Func <Exception, bool> test = null)
        {
            return(Completer.create().future);
        }