|
|
|
@ -2,6 +2,8 @@ import 'dart:io';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
|
|
|
|
import '../../core/logger.dart';
|
|
|
|
|
|
|
|
import '../../core/service_locator.dart';
|
|
|
|
import '../../data/local/local_storage_service.dart';
|
|
|
|
import '../../data/local/local_storage_service.dart';
|
|
|
|
import '../../data/immich/immich_service.dart';
|
|
|
|
import '../../data/immich/immich_service.dart';
|
|
|
|
import '../../data/immich/models/immich_asset.dart';
|
|
|
|
import '../../data/immich/models/immich_asset.dart';
|
|
|
|
@ -11,14 +13,7 @@ import '../../data/immich/models/immich_asset.dart';
|
|
|
|
/// Displays images from Immich in a grid layout with pull-to-refresh.
|
|
|
|
/// Displays images from Immich in a grid layout with pull-to-refresh.
|
|
|
|
/// Shows cached images first, then fetches from API.
|
|
|
|
/// Shows cached images first, then fetches from API.
|
|
|
|
class ImmichScreen extends StatefulWidget {
|
|
|
|
class ImmichScreen extends StatefulWidget {
|
|
|
|
final LocalStorageService? localStorageService;
|
|
|
|
const ImmichScreen({super.key});
|
|
|
|
final ImmichService? immichService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ImmichScreen({
|
|
|
|
|
|
|
|
super.key,
|
|
|
|
|
|
|
|
this.localStorageService,
|
|
|
|
|
|
|
|
this.immichService,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
State<ImmichScreen> createState() => _ImmichScreenState();
|
|
|
|
State<ImmichScreen> createState() => _ImmichScreenState();
|
|
|
|
@ -41,7 +36,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
|
|
|
|
|
|
|
/// Loads assets from cache first, then fetches from API.
|
|
|
|
/// Loads assets from cache first, then fetches from API.
|
|
|
|
Future<void> _loadAssets({bool forceRefresh = false}) async {
|
|
|
|
Future<void> _loadAssets({bool forceRefresh = false}) async {
|
|
|
|
if (widget.immichService == null) {
|
|
|
|
if (ServiceLocator.instance.immichService == null) {
|
|
|
|
setState(() {
|
|
|
|
setState(() {
|
|
|
|
_errorMessage = 'Immich service not available';
|
|
|
|
_errorMessage = 'Immich service not available';
|
|
|
|
_isLoading = false;
|
|
|
|
_isLoading = false;
|
|
|
|
@ -57,7 +52,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// First, try to load cached assets
|
|
|
|
// First, try to load cached assets
|
|
|
|
if (!forceRefresh) {
|
|
|
|
if (!forceRefresh) {
|
|
|
|
final cachedAssets = await widget.immichService!.getCachedAssets();
|
|
|
|
final cachedAssets = await ServiceLocator.instance.immichService!.getCachedAssets();
|
|
|
|
if (cachedAssets.isNotEmpty) {
|
|
|
|
if (cachedAssets.isNotEmpty) {
|
|
|
|
setState(() {
|
|
|
|
setState(() {
|
|
|
|
_assets = cachedAssets;
|
|
|
|
_assets = cachedAssets;
|
|
|
|
@ -82,7 +77,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
/// Fetches assets from Immich API.
|
|
|
|
/// Fetches assets from Immich API.
|
|
|
|
Future<void> _fetchFromApi() async {
|
|
|
|
Future<void> _fetchFromApi() async {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
final assets = await widget.immichService!.fetchAssets(limit: 100);
|
|
|
|
final assets = await ServiceLocator.instance.immichService!.fetchAssets(limit: 100);
|
|
|
|
setState(() {
|
|
|
|
setState(() {
|
|
|
|
_assets = assets;
|
|
|
|
_assets = assets;
|
|
|
|
_isLoading = false;
|
|
|
|
_isLoading = false;
|
|
|
|
@ -97,7 +92,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
|
|
|
|
|
|
|
/// Gets the thumbnail URL for an asset with proper headers.
|
|
|
|
/// Gets the thumbnail URL for an asset with proper headers.
|
|
|
|
String _getThumbnailUrl(ImmichAsset asset) {
|
|
|
|
String _getThumbnailUrl(ImmichAsset asset) {
|
|
|
|
return widget.immichService!.getThumbnailUrl(asset.id);
|
|
|
|
return ServiceLocator.instance.immichService!.getThumbnailUrl(asset.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
@ -305,7 +300,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
// Use FutureBuilder to fetch image bytes via ImmichService with proper auth
|
|
|
|
// Use FutureBuilder to fetch image bytes via ImmichService with proper auth
|
|
|
|
return FutureBuilder<Uint8List?>(
|
|
|
|
return FutureBuilder<Uint8List?>(
|
|
|
|
future:
|
|
|
|
future:
|
|
|
|
widget.immichService?.fetchImageBytes(asset.id, isThumbnail: true),
|
|
|
|
ServiceLocator.instance.immichService?.fetchImageBytes(asset.id, isThumbnail: true),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
return const Center(
|
|
|
|
return const Center(
|
|
|
|
@ -350,7 +345,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
Expanded(
|
|
|
|
child: FutureBuilder<Uint8List?>(
|
|
|
|
child: FutureBuilder<Uint8List?>(
|
|
|
|
future: widget.immichService
|
|
|
|
future: ServiceLocator.instance.immichService
|
|
|
|
?.fetchImageBytes(asset.id, isThumbnail: false),
|
|
|
|
?.fetchImageBytes(asset.id, isThumbnail: false),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
@ -405,7 +400,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
|
|
|
|
|
|
|
/// Tests the connection to Immich server by calling /api/server/about.
|
|
|
|
/// Tests the connection to Immich server by calling /api/server/about.
|
|
|
|
Future<void> _testServerConnection() async {
|
|
|
|
Future<void> _testServerConnection() async {
|
|
|
|
if (widget.immichService == null) {
|
|
|
|
if (ServiceLocator.instance.immichService == null) {
|
|
|
|
if (mounted) {
|
|
|
|
if (mounted) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
@ -426,7 +421,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
final serverInfo = await widget.immichService!.getServerInfo();
|
|
|
|
final serverInfo = await ServiceLocator.instance.immichService!.getServerInfo();
|
|
|
|
|
|
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
|
|
|
|
|
|
@ -493,7 +488,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _deleteSelectedAssets() async {
|
|
|
|
Future<void> _deleteSelectedAssets() async {
|
|
|
|
if (_selectedAssetIds.isEmpty || widget.immichService == null) {
|
|
|
|
if (_selectedAssetIds.isEmpty || ServiceLocator.instance.immichService == null) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -527,7 +522,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
final assetIdsToDelete = _selectedAssetIds.toList();
|
|
|
|
final assetIdsToDelete = _selectedAssetIds.toList();
|
|
|
|
await widget.immichService!.deleteAssets(assetIdsToDelete);
|
|
|
|
await ServiceLocator.instance.immichService!.deleteAssets(assetIdsToDelete);
|
|
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
if (mounted) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
@ -585,7 +580,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
|
|
|
|
|
|
|
/// Opens image picker and uploads selected images to Immich.
|
|
|
|
/// Opens image picker and uploads selected images to Immich.
|
|
|
|
Future<void> _pickAndUploadImages() async {
|
|
|
|
Future<void> _pickAndUploadImages() async {
|
|
|
|
if (widget.immichService == null) {
|
|
|
|
if (ServiceLocator.instance.immichService == null) {
|
|
|
|
if (mounted) {
|
|
|
|
if (mounted) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
@ -644,8 +639,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (pickError, stackTrace) {
|
|
|
|
} catch (pickError, stackTrace) {
|
|
|
|
// Handle image picker specific errors
|
|
|
|
// Handle image picker specific errors
|
|
|
|
debugPrint('Image picker error: $pickError');
|
|
|
|
Logger.error('Image picker error: $pickError', pickError, stackTrace);
|
|
|
|
debugPrint('Stack trace: $stackTrace');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
if (mounted) {
|
|
|
|
String errorMessage = 'Failed to open gallery';
|
|
|
|
String errorMessage = 'Failed to open gallery';
|
|
|
|
@ -732,13 +726,12 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
for (final pickedFile in pickedFiles) {
|
|
|
|
for (final pickedFile in pickedFiles) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
final file = File(pickedFile.path);
|
|
|
|
final file = File(pickedFile.path);
|
|
|
|
debugPrint('Uploading file: ${pickedFile.name}');
|
|
|
|
Logger.debug('Uploading file: ${pickedFile.name}');
|
|
|
|
final uploadResponse = await widget.immichService!.uploadImage(file);
|
|
|
|
final uploadResponse = await ServiceLocator.instance.immichService!.uploadImage(file);
|
|
|
|
debugPrint('Upload successful for ${pickedFile.name}: ${uploadResponse.id}');
|
|
|
|
Logger.info('Upload successful for ${pickedFile.name}: ${uploadResponse.id}');
|
|
|
|
successCount++;
|
|
|
|
successCount++;
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
debugPrint('Upload failed for ${pickedFile.name}: $e');
|
|
|
|
Logger.error('Upload failed for ${pickedFile.name}: $e', e, stackTrace);
|
|
|
|
debugPrint('Stack trace: $stackTrace');
|
|
|
|
|
|
|
|
failureCount++;
|
|
|
|
failureCount++;
|
|
|
|
errors.add('${pickedFile.name}: ${e.toString()}');
|
|
|
|
errors.add('${pickedFile.name}: ${e.toString()}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -788,9 +781,9 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Refresh the asset list
|
|
|
|
// Refresh the asset list
|
|
|
|
debugPrint('Refreshing asset list after upload');
|
|
|
|
Logger.debug('Refreshing asset list after upload');
|
|
|
|
await _loadAssets(forceRefresh: true);
|
|
|
|
await _loadAssets(forceRefresh: true);
|
|
|
|
debugPrint('Asset list refreshed, current count: ${_assets.length}');
|
|
|
|
Logger.debug('Asset list refreshed, current count: ${_assets.length}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
setState(() {
|
|
|
|
setState(() {
|
|
|
|
@ -799,8 +792,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
|
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
if (mounted) {
|
|
|
|
// Log the full error for debugging
|
|
|
|
// Log the full error for debugging
|
|
|
|
debugPrint('Image picker error: $e');
|
|
|
|
Logger.error('Image picker error: $e', e, stackTrace);
|
|
|
|
debugPrint('Stack trace: $stackTrace');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String errorMessage = 'Failed to pick images';
|
|
|
|
String errorMessage = 'Failed to pick images';
|
|
|
|
if (e.toString().contains('Permission')) {
|
|
|
|
if (e.toString().contains('Permission')) {
|
|
|
|
|