dth-pingpong-mobileapp/lib/pages/home.dart

124 lines
3.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'login.dart';
import '../navbar.dart';
import 'views/leaderboard.dart';
import 'views/joinmatch.dart';
import 'views/creatematch.dart';
import 'views/friendlist.dart';
import 'views/myprofile.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import '../globals.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _selectedIndex = 0;
final List<Widget> _pages = [
2024-12-22 20:35:58 +01:00
LeaderboardPage(),
JoinMatchPage(),
CreateMatchPage(),
AddFriendPage(),
ProfilePage(),
];
void _onBottomNavTap(int index) {
setState(() {
_selectedIndex = index;
});
}
Future<void> _logout(BuildContext context) async {
final prefs = await SharedPreferences.getInstance();
2024-12-22 20:35:58 +01:00
await prefs.remove('token');
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => LoginPage()),
);
}
Future<Map<String, String>> fetchCommitHashes() async {
const apiUrl = '$apiurl/version';
try {
final response = await http.get(Uri.parse(apiUrl));
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
String formatHash(String? hash) {
if (hash == null) return 'Unknown';
return '#${hash.substring(0, 8).toUpperCase()}';
}
return {
'backend': formatHash(data['backend']),
'frontend': formatHash(data['frontend']),
};
} else {
throw Exception('Failed to fetch commit hashes');
}
} catch (e) {
return {
'backend': 'Error fetching hash',
'frontend': 'Error fetching hash',
};
}
}
2024-12-23 22:10:06 +01:00
Future<void> _showOpenSourceLicenses() async {
final commitHashes = await fetchCommitHashes();
2024-12-23 22:10:06 +01:00
showDialog(
context: context,
builder: (BuildContext context) => AboutDialog(
applicationIcon: const Icon(Icons.code),
applicationLegalese: '© 2024 Thomas Bassi @ Defence Tech.',
applicationName: 'DTHPP',
applicationVersion:
'API: ${commitHashes['backend']} - UI: ${commitHashes['frontend']}',
2024-12-23 22:10:06 +01:00
children: [
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: Text(
"Do people even care about licenses? Is this ever going to be opened? Anywho, i hope you're enjoying the app and having fun on your break :D",
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w400),
),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Ping Pong Tracker'),
actions: [
2024-12-23 22:10:06 +01:00
IconButton(
icon: Icon(Icons.info),
onPressed: _showOpenSourceLicenses,
tooltip: 'Open Source Licenses',
),
IconButton(
icon: Icon(Icons.logout),
onPressed: () => _logout(context),
tooltip: 'Logout',
),
],
),
2024-12-22 20:35:58 +01:00
body: _pages[_selectedIndex],
bottomNavigationBar: BottomNavigationBarCustom(
currentIndex: _selectedIndex,
onTap: _onBottomNavTap,
),
);
}
}