2023-06-05 22:21:29 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
2023-06-06 21:04:49 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-06-10 21:56:14 +00:00
|
|
|
import 'package:tetra_stats/services/crud_exceptions.dart';
|
|
|
|
import 'package:tetra_stats/services/tetrio_crud.dart';
|
2023-06-05 22:21:29 +00:00
|
|
|
|
|
|
|
class SettingsView extends StatefulWidget {
|
|
|
|
const SettingsView({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => SettingsState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class SettingsState extends State<SettingsView> {
|
|
|
|
PackageInfo _packageInfo = PackageInfo(appName: "TetraStats", packageName: "idk man", version: "some numbers", buildNumber: "anotherNumber");
|
2023-06-06 21:04:49 +00:00
|
|
|
late SharedPreferences prefs;
|
2023-06-10 21:56:14 +00:00
|
|
|
final TetrioService teto = TetrioService();
|
|
|
|
String defaultNickname = "Checking...";
|
2023-06-06 21:04:49 +00:00
|
|
|
final TextEditingController _playertext = TextEditingController();
|
2023-06-05 22:21:29 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
_initPackageInfo();
|
2023-06-06 21:04:49 +00:00
|
|
|
_getPreferences();
|
2023-06-10 21:56:14 +00:00
|
|
|
super.initState();
|
2023-06-05 22:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _initPackageInfo() async {
|
|
|
|
final info = await PackageInfo.fromPlatform();
|
|
|
|
setState(() {
|
|
|
|
_packageInfo = info;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-06 21:04:49 +00:00
|
|
|
Future<void> _getPreferences() async {
|
|
|
|
prefs = await SharedPreferences.getInstance();
|
2023-06-10 21:56:14 +00:00
|
|
|
_setDefaultNickname(prefs.getString("player"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _setDefaultNickname(String? n) async {
|
|
|
|
if (n != null) {
|
|
|
|
try {
|
|
|
|
defaultNickname = await teto.getNicknameByID(n);
|
|
|
|
} on TetrioPlayerNotExist {
|
|
|
|
defaultNickname = n;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defaultNickname = "dan63047";
|
|
|
|
}
|
|
|
|
setState(() {});
|
2023-06-06 21:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _setPlayer(String player) async {
|
|
|
|
await prefs.setString('player', player);
|
2023-06-10 21:56:14 +00:00
|
|
|
await _setDefaultNickname(player);
|
2023-06-06 21:04:49 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 22:21:29 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text("Settings"),
|
|
|
|
),
|
|
|
|
backgroundColor: Colors.black,
|
|
|
|
body: SafeArea(
|
|
|
|
child: ListView(
|
|
|
|
children: [
|
|
|
|
ListTile(
|
|
|
|
title: const Text("So there you gonna be able to change some settings"),
|
2023-06-10 21:56:14 +00:00
|
|
|
subtitle: const Text("Only \"Your TETR.IO account\" implemented yet. In the future you will able to import and export app sqlite database."),
|
2023-06-05 22:21:29 +00:00
|
|
|
trailing: Switch(
|
|
|
|
value: true,
|
|
|
|
onChanged: (bool value) {},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
ListTile(
|
2023-06-10 21:56:14 +00:00
|
|
|
title: const Text("Your TETR.IO account"),
|
|
|
|
trailing: Text(defaultNickname),
|
2023-06-05 22:21:29 +00:00
|
|
|
onTap: () => showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) => AlertDialog(
|
2023-06-06 21:04:49 +00:00
|
|
|
title: const Text("Your TETR.IO account nickname or ID", style: TextStyle(fontFamily: "Eurostile Round Extended")),
|
2023-06-05 22:21:29 +00:00
|
|
|
content: SingleChildScrollView(
|
2023-06-10 21:56:14 +00:00
|
|
|
child: ListBody(children: [
|
|
|
|
const Text(
|
|
|
|
"Every time when app loads, stats of that player will be fetched. Please prefer ID over nickname because nickname can be changed."),
|
|
|
|
TextField(controller: _playertext, maxLength: 25)
|
|
|
|
]),
|
2023-06-05 22:21:29 +00:00
|
|
|
),
|
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
|
|
|
child: const Text('Cancel'),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
child: const Text('Submit'),
|
|
|
|
onPressed: () {
|
2023-06-06 21:04:49 +00:00
|
|
|
_setPlayer(_playertext.text.toLowerCase().trim());
|
2023-06-05 22:21:29 +00:00
|
|
|
Navigator.of(context).pop();
|
2023-06-06 21:04:49 +00:00
|
|
|
setState(() {});
|
2023-06-05 22:21:29 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)),
|
|
|
|
),
|
2023-06-06 21:04:49 +00:00
|
|
|
const Divider(),
|
2023-06-05 22:21:29 +00:00
|
|
|
ListTile(
|
|
|
|
title: const Text("About app"),
|
|
|
|
subtitle: Text("${_packageInfo.appName} (${_packageInfo.packageName}) Version ${_packageInfo.version} Build ${_packageInfo.buildNumber}"),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|