2023-12-29 21:39:54 +01:00
|
|
|
// ignore_for_file: inference_failure_on_function_invocation
|
|
|
|
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:flutter_tesseract_ocr/flutter_tesseract_ocr.dart';
|
|
|
|
import 'package:prasule/main.dart';
|
|
|
|
import 'package:prasule/network/tessdata.dart';
|
|
|
|
import 'package:prasule/pw/platformbutton.dart';
|
|
|
|
import 'package:prasule/pw/platformdialog.dart';
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Used to manage downloaded Tessdata for OCR
|
2023-09-08 11:50:21 +02:00
|
|
|
class TessdataListView extends StatefulWidget {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Used to manage downloaded Tessdata for OCR
|
2023-09-08 11:50:21 +02:00
|
|
|
const TessdataListView({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<TessdataListView> createState() => _TessdataListViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _TessdataListViewState extends State<TessdataListView> {
|
|
|
|
final _tessdata = [
|
2023-12-29 21:39:54 +01:00
|
|
|
{"eng": true},
|
2023-09-08 11:50:21 +02:00
|
|
|
];
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
loadAllTessdata();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2023-12-25 19:03:52 +01:00
|
|
|
appBar: AppBar(title: Text(AppLocalizations.of(context).ocrData)),
|
2023-09-08 11:50:21 +02:00
|
|
|
body: Center(
|
|
|
|
child: SizedBox(
|
|
|
|
width: MediaQuery.of(context).size.width * 0.9,
|
|
|
|
height: MediaQuery.of(context).size.height,
|
|
|
|
child: (_tessdata.length == 1)
|
|
|
|
? const Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
width: 80,
|
|
|
|
height: 80,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
: ListView.builder(
|
|
|
|
itemBuilder: (context, i) => ListTile(
|
|
|
|
title: Text(_tessdata[i].keys.first),
|
|
|
|
trailing: TextButton(
|
2023-12-29 21:39:54 +01:00
|
|
|
child: Text(
|
|
|
|
_tessdata[i][_tessdata[i].keys.first]!
|
|
|
|
? AppLocalizations.of(context).downloaded
|
|
|
|
: AppLocalizations.of(context).download,
|
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
onPressed: () async {
|
2023-12-29 21:39:54 +01:00
|
|
|
final lang = _tessdata[i].keys.first;
|
2023-09-08 11:50:21 +02:00
|
|
|
if (_tessdata[i][lang]!) {
|
|
|
|
// deleting data
|
2023-12-29 21:39:54 +01:00
|
|
|
await showDialog(
|
2023-09-08 11:50:21 +02:00
|
|
|
context: context,
|
|
|
|
builder: (context) => PlatformDialog(
|
2023-12-25 19:03:52 +01:00
|
|
|
title: AppLocalizations.of(context).sureDialog,
|
2023-12-29 21:39:54 +01:00
|
|
|
content: Text(
|
|
|
|
AppLocalizations.of(context).deleteOcr(lang),
|
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
actions: [
|
|
|
|
PlatformButton(
|
2023-12-25 19:03:52 +01:00
|
|
|
text: AppLocalizations.of(context).yes,
|
2023-09-08 11:50:21 +02:00
|
|
|
onPressed: () async {
|
|
|
|
await TessdataApi.deleteData(lang);
|
|
|
|
_tessdata[i][lang] = true;
|
2024-01-22 14:38:01 +01:00
|
|
|
if (context.mounted) {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
2023-09-08 11:50:21 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
PlatformButton(
|
2023-12-25 19:03:52 +01:00
|
|
|
text: AppLocalizations.of(context).no,
|
2023-09-08 11:50:21 +02:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
setState(() {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle wifi errors
|
|
|
|
//* downloading traineddata
|
2023-12-29 21:39:54 +01:00
|
|
|
final progressStream = StreamController<double>();
|
2023-09-08 11:50:21 +02:00
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
unawaited(
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (c) => PlatformDialog(
|
|
|
|
title: AppLocalizations.of(context)
|
|
|
|
.langDownloadDialog(lang),
|
|
|
|
content: StreamBuilder(
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState ==
|
|
|
|
ConnectionState.waiting) {
|
|
|
|
return const CircularProgressIndicator();
|
|
|
|
}
|
|
|
|
if (snapshot.hasError) {
|
|
|
|
return const Text("Error");
|
|
|
|
}
|
|
|
|
return Text(
|
|
|
|
AppLocalizations.of(context)
|
|
|
|
.langDownloadProgress(snapshot.data!),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
stream: progressStream.stream,
|
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2023-12-29 21:39:54 +01:00
|
|
|
await TessdataApi.downloadData(
|
|
|
|
lang,
|
|
|
|
callback: (a, b) {
|
|
|
|
if (progressStream.isClosed) return;
|
|
|
|
final p = a / b * 1000;
|
|
|
|
progressStream.add(p.roundToDouble() / 10);
|
|
|
|
if (p / 10 >= 100) {
|
|
|
|
logger.i("Done");
|
|
|
|
Navigator.of(context, rootNavigator: true)
|
|
|
|
.pop("dialog");
|
|
|
|
_tessdata[i][lang] = true;
|
|
|
|
progressStream.close();
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
);
|
2023-09-08 11:50:21 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
itemCount: _tessdata.length,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used to find which `.traineddata` is already downloaded and which not
|
|
|
|
/// so we can show it to the user
|
2023-12-29 21:39:54 +01:00
|
|
|
Future<void> loadAllTessdata() async {
|
|
|
|
final tessDir = Directory(await FlutterTesseractOcr.getTessdataPath());
|
|
|
|
final d = await TessdataApi.getAvailableData();
|
|
|
|
final dataStatus = <Map<String, bool>>[];
|
|
|
|
for (final data in d) {
|
|
|
|
final e = <String, bool>{};
|
2023-09-08 11:50:21 +02:00
|
|
|
e[data] = false;
|
|
|
|
dataStatus.add(e);
|
|
|
|
}
|
2023-12-29 21:39:54 +01:00
|
|
|
final appDir = tessDir.listSync();
|
|
|
|
for (final file in appDir) {
|
2023-09-08 11:50:21 +02:00
|
|
|
if (file is! File ||
|
|
|
|
!file.path.endsWith("traineddata") ||
|
|
|
|
file.path.endsWith("eng.traineddata")) continue;
|
|
|
|
logger.i(file.path);
|
2023-12-29 21:39:54 +01:00
|
|
|
final filename = file.path.split("/").last;
|
|
|
|
dataStatus[dataStatus.indexWhere(
|
|
|
|
(element) =>
|
|
|
|
element.keys.first == filename.replaceAll(".traineddata", ""),
|
|
|
|
)][filename.replaceAll(".traineddata", "")] = true;
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|
|
|
|
_tessdata.addAll(dataStatus);
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
}
|