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 ' ;
2024-09-05 21:42:21 +00:00
import ' package:tetra_stats/data_objects/tetra_league.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 ;
2024-09-04 21:12:26 +00:00
import ' package:tetra_stats/utils/numers_formats.dart ' ;
2023-09-23 19:09:36 +00:00
import ' package:tetra_stats/views/mathes_view.dart ' ;
2023-06-17 21:50:52 +00:00
import ' package:tetra_stats/views/state_view.dart ' ;
2024-06-11 16:30:13 +00:00
import ' package:tetra_stats/widgets/text_timestamp.dart ' ;
2023-10-09 18:48:50 +00:00
import ' package:window_manager/window_manager.dart ' ;
2023-06-17 21:50:52 +00:00
class StatesView extends StatefulWidget {
2024-09-04 21:12:26 +00:00
final String nickname ;
final String id ;
const StatesView ( { required this . nickname , required this . id , super . key } ) ;
2023-06-17 21:50:52 +00:00
@ override
State < StatefulWidget > createState ( ) = > StatesState ( ) ;
}
2023-10-09 18:48:50 +00:00
late String oldWindowTitle ;
2023-06-17 21:50:52 +00:00
class StatesState extends State < StatesView > {
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 ) ;
2024-09-04 21:12:26 +00:00
//windowManager.setTitle("Tetra Stats: ${t.statesViewTitle(number: widget.states.length, nickname: widget.states.last.id.toUpperCase())}");
2023-10-09 18:48:50 +00:00
}
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 ) ;
2023-06-17 21:50:52 +00:00
return Scaffold (
appBar: AppBar (
2024-09-04 21:12:26 +00:00
title: Text ( t . statesViewTitle ( number: " " , nickname: widget . nickname ) ) ,
2023-09-23 19:09:36 +00:00
actions: [
IconButton (
onPressed: ( ) {
Navigator . push (
2024-09-04 21:12:26 +00:00
context ,
MaterialPageRoute (
builder: ( context ) = > MatchesView ( userID: widget . id , username: widget . nickname ) ,
) ,
) ;
2023-09-23 19:09:36 +00:00
} , icon: const Icon ( Icons . list ) , tooltip: t . viewAllMatches )
] ,
2023-06-17 21:50:52 +00:00
) ,
backgroundColor: Colors . black ,
body: SafeArea (
2024-09-04 21:12:26 +00:00
child: FutureBuilder < List < TetraLeague > > ( future: teto . getStates ( widget . id ) , builder: ( context , snapshot ) {
switch ( snapshot . connectionState ) {
case ConnectionState . none:
case ConnectionState . waiting:
case ConnectionState . active:
return const Center ( child: CircularProgressIndicator ( color: Colors . white ) ) ;
case ConnectionState . done:
if ( snapshot . hasData ) {
return ListView . builder (
itemCount: snapshot . data ! . length ,
prototypeItem: ListTile (
title: Text ( " " ) ,
subtitle: Text ( " " , style: TextStyle ( color: Colors . grey ) ) ,
trailing: IconButton ( icon: const Icon ( Icons . delete_forever ) , onPressed: ( ) { } ) ,
2023-06-17 21:50:52 +00:00
) ,
2024-09-04 21:12:26 +00:00
itemBuilder: ( context , index ) {
return ListTile (
title: Text ( timestamp ( snapshot . data ! [ index ] . timestamp ) ) ,
subtitle: Text (
t . statesViewEntry ( level: f2 . format ( snapshot . data ! [ index ] . tr ) , games: intf . format ( snapshot . data ! [ index ] . gamesPlayed ) , glicko: snapshot . data ! [ index ] . glicko ! = null ? f2 . format ( snapshot . data ! [ index ] . glicko ) : " --- " , rd: snapshot . data ! [ index ] . rd ! = null ? f2 . format ( snapshot . data ! [ index ] . rd ) : " -- " ) ,
style: TextStyle ( color: Colors . grey ) ,
) ,
trailing: IconButton (
icon: const Icon ( Icons . delete_forever ) ,
onPressed: ( ) {
teto . deleteState ( snapshot . data ! [ index ] . id + snapshot . data ! [ index ] . timestamp . millisecondsSinceEpoch . toRadixString ( 16 ) ) . then ( ( value ) = > setState ( ( ) {
ScaffoldMessenger . of ( context ) . showSnackBar ( SnackBar ( content: Text ( t . stateRemoved ( date: timestamp ( snapshot . data ! [ index ] . timestamp ) ) ) ) ) ;
} ) ) ;
} ,
) ,
onTap: ( ) {
Navigator . push (
context ,
MaterialPageRoute (
builder: ( context ) = > StateView ( state: snapshot . data ! [ index ] ) ,
) ,
) ;
} ,
) ;
} ) ;
} else if ( snapshot . hasError ) {
return Center ( child:
Column (
mainAxisSize: MainAxisSize . min ,
children: [
Text ( snapshot . error . toString ( ) , style: const TextStyle ( fontFamily: " Eurostile Round " , fontSize: 42 , fontWeight: FontWeight . bold ) , textAlign: TextAlign . center ) ,
Padding (
padding: const EdgeInsets . only ( top: 8.0 ) ,
child: Text ( snapshot . stackTrace . toString ( ) , style: const TextStyle ( fontFamily: " Eurostile Round " , fontSize: 18 ) , textAlign: TextAlign . center ) ,
) ,
] ,
)
2023-06-17 21:50:52 +00:00
) ;
2024-09-04 21:12:26 +00:00
}
break ;
}
return const Center ( child: Text ( ' default case of FutureBuilder ' , style: TextStyle ( fontFamily: " Eurostile Round Extended " , fontSize: 42 ) , textAlign: TextAlign . center ) ) ;
}
) ) ) ; }
2023-06-17 21:50:52 +00:00
}