Skip to content

Commit cdd924d

Browse files
committed
♻️ Take out things that belongs to the builder.
1 parent 7a4a10f commit cdd924d

File tree

2 files changed

+67
-60
lines changed

2 files changed

+67
-60
lines changed

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ abstract class AssetPickerBuilderDelegate<A, P> {
1919
Color themeColor,
2020
AssetsPickerTextDelegate textDelegate,
2121
this.pickerTheme,
22+
this.customItemPosition = CustomItemPosition.none,
23+
this.customItemBuilder,
2224
}) : assert(
2325
pickerTheme == null || themeColor == null,
2426
'Theme and theme color cannot be set at the same time.',
@@ -50,6 +52,14 @@ abstract class AssetPickerBuilderDelegate<A, P> {
5052
/// 通常情况下微信选择器使用的是暗色(暗色背景)的主题,但某些情况下开发者需要亮色或自定义主题。
5153
final ThemeData pickerTheme;
5254

55+
/// Allow users set custom item in the picker with several positions.
56+
/// 允许用户在选择器中添加一个自定义item,并指定位置。
57+
final CustomItemPosition customItemPosition;
58+
59+
/// The widget builder for the custom item.
60+
/// 自定义item的构造方法
61+
final WidgetBuilder customItemBuilder;
62+
5363
/// [ThemeData] for the picker.
5464
/// 选择器使用的主题
5565
ThemeData get theme => pickerTheme ?? AssetPicker.themeData(themeColor);
@@ -387,10 +397,10 @@ class DefaultAssetPickerBuilderDelegate
387397
Color themeColor,
388398
AssetsPickerTextDelegate textDelegate,
389399
ThemeData pickerTheme,
400+
CustomItemPosition customItemPosition = CustomItemPosition.none,
401+
WidgetBuilder customItemBuilder,
390402
this.previewThumbSize,
391403
this.specialPickerType,
392-
this.customItemPosition = CustomItemPosition.none,
393-
this.customItemBuilder,
394404
}) : assert(
395405
provider != null,
396406
'AssetPickerProvider must be provided and not null.',
@@ -405,6 +415,8 @@ class DefaultAssetPickerBuilderDelegate
405415
themeColor: themeColor,
406416
textDelegate: textDelegate,
407417
pickerTheme: pickerTheme,
418+
customItemPosition: customItemPosition,
419+
customItemBuilder: customItemBuilder,
408420
);
409421

410422
/// Thumb size for the preview of images in the viewer.
@@ -431,14 +443,6 @@ class DefaultAssetPickerBuilderDelegate
431443
/// * [SpecialPickerType.wechatMoment] 微信朋友圈模式。当用户选择了视频,将不能选择图片。
432444
final SpecialPickerType specialPickerType;
433445

434-
/// The widget builder for the custom item.
435-
/// 自定义item的构造方法
436-
final WidgetBuilder customItemBuilder;
437-
438-
/// Allow users set custom item in the picker with several positions.
439-
/// 允许用户在选择器中添加一个自定义item,并指定位置。
440-
final CustomItemPosition customItemPosition;
441-
442446
/// [Duration] when triggering path switching.
443447
/// 切换路径时的动画时长
444448
Duration get switchingPathDuration => kThemeAnimationDuration * 1.5;
@@ -570,10 +574,11 @@ class DefaultAssetPickerBuilderDelegate
570574
int index,
571575
List<AssetEntity> currentAssets,
572576
) {
573-
final AssetPathEntity currentPath = Provider.of<DefaultAssetPickerProvider>(
577+
final AssetPathEntity currentPathEntity =
578+
Provider.of<DefaultAssetPickerProvider>(
574579
context,
575580
listen: false,
576-
).currentPath;
581+
).currentPathEntity;
577582

578583
int currentIndex;
579584
switch (customItemPosition) {
@@ -585,7 +590,7 @@ class DefaultAssetPickerBuilderDelegate
585590
currentIndex = index - 1;
586591
break;
587592
}
588-
if (!currentPath.isAll) {
593+
if (!currentPathEntity.isAll) {
589594
currentIndex = index;
590595
}
591596

@@ -594,7 +599,7 @@ class DefaultAssetPickerBuilderDelegate
594599
provider.loadMoreAssets();
595600
}
596601

597-
if (currentPath.isAll) {
602+
if (currentPathEntity.isAll) {
598603
if ((index == currentAssets.length &&
599604
customItemPosition == CustomItemPosition.append) ||
600605
(index == 0 && customItemPosition == CustomItemPosition.prepend)) {
@@ -631,14 +636,15 @@ class DefaultAssetPickerBuilderDelegate
631636
BuildContext context,
632637
List<AssetEntity> currentAssets,
633638
) {
634-
final AssetPathEntity currentPath = Provider.of<DefaultAssetPickerProvider>(
639+
final AssetPathEntity currentPathEntity =
640+
Provider.of<DefaultAssetPickerProvider>(
635641
context,
636642
listen: false,
637-
).currentPath;
643+
).currentPathEntity;
638644

639645
/// Return actual length if current path is all.
640646
/// 如果当前目录是全部内容,则返回实际的内容数量。
641-
if (!currentPath.isAll) {
647+
if (!currentPathEntity.isAll) {
642648
return currentAssets.length;
643649
}
644650
int length;
@@ -867,9 +873,8 @@ class DefaultAssetPickerBuilderDelegate
867873
ignoring: !isSwitchingPath,
868874
child: GestureDetector(
869875
onTap: () {
870-
context
871-
.read<AssetPickerProvider<AssetEntity, AssetPathEntity>>()
872-
.isSwitchingPath = false;
876+
context.read<DefaultAssetPickerProvider>().isSwitchingPath =
877+
false;
873878
},
874879
child: AnimatedOpacity(
875880
duration: switchingPathDuration,
@@ -977,10 +982,10 @@ class DefaultAssetPickerBuilderDelegate
977982
child: Row(
978983
mainAxisSize: MainAxisSize.min,
979984
children: <Widget>[
980-
if (provider.currentPath != null)
985+
if (provider.currentPathEntity != null)
981986
Flexible(
982987
child: Text(
983-
provider.currentPath.name ?? '',
988+
provider.currentPathEntity.name ?? '',
984989
style: const TextStyle(
985990
fontSize: 18.0,
986991
fontWeight: FontWeight.normal,
@@ -1107,7 +1112,7 @@ class DefaultAssetPickerBuilderDelegate
11071112
BuildContext _,
11081113
DefaultAssetPickerProvider provider,
11091114
) =>
1110-
provider.currentPath,
1115+
provider.currentPathEntity,
11111116
builder: (
11121117
BuildContext _,
11131118
AssetPathEntity currentPathEntity,

lib/src/provider/asset_picker_provider.dart

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@ abstract class AssetPickerProvider<A, P> extends ChangeNotifier {
1919
this.pageSize = 320,
2020
this.pathThumbSize = 80,
2121
List<A> selectedAssets,
22-
Duration routeDuration,
2322
}) {
2423
if (selectedAssets?.isNotEmpty ?? false) {
2524
_selectedAssets = List<A>.from(selectedAssets);
2625
}
27-
Future<void>.delayed(routeDuration).then(
28-
(dynamic _) async {
29-
await getAssetPathList();
30-
await getAssetList();
31-
},
32-
);
3326
}
3427

3528
/// Maximum count for asset selection.
@@ -51,7 +44,7 @@ abstract class AssetPickerProvider<A, P> extends ChangeNotifier {
5144
_isAssetsEmpty = false;
5245
_isSwitchingPath = false;
5346
_pathEntityList.clear();
54-
_currentPath = null;
47+
_currentPathEntity = null;
5548
_currentAssets = null;
5649
_selectedAssets.clear();
5750
super.dispose();
@@ -136,16 +129,16 @@ abstract class AssetPickerProvider<A, P> extends ChangeNotifier {
136129

137130
/// The path which is currently using.
138131
/// 正在查看的资源路径
139-
P _currentPath;
132+
P _currentPathEntity;
140133

141-
P get currentPath => _currentPath;
134+
P get currentPathEntity => _currentPathEntity;
142135

143136
set currentPathEntity(P value) {
144137
assert(value != null);
145-
if (value == _currentPath) {
138+
if (value == _currentPathEntity) {
146139
return;
147140
}
148-
_currentPath = value;
141+
_currentPathEntity = value;
149142
notifyListeners();
150143
}
151144

@@ -186,17 +179,13 @@ abstract class AssetPickerProvider<A, P> extends ChangeNotifier {
186179
/// 获取所有的资源路径
187180
Future<void> getAssetPathList();
188181

189-
/// Get assets list from current path entity.
190-
/// 从当前已选路径获取资源列表
191-
Future<void> getAssetList();
192-
193182
/// Get thumb data from the first asset under the specific path entity.
194183
/// 获取指定路径下的第一个资源的缩略数据
195-
Future<Uint8List> getFirstThumbFromPathEntity(P path);
184+
Future<Uint8List> getFirstThumbFromPathEntity(P pathEntity);
196185

197186
/// Get assets under the specific path entity.
198187
/// 获取指定路径下的资源
199-
Future<void> getAssetsFromEntity(int page, P path);
188+
Future<void> getAssetsFromEntity(int page, P pathEntity);
200189

201190
/// Load more assets.
202191
/// 加载更多资源
@@ -223,7 +212,7 @@ abstract class AssetPickerProvider<A, P> extends ChangeNotifier {
223212

224213
/// Switch path entity.
225214
/// 切换路径
226-
void switchPath(P path);
215+
void switchPath(P pathEntity);
227216
}
228217

229218
class DefaultAssetPickerProvider
@@ -242,9 +231,14 @@ class DefaultAssetPickerProvider
242231
pageSize: pageSize,
243232
pathThumbSize: pathThumbSize,
244233
selectedAssets: selectedAssets,
245-
routeDuration: routeDuration,
246234
) {
247235
Constants.sortPathDelegate = sortPathDelegate ?? SortPathDelegate.common;
236+
Future<void>.delayed(routeDuration).then(
237+
(dynamic _) async {
238+
await getAssetPathList();
239+
await getAssetList();
240+
},
241+
);
248242
}
249243

250244
/// Request assets type.
@@ -304,34 +298,37 @@ class DefaultAssetPickerProvider
304298

305299
/// Set first path entity as current path entity.
306300
if (_pathEntityList.isNotEmpty) {
307-
_currentPath ??= pathEntityList.keys.elementAt(0);
301+
_currentPathEntity ??= pathEntityList.keys.elementAt(0);
308302
}
309303
}
310304

311-
@override
305+
/// Get assets list from current path entity.
306+
/// 从当前已选路径获取资源列表
312307
Future<void> getAssetList() async {
313308
if (_pathEntityList.isNotEmpty) {
314-
_currentPath = pathEntityList.keys.elementAt(0);
315-
totalAssetsCount = currentPath.assetCount;
316-
await getAssetsFromEntity(0, currentPath);
309+
_currentPathEntity = _pathEntityList.keys.elementAt(0);
310+
totalAssetsCount = currentPathEntity.assetCount;
311+
await getAssetsFromEntity(0, currentPathEntity);
317312
// Update total assets count.
318313
} else {
319314
isAssetsEmpty = true;
320315
}
321316
}
322317

323318
@override
324-
Future<void> getAssetsFromEntity(int page, AssetPathEntity path) async {
325-
_currentAssets =
326-
(await path.getAssetListPaged(page, pageSize ?? path.assetCount))
327-
.toList();
319+
Future<void> getAssetsFromEntity(int page, AssetPathEntity pathEntity) async {
320+
_currentAssets = (await pathEntity.getAssetListPaged(
321+
page,
322+
pageSize ?? pathEntity.assetCount,
323+
))
324+
.toList();
328325
_hasAssetsToDisplay = currentAssets?.isNotEmpty ?? false;
329326
notifyListeners();
330327
}
331328

332329
@override
333330
Future<void> loadMoreAssets() async {
334-
final List<AssetEntity> assets = (await currentPath.getAssetListPaged(
331+
final List<AssetEntity> assets = (await currentPathEntity.getAssetListPaged(
335332
currentAssetsListPage,
336333
pageSize,
337334
))
@@ -347,18 +344,23 @@ class DefaultAssetPickerProvider
347344
}
348345

349346
@override
350-
void switchPath(AssetPathEntity path) {
347+
void switchPath(AssetPathEntity pathEntity) {
351348
_isSwitchingPath = false;
352-
_currentPath = path;
353-
_totalAssetsCount = path.assetCount;
349+
_currentPathEntity = pathEntity;
350+
_totalAssetsCount = pathEntity.assetCount;
354351
notifyListeners();
355-
getAssetsFromEntity(0, currentPath);
352+
getAssetsFromEntity(0, currentPathEntity);
356353
}
357354

358355
@override
359-
Future<Uint8List> getFirstThumbFromPathEntity(AssetPathEntity path) async {
360-
final AssetEntity asset =
361-
(await path.getAssetListRange(start: 0, end: 1)).elementAt(0);
356+
Future<Uint8List> getFirstThumbFromPathEntity(
357+
AssetPathEntity pathEntity,
358+
) async {
359+
final AssetEntity asset = (await pathEntity.getAssetListRange(
360+
start: 0,
361+
end: 1,
362+
))
363+
.elementAt(0);
362364
final Uint8List assetData =
363365
await asset.thumbDataWithSize(pathThumbSize, pathThumbSize);
364366
return assetData;

0 commit comments

Comments
 (0)