public static async Task ModifyPlantTaskAsync(PlantTask task, List <Plant> plantsToPerformTaskOn) { var oldConnections = await PlantActivityService.GetPlantTaskPlantConnectionsAsync(task); //Remove old connections. var plantIdsToRemove = oldConnections .Select(oldConn => oldConn.PlantFk) .Except(plantsToPerformTaskOn.Select(p => p.Id)); var connectionsToRemove = (await asyncDb .Table <PlantTaskPlantConnection>() .Where(conn => conn.PlantTaskFk == task.Id) //Only this task .ToListAsync()) .Where(conn => plantIdsToRemove.Any(id => id == conn.PlantFk)); //Only the plants to remove await Task.WhenAll(connectionsToRemove .Select(conn => asyncDb.DeleteAsync(conn))); //Add new connections. var connectionsToAdd = plantsToPerformTaskOn .Select(p => p.Id) .Except(oldConnections.Select(oldConn => oldConn.PlantFk)) .Select(id => new PlantTaskPlantConnection() { PlantFk = id, PlantTaskFk = task.Id }); await asyncDb.InsertAllAsync(connectionsToAdd); //Update the task and recreate the activities. await asyncDb.UpdateAsync(task); await AddActivitiesFromTaskAsync(task, DateTime.Today); }
public static async Task AddDailyNotifications(TimeSpan?timeOfDay = null) { var activities = await PlantActivityService.GetUpcomingActivitiesByDayAsync(DateTime.Today, DateTime.Today.AddDays(60)); Task remindersTask = timeOfDay != null? platformNotiSvc.CreateDailyReminders(activities, (byte)timeOfDay?.Hours, (byte)timeOfDay?.Minutes) : platformNotiSvc.CreateDailyReminders(activities); await remindersTask; }
public static async Task DeletePlantAsync(Plant plantToDelete) { //First delete all photos. var photos = await PlantService.GetPhotosOfPlantAsync(plantToDelete); var deletePhotosTask = Task.WhenAll(photos.Select(async(photo) => { await PlantService.RemovePlantPhotoAsync(photo); })); //Will be waited on at the end. //Get Tasks before removing the PlantTaskConnections var tasks = await PlantActivityService.GetTasksOfPlantAsync(plantToDelete); //Remove PlantTaskPlantConnections var deletePlantTaskConnectionsTask = PlantActivityService.RemovePlantTaskPlantConnectionsForPlantTaskAsync(plantToDelete); //Waited on at the end. //Finally, remove the plant. await asyncDb.DeleteAsync(plantToDelete); await deletePhotosTask; await deletePlantTaskConnectionsTask; }
/// <summary> /// Adds the PlantTask to the local storage, asynchronously. Then, creates the associated PlantActivityItems. /// </summary> /// <param name="task">The PlantTask to add.</param> /// <param name="plantsToPerformTaskOn">The list of Plants this task should be performed on.</param> /// <returns>A Task object that, when finished, creates the resources mentioned above.</returns> public static async Task AddPlantTaskAsync(PlantTask task, List <Plant> plantsToPerformTaskOn) { //Inserting the new PlantTask into DB. await asyncDb.InsertAsync(task); //This will populate the id field. //Creating the PlantTask - Plant connections. var connectionList = new List <PlantTaskPlantConnection>(); foreach (var plant in plantsToPerformTaskOn) { var plantTaskPlantConnection = new PlantTaskPlantConnection { PlantFk = plant.Id, PlantTaskFk = task.Id }; connectionList.Add(plantTaskPlantConnection); } await PlantActivityService.AddPlantTaskPlantConnectionsAsync(connectionList); //Adding activities for the task + recreating the daily reminders. await AddActivitiesFromTaskAsync(task, DateTime.Today); }