TetraStats/lib/views/settings_view.dart

303 lines
13 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:go_router/go_router.dart';
import 'package:tetra_stats/data_objects/tetrio.dart';
2024-06-11 20:51:57 +00:00
import 'package:tetra_stats/main.dart' show packageInfo, teto, prefs;
import 'package:file_selector/file_selector.dart';
import 'package:file_picker/file_picker.dart';
2023-07-20 20:56:00 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tetra_stats/gen/strings.g.dart';
import 'package:tetra_stats/services/crud_exceptions.dart';
import 'package:tetra_stats/utils/open_in_browser.dart';
import 'package:window_manager/window_manager.dart';
late String oldWindowTitle;
TextStyle subtitleStyle = const TextStyle(fontFamily: "Eurostile Round Condensed", color: Colors.grey);
class SettingsView extends StatefulWidget {
const SettingsView({super.key});
@override
State<StatefulWidget> createState() => SettingsState();
}
class SettingsState extends State<SettingsView> {
String defaultNickname = "Checking...";
2024-03-06 22:34:15 +00:00
late bool showPositions;
late bool updateInBG;
final TextEditingController _playertext = TextEditingController();
@override
void initState() {
2023-10-10 20:20:27 +00:00
if (!kIsWeb && !Platform.isAndroid && !Platform.isIOS){
windowManager.getTitle().then((value) => oldWindowTitle = value);
windowManager.setTitle("Tetra Stats: ${t.settings}");
}
_getPreferences();
super.initState();
}
@override
void dispose(){
2023-10-10 20:20:27 +00:00
if (!kIsWeb && !Platform.isAndroid && !Platform.isIOS) windowManager.setTitle(oldWindowTitle);
super.dispose();
}
2024-06-11 20:51:57 +00:00
void _getPreferences() {
2024-05-04 21:17:26 +00:00
showPositions = prefs.getBool("showPositions") ?? false;
updateInBG = prefs.getBool("updateInBG") ?? false;
_setDefaultNickname(prefs.getString("player"));
}
Future<void> _setDefaultNickname(String? n) async {
if (n != null) {
try {
defaultNickname = await teto.getNicknameByID(n);
} on TetrioPlayerNotExist {
defaultNickname = n;
}
} else {
2024-05-04 21:17:26 +00:00
defaultNickname = "dan63047";
}
setState(() {});
}
Future<void> _setPlayer(String player) async {
await prefs.setString('player', player);
await _setDefaultNickname(player);
}
Future<void> _removePlayer() async {
await prefs.remove('player');
await _setDefaultNickname("6098518e3d5155e6ec429cdc");
}
@override
Widget build(BuildContext context) {
final t = Translations.of(context);
List<DropdownMenuItem<AppLocale>>? locales = <DropdownMenuItem<AppLocale>>[];
for (var v in AppLocale.values){
locales.add(DropdownMenuItem<AppLocale>(
value: v, child: Text(t.locales[v.languageTag]!)));
}
return Scaffold(
appBar: AppBar(
2023-07-12 15:14:25 +00:00
title: Text(t.settings),
),
backgroundColor: Colors.black,
body: SafeArea(
child: ListView(
children: [
ListTile(
2023-07-12 15:14:25 +00:00
title: Text(t.exportDB),
subtitle: Text(t.exportDBDescription, style: subtitleStyle),
onTap: () {
2023-07-20 20:56:00 +00:00
if (kIsWeb){
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.notForWeb)));
} else if (Platform.isAndroid){
var downloadFolder = Directory("/storage/emulated/0/Download");
File exportedDB = File("${downloadFolder.path}/TetraStats.db");
getApplicationDocumentsDirectory().then((value) {
exportedDB.writeAsBytes(File("${value.path}/TetraStats.db").readAsBytesSync());
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
2023-07-12 15:14:25 +00:00
title: Text(t.androidExportAlertTitle,
style: const TextStyle(
fontFamily: "Eurostile Round Extended")),
content: SingleChildScrollView(
2023-07-12 15:14:25 +00:00
child: ListBody(children: [Text(t.androidExportText(exportedDB: exportedDB))]),
),
actions: <Widget>[
TextButton(
2023-07-12 15:14:25 +00:00
child: Text(t.popupActions.ok),
onPressed: () {
Navigator.of(context).pop();
},
),
],
));
});
2023-07-20 20:56:00 +00:00
} else if (Platform.isLinux || Platform.isWindows) {
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text(t.desktopExportAlertTitle,
style: const TextStyle(
fontFamily: "Eurostile Round Extended")),
content: SingleChildScrollView(
child: ListBody(children: [
Text(t.desktopExportText)
]),
),
actions: <Widget>[
TextButton(
child: Text(t.popupActions.ok),
onPressed: () {
Navigator.of(context).pop();
},
),
],
));
}
},
),
ListTile(
2023-07-12 15:14:25 +00:00
title: Text(t.importDB),
subtitle: Text(t.importDBDescription, style: subtitleStyle),
onTap: () {
2023-07-20 20:56:00 +00:00
if (kIsWeb){
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.notForWeb)));
}else if(Platform.isAndroid){
FilePicker.platform.pickFiles(
type: FileType.any,
).then((value){
if (value != null){
var newDB = value.paths[0]!;
teto.close().then((value){
if(!newDB.endsWith("db")){
2023-07-12 15:14:25 +00:00
return ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.importWrongFileType)));
}
getApplicationDocumentsDirectory().then((value){
var oldDB = File("${value.path}/TetraStats.db");
oldDB.writeAsBytes(File(newDB).readAsBytesSync(), flush: true).then((value){
teto.open();
2023-07-12 15:14:25 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.importSuccess)));
});
});
});
} else {
2023-07-12 15:14:25 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.importCancelled)));
}
});
}else{
const XTypeGroup typeGroup = XTypeGroup(
label: 'Tetra Stats Database',
extensions: <String>['db'],
);
openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]).then((value){
if (value != null){
var newDB = value.path;
teto.close().then((value){
getApplicationDocumentsDirectory().then((value){
var oldDB = File("${value.path}/TetraStats.db");
oldDB.writeAsBytes(File(newDB).readAsBytesSync()).then((value){
teto.open();
2023-07-12 15:14:25 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.importSuccess)));
});
});
});
} else {
2023-07-12 15:14:25 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.importCancelled)));
}
});
}
},
),
ListTile(
2023-07-12 15:14:25 +00:00
title: Text(t.yourID),
subtitle: Text(t.yourIDText, style: subtitleStyle),
trailing: Text(defaultNickname),
onTap: () => showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
2023-07-12 15:14:25 +00:00
title: Text(t.yourIDAlertTitle,
style: const TextStyle(
fontFamily: "Eurostile Round Extended")),
content: SingleChildScrollView(
child: ListBody(children: [
2023-07-12 15:14:25 +00:00
Text(t.yourIDText),
TextField(controller: _playertext, maxLength: 25)
]),
),
actions: <Widget>[
TextButton(
2023-07-12 15:14:25 +00:00
child: Text(t.popupActions.cancel),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
2023-07-12 15:14:25 +00:00
child: Text(t.popupActions.submit),
onPressed: () async {
if (_playertext.text.isEmpty) {
_removePlayer();
Navigator.of(context).pop();
return;
}
late TetrioPlayer user;
try{
user = await teto.fetchPlayer(_playertext.text.toLowerCase().trim());
}on Exception{
if (context.mounted) ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.errors.noSuchUser)));
return;
}
_setPlayer(user.userId);
if (context.mounted) Navigator.of(context).pop();
setState(() {});
},
)
],
)),
),
ListTile(
2023-07-12 15:14:25 +00:00
title: Text(t.language),
subtitle: Text("By default, the system language will be selected (if available among Tetra Stats locales, otherwise English)", style: subtitleStyle),
trailing: DropdownButton(
items: locales,
value: LocaleSettings.currentLocale,
2023-07-15 16:22:25 +00:00
onChanged: (value){
LocaleSettings.setLocale(value!);
if(value.languageCode == Platform.localeName.substring(0, 2)){
prefs.remove('locale');
}else{
prefs.setString('locale', value.languageCode);
}
},
),
),
ListTile(title: Text(t.customization),
2024-05-04 20:04:48 +00:00
subtitle: Text(t.customizationDescription, style: const TextStyle(fontFamily: "Eurostile Round Condensed", color: Colors.grey)),
trailing: const Icon(Icons.arrow_right),
onTap: () {
2024-06-11 16:30:13 +00:00
context.go("/settings/customization");
},),
2024-06-16 17:13:26 +00:00
ListTile(title: Text(t.updateInBackground),
subtitle: Text(t.updateInBackgroundDescription, style: const TextStyle(fontFamily: "Eurostile Round Condensed", color: Colors.grey)),
trailing: Switch(value: updateInBG, onChanged: (bool value){
prefs.setBool("updateInBG", value);
setState(() {
updateInBG = value;
});
}),),
ListTile(title: Text(t.lbStats),
2024-05-04 20:04:48 +00:00
subtitle: Text(t.lbStatsDescription, style: const TextStyle(fontFamily: "Eurostile Round Condensed", color: Colors.grey)),
2024-03-06 22:34:15 +00:00
trailing: Switch(value: showPositions, onChanged: (bool value){
prefs.setBool("showPositions", value);
setState(() {
2024-03-06 22:34:15 +00:00
showPositions = value;
});
}),),
const Divider(),
ListTile(
2023-07-20 20:56:00 +00:00
onTap: (){
launchInBrowser(Uri.https("github.com", "dan63047/TetraStats"));
2023-07-20 20:56:00 +00:00
},
title: Text(t.aboutApp, style: const TextStyle(fontWeight: FontWeight.w500),),
subtitle: Text(t.aboutAppText(appName: packageInfo.appName, packageName: packageInfo.packageName, version: packageInfo.version, buildNumber: packageInfo.buildNumber)),
trailing: const Icon(Icons.arrow_right)
),
Wrap(
alignment: WrapAlignment.center,
spacing: 8,
children: [
TextButton(child: Text("Donate to me"), onPressed: (){},),TextButton(child: Text("Donate to NOT me"), onPressed: (){},),TextButton(child: Text("Donate to someone else"), onPressed: (){},),
],
),
],
)),
);
}
}