Skip to content

Commit f28a701

Browse files
authored
🚸 Update how paths get update (#312)
1 parent adc6c02 commit f28a701

File tree

2 files changed

+95
-27
lines changed

2 files changed

+95
-27
lines changed

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -810,10 +810,33 @@ class DefaultAssetPickerBuilderDelegate
810810
return;
811811
}
812812
final AssetPathEntity? _currentPathEntity = provider.currentPath;
813+
if (call.arguments is Map) {
814+
final Map<dynamic, dynamic> arguments =
815+
call.arguments as Map<dynamic, dynamic>;
816+
if (arguments['newCount'] == 0) {
817+
provider
818+
..currentAssets = <AssetEntity>[]
819+
..currentPath = null
820+
..hasAssetsToDisplay = false
821+
..isAssetsEmpty = true;
822+
return;
823+
}
824+
if (_currentPathEntity == null) {
825+
await provider.getPaths();
826+
}
827+
}
813828
if (_currentPathEntity != null) {
814-
provider.currentPath = await _currentPathEntity.obtainForNewProperties();
815-
await provider.switchPath(_currentPathEntity);
829+
final AssetPathEntity newPath =
830+
await _currentPathEntity.obtainForNewProperties();
831+
provider
832+
..currentPath = newPath
833+
..hasAssetsToDisplay = newPath.assetCount != 0
834+
..isAssetsEmpty = newPath.assetCount == 0
835+
..totalAssetsCount = newPath.assetCount;
816836
isSwitchingPath.value = false;
837+
if (newPath.isAll) {
838+
await provider.getAssetsFromCurrentPath();
839+
}
817840
}
818841
}
819842

@@ -1375,7 +1398,7 @@ class DefaultAssetPickerBuilderDelegate
13751398
/// Return actual length if current path is all.
13761399
/// 如果当前目录是全部内容,则返回实际的内容数量。
13771400
final int _length = assets.length + placeholderCount;
1378-
if (!currentPathEntity!.isAll) {
1401+
if (currentPathEntity?.isAll != true) {
13791402
return _length;
13801403
}
13811404
switch (specialItemPosition) {
@@ -1679,10 +1702,34 @@ class DefaultAssetPickerBuilderDelegate
16791702

16801703
@override
16811704
Widget pathEntitySelector(BuildContext context) {
1705+
Widget _text(
1706+
BuildContext context,
1707+
String text,
1708+
String semanticsText,
1709+
) {
1710+
return Flexible(
1711+
child: ScaleText(
1712+
text,
1713+
style: const TextStyle(
1714+
fontSize: 17,
1715+
fontWeight: FontWeight.normal,
1716+
),
1717+
maxLines: 1,
1718+
overflow: TextOverflow.fade,
1719+
maxScaleFactor: 1.2,
1720+
semanticsLabel: semanticsText,
1721+
),
1722+
);
1723+
}
1724+
16821725
return UnconstrainedBox(
16831726
child: GestureDetector(
16841727
onTap: () {
16851728
Feedback.forTap(context);
1729+
if (isPermissionLimited && provider.isAssetsEmpty) {
1730+
PhotoManager.presentLimited();
1731+
return;
1732+
}
16861733
isSwitchingPath.value = !isSwitchingPath.value;
16871734
},
16881735
child: Container(
@@ -1700,23 +1747,21 @@ class DefaultAssetPickerBuilderDelegate
17001747
builder: (_, AssetPathEntity? p, Widget? w) => Row(
17011748
mainAxisSize: MainAxisSize.min,
17021749
children: <Widget>[
1750+
if (p == null && isPermissionLimited)
1751+
_text(
1752+
context,
1753+
textDelegate.changeAccessibleLimitedAssets,
1754+
semanticsTextDelegate.changeAccessibleLimitedAssets,
1755+
),
17031756
if (p != null)
1704-
Flexible(
1705-
child: ScaleText(
1706-
isPermissionLimited && p.isAll
1707-
? textDelegate.accessiblePathName
1708-
: pathNameBuilder?.call(p) ?? p.name,
1709-
style: const TextStyle(
1710-
fontSize: 17,
1711-
fontWeight: FontWeight.normal,
1712-
),
1713-
maxLines: 1,
1714-
overflow: TextOverflow.ellipsis,
1715-
maxScaleFactor: 1.2,
1716-
semanticsLabel: isPermissionLimited && p.isAll
1717-
? semanticsTextDelegate.accessiblePathName
1718-
: pathNameBuilder?.call(p) ?? p.name,
1719-
),
1757+
_text(
1758+
context,
1759+
isPermissionLimited && p.isAll
1760+
? textDelegate.accessiblePathName
1761+
: pathNameBuilder?.call(p) ?? p.name,
1762+
isPermissionLimited && p.isAll
1763+
? semanticsTextDelegate.accessiblePathName
1764+
: pathNameBuilder?.call(p) ?? p.name,
17201765
),
17211766
w!,
17221767
],

lib/src/provider/asset_picker_provider.dart

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
155155
Path? _currentPath;
156156

157157
set currentPath(Path? value) {
158-
if (value == null || value == _currentPath) {
158+
if (value == _currentPath) {
159159
return;
160160
}
161161
_currentPath = value;
@@ -282,6 +282,30 @@ class DefaultAssetPickerProvider
282282
/// 将会与基础条件进行合并。
283283
final FilterOptionGroup? filterOptions;
284284

285+
@override
286+
set currentPath(AssetPathEntity? value) {
287+
if (value == _currentPath) {
288+
return;
289+
}
290+
_currentPath = value;
291+
if (value != null &&
292+
_pathsList.keys.any((AssetPathEntity p) => p.id == value.id)) {
293+
final AssetPathEntity previous = _pathsList.keys.singleWhere(
294+
(AssetPathEntity p) => p.id == value.id,
295+
);
296+
final int index = _pathsList.keys.toList().indexOf(previous);
297+
final List<MapEntry<AssetPathEntity, Uint8List?>> newEntries =
298+
_pathsList.entries.toList()
299+
..removeAt(index)
300+
..insert(index, MapEntry<AssetPathEntity, Uint8List?>(value, null));
301+
_pathsList
302+
..clear()
303+
..addEntries(newEntries);
304+
getThumbnailFromPath(value);
305+
}
306+
notifyListeners();
307+
}
308+
285309
@override
286310
Future<void> getPaths() async {
287311
// Initial base options.
@@ -314,12 +338,7 @@ class DefaultAssetPickerProvider
314338
for (final AssetPathEntity pathEntity in _list) {
315339
// Use sync method to avoid unnecessary wait.
316340
_pathsList[pathEntity] = null;
317-
if (requestType != RequestType.audio) {
318-
getThumbnailFromPath(pathEntity).then((Uint8List? data) {
319-
_pathsList[pathEntity] = data;
320-
notifyListeners();
321-
});
322-
}
341+
getThumbnailFromPath(pathEntity);
323342
}
324343

325344
// Set first path entity as current path entity.
@@ -402,6 +421,9 @@ class DefaultAssetPickerProvider
402421
return true;
403422
}(),
404423
);
424+
if (requestType == RequestType.audio) {
425+
return null;
426+
}
405427
final List<AssetEntity> assets = await path.getAssetListRange(
406428
start: 0,
407429
end: 1,
@@ -417,14 +439,15 @@ class DefaultAssetPickerProvider
417439
final Uint8List? assetData = await asset.thumbnailDataWithSize(
418440
pathThumbnailSize,
419441
);
442+
_pathsList[path] = assetData;
443+
notifyListeners();
420444
return assetData;
421445
}
422446

423447
/// Get assets list from current path entity.
424448
/// 从当前已选路径获取资源列表
425449
Future<void> getAssetsFromCurrentPath() async {
426450
if (_pathsList.isNotEmpty) {
427-
_currentPath = _pathsList.keys.elementAt(0);
428451
totalAssetsCount = currentPath!.assetCount;
429452
await getAssetsFromPath(0, currentPath!);
430453
} else {

0 commit comments

Comments
 (0)