improved performance and battery usage on older devices
This commit is contained in:
@@ -2,10 +2,9 @@ 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/placeholder_view.dart';
|
||||
import '../../api/request_cache.dart';
|
||||
import '../../api/cache_store.dart';
|
||||
import '../app_progress_indicator.dart';
|
||||
import 'json_viewer.dart';
|
||||
|
||||
@@ -15,31 +14,11 @@ class CacheView extends StatefulWidget {
|
||||
@override
|
||||
State<CacheView> createState() => _CacheViewState();
|
||||
|
||||
Future<void> clear() async {
|
||||
await Localstore.instance.collection(RequestCache.collection).delete();
|
||||
}
|
||||
|
||||
Future<int> totalSize() async {
|
||||
final data = await Localstore.instance
|
||||
.collection(RequestCache.collection)
|
||||
.get();
|
||||
if (data == null || data.isEmpty) return 0;
|
||||
return data.values.fold<int>(
|
||||
0,
|
||||
(sum, value) => sum + jsonEncode(value).length,
|
||||
) *
|
||||
8;
|
||||
}
|
||||
}
|
||||
|
||||
class _CacheViewState extends State<CacheView> {
|
||||
late Future<Map<String, dynamic>?> files;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
files = Localstore.instance.collection(RequestCache.collection).get();
|
||||
super.initState();
|
||||
}
|
||||
late final Future<Map<String, CacheEntry>> files = CacheStore.instance
|
||||
.readAll();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Scaffold(
|
||||
@@ -47,24 +26,23 @@ class _CacheViewState extends State<CacheView> {
|
||||
body: FutureBuilder(
|
||||
future: files,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
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] as Map<String, dynamic>;
|
||||
final filename = key.split('/').last;
|
||||
final element = snapshot.data![key]!;
|
||||
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.text_snippet_outlined),
|
||||
title: Text(filename),
|
||||
title: Text(key),
|
||||
subtitle: Text(
|
||||
'${filesize(jsonEncode(element).length * 8)}, ${Jiffy.parseFromMillisecondsSinceEpoch(element['lastupdate'] as int).fromNow()}',
|
||||
'${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 String) as Map<String, dynamic>,
|
||||
jsonDecode(element.json) as Map<String, dynamic>,
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user