|
|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
|
import '../../data/local/local_storage_service.dart';
|
|
|
|
|
import '../../data/immich/immich_service.dart';
|
|
|
|
|
import '../../data/immich/models/immich_asset.dart';
|
|
|
|
|
@ -26,6 +28,8 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
List<ImmichAsset> _assets = [];
|
|
|
|
|
bool _isLoading = false;
|
|
|
|
|
String? _errorMessage;
|
|
|
|
|
final ImagePicker _imagePicker = ImagePicker();
|
|
|
|
|
bool _isUploading = false;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
@ -100,6 +104,11 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
title: const Text('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,
|
|
|
|
|
@ -118,6 +127,19 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildBody() {
|
|
|
|
|
if (_isUploading) {
|
|
|
|
|
return const Center(
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
CircularProgressIndicator(),
|
|
|
|
|
SizedBox(height: 16),
|
|
|
|
|
Text('Uploading images...'),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_isLoading && _assets.isEmpty) {
|
|
|
|
|
return const Center(
|
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
|
@ -235,7 +257,8 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
Widget _buildImageWidget(String url, ImmichAsset asset) {
|
|
|
|
|
// Use FutureBuilder to fetch image bytes via ImmichService with proper auth
|
|
|
|
|
return FutureBuilder<Uint8List?>(
|
|
|
|
|
future: widget.immichService?.fetchImageBytes(asset.id, isThumbnail: true),
|
|
|
|
|
future:
|
|
|
|
|
widget.immichService?.fetchImageBytes(asset.id, isThumbnail: true),
|
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
|
return const Center(
|
|
|
|
|
@ -280,7 +303,8 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: FutureBuilder<Uint8List?>(
|
|
|
|
|
future: widget.immichService?.fetchImageBytes(asset.id, isThumbnail: false),
|
|
|
|
|
future: widget.immichService
|
|
|
|
|
?.fetchImageBytes(asset.id, isThumbnail: false),
|
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
|
return const Center(
|
|
|
|
|
@ -288,7 +312,9 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (snapshot.hasError || !snapshot.hasData || snapshot.data == null) {
|
|
|
|
|
if (snapshot.hasError ||
|
|
|
|
|
!snapshot.hasData ||
|
|
|
|
|
snapshot.data == null) {
|
|
|
|
|
return const Center(
|
|
|
|
|
child: Text('Failed to load image'),
|
|
|
|
|
);
|
|
|
|
|
@ -429,4 +455,282 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
|
|
|
|
|
return buffer.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Opens image picker and uploads selected images to Immich.
|
|
|
|
|
Future<void> _pickAndUploadImages() async {
|
|
|
|
|
if (widget.immichService == null) {
|
|
|
|
|
if (mounted) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
const SnackBar(
|
|
|
|
|
content: Text('Immich service not available'),
|
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Show dialog to choose between single or multiple images
|
|
|
|
|
final pickerChoice = await showDialog<ImagePickerChoice>(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
title: const Text('Select Images'),
|
|
|
|
|
content: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
ListTile(
|
|
|
|
|
leading: const Icon(Icons.photo),
|
|
|
|
|
title: const Text('Pick Single Image'),
|
|
|
|
|
onTap: () =>
|
|
|
|
|
Navigator.of(context).pop(ImagePickerChoice.single),
|
|
|
|
|
),
|
|
|
|
|
ListTile(
|
|
|
|
|
leading: const Icon(Icons.photo_library),
|
|
|
|
|
title: const Text('Pick Multiple Images'),
|
|
|
|
|
onTap: () =>
|
|
|
|
|
Navigator.of(context).pop(ImagePickerChoice.multiple),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
|
child: const Text('Cancel'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (pickerChoice == null) return;
|
|
|
|
|
|
|
|
|
|
List<XFile> pickedFiles;
|
|
|
|
|
try {
|
|
|
|
|
if (pickerChoice == ImagePickerChoice.multiple) {
|
|
|
|
|
pickedFiles = await _imagePicker.pickMultiImage();
|
|
|
|
|
} else {
|
|
|
|
|
final pickedFile = await _imagePicker.pickImage(
|
|
|
|
|
source: ImageSource.gallery,
|
|
|
|
|
imageQuality: 100,
|
|
|
|
|
);
|
|
|
|
|
pickedFiles = pickedFile != null ? [pickedFile] : [];
|
|
|
|
|
}
|
|
|
|
|
} catch (pickError, stackTrace) {
|
|
|
|
|
// Handle image picker specific errors
|
|
|
|
|
debugPrint('Image picker error: $pickError');
|
|
|
|
|
debugPrint('Stack trace: $stackTrace');
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
String errorMessage = 'Failed to open gallery';
|
|
|
|
|
String errorDetails = pickError.toString();
|
|
|
|
|
|
|
|
|
|
// Check for specific error types
|
|
|
|
|
if (errorDetails.contains('Permission') ||
|
|
|
|
|
errorDetails.contains('permission') ||
|
|
|
|
|
errorDetails.contains('PERMISSION')) {
|
|
|
|
|
errorMessage =
|
|
|
|
|
'Permission denied. Please grant photo library access in app settings.';
|
|
|
|
|
} else if (errorDetails.contains('PlatformException')) {
|
|
|
|
|
// Extract the actual error message from PlatformException
|
|
|
|
|
final match = RegExp(r'PlatformException\([^,]+,\s*([^,]+)')
|
|
|
|
|
.firstMatch(errorDetails);
|
|
|
|
|
if (match != null) {
|
|
|
|
|
errorDetails = match.group(1) ?? errorDetails;
|
|
|
|
|
}
|
|
|
|
|
errorMessage = 'Failed to open gallery: $errorDetails';
|
|
|
|
|
} else {
|
|
|
|
|
errorMessage = 'Failed to open gallery: $errorDetails';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
title: const Text('Gallery Error'),
|
|
|
|
|
content: SingleChildScrollView(
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Text(errorMessage),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
const Text(
|
|
|
|
|
'Troubleshooting:',
|
|
|
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
const Text('1. Check app permissions in device settings'),
|
|
|
|
|
const Text('2. Make sure a gallery app is installed'),
|
|
|
|
|
const Text('3. Try restarting the app'),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
const Text(
|
|
|
|
|
'Full error:',
|
|
|
|
|
style:
|
|
|
|
|
TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 4),
|
|
|
|
|
Text(
|
|
|
|
|
errorDetails,
|
|
|
|
|
style: const TextStyle(fontSize: 10),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
|
child: const Text('Close'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pickedFiles.isEmpty) {
|
|
|
|
|
// User cancelled or no images selected - this is not an error
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Show upload progress
|
|
|
|
|
setState(() {
|
|
|
|
|
_isUploading = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
int successCount = 0;
|
|
|
|
|
int failureCount = 0;
|
|
|
|
|
final errors = <String>[];
|
|
|
|
|
|
|
|
|
|
// Upload each image
|
|
|
|
|
for (final pickedFile in pickedFiles) {
|
|
|
|
|
try {
|
|
|
|
|
final file = File(pickedFile.path);
|
|
|
|
|
debugPrint('Uploading file: ${pickedFile.name}');
|
|
|
|
|
final uploadResponse = await widget.immichService!.uploadImage(file);
|
|
|
|
|
debugPrint('Upload successful for ${pickedFile.name}: ${uploadResponse.id}');
|
|
|
|
|
successCount++;
|
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
|
debugPrint('Upload failed for ${pickedFile.name}: $e');
|
|
|
|
|
debugPrint('Stack trace: $stackTrace');
|
|
|
|
|
failureCount++;
|
|
|
|
|
errors.add('${pickedFile.name}: ${e.toString()}');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
_isUploading = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Show result
|
|
|
|
|
if (mounted) {
|
|
|
|
|
if (failureCount == 0) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
content: Text('Successfully uploaded $successCount image(s)'),
|
|
|
|
|
backgroundColor: Colors.green,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
content: Text('Uploaded $successCount, failed $failureCount'),
|
|
|
|
|
backgroundColor: Colors.orange,
|
|
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
|
action: SnackBarAction(
|
|
|
|
|
label: 'Details',
|
|
|
|
|
onPressed: () {
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
title: const Text('Upload Errors'),
|
|
|
|
|
content: SingleChildScrollView(
|
|
|
|
|
child: Text(errors.join('\n')),
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
|
child: const Text('Close'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Refresh the asset list
|
|
|
|
|
debugPrint('Refreshing asset list after upload');
|
|
|
|
|
await _loadAssets(forceRefresh: true);
|
|
|
|
|
debugPrint('Asset list refreshed, current count: ${_assets.length}');
|
|
|
|
|
}
|
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isUploading = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
// Log the full error for debugging
|
|
|
|
|
debugPrint('Image picker error: $e');
|
|
|
|
|
debugPrint('Stack trace: $stackTrace');
|
|
|
|
|
|
|
|
|
|
String errorMessage = 'Failed to pick images';
|
|
|
|
|
if (e.toString().contains('Permission')) {
|
|
|
|
|
errorMessage =
|
|
|
|
|
'Permission denied. Please grant photo library access in app settings.';
|
|
|
|
|
} else if (e.toString().contains('PlatformException')) {
|
|
|
|
|
errorMessage =
|
|
|
|
|
'Failed to open gallery. Please check app permissions.';
|
|
|
|
|
} else {
|
|
|
|
|
errorMessage = 'Failed to pick images: ${e.toString()}';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
content: Text(errorMessage),
|
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
|
duration: const Duration(seconds: 5),
|
|
|
|
|
action: SnackBarAction(
|
|
|
|
|
label: 'Details',
|
|
|
|
|
textColor: Colors.white,
|
|
|
|
|
onPressed: () {
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
title: const Text('Error Details'),
|
|
|
|
|
content: SingleChildScrollView(
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
Text('Error: ${e.toString()}'),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
const Text(
|
|
|
|
|
'If this is a permission error, please grant photo library access in your device settings.',
|
|
|
|
|
style: TextStyle(fontSize: 12),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
|
child: const Text('Close'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum ImagePickerChoice {
|
|
|
|
|
single,
|
|
|
|
|
multiple,
|
|
|
|
|
}
|
|
|
|
|
|