瀏覽代碼

Merge pull request #33 from CaiJingLong/auto-refresh-patch-2

update gallery change refresh
C 5 年之前
父節點
當前提交
ca58e10765
No account linked to committer's email address
共有 6 個檔案被更改,包括 122 行新增29 行删除
  1. 6
    2
      CHANGELOG.md
  2. 15
    11
      README.md
  3. 52
    0
      lib/src/engine/throttle.dart
  4. 41
    6
      lib/src/ui/page/photo_main_page.dart
  5. 3
    5
      pubspec.lock
  6. 5
    5
      pubspec.yaml

+ 6
- 2
CHANGELOG.md 查看文件

@@ -4,10 +4,14 @@
4 4
 
5 5
 **Breaking change**. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.
6 6
 
7
+add:
8
+
9
+- When the album changes, it refreshes in real time.
10
+
7 11
 fix:
8 12
 
9
-- duration badge
10
-- sort image
13
+- Video duration badge time problem.
14
+- Images sort by create time.
11 15
 
12 16
 ## [0.2.0]
13 17
 

+ 15
- 11
README.md 查看文件

@@ -14,7 +14,7 @@ use flutter as ui
14 14
 
15 15
 if you want to build custom ui, you just need api to make custom ui. to use [photo_manager](https://github.com/CaiJingLong/flutter_photo_manager) or fork the library to custom ui.
16 16
 
17
-## screenshot
17
+## Screenshot
18 18
 
19 19
 ![image](https://github.com/CaiJingLong/some_asset/blob/master/image_picker1.gif)
20 20
 
@@ -27,7 +27,7 @@ dependencies:
27 27
   photo: $latest_version
28 28
 ```
29 29
 
30
-## import
30
+## Import
31 31
 
32 32
 ```dart
33 33
 import 'package:photo/photo.dart';
@@ -81,19 +81,23 @@ void pickAsset() async {
81 81
     );
82 82
 ```
83 83
 
84
-### about photoPathList params
84
+### About photoPathList params
85 85
 
86 86
 You can use [photo_manager] package to get `List<AssetPathEntity>` and handle or cache.
87 87
 
88 88
 This parameter is then passed into the `pickAsset` method, where the incoming photoList is rendered instead of the data in the album.
89 89
 
90
-## whole example
90
+## Whole example
91 91
 
92
-you can see [github](https://github.com/caijinglong/flutter_photo/blob/master/example/) [main.dart](https://github.com/caijinglong/flutter_photo/blob/master/example/lib/main.dart)
92
+You can see [github](https://github.com/caijinglong/flutter_photo/blob/master/example/) [main.dart](https://github.com/caijinglong/flutter_photo/blob/master/example/lib/main.dart)
93 93
 
94
-## about android
94
+## About android
95 95
 
96
-### glide
96
+### Migrate to androidX
97
+
98
+See the [gitbook](https://caijinglong.gitbooks.io/migrate-flutter-to-androidx/content/)
99
+
100
+### Glide
97 101
 
98 102
 Android native use glide to create image thumb bytes, version is 4.8.0.
99 103
 
@@ -120,7 +124,7 @@ if you use the proguard
120 124
 
121 125
 see the [github](https://github.com/bumptech/glide#proguard)
122 126
 
123
-## about ios
127
+## About ios
124 128
 
125 129
 Because the album is a privacy privilege, you need user permission to access it. You must to modify the `Info.plist` file in Runner project.
126 130
 
@@ -134,15 +138,15 @@ like next
134 138
 xcode like image
135 139
 ![in xcode](https://github.com/CaiJingLong/some_asset/blob/master/flutter_photo2.png)
136 140
 
137
-### build error
141
+### Build error
138 142
 
139 143
 if you build error like include of non-modular header inside framework module, see [#10](https://github.com/CaiJingLong/flutter_photo/issues/10) or [so](https://stackoverflow.com/questions/27776497/include-of-non-modular-header-inside-framework-module)
140 144
 
141
-## thanks
145
+## Thanks
142 146
 
143 147
 Part of the Android code comes from [debuggerx01](https://github.com/debuggerx01).
144 148
 
145
-## donate
149
+## Donate
146 150
 
147 151
 If my code helps you, and you're willing to buy me a cup of coffee.
148 152
 

+ 52
- 0
lib/src/engine/throttle.dart 查看文件

@@ -0,0 +1,52 @@
1
+import 'dart:async';
2
+
3
+import 'package:meta/meta.dart';
4
+
5
+typedef VoidCallback();
6
+
7
+/// When multiple calls are repeated, only the first time is valid.
8
+///
9
+/// Like rxdart `throttle` method
10
+class Throttle {
11
+  Duration duration;
12
+
13
+  VoidCallback onCall;
14
+
15
+  bool _isRunning = false;
16
+
17
+  Timer _timer;
18
+
19
+  Throttle({
20
+    @required this.onCall,
21
+    this.duration = const Duration(seconds: 2),
22
+  });
23
+
24
+  void call() {
25
+    if (!_isRunning) {
26
+      _startTimer();
27
+      onCall?.call();
28
+    }
29
+  }
30
+
31
+  void _startTimer() {
32
+    if (_timer != null) {
33
+      _stopTimer();
34
+    }
35
+    _isRunning = true;
36
+    _timer = Timer(duration, () {
37
+      _isRunning = false;
38
+      _timer = null;
39
+    });
40
+  }
41
+
42
+  void _stopTimer() {
43
+    _timer?.cancel();
44
+    _isRunning = false;
45
+    _timer = null;
46
+  }
47
+
48
+  void dispose() {
49
+    this.onCall = null;
50
+    _stopTimer();
51
+  }
52
+}

+ 41
- 6
lib/src/ui/page/photo_main_page.dart 查看文件

@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
5 5
 import 'package:photo/src/delegate/badge_delegate.dart';
6 6
 import 'package:photo/src/delegate/loading_delegate.dart';
7 7
 import 'package:photo/src/engine/lru_cache.dart';
8
+import 'package:photo/src/engine/throttle.dart';
8 9
 import 'package:photo/src/entity/options.dart';
9 10
 import 'package:photo/src/provider/config_provider.dart';
10 11
 import 'package:photo/src/provider/gallery_list_provider.dart';
@@ -72,20 +73,24 @@ class _PhotoMainPageState extends State<PhotoMainPage>
72 73
 
73 74
   bool get useAlbum => widget.photoList == null || widget.photoList.isEmpty;
74 75
 
76
+  Throttle _changeThrottle;
77
+
75 78
   @override
76 79
   void initState() {
77 80
     super.initState();
78 81
     _refreshList();
79 82
     scaffoldKey = GlobalKey();
80 83
     scrollController = ScrollController();
81
-    PhotoManager.addChangeCallback(_onAssetChange);
84
+    _changeThrottle = Throttle(onCall: _onAssetChange);
85
+    PhotoManager.addChangeCallback(_changeThrottle.call);
82 86
     PhotoManager.startChangeNotify();
83 87
   }
84 88
 
85 89
   @override
86 90
   void dispose() {
87
-    PhotoManager.removeChangeCallback(_onAssetChange);
91
+    PhotoManager.removeChangeCallback(_changeThrottle.call);
88 92
     PhotoManager.stopChangeNotify();
93
+    _changeThrottle.dispose();
89 94
     scaffoldKey = null;
90 95
     super.dispose();
91 96
   }
@@ -353,8 +358,8 @@ class _PhotoMainPageState extends State<PhotoMainPage>
353 358
     setState(() {});
354 359
   }
355 360
 
356
-  void _onGalleryChange(AssetPathEntity value) {
357
-    _currentPath = value;
361
+  void _onGalleryChange(AssetPathEntity assetPathEntity) {
362
+    _currentPath = assetPathEntity;
358 363
 
359 364
     _currentPath.assetList.then((v) {
360 365
       _sortAssetList(v);
@@ -460,8 +465,38 @@ class _PhotoMainPageState extends State<PhotoMainPage>
460 465
 
461 466
   void _onAssetChange() {
462 467
     if (useAlbum) {
463
-      _refreshList();
468
+      _onPhotoRefresh();
469
+    }
470
+  }
471
+
472
+  void _onPhotoRefresh() async {
473
+    List<AssetPathEntity> pathList;
474
+    switch (options.pickType) {
475
+      case PickType.onlyImage:
476
+        pathList = await PhotoManager.getImageAsset();
477
+        break;
478
+      case PickType.onlyVideo:
479
+        pathList = await PhotoManager.getVideoAsset();
480
+        break;
481
+      default:
482
+        pathList = await PhotoManager.getAssetPathList();
483
+    }
484
+
485
+    if (pathList == null) {
486
+      return;
487
+    }
488
+
489
+    this.galleryPathList.clear();
490
+    this.galleryPathList.addAll(pathList);
491
+
492
+    if (!this.galleryPathList.contains(this.currentPath)) {
493
+      // current path is deleted , 当前的相册被删除, 应该提示刷新
494
+      if (this.galleryPathList.length > 0) {
495
+        _onGalleryChange(this.galleryPathList[0]);
496
+      }
497
+      return;
464 498
     }
499
+    // Not deleted
500
+    _onGalleryChange(this.currentPath);
465 501
   }
466
-  
467 502
 }

+ 3
- 5
pubspec.lock 查看文件

@@ -70,11 +70,9 @@ packages:
70 70
   photo_manager:
71 71
     dependency: "direct main"
72 72
     description:
73
-      path: "."
74
-      ref: "8ef0bf9047cdd86e2331f0db8d15c4c50f35610d"
75
-      resolved-ref: "8ef0bf9047cdd86e2331f0db8d15c4c50f35610d"
76
-      url: "https://github.com/CaiJingLong/flutter_photo_manager.git"
77
-    source: git
73
+      name: photo_manager
74
+      url: "https://pub.flutter-io.cn"
75
+    source: hosted
78 76
     version: "0.3.0"
79 77
   quiver:
80 78
     dependency: transitive

+ 5
- 5
pubspec.yaml 查看文件

@@ -11,11 +11,11 @@ dependencies:
11 11
   flutter:
12 12
     sdk: flutter
13 13
 
14
-  # photo_manager: ^0.2.1
15
-  photo_manager:
16
-    git:
17
-      url: https://github.com/CaiJingLong/flutter_photo_manager.git
18
-      ref: 8ef0bf9047cdd86e2331f0db8d15c4c50f35610d
14
+  photo_manager: ^0.3.0
15
+  # photo_manager:
16
+  #   git:
17
+  #     url: https://github.com/CaiJingLong/flutter_photo_manager.git
18
+  #     ref: 2e4821c1fa797272e4c454366b4fb264e8a054d6
19 19
   # photo_manager:
20 20
   #   path: ../flutter_photo_manager
21 21