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

91 lines
2.8 KiB
Dart
Raw Normal View History

2021-07-13 21:29:52 +02:00
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.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-07-13 21:29:52 +02:00
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/provider.dart';
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;
2021-07-13 21:29:52 +02:00
if (album.provider is AlbumStaticProvider) {
2021-07-25 07:00:38 +02:00
subtitle = L10n.of(context)
2021-07-13 21:29:52 +02:00
.albumSize(AlbumStaticProvider.of(album).items.length);
} 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}";
}
}
2021-08-13 12:45:26 +02:00
if (isShared) {
subtitle = "${L10n.of(context).albumSharedLabel} | $subtitle";
}
2021-07-13 21:29:52 +02:00
return AlbumGridItem(
cover: _buildAlbumCover(context, album),
title: album.name,
subtitle: subtitle,
subtitle2: subtitle2,
icon: album.provider is AlbumDirProvider ? Icons.folder : null,
);
}
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-07-13 21:29:52 +02:00
width: 512, height: 512);
cover = FittedBox(
clipBehavior: Clip.hardEdge,
fit: BoxFit.cover,
child: CachedNetworkImage(
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
}