diff --git a/lib/data.dart b/lib/data.dart index 5917e86..18850f1 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -1,5 +1,3 @@ -import 'dart:convert'; - import 'package:flutter/material.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; @@ -113,38 +111,9 @@ switchVals() { enum FromTo { from, to } (String?, String?, String?) lastUsed(FromTo fromto) { - final Map langUsage = jsonDecode((fromto == FromTo.from ? session.read("fromLangUsage") : session.read("toLangUsage")) ?? "{}"); - if (langUsage.isEmpty) return (null, null, null); - - (String, int)? max1; - (String, int)? max2; - (String, int)? max3; - langUsage.removeWhere((key, value) => value == 0); - langUsage.forEach((key, value) { - if (max1 == null || max1!.$2 < value) { - max3 = max2; - max2 = max1; - max1 = (key, value); - } else if (max2 == null || max2!.$2 < value) { - max3 = max2; - max2 = (key, value); - } else if (max3 == null || max3!.$2 < value) { - max3 = (key, value); - } - }); - return (max1?.$1, max2?.$1, max3?.$1); -} - -extension MoveElement on List { - void move(int from, int to) { - RangeError.checkValidIndex(from, this, "from", length); - RangeError.checkValidIndex(to, this, "to", length); - var element = this[from]; - if (from < to) { - this.setRange(from, to, this, from + 1); - } else { - this.setRange(to + 1, from + 1, this, to); - } - this[to] = element; + if (fromto == FromTo.from) { + return (session.read("fromLast1"), session.read("fromLast2"), session.read("fromLast3")); + } else { + return (session.read("toLast1"), session.read("toLast2"), session.read("toLast3")); } } diff --git a/lib/screens/translate/widgets/lang_selector/lang.dart b/lib/screens/translate/widgets/lang_selector/lang.dart index 41d58bb..bbf4273 100644 --- a/lib/screens/translate/widgets/lang_selector/lang.dart +++ b/lib/screens/translate/widgets/lang_selector/lang.dart @@ -27,22 +27,52 @@ class _GoogleLangState extends State { if (_isFirstClick) { _isFirstClick = false; var list = selLangsIterable.toList(); - final (max1, max2, max3) = lastUsed(widget.fromto); - if (max1 != null) { - final idx = list.indexOf(selLangMap[max1]!); - if (idx >= 0) list.move(idx, 0); - } - if (max2 != null) { - final idx = list.indexOf(selLangMap[max2]!); - if (idx >= 0) list.move(idx, 1); - } - if (max3 != null) { - final idx = list.indexOf(selLangMap[max3]!); - if (idx >= 0) list.move(idx, 2); + final (last1, last2, last3) = lastUsed(widget.fromto); + if (last1 != null && last2 != null && last3 != null) { + list.remove(selLangMap[last1]!); + list.remove(selLangMap[last2]!); + list.remove(selLangMap[last3]!); + if (widget.fromto == FromTo.from) { + list.remove(selLangMap['auto']!); + list = [selLangMap['auto']!, selLangMap[last1]!, selLangMap[last2]!, selLangMap[last3]!, ...list]; + } else { + list = [selLangMap[last1]!, selLangMap[last2]!, selLangMap[last3]!, ...list]; + } + } else if (last1 != null && last2 != null) { + list.remove(selLangMap[last1]!); + list.remove(selLangMap[last2]!); + if (widget.fromto == FromTo.from) { + list.remove(selLangMap['auto']!); + list = [selLangMap['auto']!, selLangMap[last1]!, selLangMap[last2]!, ...list]; + } else { + list = [selLangMap[last1]!, selLangMap[last2]!, ...list]; + } + } else if (last1 != null) { + list.remove(selLangMap[last1]!); + if (widget.fromto == FromTo.from) { + list.remove(selLangMap['auto']!); + list = [selLangMap['auto']!, selLangMap[last1]!, ...list]; + } else { + list = [selLangMap[last1]!, ...list]; + } + } else { + if (widget.fromto == FromTo.from) { + list.remove(selLangMap['auto']!); + list = [selLangMap['auto']!, ...list]; + } } return list; } else { final query = txtEditingVal.text.toLowerCase(); + if (query.trim().isEmpty) { + var list = selLangsIterable.toList(); + list.sort(); + if (widget.fromto == FromTo.from) { + list.remove(selLangMap['auto']!); + list = [selLangMap['auto']!, ...list]; + } + return list; + } var list = selLangsIterable.where((word) => word.toLowerCase().contains(query)).toList(); list.sort((a, b) => a.indexOf(query).compareTo(b.indexOf(query))); return list; @@ -65,58 +95,63 @@ class _GoogleLangState extends State { child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, - children: options.indexed.map((item) { - final (idx, option) = item; - return Container( - color: theme == Brightness.dark ? greyColor : Colors.white, - child: GestureDetector( - onTap: () async { - FocusScope.of(context).unfocus(); - if (option == (widget.fromto == FromTo.from ? fromSelLangMap[toLangVal] : toSelLangMap[fromLangVal])) { - switchVals(); - } else { - for (var i in widget.fromto == FromTo.from ? fromSelLangMap.keys : toSelLangMap.keys) { - if (option == (widget.fromto == FromTo.from ? fromSelLangMap[i] : toSelLangMap[i])) { - session.write(widget.fromto == FromTo.from ? 'from_lang' : 'to_lang', i); - if (widget.fromto == FromTo.from) { - fromLangVal = i; - changeFromTxt!(fromSelLangMap[fromLangVal]!); - } else { - toLangVal = i; - changeToTxt!(toSelLangMap[toLangVal]!); + children: options + .map( + (option) => Container( + color: theme == Brightness.dark ? greyColor : Colors.white, + child: GestureDetector( + onTap: () async { + FocusScope.of(context).unfocus(); + if (option == (widget.fromto == FromTo.from ? fromSelLangMap[toLangVal] : toSelLangMap[fromLangVal])) { + switchVals(); + } else { + for (var i in widget.fromto == FromTo.from ? fromSelLangMap.keys : toSelLangMap.keys) { + if (option == (widget.fromto == FromTo.from ? fromSelLangMap[i] : toSelLangMap[i])) { + session.write(widget.fromto == FromTo.from ? 'from_lang' : 'to_lang', i); + if (widget.fromto == FromTo.from) { + fromLangVal = i; + changeFromTxt!(fromSelLangMap[fromLangVal]!); + } else { + toLangVal = i; + changeToTxt!(toSelLangMap[toLangVal]!); + } + final translatedText = await translate(googleInCtrl.text, fromLangVal, toLangVal); + if (!isTranslationCanceled) + setStateOverlord(() { + googleOutput = translatedText; + loading = false; + }); + break; + } } - final translatedText = await translate(googleInCtrl.text, fromLangVal, toLangVal); - if (!isTranslationCanceled) - setStateOverlord(() { - googleOutput = translatedText; - loading = false; - }); - break; } - } - } - }, - child: Container( - width: double.infinity, - padding: EdgeInsets.symmetric(horizontal: 8, vertical: 18), - child: Text( - option, - style: TextStyle( - fontSize: 18, - color: () { - final (max1, max2, max3) = lastUsed(widget.fromto); - if ((idx == 0 && option == (widget.fromto == FromTo.from ? fromSelLangMap[max1] : toSelLangMap[max1])) || - (idx == 1 && option == (widget.fromto == FromTo.from ? fromSelLangMap[max2] : toSelLangMap[max2])) || - (idx == 2 && option == (widget.fromto == FromTo.from ? fromSelLangMap[max3] : toSelLangMap[max3]))) { - return greenColor; - } - }(), + }, + child: Container( + width: double.infinity, + padding: EdgeInsets.symmetric(horizontal: 8, vertical: 18), + child: Text( + option, + style: TextStyle( + fontSize: 18, + color: () { + final (last1, last2, last3) = lastUsed(widget.fromto); + if (widget.fromto == FromTo.from) { + if (option == fromSelLangMap[last1] || option == fromSelLangMap[last2] || option == fromSelLangMap[last3]) { + return greenColor; + } + } else { + if (option == toSelLangMap[last1] || option == toSelLangMap[last2] || option == toSelLangMap[last3]) { + return greenColor; + } + } + }(), + ), + ), ), ), ), - ), - ); - }).toList(), + ) + .toList(), ), ), ), diff --git a/lib/simplytranslate.dart b/lib/simplytranslate.dart index 3065182..8c7802f 100644 --- a/lib/simplytranslate.dart +++ b/lib/simplytranslate.dart @@ -21,15 +21,25 @@ Future tts(String text, String language) async { Future> translate(String text, String from, String to) async { if (text.isEmpty) return {}; - Map fromSession = jsonDecode(session.read("fromLangUsage") ?? "{}"); - Map fromLangUsage = fromSession.isEmpty ? Map.fromIterable(fromSelLangMap.keys, key: (k) => k, value: (_) => 0) : fromSession; - fromLangUsage[from] = fromLangUsage[from]! + 1; - session.write("fromLangUsage", jsonEncode(fromLangUsage)); - - Map toSession = jsonDecode(session.read("toLangUsage") ?? "{}"); - Map toLangUsage = toSession.isEmpty ? Map.fromIterable(toSelLangMap.keys, key: (k) => k, value: (_) => 0) : toSession; - toLangUsage[to] = toLangUsage[to]! + 1; - session.write("toLangUsage", jsonEncode(toLangUsage)); + + final fromLast1 = session.read("fromLast1"); + final fromLast2 = session.read("fromLast2"); + final fromLast3 = session.read("fromLast3"); + + if (from != fromLast1 && from != fromLast2 && from != fromLast3 && from != "auto") { + session.write("fromLast1", from); + session.write("fromLast2", fromLast1); + session.write("fromLast3", fromLast2); + } + + final toLast1 = session.read("toLast1"); + final toLast2 = session.read("toLast2"); + final toLast3 = session.read("toLast3"); + if (to != toLast1 && to != toLast2 && to != toLast3) { + session.write("toLast1", to); + session.write("toLast2", toLast1); + session.write("toLast3", toLast2); + } return translate_(text, from, to); }