Skip to content

Chore: improve Spell checker API to the example #2133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand All @@ -334,16 +335,10 @@ 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();
// use isServiceActive to get the state of the service
SpellCheckerServiceProvider.isServiceActive();
setState(() {});
```

Open this [page](https://pub.dev/packages/simple_spell_checker) for more information.
Expand Down
20 changes: 10 additions & 10 deletions example/lib/screens/quill/quill_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -61,26 +61,26 @@ class _QuillScreenState extends State<QuillScreen> {
@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'),
actions: [
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class SimpleSpellCheckerService
);
}

@override
void toggleChecker() => checker.toggleChecker();

@override
bool isServiceActive() => checker.isCheckerActive();

@override
void dispose({bool onlyPartial = false}) {
if (onlyPartial) {
Expand Down
2 changes: 1 addition & 1 deletion flutter_quill_extensions/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies:

# Plugins
video_player: ^2.8.1
simple_spell_checker: ^1.1.2
simple_spell_checker: ^1.1.6
youtube_player_flutter: ^9.0.1
url_launcher: ^6.2.1
super_clipboard: ^0.8.15
Expand Down
6 changes: 6 additions & 0 deletions lib/src/editor/spellchecker/default_spellchecker_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ class DefaultSpellCheckerService extends SpellCheckerService<Object?> {

@override
void updateCustomLanguageIfExist({languageIdentifier}) {}

@override
bool isServiceActive() => false;

@override
void toggleChecker() {}
}
6 changes: 6 additions & 0 deletions lib/src/editor/spellchecker/spellchecker_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ abstract class SpellCheckerService<T> {

final String language;

/// Decide if the service should be activate or deactivate
/// without dispose the service
void toggleChecker();

bool isServiceActive();

/// dispose all the resources used for SpellcheckerService
///
/// if [onlyPartial] is true just dispose a part of the SpellcheckerService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class SpellCheckerServiceProvider {
_instance.dispose(onlyPartial: onlyPartial);
}

static void toggleState() {
_instance.toggleChecker();
}

static bool isServiceActive() {
return _instance.isServiceActive();
}

static void setNewLanguageState({required String language}) {
assert(language.isNotEmpty);
_instance.setNewLanguageState(language: language);
Expand Down