Skip to content

Commit

Permalink
More methods on OpenHarmony (#1131)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Li <github@alexv525.com>
  • Loading branch information
zmtzawqlp and AlexV525 authored Jun 13, 2024
1 parent a877c21 commit d61bda8
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 68 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ To know more about breaking changes, see the [Migration Guide][].

* Restores `containsLivePhotos` to `true` by default and deprecates it.
* Use the main resource's filename for title by default on iOS.
* Support more methods on the OpenHarmony.

### Fixes

Expand Down
30 changes: 8 additions & 22 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -934,28 +934,14 @@ PhotoManager.editor.darwin.deletePath();

#### 适用于 OpenHarmony 的功能

> 鸿蒙暂不支持音频类型资源。
| Feature | OpenHarmony |
|:------------------------|:-----------:|
| getAssetPathList ||
| getAssetCountFromPath ||
| fetchPathProperties ||
| getAssetCount ||
| getAssetListPaged ||
| getOriginBytes ||
| getThumb ||
| getAssetListRange ||
| getAssetsByRange ||
| deleteWithIds ||
| getColumnNames ||
| saveImage ||
| saveImageWithPath ||
| saveVideo ||
| requestPermissionExtend ||
| ignorePermissionCheck ||
| log ||
| notify ||
目前支持大部分的功能,除了跟缓存相关。目前鸿蒙只支持图片和视频 2 种资源类型。

| Feature | OpenHarmony |
| :----------------------------- | :---------: |
| releaseCache ||
| clearFileCache ||
| requestCacheAssetsThumbnail ||
| getSubPathEntities ||


[pub package]: https://pub.flutter-io.cn/packages/photo_manager
Expand Down
30 changes: 8 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1002,28 +1002,14 @@ PhotoManager.editor.darwin.deletePath();

#### Features for OpenHarmony

> Audio is not yet supported by the OpenHarmony.
| Feature | OpenHarmony |
|:------------------------|:-----------:|
| getAssetPathList ||
| getAssetCountFromPath ||
| fetchPathProperties ||
| getAssetCount ||
| getAssetListPaged ||
| getOriginBytes ||
| getThumb ||
| getAssetListRange ||
| getAssetsByRange ||
| deleteWithIds ||
| getColumnNames ||
| saveImage ||
| saveImageWithPath ||
| saveVideo ||
| requestPermissionExtend ||
| ignorePermissionCheck ||
| log ||
| notify ||
Currently, most functions are supported, except for those related to caching. and only support image and video types.

| Feature | OpenHarmony |
| :----------------------------- | :---------: |
| releaseCache ||
| clearFileCache ||
| requestCacheAssetsThumbnail ||
| getSubPathEntities ||


[pub package]: https://pub.dev/packages/photo_manager
Expand Down
4 changes: 3 additions & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ app.*.symbols

# Obfuscation related
app.*.map.json
.metadata
.metadata

local.properties
5 changes: 3 additions & 2 deletions example/lib/page/detail_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photo_manager/platform_utils.dart';
import 'package:photo_manager_image_provider/photo_manager_image_provider.dart';

import '../util/common_util.dart';
Expand All @@ -17,7 +18,7 @@ class DetailPage extends StatefulWidget {

class _DetailPageState extends State<DetailPage> {
bool? useOrigin = true;
bool? useMediaUrl = true;
bool? useMediaUrl = true && !PlatformUtils.isOhos;

@override
Widget build(BuildContext context) {
Expand All @@ -42,7 +43,7 @@ class _DetailPageState extends State<DetailPage> {
},
value: useOrigin,
),
if (widget.entity.type == AssetType.video)
if (widget.entity.type == AssetType.video && !PlatformUtils.isOhos)
CheckboxListTile(
title: const Text('Use Media Url'),
value: useMediaUrl,
Expand Down
2 changes: 0 additions & 2 deletions example/lib/page/developer/issues_page/issue_988.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ class __DeleteAssetImageListState extends State<_DeleteAssetImageList> {
right: 0,
top: 0,
child: Checkbox(
// ignore: deprecated_member_use
overlayColor: MaterialStateProperty.all(Colors.white),
value: checked.contains(asset),
onChanged: (bool? value) {
if (value == true) {
Expand Down
18 changes: 13 additions & 5 deletions example/lib/page/image_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:photo_manager/platform_utils.dart';
import 'package:provider/provider.dart';

import '../model/photo_provider.dart';
Expand Down Expand Up @@ -192,16 +193,23 @@ class _GalleryContentListPageState extends State<GalleryContentListPage> {
<int>[500, 600, 700, 1000, 1500, 2000],
),
),
if (Platform.isIOS || Platform.isMacOS)
if (Platform.isIOS || Platform.isMacOS || PlatformUtils.isOhos)
ElevatedButton(
child: const Text('Toggle isFavorite'),
onPressed: () async {
final bool isFavorite = entity.isFavorite;
print('Current isFavorite: $isFavorite');
await PhotoManager.editor.darwin.favoriteAsset(
entity: entity,
favorite: !isFavorite,
);
if (PlatformUtils.isOhos) {
await PhotoManager.editor.ohos.favoriteAsset(
entity: entity,
favorite: !isFavorite,
);
} else {
await PhotoManager.editor.darwin.favoriteAsset(
entity: entity,
favorite: !isFavorite,
);
}
final AssetEntity? newEntity =
await entity.obtainForNewProperties();
print('New isFavorite: ${newEntity?.isFavorite}');
Expand Down
3 changes: 3 additions & 0 deletions example_ohos/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ dependencies:
path: ../example

photo_manager_image_provider: # override
video_player: # override
url_launcher: # override
path_provider: # override

dev_dependencies:
flutter_lints: any
Expand Down
38 changes: 38 additions & 0 deletions lib/src/internal/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:photo_manager/platform_utils.dart';

import '../filter/path_filter.dart';
import '../types/entity.dart';
import 'plugin.dart';

class Editor {
final DarwinEditor _darwin = const DarwinEditor();
final AndroidEditor _android = const AndroidEditor();
final OhosEditor _ohos = const OhosEditor();

/// Support iOS and macOS.
DarwinEditor get darwin {
Expand All @@ -21,16 +24,26 @@ class Editor {
throw const OSError('Darwin Editor should only be use on iOS or macOS.');
}

/// Support Android.
AndroidEditor get android {
if (Platform.isAndroid) {
return _android;
}
throw const OSError('Android Editor should only be use on Android.');
}

/// Support Openharmony.
OhosEditor get ohos {
if (PlatformUtils.isOhos) {
return _ohos;
}
throw const OSError('Ohos Editor should only be use on OpenHarmony.');
}

/// Delete entities with specific IDs.
///
/// Entities will be deleted no matter which album they're located at on iOS.
/// The method as [moveToTrash] on OpenHarmony, it's not support completely delete.
Future<List<String>> deleteWithIds(List<String> ids) {
return plugin.deleteWithIds(ids);
}
Expand Down Expand Up @@ -292,3 +305,28 @@ class AndroidEditor {
return plugin.moveToTrash(list);
}
}

/// An editor for OpenHarmony.
class OhosEditor {
/// Creates a new [OhosEditor] object.
const OhosEditor();

/// Returns column names of the photo access.
Future<List<String>> ohosColumns() {
return plugin.ohosColumns();
}

/// Sets the favorite status of the given [entity].
///
/// Returns the updated [AssetEntity] if the operation was successful; otherwise, `null`.
Future<AssetEntity?> favoriteAsset({
required AssetEntity entity,
required bool favorite,
}) async {
final bool result = await plugin.favoriteAsset(entity.id, favorite);
if (result) {
return entity.copyWith(isFavorite: favorite);
}
return null;
}
}
41 changes: 30 additions & 11 deletions lib/src/internal/plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
return _channel.invokeMethod(PMConstants.mGetOriginBytes, params);
}

Future<void> releaseCache() {
Future<void> releaseCache() async {
if (PlatformUtils.isOhos) {
return;
}
return _channel.invokeMethod(PMConstants.mReleaseMemoryCache);
}

Expand Down Expand Up @@ -335,6 +338,9 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
}

Future<String> getSystemVersion() async {
if (PlatformUtils.isOhos) {
return '';
}
return await _channel.invokeMethod<String>(
PMConstants.mSystemVersion,
) as String;
Expand Down Expand Up @@ -375,7 +381,10 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
return entity.title ?? '';
}

Future<String?> getMediaUrl(AssetEntity entity) {
Future<String?> getMediaUrl(AssetEntity entity) async {
if (PlatformUtils.isOhos) {
return entity.id;
}
return _channel.invokeMethod(
PMConstants.mGetMediaUrl,
<String, dynamic>{'id': entity.id, 'type': entity.typeInt},
Expand All @@ -385,6 +394,9 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
Future<List<AssetPathEntity>> getSubPathEntities(
AssetPathEntity pathEntity,
) async {
if (PlatformUtils.isOhos) {
return <AssetPathEntity>[];
}
final Map<dynamic, dynamic> result =
await _channel.invokeMethod<Map<dynamic, dynamic>>(
PMConstants.mGetSubPath,
Expand Down Expand Up @@ -442,7 +454,10 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
);
}

Future<void> clearFileCache() {
Future<void> clearFileCache() async {
if (PlatformUtils.isOhos) {
return;
}
return _channel.invokeMethod(PMConstants.mClearFileCache);
}

Expand All @@ -453,7 +468,10 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
Future<void> requestCacheAssetsThumbnail(
List<String> ids,
ThumbnailOption option,
) {
) async {
if (PlatformUtils.isOhos) {
return;
}
assert(ids.isNotEmpty);
return _channel.invokeMethod(
PMConstants.mRequestCacheAssetsThumb,
Expand Down Expand Up @@ -527,14 +545,15 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin, OhosPlugin {
}

Future<bool> isLocallyAvailable(String id, {bool isOrigin = false}) async {
if (Platform.isAndroid) {
return true;
if (Platform.isIOS || Platform.isMacOS) {
final bool result = await _channel.invokeMethod<bool>(
PMConstants.mIsLocallyAvailable,
<String, dynamic>{'id': id, 'isOrigin': isOrigin},
) as bool;
return result;
}
final bool result = await _channel.invokeMethod<bool>(
PMConstants.mIsLocallyAvailable,
<String, dynamic>{'id': id, 'isOrigin': isOrigin},
) as bool;
return result;

return true;
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/src/managers/caching_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PhotoCachingManager {
);

/// Request caching for assets.
/// The method does not supported on OpenHarmony.
Future<void> requestCacheAssets({
required List<AssetEntity> assets,
ThumbnailOption option = _defaultOption,
Expand All @@ -32,6 +33,7 @@ class PhotoCachingManager {
}

/// Request caching for assets' ID.
/// The method does not supported on OpenHarmony.
Future<void> requestCacheAssetsWithIds({
required List<String> assetIds,
ThumbnailOption option = _defaultOption,
Expand All @@ -41,5 +43,6 @@ class PhotoCachingManager {
}

/// Cancel all cache request.
/// The method does not supported on OpenHarmony.
Future<void> cancelCacheRequest() => plugin.cancelCacheRequests();
}
2 changes: 2 additions & 0 deletions lib/src/managers/photo_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class PhotoManager {
/// call the [getAssetPathList] to start again.
///
/// Make sure callers of this method have `await`ed properly.
/// The method does not supported on OpenHarmony.
static Future<void> releaseCache() => plugin.releaseCache();

/// {@macro photo_manager.NotifyManager.addChangeCallback}
Expand Down Expand Up @@ -171,6 +172,7 @@ class PhotoManager {
static Future<String> systemVersion() => plugin.getSystemVersion();

/// Clear all file caches.
/// The method does not supported on OpenHarmony.
static Future<void> clearFileCache() => plugin.clearFileCache();

/// Returns the count of assets.
Expand Down
8 changes: 8 additions & 0 deletions ohos/src/main/ets/components/plugin/handlers/AlbumHandler.ets
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RequestType } from '../types/RequestType';
import { dataSharePredicates } from '@kit.ArkData';
import PhotoManagerPlugin from '../PhotoManagerPlugin';
import { PermissionHandler } from './PermissionHandler';
import { PhotoAssetHandler } from './PhotoAssetHandler';
import { AsyncCallback } from '@ohos.base';
import { MapUtils } from '../utils/MapUtils';

Expand Down Expand Up @@ -247,6 +248,13 @@ export class AlbumHandler extends HandlerBase implements MethodCallHandlerBase {
return new AllAlbum(requestType, assets.getCount());

}

static async copyToAlbum(album: photoAccessHelper.Album, photoAsset: photoAccessHelper.PhotoAsset): Promise<void> {
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(PhotoManagerPlugin.uiContext);
let albumChangeRequest = new photoAccessHelper.MediaAlbumChangeRequest(album);
albumChangeRequest.addAssets([photoAsset]);
await phAccessHelper.applyChanges(albumChangeRequest);
}
}

/// For all Album and photo Album
Expand Down
Loading

0 comments on commit d61bda8

Please sign in to comment.