2023-07-29 02:17:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-10-16 22:22:50 +00:00
|
|
|
import 'package:flutter/services.dart';
|
2023-10-20 23:12:33 +00:00
|
|
|
import 'package:freezer/main.dart';
|
2023-10-16 22:22:50 +00:00
|
|
|
import 'package:freezer/page_routes/fade.dart';
|
2023-07-29 02:17:26 +00:00
|
|
|
import 'package:palette_generator/palette_generator.dart';
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import 'package:photo_view/photo_view.dart';
|
|
|
|
import 'package:freezer/translations.i18n.dart';
|
|
|
|
|
|
|
|
ImagesDatabase imagesDatabase = ImagesDatabase();
|
|
|
|
|
|
|
|
class ImagesDatabase {
|
|
|
|
/*
|
|
|
|
!!! Using the wrappers so i don't have to rewrite most of the code, because of migration to cached network image
|
|
|
|
*/
|
|
|
|
|
|
|
|
void saveImage(String url) {
|
2023-10-20 23:12:33 +00:00
|
|
|
CachedNetworkImageProvider(url, cacheManager: cacheManager);
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<PaletteGenerator> getPaletteGenerator(String url) {
|
2023-10-20 23:12:33 +00:00
|
|
|
return PaletteGenerator.fromImageProvider(
|
|
|
|
CachedNetworkImageProvider(url, cacheManager: cacheManager));
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Color> getPrimaryColor(String url) async {
|
|
|
|
PaletteGenerator paletteGenerator = await getPaletteGenerator(url);
|
|
|
|
return paletteGenerator.dominantColor!.color;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> isDark(String url) async {
|
|
|
|
PaletteGenerator paletteGenerator = await getPaletteGenerator(url);
|
|
|
|
return paletteGenerator.colors.first.computeLuminance() > 0.5
|
|
|
|
? false
|
|
|
|
: true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CachedImage extends StatefulWidget {
|
2023-09-26 00:06:59 +00:00
|
|
|
final String url;
|
2023-07-29 02:17:26 +00:00
|
|
|
final double? width;
|
|
|
|
final double? height;
|
|
|
|
final bool circular;
|
|
|
|
final bool fullThumb;
|
|
|
|
final bool rounded;
|
|
|
|
|
|
|
|
const CachedImage(
|
|
|
|
{Key? key,
|
2023-09-26 00:06:59 +00:00
|
|
|
required this.url,
|
2023-07-29 02:17:26 +00:00
|
|
|
this.height,
|
|
|
|
this.width,
|
|
|
|
this.circular = false,
|
|
|
|
this.fullThumb = false,
|
|
|
|
this.rounded = false})
|
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
2023-10-12 22:09:37 +00:00
|
|
|
State<CachedImage> createState() => _CachedImageState();
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _CachedImageState extends State<CachedImage> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-10-12 22:09:37 +00:00
|
|
|
final Widget child;
|
|
|
|
if (!widget.url.startsWith('http')) {
|
|
|
|
child = Image.asset(
|
|
|
|
widget.url,
|
|
|
|
width: widget.width,
|
|
|
|
height: widget.height,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
child = CachedNetworkImage(
|
|
|
|
fit: BoxFit.scaleDown,
|
|
|
|
imageUrl: widget.url,
|
|
|
|
width: widget.width,
|
|
|
|
height: widget.height,
|
2023-10-20 23:12:33 +00:00
|
|
|
cacheManager: cacheManager,
|
2023-10-12 22:09:37 +00:00
|
|
|
placeholder: (context, url) {
|
|
|
|
if (widget.fullThumb) {
|
|
|
|
return Image.asset(
|
|
|
|
'assets/cover.jpg',
|
|
|
|
width: widget.width,
|
|
|
|
height: widget.height,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Image.asset('assets/cover_thumb.jpg',
|
|
|
|
width: widget.width, height: widget.height);
|
|
|
|
},
|
|
|
|
errorWidget: (context, url, error) => Image.asset('assets/cover.jpg',
|
|
|
|
width: widget.width, height: widget.height),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widget.rounded) {
|
2023-07-29 02:17:26 +00:00
|
|
|
return ClipRRect(
|
2023-10-12 22:09:37 +00:00
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(4.0)),
|
|
|
|
child: child,
|
2023-07-29 02:17:26 +00:00
|
|
|
);
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
if (widget.circular) {
|
2023-07-29 02:17:26 +00:00
|
|
|
return ClipOval(
|
2023-10-12 22:09:37 +00:00
|
|
|
child: child,
|
2023-07-29 02:17:26 +00:00
|
|
|
);
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
return child;
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
class ZoomableImage extends StatelessWidget {
|
2023-09-26 00:06:59 +00:00
|
|
|
final String url;
|
2023-07-29 02:17:26 +00:00
|
|
|
final bool rounded;
|
|
|
|
final double? width;
|
|
|
|
final bool enableHero;
|
|
|
|
final Object? heroTag;
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
ZoomableImage({
|
2023-10-12 22:09:37 +00:00
|
|
|
super.key,
|
2023-07-29 02:17:26 +00:00
|
|
|
required this.url,
|
|
|
|
this.rounded = false,
|
|
|
|
this.width,
|
|
|
|
this.enableHero = true,
|
|
|
|
this.heroTag,
|
|
|
|
});
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
late final Object? _key = enableHero ? (heroTag ?? UniqueKey()) : null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
print('key: $_key');
|
|
|
|
final image = CachedImage(
|
|
|
|
url: url,
|
|
|
|
rounded: rounded,
|
|
|
|
width: width,
|
|
|
|
fullThumb: true,
|
|
|
|
);
|
|
|
|
final child = _key != null
|
|
|
|
? Hero(
|
|
|
|
tag: _key!,
|
|
|
|
child: image,
|
|
|
|
)
|
|
|
|
: image;
|
|
|
|
return GestureDetector(
|
|
|
|
child: Semantics(
|
|
|
|
label: "Album art".i18n,
|
|
|
|
child: child,
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(FadePageRoute(
|
|
|
|
builder: (context) =>
|
|
|
|
ZoomableImageRoute(imageUrl: url, heroKey: _key),
|
2023-10-17 14:03:39 +00:00
|
|
|
barrierDismissible: true,
|
|
|
|
opaque: false));
|
2023-10-16 22:22:50 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ZoomableImageRoute extends StatefulWidget {
|
|
|
|
final Object? heroKey;
|
|
|
|
final String imageUrl;
|
|
|
|
const ZoomableImageRoute({required this.imageUrl, super.key, this.heroKey});
|
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
@override
|
2023-10-16 22:22:50 +00:00
|
|
|
State<ZoomableImageRoute> createState() => _ZoomableImageRouteState();
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
class _ZoomableImageRouteState extends State<ZoomableImageRoute> {
|
2023-10-17 14:03:39 +00:00
|
|
|
bool photoViewOpened = true;
|
2023-10-16 22:22:50 +00:00
|
|
|
final controller = PhotoViewController();
|
2023-10-16 22:57:55 +00:00
|
|
|
final _focusNode = FocusScopeNode();
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2023-10-16 22:22:50 +00:00
|
|
|
controller.outputStateStream.listen(listener);
|
|
|
|
_focusNode.requestFocus();
|
2023-07-29 02:17:26 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
controller.dispose();
|
|
|
|
_focusNode.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
void listener(PhotoViewControllerValue value) {
|
|
|
|
if (value.scale! < 0.16 && photoViewOpened) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
photoViewOpened =
|
|
|
|
false; // to avoid multiple pop() when picture are being scaled out too slowly
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-10-16 22:22:50 +00:00
|
|
|
return RawKeyboardListener(
|
|
|
|
focusNode: _focusNode,
|
|
|
|
onKey: (event) {
|
|
|
|
if (event is! KeyUpEvent) return;
|
|
|
|
|
|
|
|
if (event.isKeyPressed(LogicalKeyboardKey.escape)) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
},
|
2023-10-16 22:22:50 +00:00
|
|
|
child: PhotoView(
|
2023-10-20 23:12:33 +00:00
|
|
|
imageProvider: CachedNetworkImageProvider(widget.imageUrl,
|
|
|
|
cacheManager: cacheManager),
|
2023-10-16 22:22:50 +00:00
|
|
|
maxScale: 8.0,
|
|
|
|
minScale: 0.2,
|
|
|
|
controller: controller,
|
|
|
|
heroAttributes: widget.heroKey == null
|
|
|
|
? null
|
|
|
|
: PhotoViewHeroAttributes(tag: widget.heroKey!),
|
|
|
|
backgroundDecoration:
|
2023-10-16 22:57:55 +00:00
|
|
|
const BoxDecoration(color: Color.fromARGB(85, 0, 0, 0))),
|
2023-07-29 02:17:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|