/// <summary> /// Handle event when user clicks delete quiz button. /// </summary> /// <param name="e"></param> private async void ButtonDelete_Clicked(string deleteType, string DBId) { if (CredentialManager.IsLoggedIn) { bool unsubscribe = deleteType == "Unsubscribe"; string question; string message; if (unsubscribe) { question = "Are you sure you want to unsubscribe?"; message = "This will remove the copy from your device"; } else { question = "Are you sure you want to delete this quiz?"; message = "This will delete the copy on your device and in the cloud. This is not reversable."; } bool answer = await this.DisplayAlert(question, message, "Yes", "No"); if (answer) { // Acquire QuizInfo from roster QuizInfo rosterInfo = QuizRosterDatabase.GetQuizInfo(DBId); // Author string path = rosterInfo.RelativePath; // tell the roster that the quiz is deleted QuizInfo rosterInfoUpdated = new QuizInfo(rosterInfo) { IsDeletedLocally = true, LastModifiedDate = DateTime.Now.ToString() }; QuizRosterDatabase.EditQuizInfo(rosterInfoUpdated); // If connected, tell server to delete this quiz If not, it will tell server to delete next time it is connected in QuizRosterDatabase.UpdateLocalDatabase() if (CrossConnectivity.Current.IsConnected) { OperationReturnMessage returnMessage; if (unsubscribe) { returnMessage = await SubscribeUtils.UnsubscribeFromQuizAsync(DBId); } else { returnMessage = await ServerOperations.DeleteQuiz(DBId); } if (System.IO.Directory.Exists(path)) { Directory.Delete(path, true); } this.Setup(); } } } else { await this.DisplayAlert("Hold on!", "You must be create an account or log in before you can unsubscribe", "OK"); } }
/// <summary> /// Deletes the quiz from the roster /// </summary> private async Task ButtonDelete_Clicked(string DBId) { bool answer = await this.DisplayAlert("Are you sure you want to delete this quiz?", "This will delete the copy on your device and in the cloud. This is not reversable.", "Yes", "No"); if (answer) { // Acquire QuizInfo from roster QuizInfo rosterInfo = QuizRosterDatabase.GetQuizInfo(DBId); string path = rosterInfo.RelativePath; if (rosterInfo != null) { string dbId = rosterInfo.DBId; // tell the roster that the quiz is deleted QuizInfo rosterInfoUpdated = new QuizInfo(rosterInfo) { IsDeletedLocally = true, LastModifiedDate = DateTime.Now.ToString() }; QuizRosterDatabase.EditQuizInfo(rosterInfoUpdated); // If connected, tell server to delete this quiz If not, it will tell server to delete next time it is connected in QuizRosterDatabase.UpdateLocalDatabase() OperationReturnMessage returnMessage = await ServerOperations.DeleteQuiz(dbId); if (returnMessage == OperationReturnMessage.True) { QuizRosterDatabase.DeleteQuizInfo(dbId); } if (System.IO.Directory.Exists(path)) { Directory.Delete(path, true); } this.QuizNumber.Text = "You have published a total of " + await Task.Run(() => ServerOperations.GetNumberOfQuizzesByAuthorName(CredentialManager.Username)) + " quizzes!"; } else { await DisplayAlert("Could not delete quiz", "This quiz could not be deleted at this time. Please try again later", "OK"); } // Setup Page again after deletion await this.UpdateProfilePageAsync(); } }
/// <summary> /// Updates the local database by syncing with the server /// </summary> /// <returns>an awaitable task for the completion of syncing</returns> public static async Task UpdateLocalDatabaseAsync() { if (App.Path != null && App.UserPath.Length > 2) { RealmConfiguration threadConfig = App.realmConfiguration(RosterPath); Realm threadInstance = Realm.GetInstance(threadConfig); List <QuizInfo> QuizInfos = new List <QuizInfo>(threadInstance.All <QuizInfo>()); if (CrossConnectivity.Current.IsConnected) { for (int i = 0; i < QuizInfos.Count(); i++) { if (CredentialManager.IsLoggedIn) { if (QuizInfos[i].IsDeletedLocally) { if (await ServerOperations.DeleteQuiz(QuizInfos[i].DBId) == OperationReturnMessage.True) { string toDeleteDBId = QuizInfos[i].DBId; QuizInfos.Remove(QuizInfos[i]); DeleteQuizInfo(toDeleteDBId); } } else if (QuizInfos[i].SyncStatus != (int)SyncStatusEnum.NotDownloadedAndNeedDownload) { string lastModifiedDate = ServerOperations.GetLastModifiedDate(QuizInfos[i].DBId); if (lastModifiedDate == "") // returns empty string could not reach server { QuizInfo copy = new QuizInfo(QuizInfos[i]) { SyncStatus = (int)SyncStatusEnum.Offline // 3 represents offline }; EditQuizInfo(copy, threadInstance); } else { // Server returns "false" if quiz is not already on the server if (lastModifiedDate == "false" || lastModifiedDate == null) { QuizInfo copy = new QuizInfo(QuizInfos[i]) { SyncStatus = (int)SyncStatusEnum.NeedUpload // 1 represents need upload }; EditQuizInfo(copy, threadInstance); } else { DateTime localModifiedDateTime = Convert.ToDateTime(QuizInfos[i].LastModifiedDate); DateTime serverModifiedDateTime = Convert.ToDateTime(lastModifiedDate); if (localModifiedDateTime > serverModifiedDateTime) { QuizInfo copy = new QuizInfo(QuizInfos[i]) { SyncStatus = (int)SyncStatusEnum.NeedUpload // 1 represents need upload }; EditQuizInfo(copy, threadInstance); } else if (localModifiedDateTime < serverModifiedDateTime) { QuizInfo copy = new QuizInfo(QuizInfos[i]) { SyncStatus = (int)SyncStatusEnum.NeedDownload // 0 represents needs download }; EditQuizInfo(copy, threadInstance); } else if (localModifiedDateTime == serverModifiedDateTime) { QuizInfo copy = new QuizInfo(QuizInfos[i]) { SyncStatus = (int)SyncStatusEnum.Synced // 2 represents in sync }; EditQuizInfo(copy, threadInstance); } } } } } else { QuizInfo copy = new QuizInfo(QuizInfos[i]) { SyncStatus = (int)SyncStatusEnum.Synced // 2 represents in sync }; EditQuizInfo(copy, threadInstance); } } string[] dbIds = new string[QuizInfos.Count]; for (int i = 0; i < dbIds.Length; i++) { dbIds[i] = QuizInfos[i].DBId; } List <string[]> missingQuizs = ServerOperations.GetMissingQuizzesByAuthorName(CredentialManager.Username, dbIds); foreach (string[] missingQuiz in missingQuizs) { QuizInfo info = new QuizInfo { DBId = missingQuiz[0], AuthorName = missingQuiz[1], QuizName = missingQuiz[2], Category = missingQuiz[3], LastModifiedDate = missingQuiz[4], SubscriberCount = int.Parse(missingQuiz[5]), SyncStatus = (int)SyncStatusEnum.NotDownloadedAndNeedDownload }; threadInstance.Write(() => { threadInstance.Add(info); }); } } else { for (int i = 0; i < QuizInfos.Count; i++) { QuizInfo copy = new QuizInfo(QuizInfos[i]) { SyncStatus = (int)SyncStatusEnum.Offline // 3 represents offline }; EditQuizInfo(copy, threadInstance); } } } }