Skip to content

Commit

Permalink
Improved favorite language
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Mar 25, 2024
1 parent dc03f22 commit fb557ab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,16 @@ enum FromTo { from, to }
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);
}

Expand Down
13 changes: 6 additions & 7 deletions lib/screens/translate/widgets/lang_selector/lang.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:flutter/material.dart';
import 'package:simplytranslate_mobile/simplytranslate.dart';
import '/data.dart';

bool _isFirstClick = false;

class GoogleLang extends StatefulWidget {
final FromTo fromto;
GoogleLang(
Expand All @@ -15,6 +13,8 @@ class GoogleLang extends StatefulWidget {
State<GoogleLang> createState() => _GoogleLangState();
}

bool _isFirstClick = true;

class _GoogleLangState extends State<GoogleLang> {
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
Expand Down Expand Up @@ -66,7 +66,7 @@ class _GoogleLangState extends State<GoogleLang> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: options.indexed.map((item) {
final option = item.$2;
final (idx, option) = item;
return Container(
color: theme == Brightness.dark ? greyColor : Colors.white,
child: GestureDetector(
Expand Down Expand Up @@ -105,10 +105,9 @@ class _GoogleLangState extends State<GoogleLang> {
fontSize: 18,
color: () {
final (max1, max2, max3) = lastUsed(widget.fromto);
final idx = item.$1 + 1;
if ((idx == 1 && option == (widget.fromto == FromTo.from ? fromSelLangMap[max1] : toSelLangMap[max1])) ||
(idx == 2 && option == (widget.fromto == FromTo.from ? fromSelLangMap[max2] : toSelLangMap[max2])) ||
(idx == 3 && option == (widget.fromto == FromTo.from ? fromSelLangMap[max3] : toSelLangMap[max3]))) {
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;
}
}(),
Expand Down
8 changes: 4 additions & 4 deletions lib/simplytranslate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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;

fromLangUsage[from] = fromLangUsage[from]! + 1;
toLangUsage[to] = toLangUsage[to]! + 1;

session.write("fromLangUsage", jsonEncode(fromLangUsage));
session.write("toLangUsage", jsonEncode(toLangUsage));

return translate_(text, from, to);
Expand Down

0 comments on commit fb557ab

Please sign in to comment.