From f69add159cad513fc6ff62bb1bdb0bc05e6d1ee7 Mon Sep 17 00:00:00 2001 From: CatHood0 Date: Wed, 21 Aug 2024 07:44:44 -0400 Subject: [PATCH] Feat: update example of spell checker to a better API implementation --- README.md | 18 ++++++----------- example/lib/screens/quill/quill_screen.dart | 20 +++++++++---------- .../simple_spell_checker_service.dart | 9 ++++++--- .../default_spellchecker_service.dart | 3 ++- .../spellchecker/spellchecker_service.dart | 3 ++- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 27639a50..a8ecffa1 100644 --- a/README.md +++ b/README.md @@ -308,7 +308,7 @@ A spell checker is a software tool or feature integrated into various text proce * **Portuguese** - `pt` (may contain errors or missing words) * **Swedish** - `sv` (may contain errors or missing words) -_**Note**: **If you have knowledge about any of these available languages or the unsupported ones**, you can make a pull request to add support or add words that are not currently in [simple_spell_checker](https://github.com/CatHood0/simple_spell_checker)_. +_**Note**: If you have knowledge about any of these available languages or the unsupported ones, you can make a pull request to add support or add words that are not currently in [simple_spell_checker](https://github.com/CatHood0/simple_spell_checker)_. In order to activate this functionality you can use the following code: @@ -324,7 +324,8 @@ When you no longer need to have the Spell checker activated you can simply use ` // dispose all service and it cannot be used after this SpellCheckerServiceProvider.dispose(); ``` -If what we want is to **temporarily deactivate the service** without deleting the values that are already stored in it, we can set `onlyPartial` to `true` so that it only closes the internal `streams` and prevents the dictionaries and values already registered from being reset. + +If what we want is to **close the StreamControllers** without deleting the values that are already stored in it, we can set `onlyPartial` to `true`. ```dart // it can be still used by the editor @@ -334,16 +335,9 @@ SpellCheckerServiceProvider.dispose(onlyPartial: true); One use of this would be having the opportunity to **activate and deactivate** the service when we want, we can see this in the example that we have in this package, in which you can see that on each screen, we have a button that dynamically activates and deactivates the service. To do this is pretty simple: ```dart -final bool _isActivatedSpellChecker = false; -if (!_isActivatedSpellChecker) { - FlutterQuillExtensions.useSpellCheckerService(Localizations.localeOf(context).languageCode); -} else { - // close the internal streams without completely closing the service - SpellCheckerServiceProvider.dispose(onlyPartial: true); - // since onlyPartial is true then we must manually disable the service for it to take effect in the UI - SpellCheckerServiceProvider.turnOffService(); -} -_isActivatedSpellChecker = !_isActivatedSpellChecker; + SpellCheckerServiceProvider.toggleState(); + isActivatedSpellChecker = SpellCheckerServiceProvider.isServiceActive(); + setState(() {}); ``` Open this [page](https://pub.dev/packages/simple_spell_checker) for more information. diff --git a/example/lib/screens/quill/quill_screen.dart b/example/lib/screens/quill/quill_screen.dart index d4d00e97..db59e539 100644 --- a/example/lib/screens/quill/quill_screen.dart +++ b/example/lib/screens/quill/quill_screen.dart @@ -14,7 +14,7 @@ import '../shared/widgets/home_screen_button.dart'; import 'my_quill_editor.dart'; import 'my_quill_toolbar.dart'; -var _isActivatedSpellChecker = false; +var _isSpellcheckerActive = false; @immutable class QuillScreenArgs { @@ -61,6 +61,11 @@ class _QuillScreenState extends State { @override Widget build(BuildContext context) { _controller.readOnly = _isReadOnly; + if (!_isSpellcheckerActive) { + _isSpellcheckerActive = true; + FlutterQuillExtensions.useSpellCheckerService( + Localizations.localeOf(context).languageCode); + } return Scaffold( appBar: AppBar( title: const Text('Flutter Quill'), @@ -68,19 +73,14 @@ class _QuillScreenState extends State { IconButton( tooltip: 'Spell-checker', onPressed: () { - if (!_isActivatedSpellChecker) { - FlutterQuillExtensions.useSpellCheckerService( - Localizations.localeOf(context).languageCode); - } else { - SpellCheckerServiceProvider.dispose(onlyPartial: true); - SpellCheckerServiceProvider.turnOffService(); - } - _isActivatedSpellChecker = !_isActivatedSpellChecker; + SpellCheckerServiceProvider.toggleState(); setState(() {}); }, icon: Icon( Icons.document_scanner, - color: _isActivatedSpellChecker ? Colors.red : null, + color: SpellCheckerServiceProvider.isServiceActive() + ? Colors.red.withOpacity(0.5) + : null, ), ), IconButton( diff --git a/flutter_quill_extensions/lib/src/editor/spell_checker/simple_spell_checker_service.dart b/flutter_quill_extensions/lib/src/editor/spell_checker/simple_spell_checker_service.dart index 8faed72b..c804a8d1 100644 --- a/flutter_quill_extensions/lib/src/editor/spell_checker/simple_spell_checker_service.dart +++ b/flutter_quill_extensions/lib/src/editor/spell_checker/simple_spell_checker_service.dart @@ -5,7 +5,8 @@ import 'package:simple_spell_checker/simple_spell_checker.dart'; /// SimpleSpellChecker is a simple spell checker for get /// all words divide on different objects if them are wrong or not -class SimpleSpellCheckerService extends SpellCheckerService { +class SimpleSpellCheckerService + extends SpellCheckerService { SimpleSpellCheckerService({required super.language}) : checker = SimpleSpellChecker( language: language, @@ -20,11 +21,13 @@ class SimpleSpellCheckerService extends SpellCheckerService @override List? checkSpelling( String text, { - LongPressGestureRecognizer Function(String word)? customLongPressRecognizerOnWrongSpan, + LongPressGestureRecognizer Function(String word)? + customLongPressRecognizerOnWrongSpan, }) { return checker.check( text, - customLongPressRecognizerOnWrongSpan: customLongPressRecognizerOnWrongSpan, + customLongPressRecognizerOnWrongSpan: + customLongPressRecognizerOnWrongSpan, ); } diff --git a/lib/src/editor/spellchecker/default_spellchecker_service.dart b/lib/src/editor/spellchecker/default_spellchecker_service.dart index 5fa44d81..4a8e6b1a 100644 --- a/lib/src/editor/spellchecker/default_spellchecker_service.dart +++ b/lib/src/editor/spellchecker/default_spellchecker_service.dart @@ -14,7 +14,8 @@ class DefaultSpellCheckerService extends SpellCheckerService { @override List? checkSpelling( String text, { - LongPressGestureRecognizer Function(String p1)? customLongPressRecognizerOnWrongSpan, + LongPressGestureRecognizer Function(String p1)? + customLongPressRecognizerOnWrongSpan, }) { return null; } diff --git a/lib/src/editor/spellchecker/spellchecker_service.dart b/lib/src/editor/spellchecker/spellchecker_service.dart index 10235ceb..0e2ebd48 100644 --- a/lib/src/editor/spellchecker/spellchecker_service.dart +++ b/lib/src/editor/spellchecker/spellchecker_service.dart @@ -34,5 +34,6 @@ abstract class SpellCheckerService { /// /// Returns a [List] with all misspelled words divide from the right words. List? checkSpelling(String text, - {LongPressGestureRecognizer Function(String)? customLongPressRecognizerOnWrongSpan}); + {LongPressGestureRecognizer Function(String)? + customLongPressRecognizerOnWrongSpan}); }