import 'dart:convert'; import 'package:filesize/filesize.dart'; import 'package:flutter/material.dart'; import 'package:jiffy/jiffy.dart'; import '../../../widget/placeholder_view.dart'; import '../../api/cache_store.dart'; import '../app_progress_indicator.dart'; import 'json_viewer.dart'; class CacheView extends StatefulWidget { const CacheView({super.key}); @override State createState() => _CacheViewState(); } class _CacheViewState extends State { late final Future> files = CacheStore.instance .readAll(); @override Widget build(BuildContext context) => Scaffold( appBar: AppBar(title: const Text('Cache storage')), body: FutureBuilder( future: files, builder: (context, snapshot) { if (snapshot.hasData && snapshot.data!.isNotEmpty) { return ListView.builder( itemCount: snapshot.data!.length, itemBuilder: (context, index) { final key = snapshot.data!.keys.elementAt(index); final element = snapshot.data![key]!; return ListTile( leading: const Icon(Icons.text_snippet_outlined), title: Text(key), subtitle: Text( '${filesize(utf8.encode(element.json).length)}, ${Jiffy.parseFromMillisecondsSinceEpoch(element.lastUpdate).fromNow()}', ), trailing: const Icon(Icons.arrow_right), onTap: () => JsonViewer.asDialog( context, jsonDecode(element.json) as Map, ), ); }, ); } else if (snapshot.connectionState != ConnectionState.done) { return const Center(child: AppProgressIndicator.large()); } else { return const Center( child: PlaceholderView( icon: Icons.hourglass_empty, text: 'Keine Daten', ), ); } }, ), ); }