barcode_scanner/lib/backend/objectbox/objectbox_manager.dart
mandreshope 09405b5da7 feat: Implement local product storage using ObjectBox
Configures the ObjectBox ProductEntity to allow assigning IDs. Adds a helper to convert ProductStruct to ProductEntity for easier storage.

Saves scanned products directly to the local ObjectBox store.

Updates the ProductListPage to fetch and display locally stored products from ObjectBox upon initialization.

Includes minor UI adjustments in the scanned product component and scanner page, and fixes loading state handling in HomePageModel.
2025-07-03 21:09:49 +03:00

36 lines
1.0 KiB
Dart

import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:barcode_scanner/backend/objectbox/objectbox.g.dart';
export 'package:barcode_scanner/backend/objectbox/objectbox.g.dart';
late ObjectboxManager objectboxManager;
class ObjectboxManager {
/// The Store of this app.
late final Store store;
ObjectboxManager._create(this.store);
/// Create an instance of ObjectBox to use throughout the app.
static Future<ObjectboxManager> create() async {
Directory directory;
if (Platform.isIOS) {
directory = await getLibraryDirectory();
} else {
directory = await getApplicationSupportDirectory();
}
// Future<Store> openStore() {...} is defined in the generated objectbox.g.dart
final store = await openStore(
directory: p.join(directory.path, "objectbox_db"),
);
return ObjectboxManager._create(store);
}
/// Call it before `runApp()` in main.dart
static Future init() async {
objectboxManager = await ObjectboxManager.create();
}
}