ente/lib/ui/collections/album/row_item.dart

171 lines
5.6 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2023-05-05 10:04:48 +00:00
import "package:intl/intl.dart";
import 'package:photos/db/files_db.dart';
2022-12-15 11:24:51 +00:00
import 'package:photos/models/collection.dart';
import 'package:photos/models/collection_items.dart';
import "package:photos/models/file.dart";
2022-12-15 11:24:51 +00:00
import 'package:photos/models/gallery_type.dart';
import "package:photos/services/collections_service.dart";
2022-10-19 11:16:27 +00:00
import 'package:photos/theme/ente_theme.dart';
2023-01-17 08:30:33 +00:00
import 'package:photos/ui/viewer/file/file_icons_widget.dart';
import 'package:photos/ui/viewer/file/no_thumbnail_widget.dart';
import 'package:photos/ui/viewer/file/thumbnail_widget.dart';
import 'package:photos/ui/viewer/gallery/collection_page.dart';
import 'package:photos/utils/navigation_util.dart';
import 'package:visibility_detector/visibility_detector.dart';
2023-06-06 16:43:32 +00:00
class AlbumRowItemWidget extends StatelessWidget {
final Collection c;
2022-10-19 11:16:27 +00:00
final double sideOfThumbnail;
final bool shouldRender;
final bool showFileCount;
static const tagPrefix = "collection";
2022-10-19 11:16:27 +00:00
2023-06-06 16:43:32 +00:00
AlbumRowItemWidget(
2022-10-19 11:16:27 +00:00
this.c,
this.sideOfThumbnail, {
this.shouldRender = false,
this.showFileCount = true,
2022-10-19 09:01:14 +00:00
Key? key,
}) : super(key: Key(c.id.toString()));
@override
Widget build(BuildContext context) {
2022-10-19 11:16:27 +00:00
final enteColorScheme = getEnteColorScheme(context);
final enteTextTheme = getEnteTextTheme(context);
return GestureDetector(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2022-11-17 05:32:24 +00:00
Stack(
children: [
ClipRRect(
2022-11-21 12:07:24 +00:00
borderRadius: BorderRadius.circular(1),
2022-11-17 05:32:24 +00:00
child: SizedBox(
height: sideOfThumbnail,
width: sideOfThumbnail,
child: FutureBuilder<File?>(
future: CollectionsService.instance.getCover(c),
builder: (context, snapshot) {
if (snapshot.hasData) {
final thumbnail = snapshot.data!;
final String heroTag = tagPrefix + thumbnail.tag;
return Hero(
tag: heroTag,
child: CollectionItemThumbnailWidget(
c: CollectionWithThumbnail(c, thumbnail),
heroTag: heroTag,
),
);
} else {
return const NoThumbnailWidget();
}
},
2022-11-17 05:32:24 +00:00
),
),
),
2022-11-17 05:32:24 +00:00
],
),
2022-10-19 11:16:27 +00:00
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2022-11-21 12:07:24 +00:00
const SizedBox(height: 2),
2022-10-19 11:16:27 +00:00
Text(
(c.displayName).trim(),
2022-10-19 11:16:27 +00:00
style: enteTextTheme.small,
overflow: TextOverflow.ellipsis,
),
showFileCount
? FutureBuilder<int>(
future: FilesDB.instance.collectionFileCount(c.id),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(
2023-05-05 10:04:48 +00:00
NumberFormat().format(snapshot.data),
style: enteTextTheme.small.copyWith(
color: enteColorScheme.textMuted,
),
);
} else {
return Text(
"",
style: enteTextTheme.small.copyWith(
color: enteColorScheme.textMuted,
),
);
}
},
)
: const SizedBox.shrink(),
],
),
],
),
onTap: () async {
final thumbnail = await CollectionsService.instance.getCover(c);
2022-12-15 11:24:51 +00:00
routeToPage(
context,
CollectionPage(
CollectionWithThumbnail(c, thumbnail),
tagPrefix: tagPrefix,
appBarType: (c.type == CollectionType.favorites
2022-12-15 11:24:51 +00:00
? GalleryType.favorite
: GalleryType.ownedCollection),
),
);
},
);
}
}
class CollectionItemThumbnailWidget extends StatefulWidget {
const CollectionItemThumbnailWidget({
Key? key,
required this.c,
required this.heroTag,
this.shouldRender = false,
}) : super(key: key);
final CollectionWithThumbnail c;
final String heroTag;
final bool shouldRender;
@override
State<CollectionItemThumbnailWidget> createState() =>
_CollectionItemThumbnailWidgetState();
}
class _CollectionItemThumbnailWidgetState
extends State<CollectionItemThumbnailWidget> {
bool _shouldRender = false;
@override
void initState() {
super.initState();
_shouldRender = widget.shouldRender;
}
@override
Widget build(BuildContext context) {
return VisibilityDetector(
key: Key("collection_item" + widget.c.thumbnail!.tag),
onVisibilityChanged: (visibility) {
final shouldRender = visibility.visibleFraction > 0;
if (mounted && shouldRender && !_shouldRender) {
setState(() {
_shouldRender = shouldRender;
});
}
},
child: _shouldRender
? ThumbnailWidget(
widget.c.thumbnail,
shouldShowArchiveStatus: widget.c.collection.isArchived(),
showFavForAlbumOnly: true,
key: Key(widget.heroTag),
)
2023-01-17 08:30:33 +00:00
: const ThumbnailPlaceHolder(),
);
}
}