Files
lunasea/lib/core/database/database.dart
Jagandeep Brar c6a67092bc [Release] v5.0.0 (50000002) (#316)
NEW
- [Dashboard] "Home" has been renamed to "Dashboard"
- [Radarr/Catalogue] If no movie is found in the search query, given an option to search to add the movie
- [Radarr/Discover] Add the ability to discover movies from your import lists & Radarr recommendations
- [Radarr/Releases] Ability to set sorting direction, category, and filtering method
- [Settings/Radarr] Ability to toggle on or off Radarr suggestions in the discover page

TWEAKS
- [Drawer] Removed the option to use categories/folders in the drawer
- [Settings/Config] Sort configuration options within modules for cleaner isolation and easier expansion for future configuration options
- [Settings/Profiles] Renaming a profile now checks for an existing profile with the same name within the prompt
- [Settings/Profiles] Adding a profile now checks for an existing profile with the same name within the prompt
- [Settings/Profiles] The delete profile prompt now hides the currently enabled profile, and shows a snackbar if only one profile exists
- [Settings/Wake on LAN] Allow clearing the broadcast and MAC addresses to empty values

FIXES
- [Radarr] Support for Radarr v3.1.0+ (nightly builds)
- [Radarr/Add] Instantly show a movie as addable again if removed right after adding it
- [Radarr/Cast] If a job or character name is empty, show a dash instead of an empty space
- [Radarr/Catalogue] Instantly remove the movie tile when removing a movie
- [Radarr/Edit] Padding around the "Update Movie" button was incorrect
- [Toasts] In some cases, the error was not being passed through to the error viewer
- [UI/UX] Upstream framework support for basic mouse/trackpad highlight and scrolling support
- [UI/UX] Tapping the AppBar to scroll back in a nested route would scroll back the parent route as well
- [UI/UX] Use the recommended safe area padding at the bottom of lists for fullscreen devices
2021-02-20 11:56:18 -06:00

84 lines
3.0 KiB
Dart

import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:lunasea/core.dart';
import 'package:lunasea/modules.dart';
class Database {
static const String _DATABASE_PATH = 'database';
/// Initialize the database.
///
/// - Initializes Hive database path
/// - Register all adapters
/// - Open all boxes
/// - Set default database state of necessary
Future<void> initialize() async {
await Hive.initFlutter(_DATABASE_PATH);
_registerAdapters();
await _openBoxes();
if(profilesBox.keys.length == 0) setDefaults(clearAlerts: true);
}
/// Deinitialize the database by closing all open hive boxes.
Future<void> deinitialize() async => await Hive.close();
/// Registers all necessary object adapters for Hive.
void _registerAdapters() {
//Core
Hive.registerAdapter(IndexerHiveObjectAdapter());
Hive.registerAdapter(ProfileHiveObjectAdapter());
//General
LunaDatabase().registerAdapters();
DashboardDatabase().registerAdapters();
SearchDatabase().registerAdapters();
//Automation
LidarrDatabase().registerAdapters();
RadarrDatabase().registerAdapters();
SonarrDatabase().registerAdapters();
//Clients
NZBGetDatabase().registerAdapters();
SABnzbdDatabase().registerAdapters();
//Monitoring
TautulliDatabase().registerAdapters();
}
/// Open all Hive boxes for reading.
Future<void> _openBoxes() async {
await Hive.openBox('lunasea');
await Hive.openBox('alerts');
await Hive.openBox<IndexerHiveObject>('indexers');
await Hive.openBox<ProfileHiveObject>('profiles');
}
/// Set the default state for all hive boxes.
void setDefaults({ bool clearAlerts = false }) {
//Clear all the boxes
clearAllBoxes(clearAlerts: clearAlerts);
//Set default profile & enabled profile
profilesBox.put('default', ProfileHiveObject.empty());
lunaSeaBox.put(LunaDatabaseValue.ENABLED_PROFILE.key, 'default');
}
//Get boxes
static Box get lunaSeaBox => Hive.box('lunasea');
static Box get alertsBox => Hive.box('alerts');
static Box get profilesBox => Hive.box<ProfileHiveObject>('profiles');
static Box get indexersBox => Hive.box<IndexerHiveObject>('indexers');
//Clear boxes
void clearLunaSeaBox() => lunaSeaBox.deleteAll(lunaSeaBox.keys);
void clearProfilesBox() => profilesBox.deleteAll(profilesBox.keys);
void clearIndexersBox() => indexersBox.deleteAll(indexersBox.keys);
void clearAlertsBox() => alertsBox.deleteAll(alertsBox.keys);
void clearAllBoxes({ bool clearAlerts = false }) {
clearLunaSeaBox();
clearProfilesBox();
clearIndexersBox();
if(clearAlerts) clearAlertsBox();
}
//Profile values
static String get currentProfile => lunaSeaBox.get('profile');
static ProfileHiveObject get currentProfileObject => profilesBox.get(currentProfile);
}