Skip to content

Commit

Permalink
Merge pull request #241 from CaiJingLong/remove-in-album-on-iOS
Browse files Browse the repository at this point in the history
Remove in album.
  • Loading branch information
CaiJingLong committed Mar 29, 2020
2 parents 7489459 + 9998ced commit 8ab0103
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 14 deletions.
27 changes: 19 additions & 8 deletions example/lib/model/photo_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PhotoProvider extends ChangeNotifier {

var onlyAll = false;

Map<AssetPathEntity, PathProvider> pathProviderMap = {};
Map<AssetPathEntity, AssetPathProvider> pathProviderMap = {};

bool _notifying = false;

Expand Down Expand Up @@ -144,8 +144,8 @@ class PhotoProvider extends ChangeNotifier {
this.list.addAll(galleryList);
}

PathProvider getOrCreatePathProvider(AssetPathEntity pathEntity) {
pathProviderMap[pathEntity] ??= PathProvider(pathEntity);
AssetPathProvider getOrCreatePathProvider(AssetPathEntity pathEntity) {
pathProviderMap[pathEntity] ??= AssetPathProvider(pathEntity);
return pathProviderMap[pathEntity];
}

Expand Down Expand Up @@ -208,13 +208,13 @@ class PhotoProvider extends ChangeNotifier {
}
}

class PathProvider extends ChangeNotifier {
class AssetPathProvider extends ChangeNotifier {
static const loadCount = 50;

bool isInit = false;

final AssetPathEntity path;
PathProvider(this.path);
AssetPathProvider(this.path);

List<AssetEntity> list = [];

Expand All @@ -229,6 +229,7 @@ class PathProvider extends ChangeNotifier {
}

Future onRefresh() async {
await path.refreshPathProperties();
final list = await path.getAssetListPaged(0, loadCount);
page = 0;
this.list.clear();
Expand All @@ -253,13 +254,23 @@ class PathProvider extends ChangeNotifier {
void delete(AssetEntity entity) async {
final result = await PhotoManager.editor.deleteWithIds([entity.id]);
if (result.isNotEmpty) {
await Future.delayed(Duration(seconds: 3));
final rangeEnd = this.list.length;
await provider.refreshAllGalleryProperties();
final list =
await path.getAssetListRange(start: 0, end: this.list.length);
final list = await path.getAssetListRange(start: 0, end: rangeEnd);
this.list.clear();
this.list.addAll(list);
printListLength("deleted");
}
}

void removeInAlbum(AssetEntity entity) async {
if (await PhotoManager.editor.iOS.removeInAlbum(entity, path)) {
final rangeEnd = this.list.length;
await provider.refreshAllGalleryProperties();
final list = await path.getAssetListRange(start: 0, end: rangeEnd);
this.list.clear();
this.list.addAll(list);
printListLength("removeInAlbum");
}
}

Expand Down
18 changes: 17 additions & 1 deletion example/lib/page/image_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _GalleryContentListPageState extends State<GalleryContentListPage> {

PhotoProvider get photoProvider => Provider.of<PhotoProvider>(context);

PathProvider get provider =>
AssetPathProvider get provider =>
Provider.of<PhotoProvider>(context).getOrCreatePathProvider(path);

@override
Expand Down Expand Up @@ -150,6 +150,7 @@ class _GalleryContentListPageState extends State<GalleryContentListPage> {
child: Text("Copy to another path"),
onPressed: () => copyToAnotherPath(entity),
),
_buildRemoveInAlbumWidget(entity),
],
),
);
Expand Down Expand Up @@ -277,4 +278,19 @@ class _GalleryContentListPageState extends State<GalleryContentListPage> {
),
);
}

Widget _buildRemoveInAlbumWidget(AssetEntity entity) {
if (!Platform.isIOS) {
return Container();
}

return RaisedButton(
child: Text("Remove in album"),
onPressed: () => deleteAssetInAlbum(entity),
);
}

void deleteAssetInAlbum(entity) {
provider.removeInAlbum(entity);
}
}
2 changes: 2 additions & 0 deletions ios/Classes/core/PMManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ typedef void (^AssetResult)(PMAssetEntity *);
- (void)createFolderWithName:(NSString *)name parentId:(NSString *)id block:(void (^)(NSString *, NSString *))block;

- (void)createAlbumWithName:(NSString *)name parentId:(NSString *)id block:(void (^)(NSString *, NSString *))block;

- (void)removeInAlbumWithAssetId:(NSArray *)id albumId:(NSString *)albumId block:(void (^)(NSString *))block;
@end
30 changes: 30 additions & 0 deletions ios/Classes/core/PMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1046,4 +1046,34 @@ - (void)createAlbumWithName:(NSString *)name parentId:(NSString *)id block:(void
block(targetId, error.localizedDescription);
}
}

- (void)removeInAlbumWithAssetId:(NSArray *)id albumId:(NSString *)albumId block:(void (^)(NSString *))block {
PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumId] options:nil];
PHAssetCollection *collection;
if (result && result.count > 0) {
collection = result.firstObject;
} else {
block(@"Can't found the collection.");
return;
}

if (![collection canPerformEditOperation:PHCollectionEditOperationRemoveContent]) {
block(@"The collection cannot remove asset by user.");
return;
}

PHFetchResult<PHAsset *> *assetResult = [PHAsset fetchAssetsWithLocalIdentifiers:id options:nil];
NSError *error;
[PHPhotoLibrary.sharedPhotoLibrary
performChangesAndWait:^{
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
[request removeAssets:assetResult];
} error:&error];
if (error) {
block([NSString stringWithFormat:@"Remove error: %@", error]);
return;
}

block(nil);
}
@end
36 changes: 31 additions & 5 deletions ios/Classes/core/PMPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,38 @@ - (void)onAuth:(FlutterMethodCall *)call result:(FlutterResult)result {
BOOL isRoot = [call.arguments[@"isRoot"] boolValue];
NSString *parentId = call.arguments[@"folderId"];

if(isRoot){
if (isRoot) {
parentId = nil;
}

[manager createFolderWithName:name parentId:parentId block:^(NSString*id,NSString *errorMsg){

[manager createFolderWithName:name parentId:parentId block:^(NSString *id, NSString *errorMsg) {
[handler reply:[self convertToResult:@{@"id": id, @"errorMsg": errorMsg}]];
}];

} else if ([@"createAlbum" isEqualToString:call.method]) {
NSString *name = call.arguments[@"name"];
BOOL isRoot = [call.arguments[@"isRoot"] boolValue];
NSString *parentId = call.arguments[@"folderId"];

if(isRoot){
if (isRoot) {
parentId = nil;
}

[manager createAlbumWithName:name parentId:parentId block:^(NSString*id,NSString *errorMsg){
[manager createAlbumWithName:name parentId:parentId block:^(NSString *id, NSString *errorMsg) {
NSDictionary *dictionary = @{@"id": id, @"errorMsg": errorMsg};
[handler reply:[self convertToResult:dictionary]];
}];

} else if ([@"removeInAlbum" isEqualToString:call.method]) {
NSArray *assetId = call.arguments[@"assetId"];
NSString *pathId = call.arguments[@"pathId"];

[manager removeInAlbumWithAssetId:assetId albumId:pathId block:^(NSString *msg) {
if (msg) {
[handler reply:@{@"msg": msg}];
} else {
[handler reply:@{@"success": @YES}];
}
}];

} else {
Expand All @@ -285,6 +298,19 @@ - (void)onAuth:(FlutterMethodCall *)call result:(FlutterResult)result {

}

- (NSDictionary *)convertToResult:(NSDictionary *)dict {
NSMutableDictionary *result = [NSMutableDictionary new];

for (id key in dict.allKeys) {
id value = dict[key];
if (value) {
result[key] = value;
}
}

return result;
}

- (unsigned long)getTimestamp:(FlutterMethodCall *)call {
unsigned long timestamp = [call.arguments[@"timestamp"] unsignedLongValue];
return timestamp;
Expand Down
38 changes: 38 additions & 0 deletions lib/src/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,44 @@ class IosEditor {
return _plugin.iosCreateAlbum(name, false, parent);
}
}

Future<bool> removeInAlbum(AssetEntity entity, AssetPathEntity path) async {
if (entity == null || path == null) {
assert(entity != null, "");
assert(path != null, "");
return false;
}
if (path.albumType == 2 || path.isAll) {
assert(path.albumType == 1, "The path must is album");
assert(
!path.isAll,
"The ${path.name}'s asset can't be remove. Use PhotoManager.editor.deleteAsset",
);
return false;
}
return _plugin.iosRemoveInAlbum([entity], path);
}

Future<bool> removeAssetsInAlbum(
List<AssetEntity> list, AssetPathEntity path) async {
if (list == null || path == null) {
assert(list != null, "");
assert(path != null, "");
return false;
}
if (list.isEmpty) {
return null;
}
if (path.albumType == 2 || path.isAll) {
assert(path.albumType == 1, "The path must is album");
assert(
!path.isAll,
"The ${path.name} can't be remove. Use PhotoManager.editor.deleteAsset",
);
return false;
}
return _plugin.iosRemoveInAlbum(list, path);
}
}

class AndroidEditor {}
28 changes: 28 additions & 0 deletions lib/src/plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ mixin IosPlugin on BasePlugin {
return null;
}

if (result["errorMsg"] != null) {
print("errorMsg");
return null;
}

return AssetPathEntity()
..id = result["id"]
..name = name
Expand All @@ -348,13 +353,36 @@ mixin IosPlugin on BasePlugin {
return null;
}

if (result["errorMsg"] != null) {
print("errorMsg");
return null;
}

return AssetPathEntity()
..id = result["id"]
..name = name
..isAll = false
..assetCount = 0
..albumType = 1;
}

Future<bool> iosRemoveInAlbum(
List<AssetEntity> entities, AssetPathEntity path) async {
final result = await _channel.invokeMethod(
"removeInAlbum",
{
"assetId": entities.map((e) => e.id).toList(),
"pathId": path.id,
},
);

if (result["msg"] != null) {
print("cannot remove, cause by: ${result["msg"]}");
return false;
}

return true;
}
}

mixin AndroidPlugin on BasePlugin {}

0 comments on commit 8ab0103

Please sign in to comment.