Skip to content

Commit

Permalink
fix(database): validate enabled profile exists on restore
Browse files Browse the repository at this point in the history
  • Loading branch information
JagandeepBrar committed Jun 15, 2022
1 parent 910bb43 commit d8506ee
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 44 deletions.
4 changes: 2 additions & 2 deletions lib/database/box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum LunaBox<T> {
alerts<dynamic>('alerts'),
externalModules<LunaExternalModule>('external_modules'),
indexers<LunaIndexer>('indexers'),
logs<LunaLogHiveObject>('logs'),
logs<LunaLog>('logs'),
lunasea<dynamic>('lunasea'),
profiles<LunaProfile>('profiles');

Expand Down Expand Up @@ -92,7 +92,7 @@ extension LunaBoxExtension on LunaBox {
List<Map<String, dynamic>> export() {
try {
return _instance.keys
.map<Map<String, dynamic>>((k) => _instance.get(k)!.toMap())
.map<Map<String, dynamic>>((k) => _instance.get(k)!.toJson())
.toList();
} catch (error, stack) {
LunaLogger().error('Failed to export LunaBox', error, stack);
Expand Down
12 changes: 9 additions & 3 deletions lib/database/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ class LunaConfig {
await LunaDatabase().clear();

try {
Map config = json.decode(data);
Map<String, dynamic> config = json.decode(data);

_setProfiles(config[LunaBox.profiles.key]);
_setIndexers(config[LunaBox.indexers.key]);
_setExternalModules(config[LunaBox.externalModules.key]);
for (final table in LunaTable.values) table.import(config[table.key]);

if (!LunaProfile.list.contains(LunaSeaDatabase.ENABLED_PROFILE.read())) {
LunaSeaDatabase.ENABLED_PROFILE.update(LunaProfile.list[0]);
}
} catch (error, stack) {
LunaLogger().error(
'Failed to import configuration, resetting to default',
Expand All @@ -41,8 +46,9 @@ class LunaConfig {
if (data == null) return;

for (final item in data) {
final key = item['key'];
final obj = LunaProfile.fromJson((item as Map).cast<String, dynamic>());
final content = (item as Map).cast<String, dynamic>();
final key = content['key'] ?? 'default';
final obj = LunaProfile.fromJson(content);
LunaBox.profiles.update(key, obj);
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/database/models/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'package:stack_trace/stack_trace.dart';

part 'log.g.dart';

@HiveType(typeId: 23, adapterName: 'LunaLogHiveObjectAdapter')
class LunaLogHiveObject extends HiveObject {
@HiveType(typeId: 23, adapterName: 'LunaLogAdapter')
class LunaLog extends HiveObject {
@HiveField(0)
final int timestamp;
@HiveField(1)
Expand All @@ -21,7 +21,7 @@ class LunaLogHiveObject extends HiveObject {
@HiveField(6)
final String? stackTrace;

LunaLogHiveObject({
LunaLog({
required this.timestamp,
required this.type,
this.className,
Expand All @@ -31,21 +31,21 @@ class LunaLogHiveObject extends HiveObject {
this.stackTrace,
});

factory LunaLogHiveObject.withMessage({
factory LunaLog.withMessage({
required LunaLogType type,
required String message,
String? className,
String? methodName,
}) {
int timestamp = DateTime.now().millisecondsSinceEpoch;
return LunaLogHiveObject(
return LunaLog(
timestamp: timestamp,
type: type,
message: message,
);
}

factory LunaLogHiveObject.withError({
factory LunaLog.withError({
required LunaLogType type,
required String message,
required dynamic error,
Expand All @@ -60,7 +60,7 @@ class LunaLogHiveObject extends HiveObject {
_className = trace?.frames[0].uri.toString();
_methodName = trace?.frames[0].member.toString();
}
return LunaLogHiveObject(
return LunaLog(
timestamp: timestamp,
type: type,
className: className ?? _className,
Expand Down
22 changes: 13 additions & 9 deletions lib/database/models/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LunaProfile extends HiveObject {

@JsonKey()
@HiveField(26)
Map lidarrHeaders;
Map<String, dynamic> lidarrHeaders;

Map<String, dynamic> getLidarr() {
return {
Expand All @@ -59,7 +59,7 @@ class LunaProfile extends HiveObject {
String radarrKey;

@HiveField(27)
Map radarrHeaders;
Map<String, dynamic> radarrHeaders;

Map<String, dynamic> getRadarr() {
return {
Expand All @@ -84,7 +84,7 @@ class LunaProfile extends HiveObject {

@JsonKey()
@HiveField(28)
Map sonarrHeaders;
Map<String, dynamic> sonarrHeaders;

Map<String, dynamic> getSonarr() {
return {
Expand All @@ -110,7 +110,7 @@ class LunaProfile extends HiveObject {
@JsonKey()
@JsonKey()
@HiveField(29)
Map sabnzbdHeaders;
Map<String, dynamic> sabnzbdHeaders;

Map<String, dynamic> getSABnzbd() {
return {
Expand Down Expand Up @@ -139,7 +139,7 @@ class LunaProfile extends HiveObject {

@JsonKey()
@HiveField(30)
Map nzbgetHeaders;
Map<String, dynamic> nzbgetHeaders;

Map<String, dynamic> getNZBGet() {
return {
Expand Down Expand Up @@ -185,7 +185,7 @@ class LunaProfile extends HiveObject {

@JsonKey()
@HiveField(35)
Map tautulliHeaders;
Map<String, dynamic> tautulliHeaders;

Map<String, dynamic> getTautulli() {
return {
Expand All @@ -210,7 +210,7 @@ class LunaProfile extends HiveObject {

@JsonKey()
@HiveField(43)
Map overseerrHeaders;
Map<String, dynamic> overseerrHeaders;

Map<String, dynamic> getOverseerr() {
return {
Expand Down Expand Up @@ -267,14 +267,18 @@ class LunaProfile extends HiveObject {
@override
String toString() => json.encode(this.toJson());

Map<String, dynamic> toJson() => _$LunaProfileToJson(this);
Map<String, dynamic> toJson() {
final json = _$LunaProfileToJson(this);
json['key'] = key.toString();
return json;
}

factory LunaProfile.fromJson(Map<String, dynamic> json) {
return _$LunaProfileFromJson(json);
}

factory LunaProfile.clone(LunaProfile profile) {
return LunaProfile.fromJson(profile.toJson());
return LunaProfile.fromJson(profile.toJson().cast<String, dynamic>());
}

factory LunaProfile.get(String key) {
Expand Down
2 changes: 1 addition & 1 deletion lib/database/tables/lunasea.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ enum LunaSeaDatabase<T> with LunaTableMixin<T> {
Hive.registerAdapter(LunaExternalModuleAdapter());
Hive.registerAdapter(LunaIndexerAdapter());
Hive.registerAdapter(LunaProfileAdapter());
Hive.registerAdapter(LunaLogHiveObjectAdapter());
Hive.registerAdapter(LunaLogAdapter());
Hive.registerAdapter(LunaIndexerIconAdapter());
Hive.registerAdapter(LunaLogTypeAdapter());
Hive.registerAdapter(LunaModuleAdapter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class _State extends State<_Widget> with LunaScrollControllerMixin {

Widget _body() {
return LunaBox.logs.watch(builder: (context, _) {
List<LunaLogHiveObject> logs = filter();
List<LunaLog> logs = filter();
if (logs.isEmpty) {
return LunaMessage.goBack(
context: context,
Expand All @@ -86,8 +86,8 @@ class _State extends State<_Widget> with LunaScrollControllerMixin {
});
}

List<LunaLogHiveObject> filter() {
List<LunaLogHiveObject> logs;
List<LunaLog> filter() {
List<LunaLog> logs;
const box = LunaBox.logs;

switch (widget.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:lunasea/core.dart';
import 'package:lunasea/database/models/log.dart';

class SettingsSystemLogTile extends StatelessWidget {
final LunaLogHiveObject log;
final LunaLog log;

const SettingsSystemLogTile({
Key? key,
Expand Down
10 changes: 5 additions & 5 deletions lib/system/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LunaLogger {

Future<void> _compact([int count = 50]) async {
if (LunaBox.logs.data.length <= count) return;
List<LunaLogHiveObject> logs = LunaBox.logs.data.toList();
List<LunaLog> logs = LunaBox.logs.data.toList();
logs.sort((a, b) => (b.timestamp).compareTo(a.timestamp));
logs.skip(count).forEach((log) => log.delete());
}
Expand All @@ -35,15 +35,15 @@ class LunaLogger {
Future<void> clear() async => LunaBox.logs.clear();

void debug(String message) {
LunaLogHiveObject log = LunaLogHiveObject.withMessage(
LunaLog log = LunaLog.withMessage(
type: LunaLogType.DEBUG,
message: message,
);
LunaBox.logs.create(log);
}

void warning(String message, [String? className, String? methodName]) {
LunaLogHiveObject log = LunaLogHiveObject.withMessage(
LunaLog log = LunaLog.withMessage(
type: LunaLogType.WARNING,
message: message,
className: className,
Expand All @@ -54,7 +54,7 @@ class LunaLogger {

void error(String message, dynamic error, StackTrace? stackTrace) {
if (error is! NetworkImageLoadException) {
LunaLogHiveObject log = LunaLogHiveObject.withError(
LunaLog log = LunaLog.withError(
type: LunaLogType.ERROR,
message: message,
error: error,
Expand All @@ -71,7 +71,7 @@ class LunaLogger {

void critical(dynamic error, StackTrace stackTrace) {
if (error is! NetworkImageLoadException) {
LunaLogHiveObject log = LunaLogHiveObject.withError(
LunaLog log = LunaLog.withError(
type: LunaLogType.CRITICAL,
message: error?.toString() ?? LunaUI.TEXT_EMDASH,
error: error,
Expand Down
28 changes: 15 additions & 13 deletions lib/widgets/ui/scaffold.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:lunasea/core.dart';
import 'package:lunasea/database/tables/lunasea.dart';
import 'package:lunasea/modules.dart';
import 'package:lunasea/system/platform.dart';

class LunaScaffold extends StatelessWidget {
final GlobalKey<ScaffoldState>? scaffoldKey;
Expand Down Expand Up @@ -31,21 +33,21 @@ class LunaScaffold extends StatelessWidget {

@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
final hasDrawer = scaffoldKey?.currentState?.hasDrawer ?? false;
final isOpen = scaffoldKey?.currentState?.isDrawerOpen ?? false;
final state = scaffoldKey?.currentState;
final hasDrawer = state?.hasDrawer ?? false;

if (hasDrawer) {
if (isOpen) return true;
scaffoldKey?.currentState?.openDrawer();
if (hasDrawer && LunaPlatform.isAndroid) {
return WillPopScope(
onWillPop: () async {
if (state!.isDrawerOpen) return true;
state.openDrawer();
return false;
}
},
child: scaffold,
);
}

return true;
},
child: scaffold,
);
return scaffold;
}

Widget get scaffold {
Expand Down

0 comments on commit d8506ee

Please sign in to comment.