49 lines
1.1 KiB
Dart
49 lines
1.1 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:path/path.dart' as p;
|
|
|
|
import '../entities/album.dart';
|
|
|
|
class AlbumRepository {
|
|
final String basePath;
|
|
final List<Album> _albums = List.empty(growable: true);
|
|
DateTime _updated = DateTime.utc(1970);
|
|
|
|
AlbumRepository({required this.basePath});
|
|
|
|
Future<List<Album>> getAllAlbums() async {
|
|
if (DateTime.now().difference(_updated).inSeconds <= 60) {
|
|
return _albums;
|
|
}
|
|
_updated = DateTime.now();
|
|
|
|
final dir = Directory(basePath);
|
|
if (!await dir.exists()) {
|
|
return [];
|
|
}
|
|
|
|
var albums = await dir
|
|
.list()
|
|
.where((d) => d is Directory)
|
|
.map((d) => d as Directory)
|
|
.map((dir) {
|
|
final name = p.basename(dir.path);
|
|
return Album(
|
|
id: dir.path.hashCode,
|
|
name: name,
|
|
createdAt: dir.statSync().changed,
|
|
updatedAt: dir.statSync().changed,
|
|
);
|
|
})
|
|
.toList();
|
|
_albums.clear();
|
|
_albums.addAll(albums);
|
|
return albums;
|
|
}
|
|
|
|
Future<Album?> getAlbumById(int id) async {
|
|
final albums = await getAllAlbums();
|
|
return albums.where((a) => a.id == id).firstOrNull;
|
|
}
|
|
}
|