Exemplo n.º 1
0
        /// <summary>
        /// Handles the clicked async.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        async void Handle_ClickedAsync(object sender, System.EventArgs e)
        {
            bool allCompleted = true;

            foreach (var view in feedbackStackLayout.Children)
            {
                var mView = view as RatingStars;

                if (mView != null)
                {
                    if (mView.SelectedRating == -1)
                    {
                        allCompleted = false;
                    }
                }
            }

            if (allCompleted)
            {
                string returnString = ViewTools.CommaSeparatedValue("Question,Rating",
                                                                    feedbackStackLayout, startTime,
                                                                    DateTime.Now.Subtract(startTime));

                int result = await App.Database.SaveItemAsync(new SleepFeedbackModel()
                {
                    CSV = returnString
                });


                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.UploadFeedback(new System.IO.MemoryStream(Encoding.UTF8.GetBytes(returnString)), App.Database.GetLargestFeedbackID());

                        await Task.Delay(100);
                    }
                }

                App.RefreshServer = true;

                await Navigation.PopModalAsync();
            }
            else
            {
                await UserDialogs.Instance.AlertAsync("Please answer all questions", Title = "Error", okText : "Okay");
            }
        }
Exemplo n.º 2
0
        /// <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());
                }
            }
        }