/// <summary>
        /// After recreation, the host activity call this method to executes all pending ui operation.
        /// </summary>
        /// <returns><c>true</c>, if one or more pending ui operation was executed, <c>false</c> otherwise.</returns>
        /// <param name="currentActiviy">Current activiy.</param>
        public bool ExecutePendingUIOperation(IAsyncTaskActivity currentActiviy)
        {
            bool hasPendingUIOp = false;

            pendingUIOperationList.RemoveAll(opItem =>
            {
                if (!IsCurrentUserOperation(opItem))
                {
                    // The task is not belong to current user,
                    // just remove it.
                    return(true);
                }

                bool match = opItem.Item1 != null &&
                             opItem.Item1.AsyncTaskActivityGUID == currentActiviy.AsyncTaskActivityGUID;
                if (!match && opItem.Item3 != null)
                {
                    match = opItem.Item3(currentActiviy);
                }

                if (match)
                {
                    Application.SynchronizationContext.Post(_ =>
                                                            opItem.Item2(currentActiviy), null);
                    hasPendingUIOp = true;
                    return(true);
                }

                return(false);
            });

            return(hasPendingUIOp);
        }
Exemplo n.º 2
0
        /*
         * public async Task<DownloadResult> DownloadPublicationByBookId(int bookId, CancellationToken cancelToken, DownloadProgressEventHandler downloadHandler, bool checkNetLimitation = true)
         * {
         *      return await Task.Run(delegate{
         *              for(int i = 0; i < 100; ++i)
         *              {
         *                      if(cancelToken.IsCancellationRequested)
         *                      {
         *                              return new DownloadResult(){
         *                                      DownloadStatus = DownLoadEnum.Canceled,
         *                                      Publication = GetPublication(bookId).Value,
         *                              };
         *                      }
         *
         *                      Thread.Sleep(100);
         *                      if(cancelToken.IsCancellationRequested)
         *                      {
         *                              return new DownloadResult(){
         *                                      DownloadStatus = DownLoadEnum.Canceled,
         *                                      Publication = GetPublication(bookId).Value,
         *                              };
         *                      }
         *
         *                      downloadHandler(i, 100);
         *              }
         *
         *              return new DownloadResult(){
         *                      DownloadStatus = DownLoadEnum.Success,
         *                      Publication = GetPublication(bookId).Value,
         *              };
         *      });
         * }
         */

        private void OnPublicationDownloadProgress(IAsyncTaskActivity currentActivity, int bookId, int progress)
        {
            var foundPub = PublicationList.Find(p => p.Value.BookId == bookId);

            if (foundPub == null)
            {
                return;
            }

            var foundExtInfo = publicationExtInfoList.Find(p => bookId == p.BookId);

            if (foundExtInfo == null)
            {
                return;
            }

            if (progress != 100 && progress - foundExtInfo.DownloadingProgress < 2)
            {
                return;
            }

            foundExtInfo.DownloadingProgress = progress;

            ((IDownloadPublicationTaskListener)currentActivity).UpdatePublicationDownloadingProgress(bookId);
        }
 public void UnregisterAsyncTaskActivity(IAsyncTaskActivity activity)
 {
     if (!currentResponsibleActivityList.Remove(activity))
     {
         throw new ArgumentException("Unable to find IAsyncTaskActivity which should be remove.", "activity");
     }
 }
        /// <summary>
        /// Submits an async ui operation to AsyncUIOperationRepeater;
        /// The AsyncUIOperationRepeater will check the status of host activity to
        /// decide if execute the ui operation immediately. If the ui operation can
        /// execute now, the AsyncUIOperationRepeater will cache the ui operation.
        /// After recreation, the activity can call ExecutePendingUIOperation in OnCreate
        /// to execute all pending ui operation.
        /// </summary>
        /// <param name="currentActiviy">Current activiy</param>
        /// <param name="op">UI operation</param>
        /// <param name="discardable">If set to <c>true</c>, the ui operation will be discarded if
        /// the operation can't be execute immediately.</param>
        /// <param name = "addtionalMatch">Addtional critrial</param>
        public void SubmitAsyncUIOperation(
            IAsyncTaskActivity currentActiviy,
            Action <IAsyncTaskActivity> op,
            bool discardable = false,
            Predicate <IAsyncTaskActivity> addtionalMatch = null)
        {
            var found = currentResponsibleActivityList.Find(a =>
            {
                if (currentActiviy != null &&
                    a.AsyncTaskActivityGUID == currentActiviy.AsyncTaskActivityGUID)
                {
                    return(true);
                }
                else if (addtionalMatch != null)
                {
                    return(addtionalMatch(a));
                }

                return(false);
            });

            if (found != null)
            {
                Application.SynchronizationContext.Post(_ => {
                    if (currentResponsibleActivityList.Contains(found))
                    {
                        op(found);
                    }
                    else if (!discardable)
                    {
                        pendingUIOperationList.Add(
                            new Tuple <IAsyncTaskActivity,
                                       Action <IAsyncTaskActivity>,
                                       Predicate <IAsyncTaskActivity>,
                                       string, string>(
                                currentActiviy, op, addtionalMatch,
                                GetCurrentUserEmail(),
                                GetCurrentUserCountryCode()));
                    }
                }, null);
                return;
            }

            if (!discardable)
            {
                pendingUIOperationList.Add(
                    new Tuple <IAsyncTaskActivity,
                               Action <IAsyncTaskActivity>,
                               Predicate <IAsyncTaskActivity>,
                               string, string>(
                        currentActiviy, op, addtionalMatch,
                        GetCurrentUserEmail(),
                        GetCurrentUserCountryCode()));
            }
        }
        public void DiscardActivitySpecificUIOperation(IAsyncTaskActivity currentActiviy)
        {
            pendingUIOperationList.RemoveAll(opItem =>
            {
                if (!IsCurrentUserOperation(opItem))
                {
                    // The task is not belong to current user,
                    // just remove it.
                    return(true);
                }

                return(opItem.Item3 == null && opItem.Item1 != null &&
                       opItem.Item1.AsyncTaskActivityGUID == currentActiviy.AsyncTaskActivityGUID);
            });
        }
        public bool HavePendingUIOperation(IAsyncTaskActivity currentActiviy)
        {
            return(null != pendingUIOperationList.Find(opItem =>
            {
                if (!IsCurrentUserOperation(opItem))
                {
                    return false;
                }

                bool match = opItem.Item1 != null &&
                             opItem.Item1.AsyncTaskActivityGUID == currentActiviy.AsyncTaskActivityGUID;
                if (!match && opItem.Item3 != null)
                {
                    match = opItem.Item3(currentActiviy);
                }

                return match;
            }));
        }
        public void RegisterAsyncTaskActivity(IAsyncTaskActivity activity)
        {
            var found = currentResponsibleActivityList.Find(a =>
                                                            a.AsyncTaskActivityGUID == activity.AsyncTaskActivityGUID);

            if (found != null)
            {
                if (found == activity)
                {
                    return;
                }
                else
                {
                    throw new ArgumentException("2 IAsyncTaskActivity has same guid.", "activity");
                }
            }

            currentResponsibleActivityList.Add(activity);
        }
Exemplo n.º 8
0
        public async void DownloadPublication(IAsyncTaskActivity activity, int bookId, bool force)
        {
            if (IsDownloading(bookId))
            {
                throw new ArgumentException("The required book is downloading.", "bookId");
            }

            var extInfo = publicationExtInfoList.Find(p => bookId == p.BookId);

            extInfo.CancellationSource  = new CancellationTokenSource();
            extInfo.DownloadingProgress = 0;

            var downloadTask = PublicationUtil.Instance.DownloadPublicationByBookId(
                bookId,
                extInfo.CancellationSource.Token,
                (int progress, long downloadSize) =>
                AsyncUIOperationRepeater.INSTATNCE.SubmitAsyncUIOperation(
                    activity,
                    currentActivity => OnPublicationDownloadProgress(currentActivity, bookId, progress),
                    true,
                    currentActivity => currentActivity is IDownloadPublicationTaskListener),
                !force);

            AsyncTaskManager.INSTATNCE.RegisterTask(downloadTask, PublicationExtInfo.DOWNLOAD_PUBLICATION_TASK, bookId);

            ((IDownloadPublicationTaskListener)activity).UpdatePublicationDownloadingProgress(bookId);

            var result = await downloadTask;

            if (!AsyncTaskManager.INSTATNCE.IsBelongToCurrentUser(downloadTask))
            {
                // The task owner has logged off
                return;
            }

            // The CancellationSource is useless now.
            extInfo.CancellationSource.Dispose();
            extInfo.CancellationSource = null;

            // All status about download publication should be update here;
            // Because the AsyncUIOperation will be set as discardable;
            // If the download result is coming and the Activity is not
            // showing on the screen, the all status about the result will be lost;
            // Like the bug: http://wiki.lexiscn.com/issues/13206.
            if (result.DownloadStatus == DownLoadEnum.Success)
            {
                DataCache.INSTATNCE.PublicationManager.UpdatePublication(result.Publication);
                DataCache.INSTATNCE.PublicationManager.SetDownloadProgress(result.Publication.BookId, 100);
            }
            else if (result.DownloadStatus == DownLoadEnum.Canceled ||
                     result.DownloadStatus == DownLoadEnum.Failure ||
                     result.DownloadStatus == DownLoadEnum.NetDisconnected)
            {
                DataCache.INSTATNCE.PublicationManager.SetDownloadFailed(bookId);
            }

            /*
             * else if(result.DownloadStatus == DownLoadEnum.NetDisconnected)
             * {
             *      DataCache.INSTATNCE.PublicationManager.SetDownloadProgress(bookId, 0);
             * }
             */

            AsyncTaskManager.INSTATNCE.UnregisterTask(downloadTask);

            AsyncUIOperationRepeater.INSTATNCE.SubmitAsyncUIOperation(
                activity,
                currentActivity =>
            {
                var pub = DataCache.INSTATNCE.PublicationManager.GetPublication(bookId);
                if (pub == null)
                {
                    return;
                }

                if (pub.Value.PublicationStatus == PublicationStatusEnum.Downloaded)
                {
                    // to fix the download status due to unknown reason.
                    // releated bug: http://wiki.lexiscn.com/issues/12947
                    result.DownloadStatus = DownLoadEnum.Success;
                }

                ((IDownloadPublicationTaskListener)currentActivity).ProcessDownloadResult(result, bookId);
            },
                true,
                currentActivity => currentActivity is IDownloadPublicationTaskListener
                );
        }
 public bool IsActivityActive(IAsyncTaskActivity activity)
 {
     return(currentResponsibleActivityList.Contains(activity));
 }