fix unloading of tiles causing high internet usage
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart' as nativeffi;
|
||||
import 'package:sqflite_common_ffi_web/sqflite_ffi_web.dart' as webffi;
|
||||
|
||||
import '../log/log.dart';
|
||||
|
||||
@ -9,19 +10,19 @@ class Db {
|
||||
late Database _db;
|
||||
|
||||
Future<void> init() async {
|
||||
if (kIsWeb) {
|
||||
Log.i("Database on web is not supported");
|
||||
return;
|
||||
}
|
||||
String dbpath = 'previews.db';
|
||||
if (defaultTargetPlatform == TargetPlatform.android ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS) {
|
||||
dbpath = join(await getDatabasesPath(), dbpath);
|
||||
} else if(kIsWeb) {
|
||||
databaseFactory = webffi.databaseFactoryFfiWeb;
|
||||
} else {
|
||||
|
||||
|
||||
// Initialize FFI
|
||||
sqfliteFfiInit();
|
||||
nativeffi.sqfliteFfiInit();
|
||||
// Change the default factory
|
||||
databaseFactory = databaseFactoryFfi;
|
||||
databaseFactory = nativeffi.databaseFactoryFfi;
|
||||
}
|
||||
|
||||
_db = await openDatabase(
|
||||
|
@ -1,3 +1,4 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:media_kit/media_kit.dart';
|
||||
@ -13,9 +14,13 @@ void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
MediaKit.ensureInitialized();
|
||||
|
||||
if (!isDesktop()) {
|
||||
if (!kIsWeb && !isDesktop()) {
|
||||
Log.i("init device info");
|
||||
await loadDeviceInfo();
|
||||
}
|
||||
|
||||
Log.i("Mediakit initialized");
|
||||
|
||||
await Db().init();
|
||||
|
||||
runApp(Shortcuts(shortcuts: <LogicalKeySet, Intent>{
|
||||
|
@ -76,7 +76,8 @@ class _PreviewTileState extends State<PreviewTile> {
|
||||
);
|
||||
|
||||
// precache image to avoid loading time to render image
|
||||
await precacheImage(img.image, context);
|
||||
if(context.mounted)
|
||||
await precacheImage(img.image, context);
|
||||
|
||||
return img;
|
||||
}
|
||||
@ -103,28 +104,28 @@ class _PreviewTileState extends State<PreviewTile> {
|
||||
child: Stack(
|
||||
children: [
|
||||
Container(
|
||||
color: const Color(0xff3f3f3f),
|
||||
child: Column(
|
||||
children: [
|
||||
ClipRRect(
|
||||
child: image,
|
||||
borderRadius: BorderRadius.circular(20.0),
|
||||
child: image,
|
||||
),
|
||||
SizedBox(
|
||||
const SizedBox(
|
||||
height: 3,
|
||||
),
|
||||
Text(
|
||||
widget.dta.title,
|
||||
style: TextStyle(
|
||||
fontSize: isTV() ? 8 : 10.5, color: Color(0xffe9e9e9)),
|
||||
fontSize: isTV() ? 8 : 10.5, color: const Color(0xffe9e9e9)),
|
||||
overflow: TextOverflow.clip,
|
||||
maxLines: 1,
|
||||
),
|
||||
SizedBox(
|
||||
const SizedBox(
|
||||
height: 3,
|
||||
),
|
||||
],
|
||||
),
|
||||
color: Color(0xff3f3f3f),
|
||||
),
|
||||
Positioned.fill(
|
||||
child: Material(
|
||||
@ -157,21 +158,24 @@ class _PreviewTileState extends State<PreviewTile> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<Image>(
|
||||
future: _preview, // a previously-obtained Future<String> or null
|
||||
builder: (BuildContext context, AsyncSnapshot<Image> snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return _buildLoader();
|
||||
}
|
||||
return ConstrainedBox(
|
||||
constraints: const BoxConstraints(minHeight: 200, minWidth: 200),
|
||||
child: FutureBuilder<Image>(
|
||||
future: _preview, // a previously-obtained Future<String> or null
|
||||
builder: (BuildContext context, AsyncSnapshot<Image> snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return _buildLoader();
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
return Text("Error");
|
||||
} else if (snapshot.hasData) {
|
||||
return _buildTile(snapshot.data!);
|
||||
} else {
|
||||
return _buildLoader();
|
||||
}
|
||||
},
|
||||
if (snapshot.hasError) {
|
||||
return Text("Error");
|
||||
} else if (snapshot.hasData) {
|
||||
return _buildTile(snapshot.data!);
|
||||
} else {
|
||||
return _buildLoader();
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user