Files
lunasea/lib/core/database/database.dart
Jagandeep Brar 7298f742cf [Release] v2.2.0 (220003) (#208)
* [Settings] Added toggle for Sonarr v3
* [UI] Removed letter spacing, use system default
* [Android] Enable billing permission
* [iOS] Enabled StoreKit API
* [Issues] Updated templates
* [Issues] Disable blank issues
* [Settings] System/Resources tile for Feedback
* [-rr/Add] Autofocus search bar
* [NZBGet] Option to use basic auth instead of URL-encoded auth
* [SABnzbd/Statistics] Show statistics for individual servers
* [Profiles] Added rapid-switching in specific module app bars
* [Settings] Added subheaders to explain some things
* [Pubspec] Updated version
* [TestFlight] 2.2.0 (220001)
* [Git] Ignore appbundle files
* [General] Added Donations IAPs, Added Licenses, Close Database Cleanly on Exit (#207)
* [Packages] Added in_app_purchase package
* [BIOS] Close Database on Clean Exit
* [Settings] Added Donations & Licenses Pages
* [Packages] Added flare/rive package
* [README] Removed spacing between shields
* [Donations/Licenses] Added Donations & Licenses Pages
* [Donations] Show localized price
* [Settings] Tweaked subtitles a little
* [IAPs] Fixed initialization on Android
2020-05-03 20:55:57 -05:00

66 lines
2.2 KiB
Dart

// Imports
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:lunasea/modules.dart';
import './adapters.dart';
// 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());
//Modules - General
LunaSeaDatabase.registerAdapters();
HomeDatabase.registerAdapters();
//Modules - Automation
LidarrDatabase.registerAdapters();
RadarrDatabase.registerAdapters();
SonarrDatabase.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);
}