Example #1
0
        /// <summary>
        /// Shows a started Progress on the specified view.
        /// </summary>
        public static IProgress Show(IView view, string progressMessage, ProgressMode mode = ProgressMode.Determinate, Action stopRequestHandler = null, Action cancelRequestHandler = null)
        {
            var progress = CreateAndStartProgress(progressMessage, mode, stopRequestHandler, cancelRequestHandler);

            Show(view, progress);
            return(progress);
        }
Example #2
0
        /// <summary>
        /// Shows a started Progress on all Views of the specified content.
        /// </summary>
        public static IProgress Show(IContent content, string progressMessage, ProgressMode mode = ProgressMode.Determinate, Action stopRequestHandler = null, Action cancelRequestHandler = null, bool addToObjectGraphObjects = true)
        {
            var progress = CreateAndStartProgress(progressMessage, mode, stopRequestHandler, cancelRequestHandler);

            Show(content, progress, addToObjectGraphObjects);
            return(progress);
        }
Example #3
0
	    public static void Notify(ProgressChangeHandler handler, ProgressMode mode, int current, int total)
		{
			if (handler == null)
				return;

			if (mode == ProgressMode.DontNotify)
				return;

			switch(mode)
			{
				case ProgressMode.NotifyBytes:
					handler(new ProgressEventArgs(mode, current, total));
					break;

				case ProgressMode.NotifyRecords:
					handler(new ProgressEventArgs(mode, current, total));
					break;

				case ProgressMode.NotifyPercent:
					if (total == -1)
						return;
					handler(new ProgressEventArgs(mode, (int) (current*100/total), 100));
					break;

			}

		}
Example #4
0
        public static void Notify(ProgressChangeHandler handler, ProgressMode mode, int current, int total)
        {
            if (handler == null)
            {
                return;
            }

            if (mode == ProgressMode.DontNotify)
            {
                return;
            }

            switch (mode)
            {
            case ProgressMode.NotifyBytes:
                handler(new ProgressEventArgs(mode, current, total));
                break;

            case ProgressMode.NotifyRecords:
                handler(new ProgressEventArgs(mode, current, total));
                break;

            case ProgressMode.NotifyPercent:
                if (total == -1)
                {
                    return;
                }
                handler(new ProgressEventArgs(mode, (int)(current * 100 / total), 100));
                break;
            }
        }
Example #5
0
 public Progress()
 {
     progressState = ProgressState.Finished;
     canBeStopped  = false;
     canBeCanceled = false;
     progressMode  = ProgressMode.Indeterminate;
     progressValue = 0.0;
 }
Example #6
0
		/// <summary>Set the handler to the engine used to notify progress into the operations.</summary>
		/// <param name="handler">Your <see cref="ProgressChangeHandler"/> method.</param>
		/// <param name="mode">The <see cref="ProgressMode"/> to use.</param>
		public void SetProgressHandler(ProgressChangeHandler handler, ProgressMode mode)
		{
			mNotifyHandler = handler;

			if (mode == ProgressMode.NotifyBytes)
				throw new NotImplementedException("Not implemented yet. Planed for version 1.5.0");

			mProgressMode = mode;
		}
Example #7
0
 public void Start(string message, ProgressMode mode = ProgressMode.Determinate)
 {
     ProgressState = ProgressState.Started;
     ProgressMode  = mode;
     if (mode == ProgressMode.Determinate)
     {
         ProgressValue = 0.0;
     }
     Message = message;
 }
Example #8
0
        /// <summary>Set the handler to the engine used to notify progress into the operations.</summary>
        /// <param name="handler">Your <see cref="ProgressChangeHandler"/> method.</param>
        /// <param name="mode">The <see cref="ProgressMode"/> to use.</param>
        public void SetProgressHandler(ProgressChangeHandler handler, ProgressMode mode)
        {
            mNotifyHandler = handler;

            if (mode == ProgressMode.NotifyBytes)
            {
                throw new NotImplementedException("Not implemented yet. Planed for version 1.5.0");
            }

            mProgressMode = mode;
        }
Example #9
0
 public void SetProgressMode(UIProgress.ProgressMode mode)
 {
     _progressMode = mode;
     if (_progressMode == ProgressMode.Horizontal)
     {
         _totalValue = _image.rectTransform.rect.width;
     }
     else
     {
         _totalValue = _image.rectTransform.rect.height;
     }
 }
Example #10
0
        private static IProgress CreateAndStartProgress(string progressMessage, ProgressMode mode, Action stopRequestHandler, Action cancelRequestHandler)
        {
            var progress = new Progress();

            if (stopRequestHandler != null)
            {
                progress.CanBeStopped   = true;
                progress.StopRequested += (s, a) => stopRequestHandler();
            }
            if (cancelRequestHandler != null)
            {
                progress.CanBeCanceled    = true;
                progress.CancelRequested += (s, a) => cancelRequestHandler();
            }
            progress.Start(progressMessage, mode);
            return(progress);
        }
 public ProgressNotificationToken(Session session,
                                  IObserver <SyncProgress> observer,
                                  ProgressDirection direction,
                                  ProgressMode mode)
 {
     _session  = session;
     _observer = observer;
     _gcHandle = GCHandle.Alloc(this);
     try
     {
         _nativeToken = _session.Handle.RegisterProgressNotifier(GCHandle.ToIntPtr(_gcHandle), direction, mode);
     }
     catch
     {
         _gcHandle.Free();
         throw;
     }
 }
Example #12
0
 internal ProgressEventArgs(ProgressMode mode, int current, int total)
 {
     mProgressMode    = mode;
     mProgressCurrent = current;
     mProgressTotal   = total;
 }
Example #13
0
 /// <summary>
 /// Gets an <see cref="IObservable{T}"/> that can be used to track upload or download progress.
 /// </summary>
 /// <remarks>
 /// To start receiving notifications, you should call <see cref="IObservable{T}.Subscribe"/> on the returned object.
 /// The token returned from <see cref="IObservable{T}.Subscribe"/> should be retained as long as progress
 /// notifications are desired. To stop receiving notifications, call <see cref="IDisposable.Dispose"/>
 /// on the token.
 /// You don't need to keep a reference to the observable itself.
 /// The progress callback will always be called once immediately upon subscribing in order to provide
 /// the latest available status information.
 /// </remarks>
 /// <returns>An observable that you can subscribe to and receive progress updates.</returns>
 /// <param name="direction">The transfer direction (upload or download) to track in the subscription callback.</param>
 /// <param name="mode">The desired behavior of this progress notification block.</param>
 /// <example>
 /// <code>
 /// class ProgressNotifyingViewModel
 /// {
 ///     private IDisposable notificationToken;
 ///
 ///     public void ShowProgress()
 ///     {
 ///         var observable = session.GetProgressObservable(ProgressDirection.Upload, ProgressMode.ReportIndefinitely);
 ///         notificationToken = observable.Subscribe(progress =>
 ///         {
 ///             // Update relevant properties by accessing
 ///             // progress.TransferredBytes and progress.TransferableBytes
 ///         });
 ///     }
 ///
 ///     public void HideProgress()
 ///     {
 ///         notificationToken?.Dispose();
 ///         notificationToken = null;
 ///     }
 /// }
 /// </code>
 /// In this example we're using <see href="https://msdn.microsoft.com/en-us/library/ff402849(v=vs.103).aspx">ObservableExtensions.Subscribe</see>
 /// found in the <see href="https://github.com/Reactive-Extensions/Rx.NET">Reactive Extensions</see> class library.
 /// If you prefer not to take a dependency on it, you can create a class that implements <see cref="IObserver{T}"/>
 /// and use it to subscribe instead.
 /// </example>
 public IObservable <SyncProgress> GetProgressObservable(ProgressDirection direction, ProgressMode mode)
 {
     RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();
     return(null);
 }
Example #14
0
        public ulong RegisterProgressNotifier(GCHandle managedHandle, ProgressDirection direction, ProgressMode mode)
        {
            var isStreaming = mode == ProgressMode.ReportIndefinitely;
            var token       = NativeMethods.register_progress_notifier(this, GCHandle.ToIntPtr(managedHandle), direction, isStreaming, out var ex);

            ex.ThrowIfNecessary();
            return(token);
        }
Example #15
0
        public void Session_ProgressObservable_UnitTests(ProgressDirection direction, ProgressMode mode)
        {
            AsyncContext.Run(async() =>
            {
                var callbacksInvoked = 0;
                var completionTCS    = new TaskCompletionSource <ulong>();

                var config  = await SyncTestHelpers.GetFakeConfigAsync();
                var realm   = GetRealm(config);
                var session = GetSession(realm);

                session.SimulateProgress(0, 100, 0, 100);

                var observable = session.GetProgressObservable(direction, mode);
                var token      = observable.Subscribe(p =>
                {
                    try
                    {
                        callbacksInvoked++;

                        // .NET Core dislikes asserts in the callback so much it crashes.
                        if (p.TransferredBytes > p.TransferableBytes)
                        {
                            throw new Exception("TransferredBytes must be less than or equal to TransferableBytes");
                        }

                        if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                        {
                            if (p.TransferableBytes != 100)
                            {
                                throw new Exception("TransferableBytes must be equal to 100");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        completionTCS.TrySetException(e);
                    }

                    if (p.TransferredBytes == p.TransferableBytes)
                    {
                        completionTCS.TrySetResult(p.TransferredBytes);
                    }
                });

                using (token)
                {
                    session.SimulateProgress(50, 150, 50, 150);
                    await Task.Delay(50);

                    session.SimulateProgress(100, 200, 100, 200);
                    await Task.Delay(50);

                    session.SimulateProgress(150, 200, 150, 200);
                    await Task.Delay(50);

                    session.SimulateProgress(200, 200, 200, 200);
                    await Task.Delay(50);

                    var totalTransferred = await completionTCS.Task;

                    if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                    {
                        Assert.That(totalTransferred, Is.EqualTo(100));
                        Assert.That(callbacksInvoked, Is.EqualTo(3));
                    }
                    else
                    {
                        Assert.That(totalTransferred, Is.EqualTo(200));
                        Assert.That(callbacksInvoked, Is.EqualTo(5));
                    }
                }
            });
        }
Example #16
0
		protected void Notify(ProgressChangeHandler handler, ProgressMode mode, int current, int total)
		{
			ProgressHelper.Notify(handler, mode, current, total);
		}
Example #17
0
 protected Progress(string title, string label, bool isCancelable, ProgressMode mode) : base(title, label, isCancelable, mode, null)
 {
 }
Example #18
0
		internal ProgressEventArgs(ProgressMode mode, int current, int total)
		{
			mProgressMode = mode;
			mProgressCurrent = current;
			mProgressTotal = total;
		}
Example #19
0
        public void Session_ProgressObservable_IntegrationTests(ProgressMode mode)
        {
            const int ObjectSize      = 1000000;
            const int ObjectsToRecord = 2;

            AsyncContext.Run(async() =>
            {
                var realm            = await SyncTestHelpers.GetIntegrationRealm("progress");
                var completionTCS    = new TaskCompletionSource <ulong>();
                var callbacksInvoked = 0;

                var session    = realm.GetSession();
                var observable = session.GetProgressObservable(ProgressDirection.Upload, mode);

                for (var i = 0; i < ObjectsToRecord; i++)
                {
                    realm.Write(() =>
                    {
                        realm.Add(new HugeSyncObject(ObjectSize));
                    });
                }
                var token = observable.Subscribe(p =>
                {
                    try
                    {
                        callbacksInvoked++;

                        Assert.That(p.TransferredBytes, Is.LessThanOrEqualTo(p.TransferableBytes));

                        if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                        {
                            Assert.That(p.TransferableBytes, Is.GreaterThan(ObjectSize));
                            Assert.That(p.TransferableBytes, Is.LessThan((ObjectsToRecord + 1) * ObjectSize));
                        }
                    }
                    catch (Exception e)
                    {
                        completionTCS.TrySetException(e);
                    }

                    if (p.TransferredBytes == p.TransferableBytes)
                    {
                        completionTCS.TrySetResult(p.TransferredBytes);
                    }
                });

                realm.Write(() =>
                {
                    realm.Add(new HugeSyncObject(ObjectSize));
                });

                var totalTransferred = await completionTCS.Task;

                if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                {
                    Assert.That(totalTransferred, Is.GreaterThanOrEqualTo(ObjectSize));

                    // We add ObjectsToRecord + 1 items, but the last item is added after subscribing
                    // so in the fixed mode, we should not get updates for it.
                    Assert.That(totalTransferred, Is.LessThan((ObjectsToRecord + 1) * ObjectSize));
                }
                else
                {
                    Assert.That(totalTransferred, Is.GreaterThanOrEqualTo((ObjectsToRecord + 1) * ObjectSize));
                }

                Assert.That(callbacksInvoked, Is.GreaterThan(1));

                token.Dispose();
                realm.Dispose();
                Realm.DeleteRealm(realm.Config);
            });
        }
 public SyncProgressObservable(Session session, ProgressDirection direction, ProgressMode mode)
 {
     _session   = session;
     _direction = direction;
     _mode      = mode;
 }
Example #21
0
 /// <summary>
 /// Gets an <see cref="IObservable{T}"/> that can be used to track upload or download progress.
 /// </summary>
 /// <remarks>
 /// To start receiving notifications, you should call <see cref="IObservable{T}.Subscribe"/> on the returned object.
 /// The token returned from <see cref="IObservable{T}.Subscribe"/> should be retained as long as progress
 /// notifications are desired. To stop receiving notifications, call <see cref="IDisposable.Dispose"/>
 /// on the token.
 /// You don't need to keep a reference to the observable itself.
 /// The progress callback will always be called once immediately upon subscribing in order to provide
 /// the latest available status information.
 /// </remarks>
 /// <returns>An observable that you can subscribe to and receive progress updates.</returns>
 /// <param name="direction">The transfer direction (upload or download) to track in the subscription callback.</param>
 /// <param name="mode">The desired behavior of this progress notification block.</param>
 /// <example>
 /// <code>
 /// class ProgressNotifyingViewModel
 /// {
 ///     private IDisposable notificationToken;
 ///
 ///     public void ShowProgress()
 ///     {
 ///         var observable = session.GetProgressObservable(ProgressDirection.Upload, ProgressMode.ReportIndefinitely);
 ///         notificationToken = observable.Subscribe(progress =>
 ///         {
 ///             // Update relevant properties by accessing
 ///             // progress.TransferredBytes and progress.TransferableBytes
 ///         });
 ///     }
 ///
 ///     public void HideProgress()
 ///     {
 ///         notificationToken?.Dispose();
 ///         notificationToken = null;
 ///     }
 /// }
 /// </code>
 /// In this example we're using <see href="https://msdn.microsoft.com/en-us/library/ff402849(v=vs.103).aspx">ObservableExtensions.Subscribe</see>
 /// found in the <see href="https://github.com/Reactive-Extensions/Rx.NET">Reactive Extensions</see> class library.
 /// If you prefer not to take a dependency on it, you can create a class that implements <see cref="IObserver{T}"/>
 /// and use it to subscribe instead.
 /// </example>
 public IObservable <SyncProgress> GetProgressObservable(ProgressDirection direction, ProgressMode mode)
 {
     return(new SyncProgressObservable(this, direction, mode));
 }
Example #22
0
 public ProgressViewModel(Progress <T> progress, ProgressMode mode) : base(mode)
 {
     Progress = progress;
 }
 public string ModeFormat(ProgressMode pos) => ModeToNames[pos];
Example #24
0
 protected void Notify(ProgressChangeHandler handler, ProgressMode mode, int current, int total)
 {
     ProgressHelper.Notify(handler, mode, current, total);
 }
Example #25
0
        public ulong RegisterProgressNotifier(IntPtr tokenPtr, ProgressDirection direction, ProgressMode mode)
        {
            NativeException ex;
            var             isStreaming = mode == ProgressMode.ReportIndefinitely;
            var             token       = NativeMethods.register_progress_notifier(this, tokenPtr, direction, isStreaming, out ex);

            ex.ThrowIfNecessary();
            return(token);
        }
Example #26
0
        public void Session_ProgressObservable_IntegrationTests(ProgressMode mode)
        {
            const int ObjectSize      = 1000000;
            const int ObjectsToRecord = 2;

            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var config      = await GetIntegrationConfigAsync("progress");
                using var realm = GetRealm(config);

                var completionTCS    = new TaskCompletionSource <ulong>();
                var callbacksInvoked = 0;

                var session = GetSession(realm);

                var observable = session.GetProgressObservable(ProgressDirection.Upload, mode);

                for (var i = 0; i < ObjectsToRecord; i++)
                {
                    realm.Write(() =>
                    {
                        realm.Add(new HugeSyncObject(ObjectSize));
                    });
                }

                using var token = observable.Subscribe(p =>
                {
                    try
                    {
                        callbacksInvoked++;

                        if (p.TransferredBytes > p.TransferableBytes)
                        {
                            // TODO https://github.com/realm/realm-dotnet/issues/2360: this seems to be a regression in Sync.
                            // throw new Exception($"Expected: {p.TransferredBytes} <= {p.TransferableBytes}");
                        }

                        if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                        {
                            if (p.TransferableBytes <= ObjectSize ||
                                p.TransferableBytes >= (ObjectsToRecord + 2) * ObjectSize)
                            {
                                throw new Exception($"Expected: {p.TransferableBytes} to be in the ({ObjectSize}, {(ObjectsToRecord + 1) * ObjectSize}) range.");
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        completionTCS.TrySetException(e);
                    }

                    if (p.TransferredBytes >= p.TransferableBytes)
                    {
                        completionTCS.TrySetResult(p.TransferredBytes);
                    }
                });

                realm.Write(() =>
                {
                    realm.Add(new HugeSyncObject(ObjectSize));
                });

                var totalTransferred = await completionTCS.Task;

                if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                {
                    Assert.That(totalTransferred, Is.GreaterThanOrEqualTo(ObjectSize));

                    // We add ObjectsToRecord + 1 items, but the last item is added after subscribing
                    // so in the fixed mode, we should not get updates for it.
                    Assert.That(totalTransferred, Is.LessThan((ObjectsToRecord + 5) * ObjectSize));
                }
                else
                {
                    Assert.That(totalTransferred, Is.GreaterThanOrEqualTo((ObjectsToRecord + 1) * ObjectSize));
                }

                Assert.That(callbacksInvoked, Is.GreaterThan(1));
            });
        }
Example #27
0
 internal ProgressEventArgs()
 {
     mProgressMode = ProgressMode.DontNotify;
 }
Example #28
0
        public void Session_ProgressObservable_UnitTests(ProgressDirection direction, ProgressMode mode)
        {
            AsyncContext.Run(async() =>
            {
                var callbacksInvoked = 0;
                var completionTCS    = new TaskCompletionSource <ulong>();

                var realm   = await SyncTestHelpers.GetFakeRealm(isUserAdmin: true);
                var session = realm.GetSession();

                session.SimulateProgress(0, 100, 0, 100);

                var observable = session.GetProgressObservable(direction, mode);
                var token      = observable.Subscribe(p =>
                {
                    try
                    {
                        callbacksInvoked++;

                        Assert.That(p.TransferredBytes, Is.LessThanOrEqualTo(p.TransferableBytes));

                        if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                        {
                            Assert.That(p.TransferableBytes, Is.EqualTo(100));
                        }
                    }
                    catch (Exception e)
                    {
                        completionTCS.TrySetException(e);
                    }

                    if (p.TransferredBytes == p.TransferableBytes)
                    {
                        completionTCS.TrySetResult(p.TransferredBytes);
                    }
                });

                session.SimulateProgress(50, 150, 50, 150);
                await Task.Delay(50);

                session.SimulateProgress(100, 200, 100, 200);
                await Task.Delay(50);

                session.SimulateProgress(150, 200, 150, 200);
                await Task.Delay(50);

                session.SimulateProgress(200, 200, 200, 200);
                await Task.Delay(50);

                var totalTransferred = await completionTCS.Task;

                if (mode == ProgressMode.ForCurrentlyOutstandingWork)
                {
                    Assert.That(totalTransferred, Is.EqualTo(100));
                    Assert.That(callbacksInvoked, Is.EqualTo(3));
                }
                else
                {
                    Assert.That(totalTransferred, Is.EqualTo(200));
                    Assert.That(callbacksInvoked, Is.EqualTo(5));
                }

                token.Dispose();
                realm.Dispose();
                Realm.DeleteRealm(realm.Config);
            });
        }
 internal ProgressEventArgs()
 {
     mProgressMode = ProgressMode.DontNotify;
 }