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';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2022-08-08 17:46:12 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:share_plus/share_plus.dart';
|
|
|
|
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);
|
|
|
|
|
2022-11-08 19:00:24 +02:00
|
|
|
Future<void> shareAsset(Asset asset) async {
|
2022-08-08 17:46:12 +02:00
|
|
|
await shareAssets([asset]);
|
|
|
|
}
|
|
|
|
|
2022-11-08 19:00:24 +02:00
|
|
|
Future<void> shareAssets(List<Asset> assets) async {
|
2023-01-30 04:46:30 +02:00
|
|
|
final downloadedXFiles = assets.map<Future<XFile>>((asset) async {
|
2022-11-08 19:00:24 +02:00
|
|
|
if (asset.isRemote) {
|
|
|
|
final tempDir = await getTemporaryDirectory();
|
2023-02-04 22:42:42 +02:00
|
|
|
final fileName = asset.fileName;
|
2022-11-08 19:00:24 +02:00
|
|
|
final tempFile = await File('${tempDir.path}/$fileName').create();
|
2023-02-04 22:42:42 +02:00
|
|
|
final res = await _apiService.assetApi
|
|
|
|
.downloadFileWithHttpInfo(asset.remoteId!);
|
2022-11-08 19:00:24 +02:00
|
|
|
tempFile.writeAsBytesSync(res.bodyBytes);
|
2023-01-30 04:46:30 +02:00
|
|
|
return XFile(tempFile.path);
|
2022-11-08 19:00:24 +02:00
|
|
|
} else {
|
|
|
|
File? f = await asset.local!.file;
|
2023-01-30 04:46:30 +02:00
|
|
|
return XFile(f!.path);
|
2022-11-08 19:00:24 +02:00
|
|
|
}
|
2022-08-08 17:46:12 +02:00
|
|
|
});
|
|
|
|
|
2023-01-30 04:46:30 +02:00
|
|
|
Share.shareXFiles(
|
|
|
|
await Future.wait(downloadedXFiles),
|
2022-08-11 15:13:33 +02:00
|
|
|
sharePositionOrigin: Rect.zero,
|
|
|
|
);
|
2022-08-08 17:46:12 +02:00
|
|
|
}
|
|
|
|
}
|