JD2022-TU1/main/tools/modules/JD.Jira.WPF/View/MyJiraViewModel.cs

118 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using ActiproSoftware.Windows.Extensions;
using Atlassian.Jira;
using JD.Jira.Service;
using JD.Jira.WPF.ViewModel;
using Plume.WPF.Commands;
using Plume.WPF.Mvvm;
using Plume.WPF.Threading;
namespace JD.Jira.WPF.View
{
public class MyJiraViewModel : BindableBase, IFilterable
{
private readonly JiraService _jiraService;
private bool _isRefreshing;
private string _filterText;
private string _jql;
private Action<IssueViewModel> _issueModificationCallback;
private readonly DebounceDispatcher _debounceDispatcher = new DebounceDispatcher();
public MyJiraViewModel(JiraService jiraService)
{
_jiraService = jiraService;
TaskLists.Add(new TaskListViewModel("Open", new []{"Open"}, Color.FromArgb(0xff, 0x00, 0x79, 0xbf)));
TaskLists.Add(new TaskListViewModel("In Progress", new[] { "Reopened", "In Progress", "Waiting For" }, Color.FromArgb(0xff, 0xfa, 0xd9, 0x00)));
TaskLists.Add(new TaskListViewModel("Closed", new[] { "Resolved", "Closed" }, Color.FromArgb(0xff, 0x3c, 0xb5, 0x00)));
RefreshCommand = new DelegateCommand(Refresh);
}
private void Refresh()
{
UpdateAsync().ConfigureAwait(false);
}
private List<IssueViewModel> Issues { get; set; } = new List<IssueViewModel>();
public async Task UpdateAsync(string jql, Action<IssueViewModel> issueModificationCallback)
{
IsRefreshing = true;
_issueModificationCallback = issueModificationCallback;
_jql = jql;
if (string.IsNullOrEmpty(_jql))
{
Issues.Clear();
}
else
{
var issues = await _jiraService.GetIssuesFromJqlAsync(jql);
Issues = issues.OrderBy(x => x.Summary).Select(x => new IssueViewModel(x, _jiraService)).ToList();
if (issueModificationCallback != null)
{
foreach (var issue in Issues)
{
issueModificationCallback.Invoke(issue);
}
}
}
UpdateVisibleIssues();
}
private async Task UpdateAsync()
{
await UpdateAsync(_jql, _issueModificationCallback);
}
private void UpdateVisibleIssues()
{
Application.Current.Dispatcher.Invoke(() =>
{
foreach (var taskList in TaskLists)
{
var issues = Issues.Where(x => string.IsNullOrEmpty(FilterText) || Contains(x.Issue.Summary, FilterText, StringComparison.OrdinalIgnoreCase) || Contains(x.Issue.Assignee, FilterText, StringComparison.OrdinalIgnoreCase));
taskList.Issues.Clear();
taskList.Issues.AddRange(issues.Where(x => taskList.Statuses.Contains(x.Issue.Status.ToString())));
foreach (var issue in taskList.Issues)
{
issue.Color = taskList.Color;
}
IsRefreshing = false;
}
});
}
public ICommand RefreshCommand { get; }
public bool IsRefreshing
{
get => _isRefreshing;
set => SetProperty(ref _isRefreshing, value);
}
public string FilterText
{
get => _filterText;
set
{
SetProperty(ref _filterText, value);
_debounceDispatcher.Debounce(100, o => UpdateVisibleIssues());
}
}
public ObservableCollection<TaskListViewModel> TaskLists { get; } = new ObservableCollection<TaskListViewModel>();
public static bool Contains(string source, string toCheck, StringComparison comp)
{
return source?.IndexOf(toCheck, comp) >= 0;
}
}
}