private static async Task AssignTag(WorkspaceModel workspace, string tagName, TimeEntryModel timeEntry) { var store = ServiceContainer.Resolve <IDataStore>(); var existing = await store.Table <TagData>() .QueryAsync(r => r.WorkspaceId == workspace.Id && r.Name == tagName) .ConfigureAwait(false); var checkRelation = true; TagModel tag; if (existing.Count > 0) { tag = new TagModel(existing [0]); } else { tag = new TagModel() { Name = tagName, Workspace = workspace, }; await tag.SaveAsync().ConfigureAwait(false); checkRelation = false; } if (timeEntry != null) { var assignTag = true; if (checkRelation) { // Check if the relation already exists before adding it var relations = await store.Table <TimeEntryTagData> () .CountAsync(r => r.TimeEntryId == timeEntry.Id && r.TagId == tag.Id && r.DeletedAt == null) .ConfigureAwait(false); if (relations < 1) { assignTag = false; } } if (assignTag) { var relationModel = new TimeEntryTagModel() { TimeEntry = timeEntry, Tag = tag, }; await relationModel.SaveAsync().ConfigureAwait(false); timeEntry.Touch(); await timeEntry.SaveAsync().ConfigureAwait(false); } } }
private async void OnOkClicked(object sender, DialogClickEventArgs e) { if (enabled && model != null) { var duration = model.GetDuration(); if (model.State == TimeEntryState.New) { duration = new TimeSpan(newDuration.Hours, newDuration.Minutes, 0); } else { // Keep the current seconds and milliseconds duration = new TimeSpan(0, newDuration.Hours, newDuration.Minutes, duration.Seconds, duration.Milliseconds); } model.SetDuration(duration); await model.SaveAsync(); } }
private async static void ReplaceTags(TimeEntryModel model, List <TimeEntryTagData> modelTags, List <TagData> selectedTags) { // Delete unused tag relations: var deleteTasks = modelTags .Where(oldTag => !selectedTags.Any(newTag => newTag.Id == oldTag.TagId)) .Select(data => new TimeEntryTagModel(data).DeleteAsync()); // Create new tag relations: var createTasks = selectedTags .Where(newTag => !modelTags.Any(oldTag => oldTag.TagId == newTag.Id)) .Select(data => new TimeEntryTagModel() { TimeEntry = model, Tag = new TagModel(data) }.SaveAsync()); await Task.WhenAll(deleteTasks.Concat(createTasks)); if (deleteTasks.Any <Task> () || createTasks.Any <Task> ()) { model.Touch(); await model.SaveAsync(); } }
private async void OnItemSelected(object sender, DialogClickEventArgs args) { if (modelLoaded && model != null) { var m = adapter.GetEntry(args.Which); TaskModel task = null; ProjectModel project = null; WorkspaceModel workspace = null; if (m is TaskData) { task = (TaskModel)(TaskData)m; if (task.Project != null) { await task.Project.LoadAsync(); project = task.Project; workspace = project.Workspace ?? task.Workspace; } else { workspace = task.Workspace; } } else if (m is ProjectAndTaskView.Project) { var wrap = (ProjectAndTaskView.Project)m; if (wrap.IsNoProject) { workspace = new WorkspaceModel(wrap.WorkspaceId); } else if (wrap.IsNewProject) { var data = wrap.Data; var ws = new WorkspaceModel(data.WorkspaceId); // Show create project dialog instead new CreateProjectDialogFragment(model, ws, data.Color) .Show(FragmentManager, "new_project_dialog"); } else { project = (ProjectModel)wrap.Data; workspace = project.Workspace; } } else if (m is ProjectAndTaskView.Workspace) { var wrap = (ProjectAndTaskView.Workspace)m; workspace = (WorkspaceModel)wrap.Data; } if (project != null || task != null || workspace != null) { model.Workspace = workspace; model.Project = project; model.Task = task; await model.SaveAsync(); } } }