Browse Source

update gallery change refresh

Caijinglong 5 years ago
parent
commit
16c88dbaee
6 changed files with 122 additions and 29 deletions
  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 View File

4
 
4
 
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.
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
 fix:
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
 ## [0.2.0]
16
 ## [0.2.0]
13
 
17
 

+ 15
- 11
README.md View File

14
 
14
 
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.
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
 ![image](https://github.com/CaiJingLong/some_asset/blob/master/image_picker1.gif)
19
 ![image](https://github.com/CaiJingLong/some_asset/blob/master/image_picker1.gif)
20
 
20
 
27
   photo: $latest_version
27
   photo: $latest_version
28
 ```
28
 ```
29
 
29
 
30
-## import
30
+## Import
31
 
31
 
32
 ```dart
32
 ```dart
33
 import 'package:photo/photo.dart';
33
 import 'package:photo/photo.dart';
81
     );
81
     );
82
 ```
82
 ```
83
 
83
 
84
-### about photoPathList params
84
+### About photoPathList params
85
 
85
 
86
 You can use [photo_manager] package to get `List<AssetPathEntity>` and handle or cache.
86
 You can use [photo_manager] package to get `List<AssetPathEntity>` and handle or cache.
87
 
87
 
88
 This parameter is then passed into the `pickAsset` method, where the incoming photoList is rendered instead of the data in the album.
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
 Android native use glide to create image thumb bytes, version is 4.8.0.
102
 Android native use glide to create image thumb bytes, version is 4.8.0.
99
 
103
 
120
 
124
 
121
 see the [github](https://github.com/bumptech/glide#proguard)
125
 see the [github](https://github.com/bumptech/glide#proguard)
122
 
126
 
123
-## about ios
127
+## About ios
124
 
128
 
125
 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.
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
 xcode like image
138
 xcode like image
135
 ![in xcode](https://github.com/CaiJingLong/some_asset/blob/master/flutter_photo2.png)
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
 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)
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
 Part of the Android code comes from [debuggerx01](https://github.com/debuggerx01).
147
 Part of the Android code comes from [debuggerx01](https://github.com/debuggerx01).
144
 
148
 
145
-## donate
149
+## Donate
146
 
150
 
147
 If my code helps you, and you're willing to buy me a cup of coffee.
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 View File

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 View File

5
 import 'package:photo/src/delegate/badge_delegate.dart';
5
 import 'package:photo/src/delegate/badge_delegate.dart';
6
 import 'package:photo/src/delegate/loading_delegate.dart';
6
 import 'package:photo/src/delegate/loading_delegate.dart';
7
 import 'package:photo/src/engine/lru_cache.dart';
7
 import 'package:photo/src/engine/lru_cache.dart';
8
+import 'package:photo/src/engine/throttle.dart';
8
 import 'package:photo/src/entity/options.dart';
9
 import 'package:photo/src/entity/options.dart';
9
 import 'package:photo/src/provider/config_provider.dart';
10
 import 'package:photo/src/provider/config_provider.dart';
10
 import 'package:photo/src/provider/gallery_list_provider.dart';
11
 import 'package:photo/src/provider/gallery_list_provider.dart';
72
 
73
 
73
   bool get useAlbum => widget.photoList == null || widget.photoList.isEmpty;
74
   bool get useAlbum => widget.photoList == null || widget.photoList.isEmpty;
74
 
75
 
76
+  Throttle _changeThrottle;
77
+
75
   @override
78
   @override
76
   void initState() {
79
   void initState() {
77
     super.initState();
80
     super.initState();
78
     _refreshList();
81
     _refreshList();
79
     scaffoldKey = GlobalKey();
82
     scaffoldKey = GlobalKey();
80
     scrollController = ScrollController();
83
     scrollController = ScrollController();
81
-    PhotoManager.addChangeCallback(_onAssetChange);
84
+    _changeThrottle = Throttle(onCall: _onAssetChange);
85
+    PhotoManager.addChangeCallback(_changeThrottle.call);
82
     PhotoManager.startChangeNotify();
86
     PhotoManager.startChangeNotify();
83
   }
87
   }
84
 
88
 
85
   @override
89
   @override
86
   void dispose() {
90
   void dispose() {
87
-    PhotoManager.removeChangeCallback(_onAssetChange);
91
+    PhotoManager.removeChangeCallback(_changeThrottle.call);
88
     PhotoManager.stopChangeNotify();
92
     PhotoManager.stopChangeNotify();
93
+    _changeThrottle.dispose();
89
     scaffoldKey = null;
94
     scaffoldKey = null;
90
     super.dispose();
95
     super.dispose();
91
   }
96
   }
353
     setState(() {});
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
     _currentPath.assetList.then((v) {
364
     _currentPath.assetList.then((v) {
360
       _sortAssetList(v);
365
       _sortAssetList(v);
460
 
465
 
461
   void _onAssetChange() {
466
   void _onAssetChange() {
462
     if (useAlbum) {
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 View File

70
   photo_manager:
70
   photo_manager:
71
     dependency: "direct main"
71
     dependency: "direct main"
72
     description:
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
     version: "0.3.0"
76
     version: "0.3.0"
79
   quiver:
77
   quiver:
80
     dependency: transitive
78
     dependency: transitive

+ 5
- 5
pubspec.yaml View File

11
   flutter:
11
   flutter:
12
     sdk: flutter
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
   # photo_manager:
19
   # photo_manager:
20
   #   path: ../flutter_photo_manager
20
   #   path: ../flutter_photo_manager
21
 
21