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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* @author lsy
* @date 2019-09-09
**/
import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:gengmei_flutter_plugin/ScanImagePlugn.dart';
import 'package:gengmei_flutter_plugin/gengmei_flutter_plugin.dart';
import 'package:gengmei_flutter_plugin_example/AlbumModel/LiveData.dart';
import 'package:gengmei_flutter_plugin_example/AlbumModel/bean/DirBean.dart';
import 'package:gengmei_flutter_plugin_example/AlbumModel/page/preview/AlbumPreviewPage.dart';
import 'package:gengmei_flutter_plugin_example/AlbumModel/repository/AlbumRepository.dart';
import '../../Anim.dart';
const String MainDir = "IsGengmeiAlbumAllImages";
const String MainDirExplain = "全部相片";
Map<String, List<ScanImageItem>> paseAlbum(Object event) {
var map = Map<String, List<dynamic>>.from(event);
var newMap = Map<String, List<ScanImageItem>>();
map.forEach((k, v) {
var list = List<Map>.from(v);
var scanList = List<ScanImageItem>();
list.forEach((listMap) {
var map2 = Map<String, dynamic>.from(listMap);
ScanImageItem item = new ScanImageItem();
item.path = map2["path"] ?? "";
item.realPath = map2["realPath"] ?? "";
item.isVideo = map2["isVideo"] == "T" ? true : false;
item.during = map2["during"] ?? "0";
item.size = map2["size"] ?? 0;
scanList.add(item);
});
newMap[k] = scanList;
});
return newMap;
}
class AlbumModel {
LiveData<List<ScanImageItem>> albumLive = LiveData();
LiveData<List<DirBean>> dirLive = LiveData();
LiveData<String> titleData = LiveData();
Map<String, List<ScanImageItem>> _mainValue = Map();
List<String> _selectList = List();
String _nowDirName = MainDir;
StreamSubscription _listen;
bool showCamera = true;
int _maxCount = 1;
get selectList => _selectList;
get maxCount => _maxCount;
get nowDirName {
if (_nowDirName == MainDir) {
return MainDirExplain;
} else {
return _nowDirName;
}
}
bool showPop = false;
bool isAndroid = Platform.isAndroid;
List<DirBean> _dirList = List();
get dirList => _dirList;
AlbumModel(this.showCamera, int maxCount, List<String> selectedList) {
this._maxCount = maxCount;
if (selectedList != null && !selectedList.isEmpty) {
this._selectList.clear();
this._selectList.addAll(selectedList);
}
}
void _onEvent(Object event) {
// var map = (event as Map);
// if (_mainValue == null || _mainValue.length == 0) {
// _mainValue = map;
// } else {
// _mainValue.forEach((k, itemList) {
// var key = map[k];
// if (key != null) {
// for (int i = 0; i < itemList.length; i++) {
// if (key[i] == null) {
// continue;
// }
// itemList[i].path = key[i]["path"];
// itemList[i].realPath = key[i]["realPath"];
// }
// }
// });
// }
// if (Platform.isAndroid) {
// var map = (event as Map);
// //Map<String, List<ScanImageItem>>
// if (_mainValue == null || _mainValue.length == 0) {
// _mainValue = map;
// } else {
// _mainValue.forEach((k, itemList) {
// var key = map[k];
// if (key != null) {
// for (int i = 0; i < itemList.length; i++) {
// if (key[i] == null) {
// continue;
// }
// itemList[i].path = key[i]["path"];
// itemList[i].realPath = key[i]["realPath"];
// }
// }
// });
// }
// albumLive.notifyView(_mainValue[_nowDirName]);
// } else {
compute(paseAlbum, event).then((value) {
_mainValue = value;
albumLive.notifyView(_mainValue[_nowDirName]);
});
// }
// _mainValue=newMap;
// var list = map[_nowDirName];
// _mainValue.forEach((k, itemList) {
// var key = map[k];
// if (key != null) {
// for (int i = 0; i < itemList.length; i++) {
// if (key[i] == null) {
// continue;
// }
// itemList[i].path = key[i]["path"];
// itemList[i].realPath = key[i]["realPath"];
// }
// }
// });
// var recordList=_mainValue[_nowDirName];
// List<ScanImageItem> tempList = new List();
// if (list != null && !list.isEmpty) {
//// for (int i = 0; i < albumLive.data.length; i++) {
//// if (list[i]["path"] != null) {
//// albumLive.data[i].path = list[i]["path"];
//// }
//// }
// for (var item in list) {
// ScanImageItem scanImageItem = new ScanImageItem();
// scanImageItem.path = item["path"];
// scanImageItem.size = item["size"];
// scanImageItem.realPath = item["realPath"];
// tempList.add(scanImageItem);
// }
// }
// print("EVENTTTT !! ${tempList.length} ${tempList}");
}
void _onError(Object error) {
print("ERROR $error");
}
void initScanImages(BuildContext context) {
// if (isAndroid) {
_listen = GengmeiFlutterPlugin.phoneImagesEvent
.receiveBroadcastStream()
.listen(_onEvent, onError: _onError);
// }
AlbumRepository.getInstance().scanPhoneImg().listen((value) {
if (value != null) {
_mainValue = value;
value.forEach((key, eachValue) {
// print("VALUE!!! $key");
if (eachValue != null && !eachValue.isEmpty) {
if (key == MainDir) {
titleData.notifyView(MainDirExplain);
albumLive.notifyView(eachValue);
_dirList.add(DirBean(
MainDirExplain, eachValue.length, File(eachValue[0].path)));
} else {
_dirList
.add(DirBean(key, eachValue.length, File(eachValue[0].path)));
}
}
});
}
}).onError((error) {
// Toast.show(context, error.toString());
print(error);
});
}
void changDir() {}
@override
void dispose() {
GengmeiFlutterPlugin.quitPage();
if (_listen != null) {
_listen.cancel();
}
albumLive.dispost();
dirLive.dispost();
titleData.dispost();
}
select(int index) {}
bool isSelect(int index) {
bool haveIt = false;
if (_selectList.isEmpty) {
return false;
}
_selectList.forEach((value) {
if (isAndroid) {
if (value == albumLive.data[index].realPath) {
haveIt = true;
}
} else {
if (value == albumLive.data[index].path) {
haveIt = true;
}
}
});
return haveIt;
}
bool isFullSelect() {
return _selectList.length == _maxCount;
}
void clickItem(BuildContext context, int index) {
String path;
if (isAndroid) {
path = albumLive.data[index].realPath;
} else {
path = albumLive.data[index].path;
}
if (!_selectList.contains(path)) {
if (_selectList.length >= _maxCount) {
// Toast.show(context, "最多选择${_maxCount}张图片");
return;
}
_selectList.add(path);
} else {
_selectList.remove(path);
}
albumLive.notifyView(_mainValue[_nowDirName]);
}
void onNext(BuildContext context) {
if (_selectList.isEmpty) {
Navigator.pop(context, "");
} else {
if (Platform.isAndroid) {
Navigator.pop(context, _selectList[0]);
} else {
iosAlbum(_selectList, context, (value) {
print(value);
// iosAlbum(_selectList, context, (image) {
// iosAlbum(_selectList, context, (video) {
// print("IM $image VIDEI ${video}");
// });
// });
Navigator.pop(context, List<Map>.from(value));
});
}
}
}
void iosAlbum(List<String> list, BuildContext context, Function fun) {
GengmeiFlutterPlugin.ios_album_path(list).then((value) {
fun(value);
}).catchError((error) {
print(error);
});
}
void nativeCamera(BuildContext context) {
if (_selectList.length == maxCount) {
// Toast.show(context, "最多选择${_maxCount}张图片");
return;
}
AlbumRepository.getInstance().nativeCamera().listen((data) {
if (data == null) {
// Toast.show(context, "没有拍摄照片");
} else {
print(data);
ScanImageItem item = new ScanImageItem();
item.realPath =
data["realPath"] == null ? null : data["realPath"] as String;
item.path = data["path"] as String;
item.isVideo = false;
String foldName = data["folderName"] as String;
var list = _mainValue[foldName];
if (list == null) {
_mainValue[foldName] = new List();
_mainValue[foldName].add(item);
} else {
list.insert(0, item);
}
_mainValue[MainDir].insert(0, item);
_selectList.add(item.realPath);
bool haveIt = false;
_dirList.forEach((it) {
if (it.dirName == foldName) {
haveIt = true;
it.picCount++;
}
});
if (!haveIt) {
_dirList.add(new DirBean(foldName, 1, File(item.path)));
}
dirLive.notifyView(_dirList);
albumLive.notifyView(_mainValue[_nowDirName]);
// selectSizeLive.notifyView(_selectList.length + _selectVideoList.length);
// Navigator.pop(context, data);
// _selectList.add(data);
//TODO
}
}).onError((error) {
// Toast.show(context, error);
print(error);
});
}
void AiCamera(BuildContext context) {
if (_selectList.length == maxCount) {
// Toast.show(context, "最多选择${_maxCount}张图片");
return;
}
// AlbumRepository.getInstance().aiCamera().listen((data) {
// if (data == null || data.isEmpty || data == "") {
//// Toast.show(context, "没有拍摄照片");
// } else {
// Navigator.pop(context, data);
//// _selectList.add(data);
// //TODO
// }
// }).onError((error) {});
}
File getDirPic(int index) {
return File(_mainValue[_dirList[index]][0].path);
}
void changPopState() {
if (showPop) {
showPop = false;
dirLive.notifyView(null);
} else {
showPop = true;
dirLive.notifyView(_dirList);
}
titleData.notifyView(nowDirName);
}
void selectDir(int index) {
String dirName = _dirList[index].dirName;
titleData.notifyView(dirName);
if (dirName == MainDirExplain) {
dirName = MainDir;
}
_nowDirName = dirName;
dirLive.notifyView(null);
showPop = false;
albumLive.notifyView(_mainValue[dirName]);
}
bool previewItemClick = false;
void previewItem(BuildContext context, int index, String pageName) {
String path;
if (Platform.isAndroid) {
path = albumLive.data[index].realPath;
if (albumLive.data[index].isVideo) {
GengmeiFlutterPlugin.playAlbumVideo(path);
} else {
Navigator.push(context, CustomRoute(AlbumPreviewPage(path, pageName)));
}
} else {
path = albumLive.data[index].path;
if (albumLive.data[index].isVideo) {
GengmeiFlutterPlugin.playAlbumVideo(path);
} else {
GengmeiFlutterPlugin.previewImage(path);
// if (previewItemClick) {
// return;
// }
// previewItemClick = true;
// iosItem(path, context, (value) {
// var realPath = value["realImagePath"];
// Navigator.push(
// context, CustomRoute(AlbumPreviewPage(realPath, pageName)));
// });
}
// Navigator.push(
// context, CustomRoute(AlbumPreviewPage(albumLive.data[index].path, pageName)));
}
}
void iosItem(String path, BuildContext context, Function fun) {
// GengmeiFlutterPlugin.ios_album_item(path).then((value) {
// if (value != null) {
// fun(Map<String, String>.from(value));
// }
// previewItemClick = false;
// }).catchError((error) {
// previewItemClick = false;
// print(error);
// });
}
}