예제 #1
0
 private void UpdateSource(Stream stream)
 {
     LottieComposition.FromInputStream(Context, stream, lottieComposition =>
     {
         try
         {
             _lottieAnimationView.SetComposition(lottieComposition);
             _sourceIsValid = true;
             if (_isIndeterminate)
             {
                 _lottieAnimationView.Progress = 0;
                 _lottieAnimationView.PlayAnimation();
                 _lottieAnimationView.Loop(true);
             }
             else
             {
                 _lottieAnimationView.PauseAnimation();
                 _lottieAnimationView.Loop(false);
             }
         }
         catch (Exception)
         {
             _sourceIsValid = false;
         }
     });
 }
예제 #2
0
        public void ShowProgressDialog(Context context, Stream stream, MaskType maskType, float progress,
                                       bool isIndeterminate  = true,
                                       DialogType dialogType = DialogType.BottomStatus, string status = null, string dismissDescription = null,
                                       TimeSpan?timeout      = null, Action clickCallback = null, Action dismissCallback = null)
        {
            if (!timeout.HasValue)
            {
                timeout = TimeSpan.Zero;
            }

            if (CurrentDialog != null && _animationView == null)
            {
                DismissCurrent(context);
            }

            lock (_dialogLock)
            {
                if (CurrentDialog == null)
                {
                    Application.SynchronizationContext.Send(state =>
                    {
                        CurrentDialog = new Dialog(context);

                        CurrentDialog.RequestWindowFeature((int)WindowFeatures.NoTitle);

                        if (maskType != MaskType.Black)
                        {
                            CurrentDialog.Window.ClearFlags(WindowManagerFlags.DimBehind);
                        }

                        if (maskType == MaskType.None)
                        {
                            CurrentDialog.Window.SetFlags(WindowManagerFlags.NotTouchModal,
                                                          WindowManagerFlags.NotTouchModal);
                        }

                        CurrentDialog.Window.SetBackgroundDrawable(new ColorDrawable(Color.Transparent));

                        var inflater = LayoutInflater.FromContext(context);

                        View view;
                        switch (dialogType)
                        {
                        case DialogType.BottomStatus:
                            view        = inflater.Inflate(Resource.Layout.bottom_header_progress_dialog, null);
                            _statusView = view.FindViewById <TextView>(Resource.Id.bottom_textViewStatus);
                            break;

                        case DialogType.TextOnly:
                            view        = inflater.Inflate(Resource.Layout.center_header_progress_dialog, null);
                            _statusView = view.FindViewById <TextView>(Resource.Id.center_textViewStatus);
                            break;

                        case DialogType.TopStatus:
                            view        = inflater.Inflate(Resource.Layout.top_header_progress_dialog, null);
                            _statusView = view.FindViewById <TextView>(Resource.Id.top_textViewStatus);
                            break;

                        case DialogType.AnimationOnly:
                            view = inflater.Inflate(Resource.Layout.animation_only_progress_dialog, null);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException(nameof(dialogType), dialogType, null);
                        }


                        if (clickCallback != null)
                        {
                            view.Click += (sender, e) => clickCallback();
                        }
                        _animationView = view.FindViewById <LottieAnimationView>(Resource.Id.lottieAnimationView);
                        try
                        {
                            LottieComposition.FromInputStream(context, stream,
                                                              lottieComposition =>
                            {
                                _animationView.SetComposition(lottieComposition);
                                _animationView.Loop(isIndeterminate);
                                if (!isIndeterminate)
                                {
                                    _animationView.PauseAnimation();
                                }
                            });
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }

                        switch (maskType)
                        {
                        case MaskType.None:
                            view.SetBackgroundResource(Resource.Drawable.roundedbgdark);
                            _statusView?.SetTextColor(Color.White);
                            break;

                        case MaskType.Clear:
                            view.SetBackgroundColor(Color.Transparent);
                            _statusView?.SetTextColor(Color.Black);
                            break;

                        case MaskType.Black:
                            view.SetBackgroundResource(Resource.Drawable.roundedbg_white);
                            _statusView?.SetTextColor(Color.Black);
                            break;

                        case MaskType.Gradient:
                            view.SetBackgroundResource(Resource.Drawable.roundedbg_white);
                            _statusView?.SetTextColor(Color.Black);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException(nameof(maskType), maskType, null);
                        }

                        if (_statusView != null)
                        {
                            _statusView.Text       = status ?? "";
                            _statusView.Visibility = string.IsNullOrEmpty(status) ? ViewStates.Gone : ViewStates.Visible;
                        }

                        CurrentDialog.SetContentView(view);

                        CurrentDialog.SetCancelable(dismissCallback != null);
                        if (dismissCallback != null)
                        {
                            CurrentDialog.CancelEvent += (sender, e) => dismissCallback();
                        }

                        CurrentDialog.Show();
                    }, null);


                    if (timeout.Value > TimeSpan.Zero)
                    {
                        Task.Factory.StartNew(() =>
                        {
                            if (!_waitDismiss.WaitOne(timeout.Value))
                            {
                                DismissCurrent(context);
                            }
                        }).ContinueWith(ct =>
                        {
                            var ex = ct.Exception;

                            if (ex != null)
                            {
                                Log.Error("LottieDialog", ex.ToString());
                            }
                        }, TaskContinuationOptions.OnlyOnFaulted);
                    }
                }
                else
                {
                    Application.SynchronizationContext.Send(state =>
                    {
                        _animationView.Progress = progress / 100;
                        if (_statusView != null)
                        {
                            _statusView.Text = status ?? "";
                        }
                    }, null);
                }
            }
        }