-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_example.dart
254 lines (215 loc) · 6.24 KB
/
document_example.dart
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
import 'dart:io';
/// Roles
/// Can render only is published
///
/// Hints:
/// Action is take on the context [document] based on the state of the document.
/// Then before go to next state check the current state and action on it
/// Interface class
abstract interface class DocumentState {
/// Context
final Document document;
const DocumentState({required this.document});
/// View or Render
void render();
/// Publish to the store
void publish();
/// Edit
void edit();
}
class DraftState extends DocumentState {
const DraftState({required super.document});
@override
void render() {
print("DraftState: render is invoked");
if (document.user == UserType.admin || document.user == UserType.editor) {
print("DraftState: rendering the draft content...");
document.showContent();
} else {
print("DraftState: Cannot render draft content for non-admin users");
}
}
@override
void publish() {
print("DraftState: publish is invoked");
if (document.user == UserType.admin) {
document.setState(PublishedState(document: document));
document.state.publish();
} else {
print("DraftState: Cannot publish draft content for non-admin users");
}
}
@override
void edit() {
print("DraftState: Editing is invoked");
if (document.user == UserType.admin || document.user == UserType.editor) {
document.setState(EditingState(document: document));
document.state.edit();
} else {
print("DraftState: Cannot Editing draft content");
}
}
}
class PublishedState extends DocumentState {
const PublishedState({required super.document});
@override
void render() {
print("PublishedState: render is invoked");
print("PublishedState: rendering the published content...");
document.showContent();
}
@override
void publish() {
print("PublishedState: publish is invoked");
if (document.user == UserType.admin) {
print("PublishedState: published....");
} else {
print("PublishedState: Cannot Publish..");
}
}
@override
void edit() {
print("PublishedState: editing is invoked");
print("PublishedState: Cannot edit published content");
}
}
class EditingState extends DocumentState {
const EditingState({required super.document});
@override
void render() {
print("EditingState: render is invoked");
if (document.user == UserType.admin || document.user == UserType.editor) {
print("EditingState: rendering the editing content...");
document.showContent();
} else {
print("EditingState: Cannot render editing content for non-admin users");
}
}
@override
void publish() {
print("EditingState: publish is invoked");
if (document.user == UserType.editor || document.user == UserType.admin) {
print("EditingState: sending the content for moderation...");
document.setState(PublishedState(document: document));
document.state.publish();
} else {
print("EditingState: Cannot publish moderation content");
}
}
@override
void edit() {
print("EditingState: editing is invoked");
if (document.user == UserType.admin || document.user == UserType.editor) {
print("EditingState: editing the content...");
while (true) {
print("\n");
print("For exit: X ");
final String? op = stdin.readLineSync()?.toLowerCase().trim();
document.showContent();
if (op?.contains('x') == true) {
document.setState(DraftState(document: document));
// document.state.render();
break;
} else {
print("Enter new content: ");
final String? newContent = stdin.readLineSync();
document.updateContent(newContent ?? "");
}
}
} else {
print("EditingState: Cannot edit content for non-admin or editor users");
}
}
}
class Document {
late DocumentState _state;
UserType _user;
String _content = "This is the content of the document";
Document(this._user) {
_state = DraftState(document: this);
}
void setState(DocumentState state) {
print("setState: $state");
_state = state;
}
void setUser(UserType user) {
print("setUser: $user");
_user = user;
}
DocumentState get state => _state;
UserType get user => _user;
void showContent() {
print("Document: Showing the content of the document...");
print("Content: $_content");
}
void updateContent(String content) {
_content = content;
print("New Content: $_content");
}
// void render() => _state.render();
// void publish() => _state.publish();
// void edit() => _state.edit();
}
void main() {
Document document = Document(UserType.admin);
while (true) {
print("\n");
print(
"Enter Operation: render: 1 -- publish: 2 -- edit: 3 -- admin: 4 -- editor: 5 -- normal: 6 -- else: exit ");
final String? op = stdin.readLineSync()?.toLowerCase().trim();
if (op?.contains('1') == true) {
document.state.render();
} else if (op?.contains('2') == true) {
document.state.publish();
} else if (op?.contains('3') == true) {
document.state.edit();
} else if (op?.contains('4') == true) {
document.setUser(UserType.admin);
} else if (op?.contains('5') == true) {
document.setUser(UserType.editor);
} else if (op?.contains('6') == true) {
document.setUser(UserType.viewer);
} else {
print("Invalid operation. Please try again.");
break;
}
}
// document.render();
// document.publish();
// document.setState(PublishedState());
// document.render();
// document.publish();
// document.setState(EditingState());
// document.render();
// document.publish();
}
enum UserType { admin, editor, viewer }
/// Old
/*
class Document {
late String _state;
Document() {
_state = "draft";
}
void publish() {
switch (_state) {
case "draft":
_moveToModeration();
break;
case "moderation":
_approveForPublication();
break;
case "published":
break;
}
}
void _moveToModeration() {
_state = "moderation";
print("Document moved to moderation status.");
}
void _approveForPublication() {
_state = "published";
print("Document approved for publishable status.");
}
}
*/