delete image working
This commit is contained in:
@@ -550,6 +550,85 @@ class ImmichService {
|
||||
};
|
||||
}
|
||||
|
||||
/// Deletes assets from Immich.
|
||||
///
|
||||
/// [assetIds] - List of asset UUIDs to delete.
|
||||
///
|
||||
/// Throws [ImmichException] if deletion fails.
|
||||
Future<void> deleteAssets(List<String> assetIds) async {
|
||||
if (assetIds.isEmpty) {
|
||||
throw ImmichException('No asset IDs provided for deletion');
|
||||
}
|
||||
|
||||
try {
|
||||
debugPrint('=== Immich Delete Assets ===');
|
||||
debugPrint('Asset IDs to delete: $assetIds');
|
||||
debugPrint('Count: ${assetIds.length}');
|
||||
|
||||
// DELETE /api/assets with ids in request body
|
||||
// According to Immich API: DELETE /api/assets with body: {"ids": ["uuid1", "uuid2", ...]}
|
||||
final requestBody = {
|
||||
'ids': assetIds,
|
||||
};
|
||||
|
||||
debugPrint('Request body: $requestBody');
|
||||
|
||||
final response = await _dio.delete(
|
||||
'/api/assets',
|
||||
data: requestBody,
|
||||
options: Options(
|
||||
headers: {
|
||||
'x-api-key': _apiKey,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
debugPrint('=== Immich Delete Response ===');
|
||||
debugPrint('Status Code: ${response.statusCode}');
|
||||
debugPrint('Response Data: ${response.data}');
|
||||
|
||||
if (response.statusCode != 200 && response.statusCode != 204) {
|
||||
final errorMessage = response.data is Map
|
||||
? (response.data as Map)['message']?.toString() ??
|
||||
response.statusMessage
|
||||
: response.statusMessage;
|
||||
throw ImmichException(
|
||||
'Delete failed: $errorMessage',
|
||||
response.statusCode,
|
||||
);
|
||||
}
|
||||
|
||||
// Remove deleted assets from local cache
|
||||
for (final assetId in assetIds) {
|
||||
try {
|
||||
await _localStorage.deleteItem('immich_$assetId');
|
||||
} catch (e) {
|
||||
debugPrint('Warning: Failed to remove asset $assetId from cache: $e');
|
||||
}
|
||||
}
|
||||
|
||||
debugPrint('Successfully deleted ${assetIds.length} asset(s)');
|
||||
} on DioException catch (e) {
|
||||
final statusCode = e.response?.statusCode;
|
||||
final errorData = e.response?.data;
|
||||
String errorMessage;
|
||||
|
||||
if (errorData is Map) {
|
||||
errorMessage = errorData['message']?.toString() ?? errorData.toString();
|
||||
} else {
|
||||
errorMessage = errorData?.toString() ?? e.message ?? 'Unknown error';
|
||||
}
|
||||
|
||||
throw ImmichException(
|
||||
'Delete failed: $errorMessage',
|
||||
statusCode,
|
||||
);
|
||||
} catch (e) {
|
||||
throw ImmichException('Delete failed: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Tests the connection to Immich server by calling the /api/server/about endpoint.
|
||||
///
|
||||
/// Returns server information including version and status.
|
||||
|
||||
@@ -127,7 +127,8 @@ class NostrService {
|
||||
}
|
||||
|
||||
// WebSocketChannel.connect can throw synchronously (e.g., host lookup failure)
|
||||
// Wrap in try-catch to ensure it's caught
|
||||
// But errors can also occur asynchronously in the stream
|
||||
// Wrap in try-catch to ensure synchronous errors are caught
|
||||
WebSocketChannel channel;
|
||||
try {
|
||||
channel = WebSocketChannel.connect(Uri.parse(relayUrl));
|
||||
@@ -139,8 +140,8 @@ class NostrService {
|
||||
final controller = StreamController<Map<String, dynamic>>.broadcast();
|
||||
_messageControllers[relayUrl] = controller;
|
||||
|
||||
// Update relay status (relay already found above)
|
||||
relay.isConnected = true;
|
||||
// Don't set isConnected = true immediately - wait for actual connection
|
||||
// The connection might fail asynchronously
|
||||
|
||||
// Listen for messages
|
||||
channel.stream.listen(
|
||||
@@ -265,10 +266,53 @@ class NostrService {
|
||||
throw Exception('Connection timeout');
|
||||
},
|
||||
);
|
||||
// Start listening to establish connection, then cancel immediately
|
||||
final subscription = stream.listen(null);
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
// Wait for connection to be established or fail
|
||||
// Listen to the stream to catch connection errors
|
||||
final completer = Completer<bool>();
|
||||
late StreamSubscription subscription;
|
||||
bool gotFirstMessage = false;
|
||||
|
||||
subscription = stream.listen(
|
||||
(data) {
|
||||
// Connection successful - we received data
|
||||
gotFirstMessage = true;
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(true);
|
||||
}
|
||||
},
|
||||
onError: (error) {
|
||||
// Connection failed
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(false);
|
||||
}
|
||||
},
|
||||
onDone: () {
|
||||
// Stream closed before connection established
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(false);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// Wait for connection to establish (first message) or fail
|
||||
// Give it a short timeout to see if connection succeeds
|
||||
final connected = await completer.future.timeout(
|
||||
const Duration(seconds: 2),
|
||||
onTimeout: () {
|
||||
subscription.cancel();
|
||||
// If we got a first message, connection was established
|
||||
return gotFirstMessage;
|
||||
},
|
||||
);
|
||||
|
||||
subscription.cancel();
|
||||
|
||||
// Check if relay is actually connected
|
||||
if (!connected || !relay.isConnected) {
|
||||
results[relay.url] = false;
|
||||
continue;
|
||||
}
|
||||
} catch (e) {
|
||||
results[relay.url] = false;
|
||||
continue;
|
||||
|
||||
@@ -30,6 +30,8 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
String? _errorMessage;
|
||||
final ImagePicker _imagePicker = ImagePicker();
|
||||
bool _isUploading = false;
|
||||
Set<String> _selectedAssetIds = {}; // Track selected assets for deletion
|
||||
bool _isSelectionMode = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -102,24 +104,40 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Immich Media'),
|
||||
title: Text(_isSelectionMode
|
||||
? '${_selectedAssetIds.length} selected'
|
||||
: 'Immich Media'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.photo_library),
|
||||
onPressed: _pickAndUploadImages,
|
||||
tooltip: 'Upload from Gallery',
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.info_outline),
|
||||
onPressed: _testServerConnection,
|
||||
tooltip: 'Test Server Connection',
|
||||
),
|
||||
if (_assets.isNotEmpty)
|
||||
if (_isSelectionMode) ...[
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: () => _loadAssets(forceRefresh: true),
|
||||
tooltip: 'Refresh',
|
||||
icon: const Icon(Icons.delete),
|
||||
onPressed: _selectedAssetIds.isEmpty ? null : _deleteSelectedAssets,
|
||||
tooltip: 'Delete Selected',
|
||||
color: Colors.red,
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: _exitSelectionMode,
|
||||
tooltip: 'Cancel Selection',
|
||||
),
|
||||
] else ...[
|
||||
IconButton(
|
||||
icon: const Icon(Icons.photo_library),
|
||||
onPressed: _pickAndUploadImages,
|
||||
tooltip: 'Upload from Gallery',
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.info_outline),
|
||||
onPressed: _testServerConnection,
|
||||
tooltip: 'Test Server Connection',
|
||||
),
|
||||
if (_assets.isNotEmpty)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: () => _loadAssets(forceRefresh: true),
|
||||
tooltip: 'Refresh',
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
body: _buildBody(),
|
||||
@@ -219,37 +237,66 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
|
||||
Widget _buildImageTile(ImmichAsset asset) {
|
||||
final thumbnailUrl = _getThumbnailUrl(asset);
|
||||
|
||||
// For Immich API, we need to pass the API key as a header
|
||||
// Since Image.network doesn't easily support custom headers,
|
||||
// we'll use a workaround: Immich might accept the key in the URL query parameter
|
||||
// OR we need to fetch images via Dio and convert to bytes.
|
||||
// Let's check ImmichService - it has Dio with headers configured.
|
||||
// Actually, we can use Image.network with headers parameter (Flutter supports this).
|
||||
// But we need the API key. Let me check if ImmichService exposes it.
|
||||
|
||||
// Since Immich API requires x-api-key header, and Image.network supports headers,
|
||||
// we need to get the API key. However, ImmichService doesn't expose it.
|
||||
// Let's modify ImmichService to expose a method that returns headers, or
|
||||
// we can fetch images via Dio and display them.
|
||||
|
||||
// For now, let's use Image.network and assume Immich might work without header
|
||||
// (which it won't, but this is a placeholder). We'll fix this properly next.
|
||||
|
||||
final isSelected = _selectedAssetIds.contains(asset.id);
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
// TODO: Navigate to full image view
|
||||
_showImageDetails(asset);
|
||||
onLongPress: () {
|
||||
if (!_isSelectionMode) {
|
||||
setState(() {
|
||||
_isSelectionMode = true;
|
||||
_selectedAssetIds.add(asset.id);
|
||||
});
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
color: Colors.grey[300],
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: _buildImageWidget(thumbnailUrl, asset),
|
||||
),
|
||||
onTap: () {
|
||||
if (_isSelectionMode) {
|
||||
setState(() {
|
||||
if (isSelected) {
|
||||
_selectedAssetIds.remove(asset.id);
|
||||
if (_selectedAssetIds.isEmpty) {
|
||||
_isSelectionMode = false;
|
||||
}
|
||||
} else {
|
||||
_selectedAssetIds.add(asset.id);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
_showImageDetails(asset);
|
||||
}
|
||||
},
|
||||
child: Stack(
|
||||
children: [
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
color: Colors.grey[300],
|
||||
border: isSelected
|
||||
? Border.all(color: Colors.blue, width: 3)
|
||||
: null,
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: _buildImageWidget(thumbnailUrl, asset),
|
||||
),
|
||||
),
|
||||
if (isSelected)
|
||||
Positioned(
|
||||
top: 8,
|
||||
right: 8,
|
||||
child: Container(
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.blue,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
padding: const EdgeInsets.all(4),
|
||||
child: const Icon(
|
||||
Icons.check_circle,
|
||||
color: Colors.white,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -438,6 +485,86 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
void _exitSelectionMode() {
|
||||
setState(() {
|
||||
_isSelectionMode = false;
|
||||
_selectedAssetIds.clear();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _deleteSelectedAssets() async {
|
||||
if (_selectedAssetIds.isEmpty || widget.immichService == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Show confirmation dialog
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Delete Assets'),
|
||||
content: Text(
|
||||
'Are you sure you want to delete ${_selectedAssetIds.length} asset(s)? This action cannot be undone.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
style: TextButton.styleFrom(foregroundColor: Colors.red),
|
||||
child: const Text('Delete'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final assetIdsToDelete = _selectedAssetIds.toList();
|
||||
await widget.immichService!.deleteAssets(assetIdsToDelete);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Successfully deleted ${assetIdsToDelete.length} asset(s)'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
|
||||
// Remove deleted assets from local list
|
||||
setState(() {
|
||||
_assets.removeWhere((asset) => assetIdsToDelete.contains(asset.id));
|
||||
_selectedAssetIds.clear();
|
||||
_isSelectionMode = false;
|
||||
});
|
||||
|
||||
// Refresh the list to ensure consistency
|
||||
await _loadAssets(forceRefresh: true);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Failed to delete assets: ${e.toString()}'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats server info map as a readable string.
|
||||
String _formatServerInfo(Map<String, dynamic> info) {
|
||||
final buffer = StringBuffer();
|
||||
|
||||
Reference in New Issue
Block a user