/// <summary> /// Function for other classes to poll to get the status of this service /// </summary> /// <returns></returns> public static DownloadsStatusUpdate GetStatus() { string message = $"Downloading article {TotalNumArticles - DownloadQueue.Count} out of {TotalNumArticles}."; if (DownloadQueue.Count == 0) { message = "There are currently no articles queued for download."; } DownloadsStatusUpdate update; // We need to lock here so that the queue consumer does not remove from the list while we are trying to make a copy of it to send away. // We have to make a copy because there are places where we iterate though the list of articles left to download to copy them into an observable, // and without a copy this service might make a change while it is copying lock (queueLock) { update = new DownloadsStatusUpdate { StatusMessage = message, TotalNumArticlesToDownload = TotalNumArticles, ArticlesLeftToDownload = DownloadQueue.ToList() }; } return(update); }
/// <summary> /// Function to update all properties of this page with new information from the download service /// </summary> /// <param name="update"></param> public async void UpdateDownloadStatus(DownloadsStatusUpdate update) { StatusText.Text = update.StatusMessage; await DownloadProgress.ProgressTo((float)update.NumberOfArticlesDownloaded / (float)update.TotalNumArticlesToDownload, 100, Easing.Linear); // Load all the articles left to download in the articles left to download // TODO: Make this faster by instead returning the articles that was just downloaded and removing it from the list downloads.Clear(); foreach (string article in update.ArticlesLeftToDownload) { downloads.Add(article); } }
public DownloadsPage() { InitializeComponent(); GoBackCommand = new Command(GoBack); backButton.Command = GoBackCommand; DownloadList.ItemsSource = DownloadListItems; DownloadsStatusUpdate update = PersistentDownloadService.GetStatus(); if (update.TotalNumArticlesToDownload == 0) { StatusText.Text = "There are currently no articles queued for download."; DownloadProgress.Progress = 0.0; } UpdateDownloadStatus(update); PersistentDownloadService.AddStatusCallBack(UpdateDownloadStatus); }
/// <summary> /// Update all subscribed callbacks with the status of the service /// </summary> private static void UpdateStatusCallbacks(string message) { if (DownloadQueue.Count == 0) { message = "There are currently no articles queued for download."; } DownloadsStatusUpdate update; lock (queueLock) { update = new DownloadsStatusUpdate { StatusMessage = message, TotalNumArticlesToDownload = TotalNumArticles, ArticlesLeftToDownload = DownloadQueue.ToList() }; } foreach (Action <DownloadsStatusUpdate> action in subscribedStatusCallbacks) { Device.BeginInvokeOnMainThread(() => action?.Invoke(update)); } }