blob: e6c41e5e1c84ea515c458043871798481942d02f (
plain)
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
|
using System;
using MVPtoMVVM.domain;
using MVPtoMVVM.mappers;
using MVPtoMVVM.repositories;
using MVPtoMVVM.views;
namespace MVPtoMVVM.presenters
{
public class TodoItemPresenter : ITodoItemPresenter
{
private ITodoItemRepository itemRepository;
private ITodoItemMapper itemMapper;
private ITodoItemView view;
public int Id { get; set; }
public TodoItemPresenter(ITodoItemRepository itemRepository, ITodoItemMapper itemMapper)
{
this.itemRepository = itemRepository;
this.itemMapper = itemMapper;
}
public void SetView(ITodoItemView view)
{
this.view = view;
InitializeView();
}
public void SetItem(TodoItem item)
{
Id = item.Id;
Description = item.Description;
DueDate = item.DueDate;
IsDirty = false;
}
private void InitializeView()
{
view.Id = Id;
view.Description = Description;
view.DueDate = DueDate;
view.SaveButtonEnabled = false;
}
public void SaveItem()
{
var item = itemMapper.MapFrom(this);
itemRepository.Save(item);
Id = item.Id;
IsDirty = false;
}
public void DeleteItem()
{
var item = itemMapper.MapFrom(this);
view.Remove(item.Id);
itemRepository.Delete(item);
}
private string description;
public string Description
{
get { return description; }
set { description = value;
IsDirty = true;
}
}
private DateTime dueDate;
public DateTime DueDate
{
get { return dueDate; }
set { dueDate = value;
IsDirty = true;
}
}
private bool isDirty;
public bool IsDirty
{
get { return isDirty; }
private set { isDirty = value;
if (view != null)
UpdateControlState();
}
}
private void UpdateControlState()
{
view.SaveButtonEnabled = IsDirty && IsDescriptionValid() && IsDueDateValid();
view.DescriptionHasValidationErrors = !IsDescriptionValid();
view.DueDateHasValidationErrors = !IsDueDateValid();
}
private bool IsDescriptionValid()
{
return description.Length > 0;
}
private bool IsDueDateValid()
{
return dueDate >= DateTime.Today;
}
}
}
|