Ver código fonte

add sort asset delegate

Caijinglong 5 anos atrás
pai
commit
927897f716

+ 18
- 0
lib/src/delegate/sort_asset_delegate.dart Ver arquivo

@@ -0,0 +1,18 @@
1
+part of './sort_delegate.dart';
2
+
3
+abstract class SortAssetDelegate {
4
+  const SortAssetDelegate();
5
+
6
+  void sort(List<AssetEntity> list);
7
+}
8
+
9
+class DefaultAssetDelegate extends SortAssetDelegate {
10
+  const DefaultAssetDelegate();
11
+
12
+  @override
13
+  void sort(List<AssetEntity> list) {
14
+    list.sort((entity1, entity2) {
15
+      return entity2.createTime.compareTo(entity1.createTime);
16
+    });
17
+  }
18
+}

+ 14
- 3
lib/src/delegate/sort_delegate.dart Ver arquivo

@@ -1,7 +1,14 @@
1 1
 import 'package:photo_manager/photo_manager.dart';
2 2
 
3
+part './sort_asset_delegate.dart';
4
+
5
+/// SortPathDelegate
3 6
 abstract class SortDelegate {
4
-  const SortDelegate();
7
+  final SortAssetDelegate assetDelegate;
8
+
9
+  const SortDelegate({
10
+    this.assetDelegate = const DefaultAssetDelegate(),
11
+  });
5 12
 
6 13
   void sort(List<AssetPathEntity> list);
7 14
 
@@ -11,14 +18,18 @@ abstract class SortDelegate {
11 18
 }
12 19
 
13 20
 class DefaultSortDelegate extends SortDelegate {
14
-  const DefaultSortDelegate();
21
+  const DefaultSortDelegate({
22
+    SortAssetDelegate assetDelegate = const DefaultAssetDelegate(),
23
+  }) : super(assetDelegate: assetDelegate);
15 24
 
16 25
   @override
17 26
   void sort(List<AssetPathEntity> list) {}
18 27
 }
19 28
 
20 29
 class CommonSortDelegate extends SortDelegate {
21
-  const CommonSortDelegate();
30
+  const CommonSortDelegate({
31
+    SortAssetDelegate assetDelegate = const DefaultAssetDelegate(),
32
+  }) : super(assetDelegate: assetDelegate);
22 33
 
23 34
   @override
24 35
   void sort(List<AssetPathEntity> list) {

+ 7
- 0
lib/src/ui/page/photo_main_page.dart Ver arquivo

@@ -178,6 +178,7 @@ class _PhotoMainPageState extends State<PhotoMainPage>
178 178
     galleryPathList.addAll(widget.photoList);
179 179
     this.list.clear();
180 180
     var assetList = await galleryPathList[0].assetList;
181
+    _sortAssetList(assetList);
181 182
     this.list.addAll(assetList);
182 183
     setState(() {
183 184
       _isInit = true;
@@ -210,6 +211,7 @@ class _PhotoMainPageState extends State<PhotoMainPage>
210 211
 
211 212
     if (pathList.isNotEmpty) {
212 213
       imageList = await pathList[0].assetList;
214
+      _sortAssetList(imageList);
213 215
       _currentPath = pathList[0];
214 216
     }
215 217
 
@@ -228,6 +230,10 @@ class _PhotoMainPageState extends State<PhotoMainPage>
228 230
     });
229 231
   }
230 232
 
233
+  void _sortAssetList(List<AssetEntity> assetList) {
234
+    options?.sortDelegate?.assetDelegate?.sort(assetList);
235
+  }
236
+
231 237
   Widget _buildBody() {
232 238
     if (!_isInit) {
233 239
       return _buildLoading();
@@ -342,6 +348,7 @@ class _PhotoMainPageState extends State<PhotoMainPage>
342 348
     _currentPath = value;
343 349
 
344 350
     _currentPath.assetList.then((v) {
351
+      _sortAssetList(v);
345 352
       list.clear();
346 353
       list.addAll(v);
347 354
       scrollController.jumpTo(0.0);