-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskItem.cs
110 lines (96 loc) · 2.66 KB
/
TaskItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.ComponentModel;
using System.Windows.Media;
public class TaskItem : INotifyPropertyChanged
{
private bool _isCompleted;
private DateTime _deadline;
private string _description;
private Brush _foregroundColor;
private string _remainingTime;
public event EventHandler TaskDeleted;
public void DeleteTask()
{
TaskDeleted?.Invoke(this, EventArgs.Empty);
}
public string Description
{
get { return _description; }
set
{
_description = value;
OnPropertyChanged("Description");
}
}
public DateTime Deadline
{
get { return _deadline; }
set
{
_deadline = value;
OnPropertyChanged("Deadline");
OnPropertyChanged("RemainingTime");
UpdateForegroundColor();
}
}
public bool IsCompleted
{
get { return _isCompleted; }
set
{
_isCompleted = value;
OnPropertyChanged("IsCompleted");
OnPropertyChanged("RemainingTime");
UpdateForegroundColor();
if (_isCompleted)
TaskCompleted?.Invoke(this, EventArgs.Empty);
}
}
public string RemainingTime
{
get
{
if (IsCompleted)
return "Completed";
var remainingTime = Deadline - DateTime.Now;
if (remainingTime.TotalSeconds <= 0)
{
ForegroundColor = Brushes.Red;
if (!IsCompleted)
{
IsCompleted = true;
_remainingTime = "You Fucked It Up";
return _remainingTime;
}
}
return _remainingTime ?? $"{(int)remainingTime.TotalDays}d {(int)remainingTime.TotalHours % 24}h {(int)remainingTime.TotalMinutes % 60}m";
}
}
public Brush ForegroundColor
{
get { return _foregroundColor; }
set
{
_foregroundColor = value;
OnPropertyChanged("ForegroundColor");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler TaskCompleted;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void UpdateForegroundColor()
{
var remainingTime = Deadline - DateTime.Now;
if (remainingTime.TotalSeconds <= 1000 && !IsCompleted)
{
ForegroundColor = Brushes.Red;
}
else
{
ForegroundColor = Brushes.Black;
}
}
}