forked from unoplatform/workshops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPageViewModel.cs
More file actions
145 lines (126 loc) · 5.88 KB
/
MainPageViewModel.cs
File metadata and controls
145 lines (126 loc) · 5.88 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using TodoApp.Shared.Models;
using Windows.UI.Xaml;
namespace TodoApp.Shared.ViewModels
{
public class MainPageViewModel : INotifyPropertyChanged
{
private string _newTodoText;
private State _state = State.Default;
private int _filter = 0;
public MainPageViewModel()
{
CreateNew = new SimpleCommand(ExecuteCreateNew);
ViewAll = new SimpleCommand(() => Filter = 0);
ViewActive = new SimpleCommand(() => throw new NotImplementedException("🎯 Create a `ICommand` that filters the ViewModel to display only Active todos."));
ViewInactive = new SimpleCommand(() => throw new NotImplementedException("🎯 Create a `ICommand` that filters the ViewModel to display only Inactive todos."));
}
public State State
{
get => _state;
private set
{
throw new NotImplementedException("🎯 Ensure that updating the `State` property also updates the backing property");
throw new NotImplementedException("🎯 Ensure that updating the `State` property also invokes `OnPropertyChanged` for `MainPageViewModel` so that databound controls update.");
throw new NotImplementedException("🎯 Ensure that updating the `State` property also invokes `OnPropertyChanged` for `MainPageViewModel.Todo` so that databound controls update.");
}
}
public Visibility IsEmpty
{
get
{
if(Todos.Count() == 0)
{
throw new NotImplementedException("🎯 Ensure that the `IsEmpty` property returns the appropriate `Windows.UI.Xaml.Visibility` so that the majority of databound controls disppear when updated.");
}
else
{
throw new NotImplementedException("🎯 Ensure that the `IsEmpty` property returns the appropriate `Windows.UI.Xaml.Visibility` so that the majority of databound controls appear when updated.");
}
}
}
public int Filter
{
get => _filter;
set
{
throw new NotImplementedException("🎯 Ensure that updating the `Filter` property also updates the backing property.");
throw new NotImplementedException("🎯 Ensure that updating the `Filter` property also invokes `OnPropertyChanged` for `MainPageViewModel` so that databound controls update.");
throw new NotImplementedException("🎯 Ensure that updating the `Filter` property also invokes `OnPropertyChanged` for `MainPageViewModel.Todo` so that databound controls update.");
}
} // 0-all, 1-active, 2-inactives
public IEnumerable<Todo> Todos
{
get
{
switch (Filter)
{
case 0:
throw new NotImplementedException("🎯 Ensure that the `Todos` property returns the appropriate filtered list from `State` when accessed.");
case 1:
throw new NotImplementedException("🎯 Ensure that the `Todos` property returns the appropriate filtered list from `State` when accessed.");
case 2:
throw new NotImplementedException("🎯 Ensure that the `Todos` property returns the appropriate filtered list from `State` when accessed.");
}
throw new InvalidOperationException();
}
}
public string NewTodoText
{
get => _newTodoText;
set
{
throw new NotImplementedException("🎯 Ensure that updating the `State` property also updates the backing property");
throw new NotImplementedException("🎯 Ensure that updating the `State` property also invokes `OnPropertyChanged` for `MainPageViewModel` so that databound controls update.");
}
}
public ICommand CreateNew { get; }
public ICommand ViewAll { get; }
public ICommand ViewActive { get; }
public ICommand ViewInactive { get; }
private void ExecuteCreateNew()
{
var newTodo = new Todo(NewTodoText);
State = State.WithTodos(todos => todos.Add(newTodo));
NewTodoText = "";
OnPropertyChanged();
OnPropertyChanged(nameof(IsEmpty));
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void ChangeState(Todo todo, bool isDone)
{
State = State.WithTodos(todos =>
{
var existing = todos.FirstOrDefault(t => t.KeyEquals(todo));
Todo newTodo = existing.WithIsDone(isDone);
return newTodo != existing ? todos.Replace(existing, newTodo) : todos;
});
}
public void ChangeText(Todo todo, string newText)
{
State = State.WithTodos(todos =>
{
var existing = todos.FirstOrDefault(t => t.KeyEquals(todo));
Todo newTodo = existing.WithText(newText);
throw new NotImplementedException("🎯 Implement `todos.Replace()`");
});
}
public void RemoveTodo(Todo todo)
{
State = State.WithTodos(todos =>
{
var existing = todos.FirstOrDefault(t => t.KeyEquals(todo));
throw new NotImplementedException("🎯 Implement `todos.Remove()`");
});
}
}
}