-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: launch recovery mode on bootstrap failure
- Loading branch information
1 parent
a9d551d
commit c062b3d
Showing
11 changed files
with
234 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
|
||
abstract class RecoveryActionTile extends StatelessWidget { | ||
final String title; | ||
final String description; | ||
final bool showConfirmDialog; | ||
|
||
const RecoveryActionTile({ | ||
required this.title, | ||
required this.description, | ||
this.showConfirmDialog = false, | ||
super.key, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ListTile( | ||
title: Text(title), | ||
subtitle: Text(description), | ||
onTap: () async => _action(context), | ||
); | ||
} | ||
|
||
Future<void> _action(BuildContext context) async { | ||
// ignore: invalid_use_of_visible_for_testing_member | ||
Hive.resetAdapters(); | ||
await Hive.close(); | ||
|
||
try { | ||
if (showConfirmDialog) { | ||
final execute = await confirm(context); | ||
if (execute) { | ||
await action(context); | ||
success(context); | ||
} | ||
} else { | ||
await action(context); | ||
success(context); | ||
} | ||
} catch (error, stack) { | ||
failure(context, error, stack); | ||
} | ||
} | ||
|
||
Future<void> action(BuildContext context); | ||
|
||
void success(BuildContext context) { | ||
showDialog( | ||
context: context, | ||
builder: (context) => const SimpleDialog( | ||
title: Text('Success'), | ||
children: [ | ||
Text('Action Completed Successfully'), | ||
], | ||
contentPadding: EdgeInsets.only( | ||
left: 24.0, | ||
right: 24.0, | ||
top: 12.0, | ||
bottom: 24.0, | ||
), | ||
), | ||
); | ||
} | ||
|
||
void failure(BuildContext context, dynamic error, StackTrace stack) { | ||
showDialog( | ||
context: context, | ||
builder: (context) => SimpleDialog( | ||
title: const Text('Error'), | ||
children: [ | ||
Text(error.toString()), | ||
Text(stack.toString()), | ||
], | ||
contentPadding: const EdgeInsets.symmetric( | ||
horizontal: 24.0, | ||
vertical: 12.0, | ||
), | ||
), | ||
); | ||
} | ||
|
||
Future<bool> confirm(BuildContext context) async { | ||
bool result = false; | ||
|
||
await showDialog( | ||
context: context, | ||
builder: (context) => AlertDialog( | ||
title: const Text('Are You Sure?'), | ||
content: const Text('Are you sure you want to do this action?'), | ||
actions: [ | ||
TextButton( | ||
onPressed: () { | ||
result = true; | ||
Navigator.of(context).pop(); | ||
}, | ||
child: const Text('Confirm'), | ||
) | ||
], | ||
), | ||
); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:lunasea/main.dart'; | ||
import 'package:lunasea/system/recovery_mode/action_tile.dart'; | ||
|
||
class BootstrapTile extends RecoveryActionTile { | ||
const BootstrapTile({ | ||
super.key, | ||
}) : super( | ||
title: 'Bootstrap LunaSea', | ||
description: 'Run the bootstrap process and show any errors', | ||
); | ||
|
||
@override | ||
Future<void> action(BuildContext context) async { | ||
await bootstrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:lunasea/database/database.dart'; | ||
import 'package:lunasea/system/recovery_mode/action_tile.dart'; | ||
|
||
class ClearDatabaseTile extends RecoveryActionTile { | ||
const ClearDatabaseTile({ | ||
super.key, | ||
}) : super( | ||
title: 'Clear Database', | ||
description: 'Clear all configured settings and modules', | ||
showConfirmDialog: true, | ||
); | ||
|
||
@override | ||
Future<void> action(BuildContext context) async { | ||
await LunaDatabase().nuke(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:lunasea/system/recovery_mode/actions/clear_database.dart'; | ||
import 'package:lunasea/system/recovery_mode/actions/bootstrap.dart'; | ||
|
||
class LunaRecoveryMode extends StatelessWidget { | ||
const LunaRecoveryMode({ | ||
super.key, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: _home(), | ||
darkTheme: ThemeData(brightness: Brightness.dark), | ||
themeMode: ThemeMode.dark, | ||
); | ||
} | ||
|
||
Widget _home() { | ||
return Scaffold( | ||
appBar: _appBar(), | ||
body: _body(), | ||
); | ||
} | ||
|
||
PreferredSizeWidget _appBar() { | ||
return AppBar( | ||
title: const Text('Recovery Mode'), | ||
); | ||
} | ||
|
||
Widget _motd() { | ||
return const Padding( | ||
child: Center( | ||
child: Text( | ||
'To exit please fully close and restart the application.', | ||
style: TextStyle( | ||
color: Colors.red, | ||
fontWeight: FontWeight.w600, | ||
), | ||
), | ||
), | ||
padding: EdgeInsets.all(12.0), | ||
); | ||
} | ||
|
||
Widget _body() { | ||
const actions = [ | ||
BootstrapTile(), | ||
ClearDatabaseTile(), | ||
]; | ||
|
||
return ListView.separated( | ||
itemCount: actions.length + 1, | ||
itemBuilder: (context, idx) { | ||
if (idx == 0) return _motd(); | ||
return actions[idx - 1]; | ||
}, | ||
separatorBuilder: (context, idx) { | ||
if (idx == 0) return const SizedBox(); | ||
return const Divider(); | ||
}, | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.