Skip to content

Commit

Permalink
refact: various clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
10dine authored and akaric108 committed Dec 20, 2024
1 parent 3e254e0 commit dc5ec44
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/client/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import 'routes.dart';
final ValueNotifier<ThemeMode> themeNotifier = ValueNotifier(ThemeMode.system);
List<CameraDescription> cameras = [];
bool soundEnabled = true;
bool globeEnabled = true;
const String globeLight = 'assets/2k_earth-day.jpg';
const String globeDark = 'assets/2k_earth-night.jpg';

enum RoomState { none, wait, owner, joiner }
final ValueNotifier<RoomState> roomState = ValueNotifier(RoomState.none);
Expand Down Expand Up @@ -75,6 +78,7 @@ class _SplashScreenState extends State<SplashScreen> {
await Future.delayed(Duration(seconds: 2)); // Optional delay for better UX
final prefs = await SharedPreferences.getInstance();
String? sessionId = prefs.getString('x-auth-token');
globeEnabled = prefs.getBool('globeEnabled') ?? true;

if (sessionId != null) {
currentUser = User(username: prefs.getString('username'), sessionId: prefs.getString('sessionId'));
Expand Down
8 changes: 6 additions & 2 deletions src/client/lib/models/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ class Game{

//TODO: when backend is sending back generalized locations of users send
//back a map of usernames and coordinates
List<String> getPlayers(){
return playerInfo.keys.toList();
List<dynamic> getPlayers(){
return playerInfo.entries.map((e) => e.value['username']).toList();
}

int getNumPlayers(){
return playerInfo.length;
}

//TODO: when backend is update with round logic and multiplayer customization
Expand Down
79 changes: 71 additions & 8 deletions src/client/lib/pages/home.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'dart:async';

import 'package:direction_guesser/widgets/leaderboard_card.dart';
import 'package:direction_guesser/widgets/points_pill.dart';
import 'package:direction_guesser/widgets/permissions_denied_card.dart';
import 'package:direction_guesser/widgets/missing_device_card.dart';
import 'package:direction_guesser/controllers/user_services.dart';
import 'package:direction_guesser/controllers/game_services.dart';
import 'package:flutter_earth_globe/globe_coordinates.dart';
import 'package:flutter_earth_globe/point.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:geolocator/geolocator.dart';
import 'package:flutter_earth_globe/flutter_earth_globe.dart';
import 'package:flutter_earth_globe/flutter_earth_globe_controller.dart';
import 'dart:math';

import '../main.dart';
import '../widgets/text_entry_pill.dart';
Expand All @@ -32,28 +33,35 @@ class _HomePageState extends State<HomePage> {
ValueNotifier<PermissionsState> permissionState =
ValueNotifier(PermissionsState.okay);
late AppLifecycleListener lifecycleListener;
String globeMode = globeDark;
late FlutterEarthGlobeController globeController =
FlutterEarthGlobeController(
isRotating: true,
rotationSpeed: 0.05,
surface: Image.asset('assets/2k_earth-night.jpg').image);
surface: Image.asset(globeMode).image);

String roomCode = "";
int playersInRoom = 0;
final random = Random();


@override
initState() {
super.initState();
//
globeMode = themeNotifier.value == ThemeMode.dark ? globeDark : globeLight;
globeController = FlutterEarthGlobeController(
isRotating: true,
rotationSpeed: 0.05,
surface: Image.asset('assets/2k_earth-night.jpg').image,
surface: Image.asset(globeMode).image,
background: Image.asset('assets/2k_stars.jpg').image
);
lifecycleListener = AppLifecycleListener(onRestart: checkSensors);
checkSensors();
}

@override

void _logout() async {
bool success = await context.read<UsersServices>().logoutUser();
if (success) {
Expand All @@ -67,7 +75,10 @@ class _HomePageState extends State<HomePage> {
void _getLobbyInfo() async {
final prefs = await SharedPreferences.getInstance();
String lobbyName = prefs.getString('currentLobby') ?? "";
await context.read<GameServices>().getLobbyInfo();
Map<String, dynamic> currentLobbyInfo = await context.read<GameServices>().getLobbyInfo();
currentGame.setLobbyUserInfo(currentLobbyInfo);
playersInRoom = currentGame.getPlayers().length;
//addUsersToGlobe();
setState(() {});
}

Expand Down Expand Up @@ -136,6 +147,7 @@ class _HomePageState extends State<HomePage> {
}
}

//TODO: Refact/ Move all sensors to separate page and check on app start and on game begin
void checkSensors() async {
await Geolocator.checkPermission().then((permission) {
if (permission == LocationPermission.deniedForever ||
Expand All @@ -155,6 +167,53 @@ class _HomePageState extends State<HomePage> {
});
}

//Add users to globe
//TODO: When Backend is capable of sending general location data of user change
// now adds randomly
void addUsersToGlobe() {
List<Point> currentPoints = globeController.points;
List<dynamic> users = currentGame.getPlayers();
//games starts and users are added
if (users.length == 0) {
for (String user in users) {
globeController.addPoint(
Point(
id: user,
label: user,
isLabelVisible: true,
coordinates: GlobeCoordinates(-90 + (random.nextDouble() * 180), -180 + (random.nextDouble() * 360)),
labelTextStyle: TextStyle(color: Colors.white)),
);
}
} else {
//remove users that are no longer in the room
for (Point point in currentPoints) {
if (!users.contains(point.label)) {
globeController.removePoint(point.id);
}
}
//add users that are not in the room
for (String user in users) {
bool found = false;
for (Point point in currentPoints) {
if (point.label == user) {
found = true;
}
}
if (!found) {
globeController.addPoint(
Point(
id: user,
label: user,
isLabelVisible: true,
coordinates: GlobeCoordinates(-90 + (random.nextDouble() * 180), -180 + (random.nextDouble() * 360)),
labelTextStyle: TextStyle(color: Colors.white)),
);
}
}
}
}

@override
Widget build(BuildContext context) {
return Container(
Expand Down Expand Up @@ -200,13 +259,13 @@ class _HomePageState extends State<HomePage> {
body: Stack(
children: [
SafeArea(
child: FlutterEarthGlobe(
child: globeEnabled ? FlutterEarthGlobe(
controller: globeController,
radius: 60,
//TODO: temporary fix globe is not centered
//will need to inspect widget tree to see why
alignment: Alignment(0, -0.4),
)),
) : Container()),
Padding(
padding: EdgeInsets.all(8),
child: Column(
Expand All @@ -215,6 +274,8 @@ class _HomePageState extends State<HomePage> {
SizedBox(height: 64),
PointsPill(points: 15827),
SizedBox(height: 32),
if (!globeEnabled)
Image.asset('assets/logo.png', height: 200),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
Expand Down Expand Up @@ -245,7 +306,9 @@ class _HomePageState extends State<HomePage> {
FilledButton.tonal(
onPressed: () => {
Navigator.pushNamed(
context, '/settings')
context, '/settings').then((_) => {
setState(() {})
})
},
child: Icon(
Icons.settings_rounded,
Expand Down
9 changes: 0 additions & 9 deletions src/client/lib/pages/score.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ class _ScorePageState extends State<ScorePage> {
setState(() {});
}

void _getLobbyInfo() async {
Map<String, dynamic> lobbyInfo = await context.read<GameServices>().getLobbyInfo();
currentGame.setLobbyUserInfo(lobbyInfo);
setState(() {});
}

void _getScore() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
Expand Down Expand Up @@ -126,9 +120,6 @@ class _ScorePageState extends State<ScorePage> {
SizedBox(height: 16),
FilledButton(
onPressed: () {
if(currentGame.isMultiplayer) {
_getLobbyInfo();
}
currentGame.incrementRound();
if (currentGame.roundNumber >= currentGame.totalRounds) {
if (currentGame.isMultiplayer) {
Expand Down
16 changes: 14 additions & 2 deletions src/client/lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class _SettingsPageState extends State<SettingsPage> {
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(height: 128),
SizedBox(height: 98),
SizedBox(
height: 280,
width: 280,
Expand Down Expand Up @@ -86,6 +86,18 @@ class _SettingsPageState extends State<SettingsPage> {
Text("Dark Mode", style: mediumStyle)
]),
Spacer(),
Row(children: [
Switch(
value: globeEnabled,
onChanged: (bool value) {
setState(() {
globeEnabled = value;
});
}),
SizedBox(width: 8),
Text("Enable 3d Globe", style: mediumStyle)
]),
Spacer(),
Row(children: [
Switch(
value: soundEnabled,
Expand All @@ -103,7 +115,7 @@ class _SettingsPageState extends State<SettingsPage> {
Spacer(),
FilledButton.tonal(
onPressed: () {
Navigator.pushNamed(context, '/home');
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back_rounded,
Expand Down
2 changes: 1 addition & 1 deletion src/client/lib/widgets/leaderboard_entry_pill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class LeaderboardEntryPill extends StatelessWidget {
]),
Column(children: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 8),
padding: EdgeInsets.symmetric(horizontal: 24),
child: Text(
formatter.format(score),
style: TextStyle(
Expand Down

0 comments on commit dc5ec44

Please sign in to comment.