예제 #1
0
 protected virtual Task <bool> UploadPhotoItem(UploadRequest request)
 {
     return(base.ExecuteFunctionAsync("UploadPhotoItem", async delegate()
     {
         try
         {
             AmazonUploadInfo info = await PerformUploadPhotoToAmazon(request);
             if (info != null && info.Success)
             {
                 if (request.CancelRequested)
                 {
                     return false;
                 }
                 if (request.IsPreUpload)
                 {
                     return true;
                 }
                 return await PerformAddUploadedPhotoToApp(request, info);
             }
             return false;
         }
         catch (Exception ex)
         {
             base.LogError(ex, "UploadPhotoItem");
             return false;
         }
     }));
 }
예제 #2
0
 public virtual void EnqueueRequest(UploadRequest request)
 {
     base.ExecuteMethod("EnqueueRequest", delegate()
     {
         Task taskToStart          = null;
         UploadToast toastToNotify = null;
         lock (_RequestSyncRoot)
         {
             this.FailedRequests.Remove(request); //jic
             this.Requests.Add(request);
             if (this.UploadTask == null || this.UploadTask.IsCompleted)
             {
                 this.UploadTaskCancellationToken = new CancellationTokenSource();
                 taskToStart     = new Task(DoUploads, UploadTaskCancellationToken.Token);
                 this.UploadTask = taskToStart;
             }
             else
             {
                 this.CurrentTotal++;
                 toastToNotify = this.CurrentToast;
             }
         }
         if (toastToNotify != null && toastToNotify.IsProcessing)
         {
             string format = this.UploadingFormat;
             if (this.CurrentTotal == 1)
             {
                 format = this.UploadingSingleFormat;
             }
             if (toastToNotify.Attempt > 1)
             {
                 format = this.RetryingFormat;
             }
             if (request.IsPhoto)
             {
                 toastToNotify.Message = string.Format(format, this.PhotoCaption, toastToNotify.CurrentIndex, this.CurrentTotal);
             }
             else
             {
                 toastToNotify.Message = string.Format(format, this.VideoCaption, toastToNotify.CurrentIndex, this.CurrentTotal);
             }
             this.CurrentToast = toastToNotify;
         }
         if (taskToStart != null)
         {
             taskToStart.Start(TaskScheduler.Default);
         }
     });
 }
예제 #3
0
        protected virtual Task <bool> DoUploadItem(UploadRequest request, UploadToast uploadToast)
        {
            return(base.ExecuteFunctionAsync("DoUploadItem", async delegate()
            {
                bool success = false;
                request.UploadAttempt = 1;
                while (!success && !request.CancelRequested && (request.UploadAttempt < this.MaxUploadAttempts))
                {
                    request.UploadAttempt++;
                    if (uploadToast != null)
                    {
                        uploadToast.Attempt = request.UploadAttempt;
                    }

                    request.InvokeProcessingActions();


                    if (request.IsPhoto)
                    {
                        success = await UploadPhotoItem(request);
                    }
                    else
                    {
                        success = await UploadVideoItem(request);
                    }
                }
                request.Succeeded = success;
                request.InvokeProcessingActions();

                if (request.StrongCallbackAction != null)
                {
                    await request.StrongCallbackAction(request);
                }
                if (request.WeakCallbackAction != null)
                {
                    Func <UploadRequest, Task> action = null;
                    request.WeakCallbackAction.TryGetTarget(out action);
                    if (action != null)
                    {
                        await action(request);
                    }
                }
                request.StrongCallbackAction = null;
                request.WeakCallbackAction = null;
                return success;
            }));
        }
예제 #4
0
 public virtual bool DequeueRequest(UploadRequest request)
 {
     return(base.ExecuteFunction("DequeueRequest", delegate()
     {
         bool result = false;
         lock (_RequestSyncRoot)
         {
             if (this.Requests.Remove(request))
             {
                 result = true;
             }
             if (this.FailedRequests.Remove(request))
             {
                 result = true;
             }
         }
         return result;
     }));
 }
예제 #5
0
 public List <UploadRequest> GetRequests()
 {
     return(base.ExecuteFunction("GetRequests", delegate()
     {
         List <UploadRequest> result = new List <UploadRequest>();
         UploadRequest request = this.CurrentToastRequest;
         if (request != null)
         {
             result.Add(request);
         }
         if (this.Requests.Count > 0 || this.FailedRequests.Count > 0) // allow for a lil leeway for ui locking performance
         {
             lock (_RequestSyncRoot)
             {
                 result.AddRange(this.Requests);
                 result.AddRange(this.FailedRequests);
             }
         }
         return result;
     }));
 }
예제 #6
0
 protected abstract Task <bool> PerformAddUploadedPhotoToApp(UploadRequest request, AmazonUploadInfo info);
예제 #7
0
 protected abstract Task <AmazonUploadInfo> PerformUploadPhotoToAmazon(UploadRequest request);
예제 #8
0
        protected virtual void DoUploads()
        {
            try
            {
                base.ExecuteMethod("DoUploads", delegate()
                {
                    // Get First
                    UploadRequest nextRequest = null;
                    CurrentTotal = 0;
                    int current  = 0;
                    lock (_RequestSyncRoot)
                    {
                        if (this.Requests.Count > 0)
                        {
                            CurrentTotal = this.Requests.Count;
                            nextRequest  = this.Requests[0];

                            this.Requests.RemoveAt(0);
                            current++;
                        }
                    }
                    if (nextRequest == null)
                    {
                        return;
                    }
                    try
                    {
                        this.OnStarted();

                        bool hadError            = false;
                        string latestCachePrefix = "";

                        UploadToast lastToast = null;

                        // Iterate
                        while (nextRequest != null)
                        {
                            UploadRequest request = nextRequest;
                            nextRequest           = null; // while breaker

                            this.CurrentToastRequest = request;


                            latestCachePrefix = request.DataCachePrefix;

                            // Notify About Process
                            UploadToast uploadToast = new UploadToast()
                            {
                                TimeOutSeconds     = 0,
                                CurrentIndex       = current,
                                IsProcessing       = true,
                                NativeImagePreview = request.NativeImagePreview
                            };
                            request.RelatedToast = uploadToast;
                            string format        = this.UploadingFormat;
                            if (this.CurrentTotal == 1)
                            {
                                format = this.UploadingSingleFormat;
                            }
                            if (uploadToast.Attempt > 1)
                            {
                                format = RetryingFormat;
                            }
                            if (request.IsPhoto)
                            {
                                uploadToast.Message = string.Format(format, this.PhotoCaption, current, CurrentTotal);
                            }
                            else
                            {
                                uploadToast.Message = string.Format(format, this.VideoCaption, current, CurrentTotal);
                            }

                            this.CurrentToast = uploadToast;
                            lastToast         = uploadToast;

                            // Process
                            Task <bool> uploadTask = this.DoUploadItem(request, uploadToast);
                            Task.WaitAll(new Task[] { uploadTask });
                            bool success = uploadTask.Result;
                            if (!success)
                            {
                                hadError = true;
                            }


                            // Get Next
                            lock (_RequestSyncRoot)
                            {
                                if (!success)
                                {
                                    this.FailedRequests.Add(request);
                                }
                                if (this.Requests.Count > 0)
                                {
                                    nextRequest = this.Requests[0];
                                    this.Requests.RemoveAt(0);
                                    current++;
                                }
                                else
                                {
                                    this.UploadTask = null; // weird, we clear our own reference so that new ones can be spawned
                                }
                            }
                        }
                        this.CurrentToastRequest = null;

                        // Notify complete
                        UploadToast finishedToast = lastToast;
                        if (finishedToast == null)
                        {
                            finishedToast = new UploadToast();
                        }
                        finishedToast.IsProcessing = false;
                        finishedToast.Message      = this.UploadCompleteCaption;

                        if (hadError)
                        {
                            finishedToast.IsError = true;
                            finishedToast.Message = this.UploadErrorCaption;
                        }
                        else
                        {
                            finishedToast.IsComplete     = true;
                            finishedToast.TimeOutSeconds = 4;
                        }

                        this.CurrentToast = finishedToast;

                        if (!string.IsNullOrEmpty(latestCachePrefix))
                        {
                            this.InvalidateCacheForPrefix(latestCachePrefix);
                        }
                    }
                    finally
                    {
                        this.OnStopped();
                    }
                });
            }
            catch (OperationCanceledException)
            {
                //gulp
            }
        }