summaryrefslogtreecommitdiff
path: root/lib/main_menu.dart
blob: f9fa2672b362d32bb74c3f750e9aad3de0f46e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import 'package:flutter/material.dart';
import 'package:mini_golf/scorekeeper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';

class MainMenu extends StatelessWidget {
  const MainMenu({super.key});

  void _showLoadGameDialog(BuildContext context) async {
    final prefs = await SharedPreferences.getInstance();
    final gameKeys = prefs.getKeys().where((key) => key.startsWith('game_')).toList();

    if (gameKeys.isEmpty) {
      // No games found, show a SnackBar instead
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('No saved games found.')),
      );
      return;
    }

    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('Select a Game to Load'),
          content: SingleChildScrollView(
            child: ListBody(
              children: gameKeys.map((key) {
                return ListTile(
                  title: Text(key.substring(5).substring(0,16).replaceAll("T", " ")),
                  onTap: () {
                    _loadGame(context, key);
                  },
                );
              }).toList(),
            ),
          ),
        );
      },
    );
  }

  Future<void> _loadGame(BuildContext context, String gameKey) async {
    final prefs = await SharedPreferences.getInstance();
    final gameDataJson = prefs.getString(gameKey);
    if (gameDataJson != null) {
      final gameData = jsonDecode(gameDataJson);
      // Assuming gameData structure. Convert data types as necessary.
      Navigator.of(context).pop(); // Close the selection dialog
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => ScoreKeeper(
            isNewGame: false,
            playerNames: List<String>.from(gameData['playerNames']),
            pars: Map.from(gameData['pars']).map((k, v) => MapEntry(int.parse(k), v)),
            scores: Map.from(gameData['scores']).map((k, v) => MapEntry(int.parse(k), List<int>.from(v))),
            gameCreationTime: DateTime.parse(gameData['creationTime']),
          ),
        ),
      );
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Mini Golf Main Menu'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ElevatedButton(
              child: const Text('Start New Game'),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => const ScoreKeeper(isNewGame: true)),
                );
              },
            ),
            const SizedBox(height: 16), // Space between the buttons
            ElevatedButton(
              child: const Text('Load Game'),
              onPressed: () => _showLoadGameDialog(context),
            ),
          ],
        ),
      ),
    );
  }
}