@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import ' package:hooks_riverpod/hooks_riverpod.dart ' ;
import ' package:immich_mobile/shared/models/asset.dart ' ;
import ' package:immich_mobile/shared/providers/api.provider.dart ' ;
import ' package:logging/logging.dart ' ;
import ' package:path_provider/path_provider.dart ' ;
import ' package:share_plus/share_plus.dart ' ;
import ' api.service.dart ' ;
@ -13,32 +14,60 @@ final shareServiceProvider =
class ShareService {
final ApiService _apiService ;
final Logger _log = Logger ( " ShareService " ) ;
ShareService ( this . _apiService ) ;
Future < void > shareAsset ( Asset asset ) async {
await shareAssets ( [ asset ] ) ;
Future < bool > shareAsset ( Asset asset ) async {
return await shareAssets ( [ asset ] ) ;
}
Future < void > shareAssets ( List < Asset > assets ) async {
final downloadedXFiles = assets . map < Future < XFile > > ( ( asset ) async {
if ( asset . isRemote ) {
final tempDir = await getTemporaryDirectory ( ) ;
final fileName = asset . fileName ;
final tempFile = await File ( ' ${ tempDir . path } / $ fileName ' ) . create ( ) ;
final res = await _apiService . assetApi
. downloadFileWithHttpInfo ( asset . remoteId ! ) ;
tempFile . writeAsBytesSync ( res . bodyBytes ) ;
return XFile ( tempFile . path ) ;
} else {
File ? f = await asset . local ! . file ;
return XFile ( f ! . path ) ;
Future < bool > shareAssets ( List < Asset > assets ) async {
try {
final downloadedXFiles = < XFile > [ ] ;
for ( var asset in assets ) {
if ( asset . isRemote ) {
final tempDir = await getTemporaryDirectory ( ) ;
final fileName = asset . fileName ;
final tempFile = await File ( ' ${ tempDir . path } / $ fileName ' ) . create ( ) ;
final res = await _apiService . assetApi
. downloadFileWithHttpInfo ( asset . remoteId ! ) ;
if ( res . statusCode ! = 200 ) {
_log . severe (
" Asset download failed with status - ${ res . statusCode } and response - ${ res . body } " ,
) ;
continue ;
}
tempFile . writeAsBytesSync ( res . bodyBytes ) ;
downloadedXFiles . add ( XFile ( tempFile . path ) ) ;
} else {
File ? f = await asset . local ! . file ;
downloadedXFiles . add ( XFile ( f ! . path ) ) ;
}
}
if ( downloadedXFiles . isEmpty ) {
_log . warning ( " No asset can be retrieved for share " ) ;
return false ;
}
if ( downloadedXFiles . length ! = assets . length ) {
_log . warning (
" Partial share - Requested: ${ assets . length } , Sharing: ${ downloadedXFiles . length } " ,
) ;
}
} ) ;
Share . shareXFiles (
await Future . wait ( downloadedXFiles ) ,
sharePositionOrigin: Rect . zero ,
) ;
Share . shareXFiles (
downloadedXFiles ,
sharePositionOrigin: Rect . zero ,
) ;
return true ;
} catch ( error ) {
_log . severe ( " Share failed with error $ error " ) ;
}
return false ;
}
}