85 lines
2.7 KiB
Dart
85 lines
2.7 KiB
Dart
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'package:filesize/filesize.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:jiffy/jiffy.dart';
|
|
import 'package:localstore/localstore.dart';
|
|
|
|
import '../../../widget/placeholderView.dart';
|
|
import '../../api/requestCache.dart';
|
|
import 'jsonViewer.dart';
|
|
|
|
class CacheView extends StatefulWidget {
|
|
const CacheView({super.key});
|
|
|
|
@override
|
|
State<CacheView> createState() => _CacheViewState();
|
|
|
|
Future<void> clear() async {
|
|
await Localstore.instance.collection(RequestCache.collection).delete();
|
|
}
|
|
|
|
Future<int> totalSize() async {
|
|
var data = await Localstore.instance.collection(RequestCache.collection).get();
|
|
if(data!.length <= 1) return jsonEncode(data.values.first).length * 8;
|
|
return data.values.reduce((a, b) => jsonEncode(a).length + jsonEncode(b).length) * 8;
|
|
}
|
|
}
|
|
|
|
class _CacheViewState extends State<CacheView> {
|
|
final Localstore storage = Localstore.instance;
|
|
late Future<Map<String, dynamic>?> files;
|
|
|
|
@override
|
|
void initState() {
|
|
files = Localstore.instance.collection(RequestCache.collection).get();
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Cache storage'),
|
|
),
|
|
body: FutureBuilder(
|
|
future: files,
|
|
builder: (context, snapshot) {
|
|
if(snapshot.hasData) {
|
|
return ListView.builder(
|
|
itemCount: snapshot.data!.length,
|
|
itemBuilder: (context, index) {
|
|
Map<String, dynamic> element = snapshot.data![snapshot.data!.keys.elementAt(index)];
|
|
var filename = snapshot.data!.keys.elementAt(index).split('/').last;
|
|
|
|
return ListTile(
|
|
leading: const Icon(Icons.text_snippet_outlined),
|
|
title: Text(filename),
|
|
subtitle: Text("${filesize(jsonEncode(element).length * 8)}, ${Jiffy.parseFromMillisecondsSinceEpoch(element['lastupdate']).fromNow()}"),
|
|
trailing: const Icon(Icons.arrow_right),
|
|
onTap: () => JsonViewer.asDialog(context, jsonDecode(element['json'])),
|
|
);
|
|
},
|
|
);
|
|
} else if(snapshot.connectionState != ConnectionState.done) {
|
|
return const Center(
|
|
child: CircularProgressIndicator()
|
|
);
|
|
} else {
|
|
return const Center(
|
|
child: PlaceholderView(icon: Icons.hourglass_empty, text: 'Keine Daten'),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
extension FutureExtension<T> on Future<T> {
|
|
bool isCompleted() {
|
|
final completer = Completer<T>();
|
|
then(completer.complete).catchError(completer.completeError);
|
|
return completer.isCompleted;
|
|
}
|
|
}
|