summaryrefslogtreecommitdiff
path: root/lib/main_menu.dart
blob: 8eda10cc56bcb73f01536d891f00812a9f255cd9 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import 'package:flutter/material.dart';
import 'package:mini_golf_scores/scorekeeper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'dart:convert';

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

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

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

    showDialog(
      context: localContext,
      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(localContext, key);
                  },
                );
              }).toList(),
            ),
          ),
        );
      },
    );
  }

  Future<void> _loadGame(BuildContext context, String gameKey) async {
    final localContext = context;
    final prefs = await SharedPreferences.getInstance();
    final gameDataJson = prefs.getString(gameKey);
    if (gameDataJson != null) {
      final gameData = jsonDecode(gameDataJson);
      Navigator.of(localContext).pop(); // Close the selection dialog
      Navigator.push(
        localContext,
        MaterialPageRoute(
          builder: (localContext) =>
              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']),
              ),
        ),
      );
    }
  }

  void showAboutDialog(BuildContext context) async {
    final localContext = context;
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String version = packageInfo.version;

    showDialog(
      context: localContext,
      builder: (BuildContext localContext) {
        return AlertDialog(
          title: const Text('About'),
          content: SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                const Text('Mini Golf Scorekeeper App'),
                const Text('Made by Connor Frank'),
                const Text('Princeton NJ ca. April 2024'),
                const Text(''),
                Text('Version: $version'), // Display app version
                const Text(''),
                const Text('Mini Golf Scores  Copyright (C) 2024 Connor Frank'),
                const Text('This program comes with ABSOLUTELY NO WARRANTY.'),
                const Text('This is free software, and you are welcome to redistribute it under certain conditions.'),
                const Text('View \'Licenses\' in main menu for details.'),
                // Add more about info here
              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: const Text('Contact Me'),
              onPressed: () async {
                Uri link = Uri(scheme: 'mailto',
                    path: 'conjfrnk+minigolf@gmail.com',
                    query: 'subject=Mini Golf Scores App',
                );
                if (await canLaunchUrl(link)) {
                  await launchUrl(link);
                } else {
                  ScaffoldMessenger.of(localContext).showSnackBar(
                    const SnackBar(content: Text('Could not launch email client')),
                  );
                }
              },
            ),
            TextButton(
              child: const Text('OK'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final localContext = context;
    return Scaffold(
      appBar: AppBar(
        title: const Text('Mini Golf Scores'),
      ),
      body: Column(
        children: [
          const Spacer(),
          // Pushes everything below it towards the middle and bottom of the screen
          // Main action buttons centered in the middle of the screen
          ElevatedButton(
            child: const Text('Start New Game'),
            onPressed: () {
              Navigator.push(
                localContext,
                MaterialPageRoute(
                    builder: (localContext) => const ScoreKeeper(isNewGame: true)),
              );
            },
          ),
          const SizedBox(height: 16),
          // Space between the buttons
          ElevatedButton(
            child: const Text('Load Game'),
            onPressed: () => _showLoadGameDialog(localContext),
          ),
          const Spacer(),
          // This will push the bottom content to the bottom of the screen
          // Information buttons at the bottom
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            // Space out the buttons evenly in the Row
            children: [
              // About Button
              ElevatedButton(
                onPressed: () {
                  showAboutDialog(localContext);
                },
                child: const Text('About'),
              ),
              // License Button
              ElevatedButton(
                onPressed: () async {
                  PackageInfo packageInfo = await PackageInfo.fromPlatform();
                  String version = packageInfo.version;
                  // Action for License button
                  showLicensePage(
                    context: localContext,
                    applicationName: 'Mini Golf Scores',
                    applicationVersion: version,
                    applicationLegalese: '© 2024 Connor Frank'
                  );
                },
                child: const Text('License'),
              ),
              // GitHub Link Button
              ElevatedButton(
                onPressed: () async {
                  Uri link = Uri(scheme: 'https',
                    host: 'github.com',
                    path: '/conjfrnk/mini-golf-scores');
                  if (await canLaunchUrl(link)) {
                    await launchUrl(link);
                  } else {
                    ScaffoldMessenger.of(localContext).showSnackBar(
                      const SnackBar(content: Text('Could not open GitHub')),
                    );
                  }
                },
                child: const Text(
                    'GitHub'), // You can use an Icon instead: Icon(Icons.link)
              ),
            ],
          ),
          const SizedBox(height: 20),
          // Ensures there's space at the very bottom
        ],
      ),
    );
  }
}