2022-08-08 17:46:12 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-08-11 15:13:33 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-08-08 17:46:12 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
|
|
|
import 'package:openapi/api.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:share_plus/share_plus.dart';
|
|
|
|
import 'package:path/path.dart' as p;
|
|
|
|
import 'api.service.dart';
|
|
|
|
|
|
|
|
final shareServiceProvider =
|
2022-08-11 15:13:33 +02:00
|
|
|
Provider((ref) => ShareService(ref.watch(apiServiceProvider)));
|
2022-08-08 17:46:12 +02:00
|
|
|
|
|
|
|
class ShareService {
|
|
|
|
final ApiService _apiService;
|
|
|
|
|
|
|
|
ShareService(this._apiService);
|
|
|
|
|
|
|
|
Future<void> shareAsset(AssetResponseDto asset) async {
|
|
|
|
await shareAssets([asset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> shareAssets(List<AssetResponseDto> assets) async {
|
|
|
|
final downloadedFilePaths = assets.map((asset) async {
|
|
|
|
final res = await _apiService.assetApi.downloadFileWithHttpInfo(
|
|
|
|
asset.deviceAssetId,
|
|
|
|
asset.deviceId,
|
|
|
|
isThumb: false,
|
|
|
|
isWeb: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
final fileName = p.basename(asset.originalPath);
|
|
|
|
|
|
|
|
final tempDir = await getTemporaryDirectory();
|
|
|
|
final tempFile = await File('${tempDir.path}/$fileName').create();
|
|
|
|
tempFile.writeAsBytesSync(res.bodyBytes);
|
|
|
|
|
|
|
|
return tempFile.path;
|
|
|
|
});
|
|
|
|
|
2022-08-11 15:13:33 +02:00
|
|
|
Share.shareFiles(
|
|
|
|
await Future.wait(downloadedFilePaths),
|
|
|
|
sharePositionOrigin: Rect.zero,
|
|
|
|
);
|
2022-08-08 17:46:12 +02:00
|
|
|
}
|
|
|
|
}
|