Skip to content

Commit

Permalink
Merge commit '319a358ae63eaf37667e030d26ddc2c842fd7b01' into l863
Browse files Browse the repository at this point in the history
* commit '319a358ae63eaf37667e030d26ddc2c842fd7b01':
  🐛 Uses correct `isOriginal` for the `LocallyAvailableBuilder` (fluttercandies#626)
新的批量选择逻辑
  • Loading branch information
tourdt committed Sep 24, 2024
2 parents bc71c69 + 319a358 commit 30588c5
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ that can be found in the LICENSE file. -->

*None.*

## 9.2.2

- Uses correct `isOriginal` for the `LocallyAvailableBuilder`.

## 9.2.1

- Improve changes when limited on iOS.
Expand Down
2 changes: 2 additions & 0 deletions example/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import Foundation
import package_info_plus
import path_provider_foundation
import photo_manager
import video_player_avfoundation

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
PhotoManagerPlugin.register(with: registry.registrar(forPlugin: "PhotoManagerPlugin"))
FVPVideoPlayerPlugin.register(with: registry.registrar(forPlugin: "FVPVideoPlayerPlugin"))
}
148 changes: 103 additions & 45 deletions lib/src/delegates/asset_picker_builder_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1307,60 +1307,117 @@ class DefaultAssetPickerBuilderDelegate
}

final int panIndex = panItemIndex();
int startIndex = p.initialPanItemIndex;
int endIndex = p.initialPanItemIndex;
bool isSelect = false;
bool sortOrderAsc = false;
// print('onPanUpdate Index: ${panIndex}');
if (panIndex >= 0 && p.initialPanItemIndex >= 0) {
// 3 => 6 => 9

if (p.initialAssetSelectedStatus) {
p.unSelectAsset(assets[p.initialPanItemIndex]);
if (panIndex != p.latestPanItemIndex) {
if (p.latestPanItemIndex > panIndex) {
for (int i = panIndex;
i <= p.latestPanItemIndex;
i++) {
p.unSelectAsset(assets[i]);
}
if (panIndex < 0 || p.initialPanItemIndex < 0) {
return;
}
print(
'latest index: ${p.latestPanItemIndex}, initIndex: ${p.initialPanItemIndex}, currIndex: $panIndex');
// 3 => 6 => 9
bool doSelect = !p.initialAssetSelectedStatus;
if (panIndex != p.latestPanItemIndex) {
if (panIndex < p.initialPanItemIndex) {
// 往下选择(当前位置小于初始位置)
if (panIndex > p.latestPanItemIndex) {
// 先下选择了很多,现在应该取消选择一部分
startIndex = p.latestPanItemIndex;
endIndex = panIndex - 1;
isSelect = !doSelect;
sortOrderAsc = true;
} else {
startIndex = p.latestPanItemIndex;
endIndex = panIndex;
isSelect = doSelect;
sortOrderAsc = false;
}
} else if (panIndex > p.initialPanItemIndex) {
// 往上选择(当前位置大于初始位置)
if (panIndex < p.latestPanItemIndex) {
// 先上选择了很多,现在应该取消选择一部分
startIndex = p.latestPanItemIndex;
endIndex = panIndex + 1;
isSelect = !doSelect;
sortOrderAsc = false;
} else {
// 当前位置等于初始位置
startIndex = p.latestPanItemIndex;
endIndex = panIndex;
isSelect = doSelect;
sortOrderAsc = true;
}
} else {}
} else {
isSelect = doSelect;
}
List<AssetEntity> selectList = [];
List<AssetEntity> unselectList = [];
if (sortOrderAsc) {
if (startIndex < p.initialPanItemIndex &&
endIndex > p.initialPanItemIndex) {
for (int i = startIndex;
i < p.initialPanItemIndex;
i++) {
print('选择Item[$i]-1: ${!isSelect}');
if (isSelect) {
unselectList.add(assets[i]);
} else {
for (int i = p.latestPanItemIndex;
i <= panIndex;
i++) {
p.selectAsset(assets[i]);
}
selectList.add(assets[i]);
}
// p.unSelectAsset(assets[panIndex]);
}
} else {
p.selectAsset(assets[p.initialPanItemIndex]);
if (panIndex != p.latestPanItemIndex) {
if (p.latestPanItemIndex > panIndex) {
for (int i = panIndex;
i <= p.latestPanItemIndex;
i++) {
p.selectAsset(assets[i]);
}
p.selectAssetList(selectList);
p.unSelectAssetList(unselectList);
startIndex = p.initialPanItemIndex;
}
selectList = [];
unselectList = [];

for (int i = startIndex; i <= endIndex; i++) {
print('选择Item[$i]-2: $isSelect');
if (isSelect) {
selectList.add(assets[i]);
} else {
unselectList.add(assets[i]);
}
}
p.selectAssetList(selectList);
p.unSelectAssetList(unselectList);
} else {
if (startIndex > p.initialPanItemIndex &&
endIndex < p.initialPanItemIndex) {
for (int i = p.initialPanItemIndex + 1;
i >= startIndex;
i--) {
print('选择Item[$i]-10: ${!isSelect}');

if (isSelect) {
unselectList.add(assets[i]);
} else {
for (int i = p.latestPanItemIndex;
i <= panIndex;
i++) {
p.unSelectAsset(assets[i]);
}
selectList.add(assets[i]);
}
// p.selectAsset(assets[panIndex]);
}
p.selectAssetList(selectList);
p.unSelectAssetList(unselectList);
startIndex = p.initialPanItemIndex;
}

selectList = [];
unselectList = [];
for (int i = startIndex; i >= endIndex; i--) {
print('选择Item[$i]-20: $isSelect');
if (isSelect) {
selectList.add(assets[i]);
} else {
unselectList.add(assets[i]);
}
}
p.updateLatestPanItemIndex(panIndex);
// if (p.initialAssetSelectedStatus) {
// p.unSelectAsset(assets[p.initialPanItemIndex]);
// if (panIndex != p.initialPanItemIndex) {
// p.unSelectAsset(assets[panIndex]);
// }
// } else {
// p.selectAsset(assets[p.initialPanItemIndex]);
// if (panIndex != p.initialPanItemIndex) {
// p.selectAsset(assets[panIndex]);
// }
// }
p.selectAssetList(selectList);
p.unSelectAssetList(unselectList);
}
p.updateLatestPanItemIndex(panIndex);
},
onPanEnd: (_) {
// print('onPanUpdate: ' + index.toString());
Expand Down Expand Up @@ -1797,6 +1854,7 @@ class DefaultAssetPickerBuilderDelegate
) {
return LocallyAvailableBuilder(
asset: asset,
isOriginal: false,
builder: (context, asset) {
final imageProvider = AssetEntityImageProvider(
asset,
Expand Down
36 changes: 36 additions & 0 deletions lib/src/provider/asset_picker_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,30 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
selectedAssets = set;
}

void selectAssetList(List<Asset> items) {
if (items.isEmpty) {
return;
}
if (selectedAssets.length == maxAssets) {
return;
}
final List<Asset> set = selectedAssets.toList();
for (int i = 0; i < items.length; i++) {
// if (selectedAssets.contains(items[i])) {
// continue;
// }
if (set.contains(items[i])) {
continue;
}
if (set.length == maxAssets) {
continue;
}
set.add(items[i]);
}

selectedAssets = set;
}

/// Un-select asset.
/// 取消选中资源
void unSelectAsset(Asset item) {
Expand All @@ -255,6 +279,17 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
selectedAssets = set;
}

void unSelectAssetList(List<Asset> items) {
if (items.isEmpty) {
return;
}
final List<Asset> set = selectedAssets.toList();
for (int i = 0; i < items.length; i++) {
set.remove(items[i]);
}
selectedAssets = set;
}

/// User gesture initial position
/// 用户手势滑动初始位子
Offset initialPanPosition = Offset.zero;
Expand Down Expand Up @@ -300,6 +335,7 @@ abstract class AssetPickerProvider<Asset, Path> extends ChangeNotifier {
initialSelectedPosition = Offset.zero;
initialAssetSelectedStatus = false;
initialPanItemIndex = -1;
latestPanItemIndex = -1;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/widget/builder/video_page_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class _VideoPageBuilderState extends State<VideoPageBuilder> {
return LocallyAvailableBuilder(
key: ValueKey<String>(widget.asset.id),
asset: widget.asset,
isOriginal: false,
builder: (BuildContext context, AssetEntity asset) {
if (hasErrorWhenInitializing) {
return Center(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: wechat_assets_picker
version: 9.2.1
version: 9.2.2
description: |
An image picker (also with videos and audio)
for Flutter projects based on WeChat's UI,
Expand Down

0 comments on commit 30588c5

Please sign in to comment.