| 1 | import 'package:file_picker/file_picker.dart'; |
| 2 | import 'package:flutter/material.dart'; |
| 3 | |
| 4 | import '../emulator/emulator_screen.dart'; |
| 5 | import 'game_library.dart'; |
| 6 | |
| 7 | /// The list of the user's games, with buttons to add one and to manage |
| 8 | /// the optional BIOS. |
| 9 | class HomeScreen extends StatefulWidget |
| 10 | const HomeScreen({super |
| 11 | |
| 12 | final GameLibrary library; |
| 13 | |
| 14 | /// Lets the user choose a file; returns its path and name, or null if |
| 15 | /// cancelled. Defaults to the system file picker. |
| 16 | final Future<({String |
| 17 | |
| 18 | @override |
| 19 | State<HomeScreen> createState |
| 20 | } |
| 21 | |
| 22 | class _HomeScreenState extends State |
| 23 | late Future<List |
| 24 | bool _busy = false; |
| 25 | |
| 26 | void _reload() { |
| 27 | final games = widget.library. |
| 28 | setState(() { |
| 29 | _games = games; |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | Future<({String path, String |
| 34 | final custom = widget.pickFile; |
| 35 | if (custom != null) return |
| 36 | // Any file type: Android has no MIME type for .gba, so an extension |
| 37 | // filter would grey out the user's ROMs. The contents are checked |
| 38 | // after picking instead. |
| 39 | final result = await FilePicker |
| 40 | final file = result?.files |
| 41 | final path = file?.path |
| 42 | if (file == null || path == |
| 43 | return (path: path, name: |
| 44 | } |
| 45 | |
| 46 | Future<void> _addGame |
| 47 | final picked = await _pick |
| 48 | if (picked == null || ! |
| 49 | setState(() => _busy = true |
| 50 | try { |
| 51 | final game = await widget. |
| 52 | _reload(); |
| 53 | _show('Added ${game. |
| 54 | } on LibraryException catch (error) { |
| 55 | _show(error.message); |
| 56 | } finally { |
| 57 | if (mounted) setState(() => _busy = |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Future<void> _importBios |
| 62 | final picked = await _pick |
| 63 | if (picked == null || ! |
| 64 | try { |
| 65 | await widget.library.importBios(picked |
| 66 | setState(() {}); |
| 67 | _show('BIOS imported. Games will use it from now on.'); |
| 68 | } on LibraryException catch (error) { |
| 69 | _show(error.message); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | Future<void> _removeBios |
| 74 | await widget.library.removeBios() |
| 75 | setState(() {}); |
| 76 | _show('Using the built-in BIOS replacement.'); |
| 77 | } |
| 78 | |
| 79 | Future<void> _play |
| 80 | await Navigator.of(context) |
| 81 | MaterialPageRoute<void>( |
| 82 | builder: (_) => EmulatorScreen( |
| 83 | romPath: game.romPath, |
| 84 | savePath: widget.library.savePathFor(game) |
| 85 | biosPath: widget.library.biosPath, |
| 86 | ), |
| 87 | ), |
| 88 | ); |
| 89 | _reload(); // A save may have appeared. |
| 90 | } |
| 91 | |
| 92 | Future<void> _delete |
| 93 | var deleteSave = false; |
| 94 | final confirmed = await showDialog |
| 95 | context: context, |
| 96 | builder: (context) => StatefulBuilder( |
| 97 | builder: (context, setDialogState) => AlertDialog |
| 98 | title: Text('Remove ${game |
| 99 | content: Column( |
| 100 | mainAxisSize: MainAxisSize.min, |
| 101 | crossAxisAlignment: CrossAxisAlignment.start, |
| 102 | children: [ |
| 103 | const Text('The copy of the ROM in this app will be deleted.') |
| 104 | if (game.hasSave) |
| 105 | CheckboxListTile( |
| 106 | contentPadding: EdgeInsets.zero, |
| 107 | value: deleteSave, |
| 108 | onChanged: (value) => |
| 109 | setDialogState(() => deleteSave = value ?? |
| 110 | title: const Text( |
| 111 | ), |
| 112 | ], |
| 113 | ), |
| 114 | actions: [ |
| 115 | TextButton( |
| 116 | onPressed: () => Navigator.pop |
| 117 | child: const Text( |
| 118 | ), |
| 119 | FilledButton( |
| 120 | onPressed: () => Navigator.pop |
| 121 | child: const Text( |
| 122 | ), |
| 123 | ], |
| 124 | ), |
| 125 | ), |
| 126 | ); |
| 127 | if (confirmed != true) return |
| 128 | await widget.library.delete(game |
| 129 | _reload(); |
| 130 | } |
| 131 | |
| 132 | void _show(String |
| 133 | if (!mounted) return; |
| 134 | ScaffoldMessenger.of( |
| 135 | context, |
| 136 | ).showSnackBar(SnackBar(content: |
| 137 | } |
| 138 | |
| 139 | @override |
| 140 | Widget build(BuildContext |
| 141 | final hasBios = widget.library. |
| 142 | return Scaffold( |
| 143 | appBar: AppBar( |
| 144 | title: const Text( |
| 145 | actions: [ |
| 146 | PopupMenuButton<String>( |
| 147 | onSelected: (choice) => switch (choice) { |
| 148 | 'bios' => _importBios(), |
| 149 | 'no-bios' => _removeBios(), |
| 150 | _ => _showAbout(), |
| 151 | }, |
| 152 | itemBuilder: (context) => [ |
| 153 | if (hasBios) |
| 154 | const PopupMenuItem( |
| 155 | value: 'no-bios', |
| 156 | child: Text('Use built-in BIOS replacement'), |
| 157 | ) |
| 158 | else |
| 159 | const PopupMenuItem( |
| 160 | value: 'bios', |
| 161 | child: Text('Import a BIOS file (optional)'), |
| 162 | ), |
| 163 | const PopupMenuItem(value: |
| 164 | ], |
| 165 | ), |
| 166 | ], |
| 167 | ), |
| 168 | floatingActionButton: FloatingActionButton.extended( |
| 169 | onPressed: _busy ? null : _addGame, |
| 170 | icon: _busy |
| 171 | ? const SizedBox. |
| 172 | dimension: 20, |
| 173 | child: CircularProgressIndicator(strokeWidth: 2 |
| 174 | ) |
| 175 | : const Icon( |
| 176 | label: const Text( |
| 177 | ), |
| 178 | body: FutureBuilder<List< |
| 179 | future: _games, |
| 180 | builder: (context, snapshot) { |
| 181 | final games = snapshot.data; |
| 182 | if (games == null) { |
| 183 | return const Center |
| 184 | } |
| 185 | if (games.isEmpty) return const |
| 186 | return ListView.builder |
| 187 | padding: const EdgeInsets. |
| 188 | itemCount: games.length, |
| 189 | itemBuilder: (context, index) { |
| 190 | final game = games[index]; |
| 191 | return ListTile( |
| 192 | leading: const Icon( |
| 193 | title: Text(game.title), |
| 194 | subtitle: Text( |
| 195 | [ |
| 196 | if (game.gameCode.isNotEmpty) game.gameCode, |
| 197 | game.fileName, |
| 198 | if (game.hasSave) 'saved', |
| 199 | ].join(' · '), |
| 200 | ), |
| 201 | onTap: () => _play(game), |
| 202 | onLongPress: () => _delete(game), |
| 203 | trailing: IconButton( |
| 204 | tooltip: 'Remove', |
| 205 | icon: const Icon( |
| 206 | onPressed: () => _delete(game), |
| 207 | ), |
| 208 | ); |
| 209 | }, |
| 210 | ); |
| 211 | }, |
| 212 | ), |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | void _showAbout() { |
| 217 | showAboutDialog( |
| 218 | context: context, |
| 219 | applicationName: 'SplashyGBA', |
| 220 | children: const [ |
| 221 | Text( |
| 222 | 'A Game Boy Advance emulator. No games or BIOS files are ' |
| 223 | 'included: add ROM files you own. Without a BIOS file, a built-in ' |
| 224 | 'replacement is used.\n\n' |
| 225 | 'Not emulated yet: sound, the link cable, and the clock and ' |
| 226 | 'sensors of some cartridges.', |
| 227 | ), |
| 228 | ], |
| 229 | ); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | class _EmptyLibrary extends StatelessWidget |
| 234 | const _EmptyLibrary(); |
| 235 | |
| 236 | @override |
| 237 | Widget build(BuildContext |
| 238 | final textTheme = Theme. |
| 239 | return Center( |
| 240 | child: Padding( |
| 241 | padding: const EdgeInsets. |
| 242 | child: Column( |
| 243 | mainAxisSize: MainAxisSize.min, |
| 244 | children: [ |
| 245 | Icon( |
| 246 | Icons.sports_esports_outlined, |
| 247 | size: 56, |
| 248 | color: Theme.of(context) |
| 249 | ), |
| 250 | const SizedBox(height: |
| 251 | Text('No games yet', style: textTheme |
| 252 | const SizedBox(height: |
| 253 | Text( |
| 254 | 'Tap "Add game" and choose a .gba file you own. No games or ' |
| 255 | 'BIOS files come with this app.', |
| 256 | style: textTheme.bodyLarge, |
| 257 | textAlign: TextAlign.center, |
| 258 | ), |
| 259 | ], |
| 260 | ), |
| 261 | ), |
| 262 | ); |
| 263 | } |
| 264 | } |