/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void bedtimeResistanceButtonBottom_Clicked(object sender, EventArgs e) { if ((sender as Button) != null) { (sender as Button).IsEnabled = false; } string returnString = ViewTools.CommaSeparatedValue("Data,Value", "Intervention,Bedtime Resistance", bedtimeResistanceStackContent, taskModels, startTime, DateTime.Now.Subtract(startTime)); Debug.WriteLineIf(App.Debugging, returnString); int result = await App.Database.SaveItemAsync(new StorageModel() { CSV = returnString, Intervention = "Bedtime Resistance" }); if (CrossConnectivity.Current.IsConnected) { CancellationTokenSource cancelSrc = new CancellationTokenSource(); ProgressDialogConfig config = new ProgressDialogConfig() .SetTitle("Uploading to Server") .SetIsDeterministic(false) .SetMaskType(MaskType.Black) .SetCancel(onCancel: cancelSrc.Cancel); using (IProgressDialog progress = UserDialogs.Instance.Progress(config)) { await DropboxServer.UploadFile(new System.IO.MemoryStream(Encoding.UTF8.GetBytes(returnString)), App.Database.GetLargestID()); await Task.Delay(100); } } App.RefreshServer = true; await Navigation.PopAsync(); }
/// <summary> /// Force re-sync of data /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ToolbarItem_Clicked(object sender, EventArgs e) { Debug.WriteLineIf(App.Debugging, "Update_Clicked()"); if (!CrossConnectivity.Current.IsConnected) { return; } //int count = 0; CancellationTokenSource cancelSrc = new CancellationTokenSource(); ProgressDialogConfig config = new ProgressDialogConfig() .SetTitle("Syncing with server") .SetIsDeterministic(false) .SetMaskType(MaskType.Black) .SetCancel(onCancel: cancelSrc.Cancel); using (IProgressDialog progress = UserDialogs.Instance.Progress(config)) { try { ListFolderResult serverFiles = await DropboxServer.CountIndividualFiles(); List <StorageModel> currentData = await App.Database.GetDataAsync(); if (serverFiles == null || currentData == null || cancelSrc.IsCancellationRequested) { // Nothing.. just move on } else if (currentData.Count == serverFiles.Entries.Count) { // Same.. no worries } else if (currentData.Count > serverFiles.Entries.Count) { List <int> localIds = currentData.Select(l => l.ID).ToList(); List <string> remoteIdsStr = serverFiles.Entries.Select(r => r.Name).ToList(); remoteIdsStr = remoteIdsStr.Select(r => r.Split('_')[1]).ToList(); remoteIdsStr = remoteIdsStr.Select(r => r.Split('.')[0]).ToList(); List <int> remoteIds = remoteIdsStr.Select(r => int.Parse(r)).ToList(); var missing = localIds.Except(remoteIds); for (int count = 0; count < missing.Count() && !cancelSrc.IsCancellationRequested; count++) { if (cancelSrc.IsCancellationRequested) { continue; } else { await Task.Delay(App.DropboxDeltaTimeout); } progress.Title = "Uploading File " + (count + 1) + " of " + missing.Count().ToString(); var mStorageModel = currentData.Single(m => m.ID == missing.ElementAt(count)); await DropboxServer.UploadFile(new System.IO.MemoryStream(Encoding.UTF8.GetBytes(mStorageModel.CSV)), mStorageModel.ID); } } } catch (Exception exc) { Debug.WriteLineIf(App.Debugging, exc.ToString()); } try { ListFolderResult serverFiles = await DropboxServer.CountFeedback(); List <SleepFeedbackModel> currentData = await App.Database.GetFeedbackAsync(); if (serverFiles == null || currentData == null || cancelSrc.IsCancellationRequested) { // Nothing.. just move on } else if (currentData.Count == serverFiles.Entries.Count) { // Same.. no worries } else if (currentData.Count > serverFiles.Entries.Count) { List <int> localIds = currentData.Select(l => l.ID).ToList(); List <string> remoteIdsStr = serverFiles.Entries.Select(r => r.Name).ToList(); remoteIdsStr = remoteIdsStr.Select(r => r.Split('_')[1]).ToList(); remoteIdsStr = remoteIdsStr.Select(r => r.Split('.')[0]).ToList(); List <int> remoteIds = remoteIdsStr.Select(r => int.Parse(r)).ToList(); var missing = localIds.Except(remoteIds); for (int count = 0; count < missing.Count() && !cancelSrc.IsCancellationRequested; count++) { if (cancelSrc.IsCancellationRequested) { continue; } else { await Task.Delay(App.DropboxDeltaTimeout); } progress.Title = "Uploading Feedback " + (count + 1) + " of " + missing.Count().ToString(); var mStorageModel = currentData.Single(m => m.ID == missing.ElementAt(count)); await DropboxServer.UploadFeedback(new System.IO.MemoryStream(Encoding.UTF8.GetBytes(mStorageModel.CSV)), mStorageModel.ID); } } } catch (Exception exc) { Debug.WriteLineIf(App.Debugging, exc.ToString()); } } }
/// <summary> /// Load Stuff /// </summary> public async void LoadAssets() { // Skip loading if no internet if (!CrossConnectivity.Current.IsConnected) { App.Current.MainPage = new NavigationPage(new TaskPageStart()); } Debug.WriteLineIf(App.Debugging, "LoadAssets()"); CancellationTokenSource cancelSrc = new CancellationTokenSource(); ProgressDialogConfig config = new ProgressDialogConfig() .SetTitle("Contacting Server") .SetIsDeterministic(false) .SetMaskType(MaskType.Black) .SetCancel(onCancel: cancelSrc.Cancel); using (IProgressDialog progress = UserDialogs.Instance.Progress(config)) { try { if (App.UpdatingAttempts) { progress.Title = "Downloading manifest"; var mManifest = await App.Database.GetManifestAsync(); if (mManifest != null && mManifest.Count == 1) { App.MainManifest = JsonConvert.DeserializeObject <Manifest>(mManifest.First().JSON); } else { App.MainManifest = null; } progress.Title = "Parsing Manifest"; await DropboxServer.DownloadManifest(App.MainManifest); } progress.Title = "Polling local database"; List <StorageModel> currentItems = null; try { currentItems = await App.Database.GetDataAsync(); } catch (Exception e) { currentItems = null; Debug.WriteLineIf(App.Debugging, e.ToString()); } progress.Title = "Polling remote database"; ListFolderResult serverFiles = await DropboxServer.CountIndividualFiles(); int count = 0; if (serverFiles == null || currentItems == null || cancelSrc.IsCancellationRequested) { // Nothing.. just move on } else if (currentItems.Count == serverFiles.Entries.Count) { // Same.. no worries } else if (currentItems.Count > serverFiles.Entries.Count) { List <int> localIds = currentItems.Select(l => l.ID).ToList(); List <string> remoteIdsStr = serverFiles.Entries.Select(r => r.Name).ToList(); remoteIdsStr = remoteIdsStr.Select(r => r.Split('_')[1]).ToList(); remoteIdsStr = remoteIdsStr.Select(r => r.Split('.')[0]).ToList(); List <int> remoteIds = remoteIdsStr.Select(r => int.Parse(r)).ToList(); var missing = localIds.Except(remoteIds); foreach (int index in missing) { if (cancelSrc.IsCancellationRequested) { continue; } progress.Title = "Uploading File " + (count + 1) + " of " + missing.Count().ToString(); var mStorageModel = currentItems.Single(m => m.ID == index); await DropboxServer.UploadFile(new System.IO.MemoryStream(Encoding.UTF8.GetBytes(mStorageModel.CSV)), mStorageModel.ID); await Task.Delay(App.DropboxDeltaTimeout); count++; } } } catch (Exception e) { Debug.WriteLineIf(App.Debugging, e.ToString()); } App.Current.MainPage = new NavigationPage(new TaskPageStart()); } }