some cleanup and some fixes
This commit is contained in:
@@ -18,11 +18,14 @@ class _AddCardScreenState extends State<AddCardScreen> {
|
||||
final _kanaKit = const KanaKit();
|
||||
final _deckRepository = CustomDeckRepository();
|
||||
bool _useInterval = false;
|
||||
late FocusNode _japaneseFocusNode;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_japaneseController.addListener(_convertToKana);
|
||||
_japaneseFocusNode = FocusNode();
|
||||
_japaneseFocusNode.addListener(_onJapaneseFocusChange);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -31,13 +34,24 @@ class _AddCardScreenState extends State<AddCardScreen> {
|
||||
_japaneseController.dispose();
|
||||
_englishController.dispose();
|
||||
_kanjiController.dispose();
|
||||
_japaneseFocusNode.removeListener(_onJapaneseFocusChange);
|
||||
_japaneseFocusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _convertToKana() {
|
||||
final text = _japaneseController.text;
|
||||
final selection = _japaneseController.selection;
|
||||
final offset = selection.baseOffset;
|
||||
|
||||
if ((offset > 1 && text[offset - 1] == 'n' && text[offset - 2] != 'n') ||
|
||||
(offset == 1 && text[offset - 1] == 'n')) {
|
||||
return;
|
||||
}
|
||||
|
||||
final converted = _kanaKit.toKana(text);
|
||||
if (text != converted) {
|
||||
|
||||
if (converted != text) {
|
||||
_japaneseController.value = _japaneseController.value.copyWith(
|
||||
text: converted,
|
||||
selection: TextSelection.fromPosition(
|
||||
@@ -47,6 +61,21 @@ class _AddCardScreenState extends State<AddCardScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
void _onJapaneseFocusChange() {
|
||||
if (!_japaneseFocusNode.hasFocus) {
|
||||
_forceNConversion();
|
||||
}
|
||||
}
|
||||
|
||||
void _forceNConversion() {
|
||||
final text = _japaneseController.text;
|
||||
if (text.isNotEmpty &&
|
||||
text.endsWith('n') &&
|
||||
_kanaKit.toKana(text) != text) {
|
||||
_japaneseController.text = _kanaKit.toKana(text);
|
||||
}
|
||||
}
|
||||
|
||||
void _saveCard() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
final srsData = _useInterval
|
||||
@@ -83,6 +112,7 @@ class _AddCardScreenState extends State<AddCardScreen> {
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _japaneseController,
|
||||
focusNode: _japaneseFocusNode,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Japanese (Kana)',
|
||||
hintText: 'Enter Japanese vocabulary or kanji',
|
||||
|
||||
Reference in New Issue
Block a user