Files
imajviewer/lib/services/image_manager.dart

99 lines
2.7 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:io';
import 'package:flutter/material.dart';
class ImageManager extends ChangeNotifier {
static final ImageManager _instance = ImageManager._();
static ImageManager get instance => _instance;
ImageManager._();
static const _supportedExtensions = [
'png', 'jpg', 'jpeg', 'jfif', 'jpe', 'webp', 'bmp', 'gif', 'wbmp',
];
final List<String> _images = [];
int _currentIndex = 0;
List<String> get images => List.unmodifiable(_images);
int get currentIndex => _currentIndex;
int get imageCount => _images.length;
String get currentImagePath {
if (_images.isEmpty) return '';
return _images[_currentIndex];
}
/// Açılan resmin bulunduğu klasörü galeri olarak kurar (en yeni önce).
void setGalleryFromPaths(List<String> paths) {
String? anchor;
for (final p in paths) {
final ext = p.toLowerCase().split('.').last;
if (_supportedExtensions.contains(ext) && File(p).existsSync()) {
anchor = p;
break;
}
}
if (anchor == null) return;
final dir = File(anchor).parent;
if (!dir.existsSync()) return;
final List<(String, DateTime)> entries = [];
for (final entity in dir.listSync()) {
if (entity is! File) continue;
final p = entity.path;
final ext = p.toLowerCase().split('.').last;
if (!_supportedExtensions.contains(ext)) continue;
try {
entries.add((p, entity.lastModifiedSync()));
} catch (_) {}
}
entries.sort((a, b) {
final byDate = b.$2.compareTo(a.$2);
return byDate != 0 ? byDate : a.$1.compareTo(b.$1);
});
_images
..clear()
..addAll(entries.map((e) => e.$1));
_currentIndex = _images.indexOf(anchor);
if (_currentIndex < 0) {
final base = anchor.split(Platform.pathSeparator).last;
_currentIndex = _images
.indexWhere((p) => p.split(Platform.pathSeparator).last == base);
}
if (_currentIndex < 0) _currentIndex = 0;
notifyListeners();
}
void setCurrentIndex(int index) {
if (index < 0 || index >= _images.length) return;
_currentIndex = index;
notifyListeners();
}
void previousImage() {
if (_images.isEmpty) return;
_currentIndex = (_currentIndex - 1 + _images.length) % _images.length;
notifyListeners();
}
void nextImage() {
if (_images.isEmpty) return;
_currentIndex = (_currentIndex + 1) % _images.length;
notifyListeners();
}
/// Configure image cache for high-res images
static void configureCache() {
PaintingBinding.instance.imageCache.maximumSize = 1000;
PaintingBinding.instance.imageCache.maximumSizeBytes = 1024 * 1024 * 1024; // 1 GB
}
void clear() {
_images.clear();
_currentIndex = 0;
notifyListeners();
}
}