Example #1
0
            public async Task ShowNotification()
            {
                try
                {
                    bool isAppInForeground = AndroidUtils.IsAppInForeground();
                    if (!isAppInForeground)
                    {
                        _telemetryService.Init();
                    }

                    if (IsAReminder)
                    {
                        _logger.Information("Trying to show scheduled notification...");
                        _notificationService.ShowTaskReminderNotification(_reminderNotification);

                        _logger.Information(
                            $"Notification was shown, removing notification " +
                            $"date for taskId = {_reminderNotification.TaskId}");
                        await _dataService.TaskService.RemoveNotificationDate(
                            _reminderNotification.TaskId,
                            TaskNotificationDateType.REMINDER_DATE);
                    }
                    else
                    {
                        _logger.Information("Showing normal notification...");
                        _notificationService.ShowNotification(_notification);
                    }
                }
                catch (Exception e)
                {
                    _logger?.Error(e, $"An unknown error occurred while trying to show notification");
                    _telemetryService?.TrackError(e);
                }
            }
Example #2
0
        public async Task Sync()
        {
            try
            {
                _logger.Information(
                    $"{nameof(Sync)}: Started {(_startedManually ? "manually" : "automatically")}");

                bool isSyncServiceRunning = (_appSettings as IAndroidAppSettings)
                                            .GetBoolean(SyncBackgroundService.IsServiceRunningKey);

                if (!_startedManually && isSyncServiceRunning)
                {
                    _logger.Warning($"{nameof(Sync)}: The sync bg service is already running...");
                    return;
                }

                string msg = $"{_textProvider.Get("Syncing")}...";
                await _dispatcher.ExecuteOnMainThreadAsync(() => _messenger.Publish(new ShowProgressOverlayMsg(this, msg: msg)));

                if (!_networkService.IsInternetAvailable())
                {
                    _logger.Warning($"{nameof(Sync)}: Network is not available...");
                    msg = _textProvider.Get("NetworkNotAvailable");
                    await _dispatcher.ExecuteOnMainThreadAsync(() => _messenger.Publish(new ShowProgressOverlayMsg(this, false, msg)));

                    return;
                }

                bool syncOnlyOneTaskList = _taskListId.HasValue;
                if (syncOnlyOneTaskList)
                {
                    _logger.Information($"{nameof(Sync)}: We will perform a partial sync for the " +
                                        $"tasklistId = {_taskListId.Value} and its associated tasks");
                }
                else
                {
                    _logger.Information($"{nameof(Sync)}: We will perform a full sync");
                }

                var syncResults = !syncOnlyOneTaskList
                    ? await _syncService.PerformFullSync(true)
                    : await _syncService.PerformSyncOnlyOn(_taskListId.Value);

                if (syncResults.Any(r => !r.Succeed))
                {
                    var errors = string.Join(
                        $".{Environment.NewLine}",
                        syncResults
                        .Where(r => !r.Succeed)
                        .Select(r => r.Message)
                        .Distinct());

                    _logger.Error($"{nameof(Sync)}: Sync completed with errors = {errors}");
                }

                string message = syncResults.Any(r => !r.Succeed) ?
                                 _textProvider.Get("SyncUnknownError") :
                                 !_startedManually
                        ? _textProvider.Get("SyncAutoCompleted")
                        : _textProvider.Get("SyncManualCompleted");

                if (string.IsNullOrEmpty(message))
                {
                    message = "An unknown error occurred while trying to perform the sync operation.";
                }

                _logger.Information($"{nameof(Sync)}: results = {message}");
                await _dispatcher.ExecuteOnMainThreadAsync(() => _messenger.Publish(new ShowProgressOverlayMsg(this, false)));

                bool isInForeground = AndroidUtils.IsAppInForeground();
                if (!isInForeground && _appSettings.ShowToastNotificationAfterFullSync)
                {
                    _logger.Information($"{nameof(Sync)}: App is not in foreground, showing a notification...");
                    var notif = new TaskNotification
                    {
                        Content = message,
                        Title   = _textProvider.Get("SyncResults")
                    };
                    _notificationService.ShowNotification(notif);
                }
                else if (isInForeground)
                {
                    _logger.Information($"{nameof(Sync)}: App is in foreground, showing the snackbar msg...");
                    await _dispatcher.ExecuteOnMainThreadAsync(() =>
                    {
                        _dialogService.ShowSnackBar(message);
                        _messenger.Publish(new OnFullSyncMsg(this));
                    });
                }

                _logger.Information($"{nameof(Sync)}: completed successfully");
            }
            catch (Exception e)
            {
                _logger?.Error(e, $"{nameof(Sync)}: An unknown error occurred while trying to sync task / tasklists");
                _telemetryService?.TrackError(e);
            }
        }
            public async Task MarkAsCompleted()
            {
                try
                {
                    bool isAppInForeground = AndroidUtils.IsAppInForeground();
                    if (!isAppInForeground)
                    {
                        _telemetryService.Init();
                    }

                    _logger.Information($"Marking taskId = {_notification.TaskId} as completed...");
                    //if i pass the notifaction in the lambda, the task crashes..
                    int taskId       = _notification.TaskId;
                    var taskResponse = await _dataService.TaskService
                                       .FirstOrDefaultAsNoTrackingAsync(t => t.ID == taskId);

                    if (!taskResponse.Succeed)
                    {
                        _logger.Error(
                            $"Couldnt retrieve taskId = {_notification.Id}. " +
                            $"Error = {taskResponse.Message}");
                        return;
                    }
                    var googleTaskId = taskResponse.Result.GoogleTaskID;
                    var response     = await _dataService.TaskService
                                       .ChangeTaskStatusAsync(googleTaskId, GoogleTaskStatus.COMPLETED);

                    if (response.Succeed)
                    {
                        _logger.Information(
                            $"TaskId = {_notification.TaskId} " +
                            $"was successfully marked as completed");

                        if (isAppInForeground)
                        {
                            var task = response.Result;
                            var msg  = new TaskStatusChangedMsg(
                                this,
                                task.GoogleTaskID,
                                task.ParentTask,
                                task.CompletedOn,
                                task.UpdatedAt,
                                task.Status);
                            _messenger.Publish(msg);
                        }
                    }
                    else
                    {
                        _logger.Error(
                            $"TaskId = {_notification.TaskId} couldnt be marked as completed. " +
                            $"Error = {response.Message}");

                        _notificationService.ShowNotification(new TaskNotification
                        {
                            Content = _textProvider.Get("TaskCouldntBeMarkedAsCompleted", _notification.TaskTitle),
                            Title   = _textProvider.Get("Error")
                        });
                    }

                    _logger.Information("Process completed...");
                }
                catch (Exception e)
                {
                    _logger?.Error(e,
                                   $"An unknown error occurred while trying " +
                                   $"to mark taskId = {_notification.Id} as completed");
                    _telemetryService?.TrackError(e);
                }
            }