Skip to content

Commit

Permalink
Converted most used languages to last used languages #244
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Mar 27, 2024
1 parent 049539e commit af61219
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 103 deletions.
39 changes: 4 additions & 35 deletions lib/data.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -113,38 +111,9 @@ switchVals() {
enum FromTo { from, to }

(String?, String?, String?) lastUsed(FromTo fromto) {
final Map<String, dynamic> 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<T> on List<T> {
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"));
}
}
153 changes: 94 additions & 59 deletions lib/screens/translate/widgets/lang_selector/lang.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,52 @@ class _GoogleLangState extends State<GoogleLang> {
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;
Expand All @@ -65,58 +95,63 @@ class _GoogleLangState extends State<GoogleLang> {
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(),
),
),
),
Expand Down
28 changes: 19 additions & 9 deletions lib/simplytranslate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,25 @@ Future<Uint8List> tts(String text, String language) async {

Future<Map<String, dynamic>> translate(String text, String from, String to) async {
if (text.isEmpty) return {};
Map<String, dynamic> fromSession = jsonDecode(session.read("fromLangUsage") ?? "{}");
Map<String, dynamic> fromLangUsage = fromSession.isEmpty ? Map.fromIterable(fromSelLangMap.keys, key: (k) => k, value: (_) => 0) : fromSession;
fromLangUsage[from] = fromLangUsage[from]! + 1;
session.write("fromLangUsage", jsonEncode(fromLangUsage));

Map<String, dynamic> toSession = jsonDecode(session.read("toLangUsage") ?? "{}");
Map<String, dynamic> 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);
}
Expand Down

0 comments on commit af61219

Please sign in to comment.