public void downloadfile() { indicator.IsRunning = true; download.IsEnabled = false; message = DependencyService.Get <Imessaging>(); try { if (string.IsNullOrEmpty(media.filePath)) { return; } indicator.IsRunning = true; download.IsEnabled = false; var current = Connectivity.NetworkAccess; if (current != NetworkAccess.Internet) { message.LongAlert("No Internet Connection"); return; } progress.IsVisible = true; //PlayButton.IsEnabled = false; dependency = DependencyService.Get <IFileHelper>(); message = DependencyService.Get <Imessaging>(); var filename = media.id; var all = App.Database.GetDownloadedAudio().Result.FirstOrDefault(x => x.name == filename); if (all != null) { message.LongAlert("This Audio has already been downloaded, please check your Downloads"); indicator.IsRunning = false; download.IsEnabled = true; // PlayButton.IsEnabled = true; return; } WebClient client = new WebClient(); var dir = dependency.GetLocalFilePath(); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (string.IsNullOrEmpty(media.filePath)) { return; } Uri uri = new Uri(media.filePath); client.OpenRead(uri); var bytes_total = client.ResponseHeaders["Content_Lenght"]; client.DownloadFileAsync(uri, $"{dir}/{filename}.mp3"); client.DownloadFileCompleted += Client_DownloadFileCompleted; client.DownloadProgressChanged += Client_DownloadProgressChanged; } catch (Exception ex) { message.LongAlert("Something went wrong please try again"); } }
public async Task <IEnumerable <Feeds> > GetAllChurchFeeds(string TenantId, string userId = "") { try { if (Connectivity.NetworkAccess != NetworkAccess.Internet && !Barrel.Current.IsExpired(key: url)) { await Task.Yield(); toast.LongAlert("Please check your internet connection"); return(Barrel.Current.Get <IEnumerable <Feeds> >(key: url)); } Guid id; if (userId == string.Empty) { id = Guid.Empty; } else { id = Guid.Parse(userId); } HttpClient client = new HttpClient(); // var response = await Policy //.Handle<HttpRequestException>() //.WaitAndRetry(retryCount: 3, sleepDurationProvider: (attempt) => TimeSpan.FromSeconds(5)) //.Execute(async () => await client.GetAsync($"{getfeedsUrl + TenantId}&userId={id}")); var response = await client.GetAsync($"{getfeedsUrl + TenantId}&userId={id}"); HttpContent httpContent = response.Content; var json = await httpContent.ReadAsStringAsync(); Preferences.Set("AllFeeds", json); var feeds = JsonConvert.DeserializeObject <IEnumerable <Feeds> >(json); //Saves the cache and pass it a timespan for expiration Barrel.Current.Add(key: $"{getfeedsUrl + TenantId}&userId={id}", data: feeds, expireIn: TimeSpan.FromDays(1)); return(feeds); } catch (Exception ex) { return(null); } }