Skip to content

Commit

Permalink
feat: ask user to update refs index
Browse files Browse the repository at this point in the history
feat: aption to use a local .zip file on android/ios
  • Loading branch information
Sivan22 committed Nov 17, 2024
1 parent 1c6a927 commit e2a18a9
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 38 deletions.
4 changes: 4 additions & 0 deletions lib/data/repository/data_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class DataRepository {
return _isarDataProvider.findRefsByRelevance(ref, limit: limit);
}

Future<int> getNumberOfBooksWithRefs() {
return _isarDataProvider.getNumberOfBooksWithRefs();
}

addAllTextsToMimir(Library library, {int start = 0, int end = 100000}) async {
_mimirDataProvider.addAllTBooksToTantivy(library, start: start, end: end);
}
Expand Down
85 changes: 84 additions & 1 deletion lib/screens/empty_library_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:http/http.dart' as http;
import 'package:archive/archive_io.dart';
import 'package:flutter_archive/flutter_archive.dart' as flutter_archive;
import 'package:file_picker/file_picker.dart';
import 'package:flutter_document_picker/flutter_document_picker.dart';

class EmptyLibraryScreen extends StatefulWidget {
final VoidCallback onLibraryLoaded;
Expand Down Expand Up @@ -76,6 +77,83 @@ class _EmptyLibraryScreenState extends State<EmptyLibraryScreen> {
}
}

Future<void> _pickAndExtractZipFile() async {
final libraryPath = Settings.getValue<String>('key-library-path') ?? '';
if (libraryPath.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('נא לבחור תיקייה תחילה')),
);
return;
}

try {
setState(() {
_isDownloading = true;
_currentOperation = 'פותח קובץ...';
_downloadProgress = 0;
});

final path = await FlutterDocumentPicker.openDocument(
params: FlutterDocumentPickerParams(
allowedFileExtensions: ['zip'],
invalidFileNameSymbols: ['/'],
),
);

if (path == null) {
setState(() {
_isDownloading = false;
_currentOperation = '';
});
return;
}

final zipFile = File(path);

try {
await flutter_archive.ZipFile.extractToDirectory(
zipFile: zipFile,
destinationDir: Directory(libraryPath),
onExtracting: (zipEntry, progress) {
setState(() {
_downloadProgress = progress;
_currentOperation = 'מחלץ: ${zipEntry.name}';
});
return flutter_archive.ZipFileOperation.includeItem;
},
);

if (mounted) {
widget.onLibraryLoaded();
setState(() {
_isDownloading = false;
_currentOperation = '';
});
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('שגיאה בחילוץ: $e')),
);
setState(() {
_isDownloading = false;
_currentOperation = '';
});
}
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('שגיאה בבחירת הקובץ: $e')),
);
setState(() {
_isDownloading = false;
_currentOperation = '';
});
}
}
}

Future<void> _downloadAndExtractLibrary() async {
if (_isDownloading) return;

Expand Down Expand Up @@ -340,7 +418,12 @@ class _EmptyLibraryScreenState extends State<EmptyLibraryScreen> {
child: const Text('בחר תיקייה'),
),
const SizedBox(height: 32),
if (!Platform.isAndroid || Platform.isIOS)
if (Platform.isAndroid)
ElevatedButton(
onPressed: _isDownloading ? null : _pickAndExtractZipFile,
child: const Text('בחר קובץ ZIP מהמכשיר'),
),
if (!Platform.isAndroid && !Platform.isIOS)
const Text(
'או',
style: TextStyle(fontSize: 18),
Expand Down
152 changes: 117 additions & 35 deletions lib/screens/find_ref_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,91 @@ class _FindRefScreenState extends State<FindRefScreen>
bool get wantKeepAlive => true;
final TextEditingController _searchController = TextEditingController();
late Future<List<Ref>> _refs;
bool _needsIndexing = false;
late AppModel appModel;

@override
void initState() {
super.initState();
appModel = Provider.of<AppModel>(context, listen: false);
_refs = findRefs(_searchController.text);
_checkIndexStatus();
}

Future<void> _checkIndexStatus() async {
final booksWithRefs =
await DataRepository.instance.getNumberOfBooksWithRefs();
final totalBooks = (await appModel.library).getAllBooks().length;

// If there's a difference of more than 5 books or if there are no refs at all
if (booksWithRefs == 0 || (totalBooks - booksWithRefs) > 5) {
setState(() {
_needsIndexing = true;
});
}
}

Future<List<Ref>> findRefs(String ref) async {
if (ref.length < 3) {
return [];
}
//ref = paraphrase(ref);
return DataRepository.instance.findRefsByRelevance(
ref,
);
}

Widget _buildIndexingMessage() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'האינדקס ריק או לא מעודכן. יש לעדכן את האינדקס כדי לחפש מקורות.',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
final result = await showDialog(
context: context,
builder: (context) => AlertDialog(
content: const Text(
'האם ברצונך ליצור אינדקס מקורות? הדבר יאפס את האינדקס הקיים ועלול לקחת זמן ארוך מאד.',
),
actions: <Widget>[
ElevatedButton(
child: const Text('ביטול'),
onPressed: () {
Navigator.pop(context, false);
},
),
ElevatedButton(
child: const Text('אישור'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
),
);
if (result == true) {
appModel.createRefsFromLibrary(0);
setState(() {
_needsIndexing = false;
});
}
},
child: const Text('יצירת אינדקס מקורות'),
),
],
),
);
}

@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
drawer: const Drawer(
shape: RoundedRectangleBorder(
Expand Down Expand Up @@ -80,41 +146,57 @@ class _FindRefScreenState extends State<FindRefScreen>
},
),
Expanded(
child: FutureBuilder<List<Ref>>(
future: _refs,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(snapshot.data![index].ref),
onTap: () {
final appModel =
Provider.of<AppModel>(context, listen: false);
if (snapshot.data![index].pdfBook) {
appModel.openBook(
PdfBook(
title: snapshot.data![index].bookTitle,
path: snapshot.data![index].pdfPath!),
snapshot.data![index].index);
} else {
appModel.openBook(
TextBook(
title: snapshot.data![index].bookTitle,
),
snapshot.data![index].index);
}
});
child: _needsIndexing
? _buildIndexingMessage()
: FutureBuilder<List<Ref>>(
future: _refs,
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else if (snapshot.data!.isEmpty &&
_searchController.text.length >= 3) {
return const Center(
child: Text(
'אין תוצאות',
style: TextStyle(fontSize: 16),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(snapshot.data![index].ref),
onTap: () {
final appModel = Provider.of<AppModel>(
context,
listen: false);
if (snapshot.data![index].pdfBook) {
appModel.openBook(
PdfBook(
title: snapshot
.data![index].bookTitle,
path: snapshot
.data![index].pdfPath!),
snapshot.data![index].index);
} else {
appModel.openBook(
TextBook(
title:
snapshot.data![index].bookTitle,
),
snapshot.data![index].index);
}
});
},
);
}
},
);
}
},
),
),
),
],
),
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.1.3"
flutter_document_picker:
dependency: "direct main"
description:
name: flutter_document_picker
sha256: "5229e22fb9ac7939c516b56736714bb92305762cf4027f34aaf9edda229c5004"
url: "https://pub.dev"
source: hosted
version: "5.2.3"
flutter_html:
dependency: "direct main"
description:
Expand Down
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msix_config:
display_name: אוצריא
publisher_display_name: sivan22
identity_name: sivan22.Otzaria
msix_version: 0.2.4.0
msix_version: 0.2.3.2
logo_path: assets/icon/icon.png
publisher: CN=sivan22, O=sivan22, C=IL
certificate_path: sivan22.pfx
Expand All @@ -33,7 +33,7 @@ msix_config:
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 0.2.4
version: 0.2.3+2

environment:
sdk: ">=3.2.6 <4.0.0"
Expand Down Expand Up @@ -81,6 +81,7 @@ dependencies:
package_info_plus: ^8.0.2
crypto: ^3.0.5
http: ^1.2.2
flutter_document_picker: ^5.2.3
search_engine:
#path: ../search_engine
git:
Expand Down

0 comments on commit e2a18a9

Please sign in to comment.