Skip to content

Commit

Permalink
Improvements about recent issues (#1186)
Browse files Browse the repository at this point in the history
Fixes #841
Fixes
#1009
Fixes
#1180
Fixes
#1181
Fixes
#1185

1. Adds a detached state for managers. Callers with those managers will
first be aware of the detaching state before any actual calls to avoid
crashes.
2. Errors replied by the channel do not include detailed messages
before. Now the code will unwrap certain exceptions to extract details
from them.
3. `PHAssetResource` with the type `PHAssetResourceTypeFullSizeVideo`
does not count as a video type before, making the resource obtain ignore
them.
4. Fixes potential range exception when converting `NSTimeInterval` on
Darwin.
5. Fixes progress not being updated when getting the non-original video
file on iOS.
6. Fixes incorrect Live Photo resource being obtained which will result
in a wrong aspect ratio.
7. Fixes Live Photos saving exceptions with the paired video.
8. Expose `progressHandler` for `AssetEntity.getMediaUrl`.
9. Expose `withSubtype` for `AssetEntity.isLocallyAvailable` to request
if a Live Photo resource is available.
10. Other lints and type promotion fixes.
  • Loading branch information
AlexV525 authored Sep 22, 2024
2 parents ff03ee8 + 9ecf239 commit 60037ba
Show file tree
Hide file tree
Showing 21 changed files with 611 additions and 500 deletions.
25 changes: 23 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,34 @@ To know more about breaking changes, see the [Migration Guide][].

## Unreleased

*None.*
### Breaking changes

`saveImage` now requires `title` rather than `filename`.

### Improvements

- Adds a detached state for managers. Callers with those managers will first be aware of the detaching state
before any actual calls to avoid crashes.
- Errors replied by the channel do not include detailed messages before.
Now the code will unwrap certain exceptions to extract details from them.
- Expose `progressHandler` for `AssetEntity.getMediaUrl`.
- Expose `withSubtype` for `AssetEntity.isLocallyAvailable` to request if a Live Photo resource is available.

### Fixes

- `PHAssetResource` with the type `PHAssetResourceTypeFullSizeVideo` does not count as a video type before,
making the resource obtain ignore them.
- Fixes potential range exception when converting `NSTimeInterval` on Darwin.
- Fixes progress not being updated when getting the non-original video file on iOS.
- Fixes incorrect Live Photo resource being obtained which will result in a wrong aspect ratio.
- Fixes Live Photos saving exceptions with the paired video.
- Other lints and type promotion fixes.

## 3.3.0

### Breaking changes

`saveImage` now requires `filename` rather than `title` other save methods do not require `title` anymore.
`saveImage` now requires `filename` rather than `title`, other save methods do not require `title` anymore.

### Improvements

Expand Down
42 changes: 36 additions & 6 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ If you want to see the new feature support, please refer to [readme][] and [chan

<!-- TOC -->
* [Migration Guide](#migration-guide)
* [3.x to 3.3](#3x-to-33)
* [3.x to 3.4](#3x-to-34)
* [Overall](#overall)
* [`saveLivePhoto`](#savelivephoto)
* [3.x to 3.3](#3x-to-33)
* [Overall](#overall-1)
* [`saveImage`](#saveimage)
* [3.0.x to 3.1](#30x-to-31)
* [Overall](#overall-1)
* [Overall](#overall-2)
* [`containsLivePhotos`](#containslivephotos)
* [`AlbumType`](#albumtype)
* [2.x to 3.0](#2x-to-30)
* [Overall](#overall-2)
* [Overall](#overall-3)
* [`AssetEntityImage` and `AssetEntityImageProvider`](#assetentityimage-and-assetentityimageprovider)
* [2.x to 2.8](#2x-to-28)
* [Overall](#overall-3)
* [2.x to 2.2](#2x-to-22)
* [Overall](#overall-4)
* [2.x to 2.2](#2x-to-22)
* [Overall](#overall-5)
* [`assetCount`](#assetcount)
* [1.x to 2.0](#1x-to-20)
* [Overall](#overall-5)
* [Overall](#overall-6)
* [API migrations](#api-migrations)
* [`getAssetListPaged`](#getassetlistpaged)
* [Filtering only videos](#filtering-only-videos)
Expand All @@ -31,6 +34,33 @@ If you want to see the new feature support, please refer to [readme][] and [chan
* [0.5 To 0.6](#05-to-06)
<!-- TOC -->

## 3.x to 3.4

### Overall

In order to let developers write the most precise API usage,
the `filename` of `saveLivePhoto` has migrated to `title`.

#### `saveLivePhoto`

Before:
```dart
final entity = await PhotoManager.editor.saveLivePhoto(
imageFile: imageFile,
videoFile: videoFile,
filename: 'live_0',
);
```

After:
```dart
final entity = await PhotoManager.editor.saveLivePhoto(
imageFile: imageFile,
videoFile: videoFile,
title: 'live_0',
);
```

## 3.x to 3.3

### Overall
Expand Down
2 changes: 1 addition & 1 deletion example/lib/page/developer/develop_index_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class _DeveloperIndexPageState extends State<DeveloperIndexPage> {
final assets = await PhotoManager.editor.darwin.saveLivePhoto(
imageFile: imgFile,
videoFile: videoFile,
filename: 'preview_0',
title: 'preview_0',
);
print('save live photo result : ${assets?.id}');
} finally {
Expand Down
2 changes: 1 addition & 1 deletion example/lib/widget/live_photos_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class _LivePhotosWidgetState extends State<LivePhotosWidget> {
}

Future<void> _initializeController() async {
if (!await widget.entity.isLocallyAvailable()) {
if (!await widget.entity.isLocallyAvailable(withSubtype: true)) {
if (widget.useOrigin) {
await widget.entity.originFileWithSubtype;
} else {
Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/PMNotificationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@

- (BOOL)isNotifying;

- (void)detach;

@end
39 changes: 29 additions & 10 deletions ios/Classes/PMNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ @implementation PMNotificationManager {
FlutterMethodChannel *channel;
BOOL _notifying;
PHFetchResult<PHAsset *> *result;
BOOL isDetach;
}

- (void)dealloc {
[self detach];
}

- (instancetype)initWithRegistrar:
Expand All @@ -21,52 +26,66 @@ - (instancetype)initWithRegistrar:
binaryMessenger:[registrar messenger]];
_notifying = NO;
}

isDetach = NO;
return self;
}

+ (instancetype)managerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
return [[self alloc] initWithRegistrar:registrar];
}

- (void)detach {
isDetach = YES;
if (_notifying) {
[self stopNotify];
}
[channel setMethodCallHandler:nil];
}

- (void)startNotify {
PHPhotoLibrary *library = PHPhotoLibrary.sharedPhotoLibrary;
[library registerChangeObserver:self];
if (isDetach) {
return;
}
[PHPhotoLibrary.sharedPhotoLibrary registerChangeObserver:self];
_notifying = YES;
[self refreshFetchResult];
}

- (void)stopNotify {
PHPhotoLibrary *library = PHPhotoLibrary.sharedPhotoLibrary;
[library unregisterChangeObserver:self];
if (!_notifying || isDetach) {
return;
}
[PHPhotoLibrary.sharedPhotoLibrary unregisterChangeObserver:self];
_notifying = NO;
}

#pragma "photo library notify"

- (void)photoLibraryDidChange:(PHChange *)changeInstance {
if (!result) {
if (isDetach || !result) {
return;
}
PHFetchResultChangeDetails *details = [changeInstance changeDetailsForFetchResult:result];
NSUInteger oldCount = result.count;
[self refreshFetchResult];
if (!result) {
if (isDetach || !result) {
return;
}
NSUInteger newCount = result.count;
NSMutableDictionary *detailResult = [self convertChangeDetailsToNotifyDetail:details];
detailResult[@"oldCount"] = @(oldCount);
detailResult[@"newCount"] = @(newCount);

[PMLogUtils.sharedInstance
info:[NSString stringWithFormat:@"on change result = %@", detailResult]];
[PMLogUtils.sharedInstance info:[NSString stringWithFormat:@"on change result = %@", detailResult]];
dispatch_async(dispatch_get_main_queue(), ^{
[channel invokeMethod:@"change" arguments:detailResult];
[self->channel invokeMethod:@"change" arguments:detailResult];
});
}

- (void)refreshFetchResult {
if (isDetach) {
return;
}
result = [self getLastAssets];
}

Expand Down
Loading

0 comments on commit 60037ba

Please sign in to comment.