The SettingsService manages application-wide settings including download paths, portal sessions, and user preferences. It uses persistent storage to maintain settings across app restarts.
Initialization
init
Initializes the settings service with persistent storage.
static Future<SettingsService> init(String databaseDirectory)
The directory path where settings database files will be stored.
A fully initialized settings service instance with loaded settings.
Example:
final settingsService = await SettingsService.init('/path/to/database');
Properties
The template used to generate download paths for books. Supports variables like author name, title, series, etc.
The separator string used when multiple authors are included in file paths (e.g., ”, ” or ” & ”).
The base directory where books are saved. Can be null if not yet configured.
A list of all portal sessions, one for each registered portal. Each session maintains portal-specific settings and authentication state.
The underlying storage implementation used for persisting settings.
Methods
loadSettings
Loads all settings from persistent storage.
Future<void> loadSettings()
This method:
- Loads download path template
- Loads authors path separator
- Loads save directory
- Creates portal sessions for all registered portals with their saved settings
Example:
await settingsService.loadSettings();
print('Loaded ${settingsService.sessions.length} portal sessions');
sessionByCode
Retrieves a portal session by its portal code.
PortalSession sessionByCode(String code)
The unique code identifier of the portal.
The portal session for the specified portal.
Throws: StateError if no session exists for the given code.
Example:
final flibustaSession = settingsService.sessionByCode('flibust');
if (flibustaSession.isAuthenticated) {
print('User is logged in to Flibusta');
}
updateDownloadPathTemplate
Updates the download path template and persists it to storage.
void updateDownloadPathTemplate(PathTemplate template)
The new path template to use for generating download paths.
Example:
final template = PathTemplate.parse('{author}/{series}/{title}');
settingsService.updateDownloadPathTemplate(template);
updateAuthorsPathSeparator
Updates the authors path separator and persists it to storage.
void updateAuthorsPathSeparator(String separator)
The separator to use between multiple author names in file paths.
Example:
settingsService.updateAuthorsPathSeparator(' & ');
// Results in paths like: "Author1 & Author2/Book Title.epub"
updateSaveDirectory
Updates the base save directory and persists it to storage.
void updateSaveDirectory(String? path)
The new base directory path, or null to unset.
Example:
settingsService.updateSaveDirectory('/home/user/Books');
Usage
Complete initialization example:
import 'package:re_ucm_lib/settings/settings_service.dart';
import 'package:re_ucm_lib/portals/portal_factory.dart';
Future<void> initializeApp() async {
// Register portals first
PortalFactory.registerAll([
FlibustPortal(),
LitresPortal(),
]);
// Initialize settings
final settings = await SettingsService.init('/path/to/database');
// Configure download settings
settings.updateSaveDirectory('/home/user/Books');
settings.updateDownloadPathTemplate(
PathTemplate.parse('{author}/{title}')
);
settings.updateAuthorsPathSeparator(', ');
// Access portal sessions
for (final session in settings.sessions) {
print('Portal: ${session.portal.code}');
}
}
Working with portal sessions:
// Get a specific portal session
final session = settingsService.sessionByCode('flibust');
// Update portal-specific settings
await session.updateSettings(newSettings);
// Check authentication status
if (!session.isAuthenticated) {
await session.login(username, password);
}