-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote_app.dart
More file actions
388 lines (319 loc) · 9.64 KB
/
note_app.dart
File metadata and controls
388 lines (319 loc) · 9.64 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// behavioral_design_patterns/mediator/examples/note_app.dart
// https://refactoring.guru/design-patterns/mediator/java/example#example-0--OutputDemo-png
// Usage examples:
// The most popular usage of the Mediator pattern in Java code is facilitating communications between GUI components of an app.
// The synonym of the Mediator is the Controller part of MVC pattern.
// Notes app
// This example shows how to organize lots of UI elements so that they cooperate with the help of a mediator but don’t depend on each other.
// ----------------------------------------------------------------------------
// Model
// ----------------------------------------------------------------------------
/// A simple data class representing a Note.
class Note {
String name;
String text;
Note({this.name = 'New note', this.text = ''});
@override
String toString() => name;
}
// ----------------------------------------------------------------------------
// Interfaces
// ----------------------------------------------------------------------------
/// The Mediator interface declares methods used by components to notify the mediator about various events.
/// The Mediator may react to these events
/// and pass the execution to other components.
abstract interface class Mediator {
void addNewNote(Note note);
void deleteNote();
void getInfoFromList(Note note);
void saveChanges();
void markNote();
void clear();
void sendToFilter(List<Note> notes);
void setElementsList(List<Note> notes);
void registerComponent(Component component);
void hideElements(bool flag);
}
/// The Base Component interface provides the basic functionality of storing
/// a mediator's instance inside component objects.
abstract interface class Component {
String getName();
}
// ----------------------------------------------------------------------------
// Concrete Components
// ----------------------------------------------------------------------------
/// Concrete Components don't talk to each other. They have only one
/// communication channel – sending requests to the mediator.
class AddButton implements Component {
final Mediator _mediator;
const AddButton(this._mediator);
@override
String getName() => "AddButton";
void press() {
print("AddButton: Clicked.");
_mediator.addNewNote(Note());
}
}
class DeleteButton implements Component {
final Mediator _mediator;
const DeleteButton(this._mediator);
@override
String getName() => "DelButton";
void press() {
print("DeleteButton: Clicked.");
_mediator.deleteNote();
}
}
class SaveButton implements Component {
final Mediator _mediator;
const SaveButton(this._mediator);
@override
String getName() => "SaveButton";
void press() {
print("SaveButton: Clicked.");
_mediator.saveChanges();
}
}
class Filter implements Component {
final Mediator _mediator;
List<Note> _originalList = [];
Filter(this._mediator);
@override
String getName() => "Filter";
void setList(List<Note> list) {
_originalList = list;
}
void search(String query) {
print("Filter: Searching for '$query'");
if (query.isEmpty) {
_mediator.setElementsList(_originalList);
return;
}
final filtered =
_originalList.where((note) => note.name.contains(query)).toList();
_mediator.setElementsList(filtered);
}
}
class NoteList implements Component {
final Mediator _mediator;
final List<Note> _notes = [];
NoteList(this._mediator);
Note? _selectedNote;
@override
String getName() => "List";
int getCount() => _notes.length;
Note? getSelectedNote() => _selectedNote;
void setElements(List<Note> notes) {
if (!identical(_notes, notes)) {
_notes.clear();
_notes.addAll(notes);
}
if (_selectedNote != null && !_notes.contains(_selectedNote)) {
_selectedNote = null;
}
print("NoteList: Updated list content: ${_notes.join(', ')}");
}
void repaint() {
print("NoteList: Repainted list content: ${_notes.join(', ')}");
}
void addElement(Note note) {
_notes.add(note);
selectElement(note); // Select the new note automatically
_mediator.sendToFilter(List.from(_notes));
}
void deleteElement() {
if (_selectedNote != null) {
_notes.remove(_selectedNote);
if (_notes.isEmpty) {
_selectedNote = null;
// Notify mediator that selection is empty to hide edit controls
_mediator.hideElements(true);
} else {
// Select the last one for simplicity
selectElement(_notes.last);
}
_mediator.sendToFilter(List.from(_notes));
}
}
void selectElement(Note note) {
if (_notes.contains(note)) {
_selectedNote = note;
print("NoteList: Selected note '${note.name}'");
_mediator.getInfoFromList(note);
_mediator.hideElements(false);
}
}
}
class Title implements Component {
final Mediator _mediator;
String _text = "";
Title(this._mediator);
@override
String getName() => "Title";
void setText(String text) {
_text = text;
print("Title: Set text to '$_text'");
}
String getText() => _text;
// Simulate user typing
void enterText(String text) {
_text = text;
_mediator.markNote(); // Mark as modified
}
}
class TextBox implements Component {
final Mediator _mediator;
String _text = "";
TextBox(this._mediator);
@override
String getName() => "TextBox";
void setText(String text) {
_text = text;
print("TextBox: Set text to '$_text'");
}
String getText() => _text;
// Simulate user typing
void enterText(String text) {
_text = text;
_mediator.markNote(); // Mark as modified
}
}
// ----------------------------------------------------------------------------
// Mediator Implementation
// ----------------------------------------------------------------------------
/// Concrete Mediator. All chaotic communications between concrete components
/// have been extracted to the mediator. Now components only talk with the
/// mediator, which knows who has to handle a request.
class Editor implements Mediator {
late Title _title;
late TextBox _textBox;
late AddButton _add;
late DeleteButton _del;
late SaveButton _save;
late NoteList _list;
late Filter _filter;
@override
void registerComponent(Component component) {
switch (component.getName()) {
case "AddButton":
_add = component as AddButton;
break;
case "DelButton":
_del = component as DeleteButton;
break;
case "Filter":
_filter = component as Filter;
break;
case "List":
_list = component as NoteList;
break;
case "SaveButton":
_save = component as SaveButton;
break;
case "TextBox":
_textBox = component as TextBox;
break;
case "Title":
_title = component as Title;
break;
}
}
@override
void addNewNote(Note note) {
_title.setText("");
_textBox.setText("");
_list.addElement(note);
}
@override
void deleteNote() {
_list.deleteElement();
}
@override
void getInfoFromList(Note note) {
_title.setText(note.name.replaceAll('*', ''));
_textBox.setText(note.text);
}
@override
void saveChanges() {
final note = _list.getSelectedNote();
if (note != null) {
note.name = _title.getText();
note.text = _textBox.getText();
print("Editor: Saved changes to note '${note.name}'");
// Force list update to reflect name change if needed
_list.setElements(_list._notes);
}
}
@override
void markNote() {
final note = _list.getSelectedNote();
if (note != null) {
String name = note.name;
if (!name.endsWith("*")) {
note.name = name + "*";
print("Editor: Marked note as modified: ${note.name}");
}
}
}
@override
void clear() {
_title.setText("");
_textBox.setText("");
print("Editor: Cleared title and text fields.");
}
@override
void sendToFilter(List<Note> notes) {
_filter.setList(notes);
}
@override
void setElementsList(List<Note> notes) {
_list.setElements(notes);
}
@override
void hideElements(bool flag) {
if (flag) {
print("Editor: Hiding editing area (No selection).");
} else {
print("Editor: Showing editing area.");
}
}
}
// ----------------------------------------------------------------------------
// Execution
// ----------------------------------------------------------------------------
void main() {
final mediator = Editor();
final addBtn = AddButton(mediator);
final delBtn = DeleteButton(mediator);
final saveBtn = SaveButton(mediator);
final filter = Filter(mediator);
final list = NoteList(mediator);
final title = Title(mediator);
final textBox = TextBox(mediator);
mediator.registerComponent(addBtn);
mediator.registerComponent(delBtn);
mediator.registerComponent(saveBtn);
mediator.registerComponent(filter);
mediator.registerComponent(list);
mediator.registerComponent(title);
mediator.registerComponent(textBox);
print("\n--- User Action: Add Note ---");
addBtn.press();
print("\n--- User Action: Edit Note ---");
title.enterText("My First Note");
textBox.enterText("This is the content of the note.");
print("\n--- User Action: Save Note ---");
saveBtn.press();
print("\n--- User Action: Add Another Note ---");
addBtn.press();
title.enterText("Shopping List");
saveBtn.press();
print("\n--- User Action: Filter 'First' ---");
filter.search("First");
print("\n--- User Action: Filter 'Shopping' ---");
filter.search("Shopping");
print("\n--- User Action: Filter Clear (Show all) ---");
filter.search("");
print("\n--- User Action: Delete Selected Note ---");
delBtn.press();
}