private void SaveConfirmedProgress(ConfirmationRequest request) { if (request.Type == ConfirmationType.Challenge) { var progress = new ChallengeProgress { ChallengeId = request.TargetId, Id = request.ChallengeProgressId, LastModified = DateTime.UtcNow, Status = ProgressStatus.Confirmed, UserId = request.UserId }; using (var db = new SQLite.SQLiteConnection(AppState.State.Instance.DbPath)) { db.InsertOrReplace(progress); } } else { using (var db = new SQLite.SQLiteConnection(AppState.State.Instance.DbPath)) { var challenge = AppState.State.Instance .Challenges.FirstOrDefault((c) => c.BasicTasks.Any(t => t.Id == request.TargetId) || c.ExtraTasks.Any(t => t.Id == request.TargetId)); var challengeProgress = db .Table <ChallengeProgress>() .ToList() .FirstOrDefault(c => c.ChallengeId == challenge.Id); // If a user started a challenge and has not synced yet, confirming a task from that challenge // could crash the app - thus, a new challenge progress object is created on the device of the // confirming participant. On nearest synchronization, thi progress is passed to the db. if (challengeProgress == null) { challengeProgress = new ChallengeProgress { ChallengeId = challenge.Id, Id = request.ChallengeProgressId, LastModified = DateTime.UtcNow, Status = ProgressStatus.InProgress, UserId = request.UserId, }; db.InsertOrReplace(challengeProgress, typeof(ChallengeProgress)); } var progress = new TaskProgress { TaskId = request.TargetId, Id = request.TaskProgressId, LastModified = DateTime.UtcNow, Status = ProgressStatus.Confirmed, ChallengeProgressId = request.ChallengeProgressId, }; db.InsertOrReplace(progress, typeof(TaskProgress)); } } }
// Dialog response private void VerifyConfirmation(Confirmation confirmation) { var signerId = confirmation.SignerId; try { var res = Verify(Newtonsoft.Json.JsonConvert.SerializeObject(confirmation.Request), confirmation.Signature, signerId); if (res) { string responseText; using (var db = new SQLite.SQLiteConnection(AppState.State.Instance.DbPath)) { if (confirmation.Request.Type == ConfirmationType.Task) { var tp = new TaskProgress { TaskId = confirmation.Request.TargetId, ChallengeProgressId = confirmation.Request.ChallengeProgressId, Id = confirmation.Request.TaskProgressId, Status = ProgressStatus.Completed, LastModified = DateTime.UtcNow, }; db.InsertOrReplace(tp); responseText = $"Splnìní úkolu úspìšnì potvrzeno! Gratulujeme"; } else { var cp = new ChallengeProgress { ChallengeId = confirmation.Request.TargetId, Id = confirmation.Request.ChallengeProgressId, Status = ProgressStatus.Completed, LastModified = DateTime.UtcNow, UserId = confirmation.Request.UserId }; db.InsertOrReplace(cp); responseText = $"Splnìní zkoušky úspìšnì potvrzeno. Gratulujeme!"; } } new AlertDialog.Builder(this) .SetMessage(responseText) .SetNeutralButton("Ok", (o, e) => { SetResult(Android.App.Result.Ok); Finish(); }) .SetCancelable(false) .Create() .Show(); } else { throw new ConfirmationException("Elektronický podpis nesouhlasí."); } } catch (Exception e) { new AlertDialog.Builder(this) .SetMessage($"Potvrzení nebylo úspìšné: {e.Message}") .SetNeutralButton("Ok", (o, ev) => { SetResult(Android.App.Result.Canceled); Finish(); }) .SetCancelable(false) .Create() .Show(); } }