-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress_cubit.dart
184 lines (171 loc) · 4.9 KB
/
progress_cubit.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
import 'dart:isolate';
import 'package:bloc/bloc.dart';
import 'package:flutter_isolate/flutter_isolate.dart';
import 'package:workers/api/network.dart';
import 'package:workers/injection.dart';
import 'package:workers/sql/database_helper.dart';
part 'progress_state.dart';
class ProgressCubit extends Cubit<ProgressState> {
ProgressCubit() : super(ProgressInitial());
var progress = 0.0;
final DatabaseHelper databaseHelper = injection<DatabaseHelper>();
//static final NetworkApi networkApi = NetworkApi();
void updateProgress() {
emit(ProgressRunState(progress: progress));
if (progress == 10.0) {
Future.delayed(const Duration(seconds: 1), () {
emit(ProgressInitial());
progress = 0.0;
});
}
}
void pauseIsolate() {
FlutterIsolate.current.pause();
emit(ProgressPausState());
}
void resumeIsolate() {
FlutterIsolate.current.resume();
updateProgress();
}
void getlocal() {
syncData(DatabaseHelper.getdataintoLoacal);
//databaseHelper.getdataintoLoacal(await databaseHelper.getCacheUser());
}
void mainIsolate() {
syncData(NetworkApi.apiOne).then((value) {
progress++;
updateProgress();
});
syncData(NetworkApi.apiTwo).then((value) {
progress++;
updateProgress();
});
syncData(NetworkApi.apiThree).then((value) {
progress++;
updateProgress();
});
syncData(NetworkApi.apiFour).then((value) {
progress++;
updateProgress();
});
syncData(NetworkApi.apiFive).then((value) {
progress++;
updateProgress();
});
syncData(NetworkApi.apiSix).then((value) {
progress++;
updateProgress();
});
syncData(NetworkApi.apiSeven).then((value) {
progress++;
updateProgress();
});
syncData(NetworkApi.apiEight).then((value) {
progress++;
updateProgress();
});
syncData(NetworkApi.apiNine).then((value) {
progress++;
updateProgress();
});
syncData(NetworkApi.apiTen).then((value) {
progress++;
updateProgress();
});
}
Future<void> syncData(void Function(SendPort) entryPoint) async {
final receivePort = ReceivePort();
final isolate =
await FlutterIsolate.spawn(entryPoint, receivePort.sendPort);
receivePort.listen((dynamic response) async {
if (response is String) {
print(response);
} else if (response is Error) {}
isolate.kill();
print("Kill Isolate");
});
}
void clearUserCatch() {
emit(ProgressRunState(progress: 0));
databaseHelper.clearCacheUser();
}
void clearNewsCatch() {
databaseHelper.clearCacheNews();
}
}
// class ProgressCubit extends Cubit<ProgressState> {
// ProgressCubit() : super(ProgressInitial());
// var progress = 0.0;
// final DatabaseHelper databaseHelper = injection<DatabaseHelper>();
// final Set<FlutterIsolate> _isolates = {};
// late FlutterIsolate isolate;
// //static final NetworkApi networkApi = NetworkApi();
// void updateProgress() {
// emit(ProgressRunState(progress: progress));
// if (progress == 10.0) {
// Future.delayed(const Duration(seconds: 1), () {
// emit(ProgressInitial());
// progress = 0.0;
// });
// }
// }
//
// void pauseIsolate() {
// isolate.pause();
// emit(ProgressPausState());
// }
//
// void resumeIsolate() {
// Completer<FlutterIsolate>().future;
// isolate.resume();
// updateProgress();
// }
//
//
// void mainIsolate() async {
// final receivePort = ReceivePort();
// isolate =
// await FlutterIsolate.spawn(apiCalling, receivePort.sendPort);
// receivePort.listen((dynamic response) async {
// if (response is String) {
// progress++;
// updateProgress();
// } else if (response is Error) {}
// if(progress==10){
// isolate.kill();
// print("Kill Isolate");
// emit(ProgressInitial());
// }
// });
// }
// @pragma('vm:entry-point')
// static void apiCalling(SendPort sendPort) async {
// // int i =1;
// // Timer.periodic(Duration(seconds:1),(timer){
// // print("Timer Running From Isolate $i");
// // i++;
// // sendPort.send("Add succeed");
// // });
// print("Call API Four");
// for(int i =0; i < 10; i++) {
// final data = await http
// .get(Uri.parse('https://random-data-api.com/api/v2/users?size=$i'));
// final dataList = jsonDecode(data.body);
// print(dataList);
// sendPort.send("Add succeed");
// }
// // sendPort.send("Add succeed");
// }
// void getlocal() {
// // syncData(DatabaseHelper.getdataintoLoacal);
// //databaseHelper.getdataintoLoacal(await databaseHelper.getCacheUser());
// }
// void clearUserCatch() {
// emit(ProgressRunState(progress: 0));
// databaseHelper.clearCacheUser();
// }
//
// void clearNewsCatch() {
// databaseHelper.clearCacheNews();
// }
// }