-
Notifications
You must be signed in to change notification settings - Fork 321
/
photo_provider.dart
119 lines (95 loc) · 2.62 KB
/
photo_provider.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
import 'package:flutter/foundation.dart';
import 'package:image_scanner_example/main.dart';
import 'package:photo_manager/photo_manager.dart';
class PhotoProvider extends ChangeNotifier {
List<AssetPathEntity> list = [];
int type = 0;
DateTime dt = DateTime.now();
var hasAll = true;
Map<AssetPathEntity, PathProvider> pathProviderMap = {};
void changeType(int v) {
this.type = v;
notifyListeners();
}
void changeHasAll(bool value) {
this.hasAll = value;
notifyListeners();
}
void changeDateToNow() {
this.dt = DateTime.now();
notifyListeners();
}
void changeDate(DateTime pickDt) {
this.dt = pickDt;
notifyListeners();
}
void reset() {
this.list.clear();
pathProviderMap.clear();
}
Future<void> refreshGalleryList() async {
var galleryList = await PhotoManager.getAssetPathList(
fetchDateTime: dt,
type: RequestType.values[type],
hasAll: hasAll,
);
galleryList.sort((s1, s2) {
return s2.assetCount.compareTo(s1.assetCount);
});
this.list.addAll(galleryList);
}
PathProvider getOrCreatePathProvider(AssetPathEntity pathEntity) {
pathProviderMap[pathEntity] ??= PathProvider(pathEntity);
return pathProviderMap[pathEntity];
}
}
class PathProvider extends ChangeNotifier {
static const loadCount = 50;
bool isInit = false;
final AssetPathEntity path;
PathProvider(this.path);
List<AssetEntity> list = [];
var page = 0;
int get showItemCount {
if (list.length == path.assetCount) {
return path.assetCount;
} else {
return path.assetCount;
}
}
Future onRefresh() async {
final list = await path.getAssetListPaged(0, loadCount);
page = 0;
this.list.clear();
this.list.addAll(list);
isInit = true;
notifyListeners();
printListLength("onRefresh");
}
Future<void> onLoadMore() async {
if (showItemCount > path.assetCount) {
print("already max");
return;
}
final list = await path.getAssetListPaged(page + 1, loadCount);
page = page + 1;
this.list.addAll(list);
notifyListeners();
printListLength("loadmore");
}
void delete(AssetEntity entity) async {
final result = await PhotoManager.editor.deleteWithIds([entity.id]);
if (result.isNotEmpty) {
await path.refreshPathProperties(dt: path.fetchDatetime);
final list =
await path.getAssetListRange(start: 0, end: provider.list.length);
printListLength("deleted");
this.list.clear();
this.list.addAll(list);
notifyListeners();
}
}
void printListLength(String tag) {
print("$tag length : ${list.length}");
}
}