-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull_tracking_example.dart
183 lines (151 loc) · 5.37 KB
/
full_tracking_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
/// Context
class LocationTracker {
/// Location tracking is enabled by default
/// Composition with the state interface
late TrackingState _trackingState;
LocationTracker() {
_trackingState = EnabledTrackingState(tracker: this);
}
void startTracking() {
_trackingState.startTracking();
}
void stopTracking() {
_trackingState.stopTracking();
}
void pauseTracking({required Duration duration}) {
_trackingState.pauseTracking(duration: duration);
}
void makeCheckIn() {
_trackingState.makeCheckIn();
}
void findMyChildren() {
_trackingState.findMyChildren();
}
void updateState({required TrackingState state}) {
_trackingState = state;
}
}
/// Abstract interface State
abstract interface class TrackingState {
void startTracking();
void stopTracking();
void pauseTracking({required Duration duration});
void makeCheckIn();
void findMyChildren();
}
class EnabledTrackingState implements TrackingState {
final LocationTracker? _tracker;
EnabledTrackingState({required LocationTracker? tracker})
: _tracker = tracker;
@override
void startTracking() {
print("EnabledTrackingState: startTracking is invoked");
print("EnabledTrackingState: tracking location....1");
print("EnabledTrackingState: tracking location....2");
print("EnabledTrackingState: tracking location....3");
}
@override
void stopTracking() {
print("EnabledTrackingState: Received 'stop tracking'");
print("EnabledTrackingState: Changing state to 'disabled'...");
_tracker?.updateState(state: DisabledTrackingState(tracker: _tracker));
_tracker?.stopTracking();
}
@override
void pauseTracking({required Duration duration}) {
print(
"EnabledTrackingState: Received 'pause tracking' for ${duration.inSeconds} seconds");
print("EnabledTrackingState: Changing state to 'disabled'...");
_tracker?.updateState(state: DisabledTrackingState(tracker: _tracker));
_tracker?.pauseTracking(duration: duration);
}
@override
void makeCheckIn() {
print("EnabledTrackingState: performing check-in at the current location");
}
@override
void findMyChildren() {
print("EnabledTrackingState: searching for children...");
}
}
class DisabledTrackingState implements TrackingState {
final LocationTracker? _tracker;
DisabledTrackingState({required LocationTracker? tracker})
: _tracker = tracker;
@override
void startTracking() {
print("DisabledTrackingState: Received 'start tracking'");
print("DisabledTrackingState: Changing state to 'enabled'...");
_tracker?.updateState(state: EnabledTrackingState(tracker: _tracker));
}
@override
void pauseTracking({required Duration duration}) async {
print("DisabledTrackingState: Pause tracking for ${duration.inSeconds}");
await Future.delayed(duration);
print("DisabledTrackingState: Time is over");
print("DisabledTrackingState: Returning to 'enabled state'...\n");
_tracker?.updateState(state: EnabledTrackingState(tracker: _tracker));
_tracker?.startTracking();
}
@override
void stopTracking() {
print("DisabledTrackingState: Received 'stop tracking'");
print("DisabledTrackingState: Do nothing...");
}
@override
void makeCheckIn() {
print("DisabledTrackingState: Received 'make check-in'");
print("DisabledTrackingState: Changing state to 'enabled'...");
_tracker?.updateState(state: EnabledTrackingState(tracker: _tracker));
_tracker?.makeCheckIn();
}
@override
void findMyChildren() {
print("DisabledTrackingState: Received 'find my children'");
print("DisabledTrackingState: Changing state to 'enabled'...");
_tracker?.updateState(state: EnabledTrackingState(tracker: _tracker));
_tracker?.findMyChildren();
}
}
void main() {
print("Client: I'm starting working with a location tracker");
final tracker = LocationTracker();
print("---------------");
tracker.startTracking();
print("---------------");
tracker.pauseTracking(duration: Duration(seconds: 2));
print("---------------");
tracker.makeCheckIn();
print("---------------");
tracker.findMyChildren();
print("---------------");
tracker.stopTracking();
}
// Output Execution result
// Client: I'm starting working with a location tracker
// ---------------
// EnabledTrackingState: startTracking is invoked
// EnabledTrackingState: tracking location....1
// EnabledTrackingState: tracking location....2
// EnabledTrackingState: tracking location....3
// ---------------
// EnabledTrackingState: Received 'pause tracking' for 2
// EnabledTrackingState: Changing state to 'disabled'...
// DisabledTrackingState: Pause tracking for 2
// ---------------
// DisabledTrackingState: Received 'make check-in'
// DisabledTrackingState: Changing state to 'enabled'...
// EnabledTrackingState: performing check-in at the current location
// ---------------
// EnabledTrackingState: searching for children...
// ---------------
// EnabledTrackingState: Received 'stop tracking'
// EnabledTrackingState: Changing state to 'disabled'...
// DisabledTrackingState: Received 'stop tracking'
// DisabledTrackingState: Do nothing...
// DisabledTrackingState: Time is over
// DisabledTrackingState: Returning to 'enabled state'...
// EnabledTrackingState: startTracking is invoked
// EnabledTrackingState: tracking location....1
// EnabledTrackingState: tracking location....2
// EnabledTrackingState: tracking location....3