2023-10-09 18:48:50 +00:00
|
|
|
import 'dart:io';
|
2023-10-10 20:20:27 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2023-06-17 21:50:52 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:tetra_stats/data_objects/tetrio.dart';
|
2023-07-12 15:14:25 +00:00
|
|
|
import 'package:tetra_stats/gen/strings.g.dart';
|
2024-06-03 23:42:44 +00:00
|
|
|
import 'package:tetra_stats/main.dart' show teto;
|
2023-10-19 21:45:07 +00:00
|
|
|
import 'package:tetra_stats/utils/filesizes_converter.dart';
|
2023-06-17 21:50:52 +00:00
|
|
|
import 'package:tetra_stats/views/states_view.dart';
|
2023-10-09 18:48:50 +00:00
|
|
|
import 'package:window_manager/window_manager.dart';
|
2023-06-17 21:50:52 +00:00
|
|
|
|
2023-10-09 18:48:50 +00:00
|
|
|
late String oldWindowTitle;
|
2023-06-17 21:50:52 +00:00
|
|
|
|
|
|
|
class TrackedPlayersView extends StatefulWidget {
|
2024-01-13 18:49:36 +00:00
|
|
|
const TrackedPlayersView({super.key});
|
2023-06-17 21:50:52 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => TrackedPlayersState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class TrackedPlayersState extends State<TrackedPlayersView> {
|
2023-10-09 18:48:50 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2023-10-10 20:20:27 +00:00
|
|
|
if (!kIsWeb && !Platform.isAndroid && !Platform.isIOS){
|
2023-10-09 18:48:50 +00:00
|
|
|
windowManager.getTitle().then((value) => oldWindowTitle = value);
|
|
|
|
windowManager.setTitle("Tetra Stats: ${t.trackedPlayersViewTitle}");
|
|
|
|
}
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2023-10-10 20:20:27 +00:00
|
|
|
if (!kIsWeb && !Platform.isAndroid && !Platform.isIOS) windowManager.setTitle(oldWindowTitle);
|
2023-10-09 18:48:50 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-06-17 21:50:52 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-07-12 15:14:25 +00:00
|
|
|
final t = Translations.of(context);
|
|
|
|
final DateFormat dateFormat = DateFormat.yMMMd(LocaleSettings.currentLocale.languageCode).add_Hms();
|
2023-06-17 21:50:52 +00:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2023-07-12 15:14:25 +00:00
|
|
|
title: Text(t.trackedPlayersViewTitle),
|
2023-10-16 21:41:45 +00:00
|
|
|
actions: [
|
|
|
|
PopupMenuButton(
|
|
|
|
icon: const Icon(Icons.settings_backup_restore),
|
|
|
|
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
|
|
|
|
PopupMenuItem(
|
|
|
|
value: 1,
|
2023-11-08 22:17:44 +00:00
|
|
|
child: Text(t.duplicatedFix),
|
2023-10-16 21:41:45 +00:00
|
|
|
),
|
2023-10-19 21:45:07 +00:00
|
|
|
PopupMenuItem(
|
|
|
|
value: 2,
|
2023-11-08 22:17:44 +00:00
|
|
|
child: Text(t.compressDB),
|
2023-10-19 21:45:07 +00:00
|
|
|
),
|
2023-10-16 21:41:45 +00:00
|
|
|
],
|
|
|
|
onSelected: (value) {
|
2023-10-19 21:45:07 +00:00
|
|
|
switch (value) {
|
|
|
|
case 1:
|
|
|
|
teto.removeDuplicatesFromTLMatches();
|
|
|
|
break;
|
|
|
|
case 2:
|
2023-11-08 22:17:44 +00:00
|
|
|
teto.compressDB().then((value) => ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.SpaceSaved(size: bytesToSize(value))))));
|
2023-10-19 21:45:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
2023-10-16 21:41:45 +00:00
|
|
|
})
|
|
|
|
],
|
2023-06-17 21:50:52 +00:00
|
|
|
),
|
|
|
|
backgroundColor: Colors.black,
|
|
|
|
body: SafeArea(
|
2024-02-06 20:38:52 +00:00
|
|
|
child: FutureBuilder(
|
|
|
|
future: teto.getAllPlayers(),
|
2023-06-17 21:50:52 +00:00
|
|
|
builder: (context, snapshot) {
|
|
|
|
switch (snapshot.connectionState) {
|
|
|
|
case ConnectionState.none:
|
|
|
|
case ConnectionState.waiting:
|
|
|
|
case ConnectionState.active:
|
2024-02-06 20:38:52 +00:00
|
|
|
return const Center(child: CircularProgressIndicator(color: Colors.white));
|
|
|
|
case ConnectionState.done:
|
2023-06-17 21:50:52 +00:00
|
|
|
final allPlayers = (snapshot.data != null) ? snapshot.data as Map<String, List<TetrioPlayer>> : <String, List<TetrioPlayer>>{};
|
|
|
|
List<String> keys = allPlayers.keys.toList();
|
|
|
|
return NestedScrollView(
|
|
|
|
headerSliverBuilder: (context, value) {
|
|
|
|
String howManyPlayers(int numberOfPlayers) => Intl.plural(
|
|
|
|
numberOfPlayers,
|
2023-07-12 15:14:25 +00:00
|
|
|
zero: t.trackedPlayersZeroEntrys,
|
|
|
|
one: t.trackedPlayersOneEntry,
|
|
|
|
other: t.trackedPlayersManyEntrys(numberOfPlayers: numberOfPlayers),
|
2023-06-17 21:50:52 +00:00
|
|
|
name: 'howManyPeople',
|
|
|
|
args: [numberOfPlayers],
|
|
|
|
desc: 'Description of how many people are seen in a place.',
|
|
|
|
examples: const {'numberOfPeople': 3},
|
|
|
|
);
|
|
|
|
return [
|
|
|
|
SliverToBoxAdapter(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 16),
|
|
|
|
child: Text(
|
|
|
|
howManyPlayers(allPlayers.length),
|
|
|
|
style: const TextStyle(color: Colors.white, fontSize: 25),
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
const SliverToBoxAdapter(child: Divider())
|
|
|
|
];
|
|
|
|
},
|
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: allPlayers.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return ListTile(
|
2023-07-12 15:14:25 +00:00
|
|
|
title: Text(t.trackedPlayersEntry(nickname: allPlayers[keys[index]]!.last.username, numberOfStates: allPlayers[keys[index]]!.length)),
|
|
|
|
subtitle: Text(t.trackedPlayersDescription(firstStateDate: dateFormat.format(allPlayers[keys[index]]!.first.state), lastStateDate: dateFormat.format(allPlayers[keys[index]]!.last.state))),
|
2023-06-17 21:50:52 +00:00
|
|
|
trailing: IconButton(
|
|
|
|
icon: const Icon(Icons.delete_forever),
|
|
|
|
onPressed: () {
|
|
|
|
String nn = allPlayers[keys[index]]!.last.username;
|
2024-02-06 20:38:52 +00:00
|
|
|
setState(() {teto.deletePlayer(keys[index]);});
|
2023-07-12 15:14:25 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(t.trackedPlayersStatesDeleted(nickname: nn))));
|
2023-06-17 21:50:52 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => StatesView(states: allPlayers[keys[index]]!),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|