Files
lunasea/lib/core/database/database.dart
Jagandeep Brar 98648ba858 [Release] v3.2.0+320003 (#230)
* [Database] Added client identifier (for later use)
* [UUID, Cleanup] Added UUID generator, follow guidelines for relative exports
* [Imports] Only show necessary imports for modules.dart import
* [Settings] Added stub Tautulli config page (hidden via module flags)
* [-arr/Details] Fix crashes related to setting state on unmounted widget
* [Radarr/Add] Fix crash when a movie does not have a minimum availability status
2020-07-29 15:49:38 -05:00

79 lines
2.5 KiB
Dart

// Imports
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import './adapters.dart';
import 'package:lunasea/modules.dart' show
HomeDatabase,
SearchDatabase,
SettingsDatabase,
LidarrDatabase,
RadarrDatabase,
SonarrDatabase,
NZBGetDatabase,
SABnzbdDatabase;
// Exports
export 'package:hive/hive.dart';
export 'package:hive_flutter/hive_flutter.dart';
class Database {
Database._();
static Future<void> initialize() async {
await Hive.initFlutter('database');
_registerAdapters();
await _openBoxes();
if(profilesBox.keys.length == 0) setDefaults();
}
static Future<void> deinitialize() async => await Hive.close();
static void _registerAdapters() {
//Core
Hive.registerAdapter(IndexerHiveObjectAdapter());
Hive.registerAdapter(ProfileHiveObjectAdapter());
//General
LunaSeaDatabase.registerAdapters();
HomeDatabase.registerAdapters();
SearchDatabase.registerAdapters();
SettingsDatabase.registerAdapters();
//Automation
LidarrDatabase.registerAdapters();
RadarrDatabase.registerAdapters();
SonarrDatabase.registerAdapters();
//Clients
NZBGetDatabase.registerAdapters();
SABnzbdDatabase.registerAdapters();
}
static Future<void> _openBoxes() async {
await Hive.openBox('lunasea');
await Hive.openBox<IndexerHiveObject>('indexers');
await Hive.openBox<ProfileHiveObject>('profiles');
}
static void setDefaults() {
//Clear all the boxes
clearLunaSeaBox();
clearProfilesBox();
clearIndexersBox();
//Set default profile & enabled profile
profilesBox.put('default', ProfileHiveObject.empty());
lunaSeaBox.put(LunaSeaDatabaseValue.ENABLED_PROFILE.key, 'default');
}
//Get boxes
static Box get lunaSeaBox => Hive.box('lunasea');
static Box get profilesBox => Hive.box<ProfileHiveObject>('profiles');
static Box get indexersBox => Hive.box<IndexerHiveObject>('indexers');
//Clear boxes
static void clearLunaSeaBox() => lunaSeaBox.deleteAll(lunaSeaBox.keys);
static void clearProfilesBox() => profilesBox.deleteAll(profilesBox.keys);
static void clearIndexersBox() => indexersBox.deleteAll(indexersBox.keys);
//Profile values
static String get currentProfile => lunaSeaBox.get('profile');
static ProfileHiveObject get currentProfileObject => profilesBox.get(currentProfile);
}