nc-photos/app/lib/widget/photo_list_util.dart

129 lines
3.4 KiB
Dart
Raw Normal View History

2022-09-05 06:49:26 +02:00
import 'dart:math' as math;
2023-03-13 12:55:39 +01:00
import 'package:clock/clock.dart';
2022-07-25 07:51:52 +02:00
import 'package:collection/collection.dart';
2022-01-15 11:35:15 +01:00
import 'package:logging/logging.dart';
import 'package:nc_photos/date_time_extension.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/cover_provider.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/entity/album/sort_provider.dart';
import 'package:nc_photos/entity/file_descriptor.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
part 'photo_list_util.g.dart';
2021-10-04 15:53:03 +02:00
2021-12-19 12:30:56 +01:00
class DateGroupHelper {
DateGroupHelper({
2021-10-04 15:53:03 +02:00
required this.isMonthOnly,
});
DateTime? onFile(FileDescriptor file) {
final newDate = file.fdDateTime.toLocal();
2021-10-04 15:53:03 +02:00
if (newDate.year != _currentDate?.year ||
newDate.month != _currentDate?.month ||
(!isMonthOnly && newDate.day != _currentDate?.day)) {
_currentDate = newDate;
return newDate;
2022-04-22 21:19:41 +02:00
} else {
return null;
2021-10-04 15:53:03 +02:00
}
}
final bool isMonthOnly;
DateTime? _currentDate;
}
2021-12-19 12:44:41 +01:00
2022-01-15 11:35:15 +01:00
/// Build memory album from files
///
/// Feb 29 is treated as Mar 1 on non leap years
2022-12-16 16:01:04 +01:00
@npLog
2022-01-15 11:35:15 +01:00
class MemoryAlbumHelper {
2022-09-05 06:49:26 +02:00
MemoryAlbumHelper({
DateTime? today,
required int dayRange,
2023-03-13 12:55:39 +01:00
}) : today = (today?.toLocal() ?? clock.now()).toMidnight(),
2022-09-05 06:49:26 +02:00
dayRange = math.max(dayRange, 0);
2022-01-15 11:35:15 +01:00
void addFile(FileDescriptor f) {
final date = f.fdDateTime.toLocal().toMidnight();
2022-01-15 11:35:15 +01:00
final diff = today.difference(date).inDays;
if (diff < 300) {
return;
}
for (final dy in [0, -1, 1]) {
if (today.copyWith(year: date.year + dy).difference(date).abs().inDays <=
2022-09-05 06:49:26 +02:00
dayRange) {
_log.fine("[addFile] Add file (${f.fdDateTime}) to ${date.year + dy}");
2022-01-15 11:35:15 +01:00
_addFileToYear(f, date.year + dy);
break;
}
}
}
/// Build list of memory albums
///
/// [nameBuilder] is a function that return the name of the album for a
/// particular year
List<Album> build(String Function(int year) nameBuilder) {
return _data.entries
.sorted((a, b) => b.key.compareTo(a.key))
.map((e) => Album(
name: nameBuilder(e.key),
provider: AlbumMemoryProvider(
year: e.key, month: today.month, day: today.day),
coverProvider:
AlbumMemoryCoverProvider(coverFile: e.value.coverFile),
2022-01-15 11:35:15 +01:00
sortProvider: const AlbumTimeSortProvider(isAscending: false),
))
.toList();
}
void _addFileToYear(FileDescriptor f, int year) {
2022-01-15 11:35:15 +01:00
final item = _data[year];
final date = today.copyWith(year: year);
if (item == null) {
_data[year] = _MemoryAlbumHelperItem(date, f);
} else {
final coverDiff = _MemoryAlbumHelperItem.getCoverDiff(date, f);
if (coverDiff < item.coverDiff) {
item.coverFile = f;
item.coverDiff = coverDiff;
}
}
}
final DateTime today;
2022-09-05 06:49:26 +02:00
final int dayRange;
2022-01-15 11:35:15 +01:00
final _data = <int, _MemoryAlbumHelperItem>{};
}
2021-12-19 12:44:41 +01:00
int getThumbSize(int zoomLevel) {
switch (zoomLevel) {
case -1:
return 96;
case 1:
return 176;
case 2:
return 256;
case 0:
default:
return 112;
}
}
2022-01-15 11:35:15 +01:00
class _MemoryAlbumHelperItem {
_MemoryAlbumHelperItem(this.date, this.coverFile)
: coverDiff = getCoverDiff(date, coverFile);
static Duration getCoverDiff(DateTime date, FileDescriptor f) =>
f.fdDateTime.difference(date.copyWith(hour: 12)).abs();
2022-01-15 11:35:15 +01:00
final DateTime date;
FileDescriptor coverFile;
2022-01-15 11:35:15 +01:00
Duration coverDiff;
}