nc-photos/app/lib/widget/builder/album_grid_item_builder.dart

101 lines
3.2 KiB
Dart
Raw Normal View History

2021-07-13 21:29:52 +02:00
import 'package:cached_network_image/cached_network_image.dart';
2021-12-10 05:53:22 +01:00
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
2021-07-13 21:29:52 +02:00
import 'package:flutter/material.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
2021-09-16 12:10:50 +02:00
import 'package:nc_photos/cache_manager_util.dart';
2021-07-13 21:29:52 +02:00
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/provider.dart';
2021-10-06 19:00:42 +02:00
import 'package:nc_photos/entity/file.dart';
2021-09-16 12:25:08 +02:00
import 'package:nc_photos/k.dart' as k;
2021-07-13 21:29:52 +02:00
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/widget/album_grid_item.dart';
/// Build a standard [AlbumGridItem] for an [Album]
class AlbumGridItemBuilder {
AlbumGridItemBuilder({
2021-07-23 22:05:57 +02:00
required this.account,
required this.album,
2021-08-13 12:45:26 +02:00
this.isShared = false,
2021-07-13 21:29:52 +02:00
});
AlbumGridItem build(BuildContext context) {
var subtitle = "";
2021-07-23 22:05:57 +02:00
String? subtitle2;
2022-01-27 22:05:02 +01:00
IconData? icon;
2021-07-13 21:29:52 +02:00
if (album.provider is AlbumStaticProvider) {
subtitle =
L10n.global().albumSize(AlbumStaticProvider.of(album).items.length);
2021-07-13 21:29:52 +02:00
} else if (album.provider is AlbumDirProvider) {
final provider = album.provider as AlbumDirProvider;
subtitle = provider.dirs.first.strippedPath;
if (provider.dirs.length > 1) {
subtitle2 = "+${provider.dirs.length - 1}";
}
2022-01-27 22:05:02 +01:00
icon = Icons.folder;
2022-01-29 12:31:32 +01:00
} else if (album.provider is AlbumTagProvider) {
final provider = album.provider as AlbumTagProvider;
subtitle = provider.tags.map((t) => t.displayName).join(", ");
icon = Icons.local_offer;
2021-07-13 21:29:52 +02:00
}
2021-08-13 12:45:26 +02:00
if (isShared) {
subtitle = "${L10n.global().albumSharedLabel} | $subtitle";
2021-08-13 12:45:26 +02:00
}
2021-07-13 21:29:52 +02:00
return AlbumGridItem(
cover: _buildAlbumCover(context, album),
title: album.name,
subtitle: subtitle,
subtitle2: subtitle2,
2022-01-27 22:05:02 +01:00
icon: icon,
2021-07-13 21:29:52 +02:00
);
}
Widget _buildAlbumCover(BuildContext context, Album album) {
Widget cover;
try {
final coverFile = album.coverProvider.getCover(album);
2021-07-23 22:05:57 +02:00
final previewUrl = api_util.getFilePreviewUrl(account, coverFile!,
2021-09-16 12:25:08 +02:00
width: k.coverSize, height: k.coverSize);
2021-07-13 21:29:52 +02:00
cover = FittedBox(
clipBehavior: Clip.hardEdge,
fit: BoxFit.cover,
child: CachedNetworkImage(
2021-09-16 12:10:50 +02:00
cacheManager: CoverCacheManager.inst,
2021-07-13 21:29:52 +02:00
imageUrl: previewUrl,
httpHeaders: {
"Authorization": Api.getAuthorizationHeaderValue(account),
},
fadeInDuration: const Duration(),
filterQuality: FilterQuality.high,
errorWidget: (context, url, error) {
// just leave it empty
return Container();
},
imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
),
);
} catch (_) {
cover = Icon(
Icons.panorama,
color: Colors.white.withOpacity(.8),
size: 88,
);
}
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Container(
color: AppTheme.getListItemBackgroundColor(context),
constraints: const BoxConstraints.expand(),
child: cover,
),
);
}
final Account account;
final Album album;
2021-08-13 12:45:26 +02:00
final bool isShared;
2021-07-13 21:29:52 +02:00
}