import 'package:mobdr/db/box_user.dart'; import 'package:mobdr/db/box_log.dart'; import 'package:mobdr/db/box_etab.dart'; import 'model.dart'; import 'dart:convert'; import 'objectbox.g.dart'; // created by `flutter pub run build_runner build` /// Provides access to the ObjectBox Store throughout the app. /// /// Create this in the apps main function. class ObjectBox { /// The Store of this app. late final Store store; /// A Box of notes. late final Box noteBox; /// A Box of user. late final Box userBox; /// A Box of Etab. late final Box etabBox; /// A Box of log. late final Box logBox; ObjectBox._create(this.store) { noteBox = Box(store); userBox = Box(store); etabBox = Box(store); logBox = Box(store); // Add some demo data if the box is empty. if (noteBox.isEmpty()) { _putDemoData(); } userBox.removeAll(); // Add some demo data if the box is empty. if (userBox.isEmpty()) { _putUserAdminData(); } } /// Create an instance of ObjectBox to use throughout the app. static Future create() async { // Future openStore() {...} is defined in the generated objectbox.g.dart final store = await openStore(); return ObjectBox._create(store); } void _putDemoData() { final demoNotes = [ Note('Quickly add a note by writing text and pressing Enter'), Note('Delete notes by tapping on one'), Note('Write a demo app for ObjectBox') ]; store.runInTransactionAsync(TxMode.write, _putNotesInTx, demoNotes); } void _putUserAdminData() { //addUSer(0, 'root', 'admim', 'admin', ''); } Stream> getNotes() { // Query for all notes, sorted by their date. // https://docs.objectbox.io/queries final builder = noteBox.query().order(Note_.date, flags: Order.descending); // Build and watch the query, // set triggerImmediately to emit the query immediately on listen. return builder .watch(triggerImmediately: true) // Map it to a list of notes to be used by a StreamBuilder. .map((query) => query.find()); } static void _putNotesInTx(Store store, List notes) => store.box().putMany(notes); /// Add a note within a transaction. /// /// To avoid frame drops, run ObjectBox operations that take longer than a /// few milliseconds, e.g. putting many objects, in an isolate with its /// own Store instance. /// For this example only a single object is put which would also be fine if /// done here directly. Future addNote(String text) => store.runInTransactionAsync(TxMode.write, _addNoteInTx, text); /// Note: due to [dart-lang/sdk#36983](https://github.com/dart-lang/sdk/issues/36983) /// not using a closure as it may capture more objects than expected. /// These might not be send-able to an isolate. See Store.runAsync for details. static void _addNoteInTx(Store store, String text) { // Perform ObjectBox operations that take longer than a few milliseconds // here. To keep it simple, this example just puts a single object. store.box().put(Note(text)); } /// USER --------------------------------------------------------------------- /// Future addUSer(int _id_utilisateur, String _login, String _nom, String _prenom, String _photo) => store.runInTransactionAsync( TxMode.write, _addUserInTx, User( id_utilisateur: _id_utilisateur, login: _login, nom: _nom, prenom: _prenom, photo: _photo)); static void _addUserInTx(Store store, _User) { // Perform ObjectBox operations that take longer than a few milliseconds // here. To keep it simple, this example just puts a single object. store.box().put(_User); } String getUserAvatar(int id_utilisateur) { final query = userBox.query(User_.id_utilisateur.equals(id_utilisateur)).build(); var p = query.findFirst(); return p!.photo; } /// /// /USER -------------------------------------------------------------------- /// ETAB --------------------------------------------------------------------- /// // A function that converts a response body into a List. List parseEtabs(List responseData) { final parsed = responseData.cast>(); return parsed.map((json) => Etab.fromJson(json)).toList(); } Future addEtabs(List _etabs) => store.runInTransactionAsync( TxMode.write, _addEtabsInTx, parseEtabs(_etabs)); static void _addEtabsInTx(Store store, _Etabs) { // Perform ObjectBox operations that take longer than a few milliseconds // here. To keep it simple, this example just puts multiple object. store.box().putMany(_Etabs); } Future addEtab(int _id_etab, String _nom, String _mail, String _tel, String _photo_principale, String _longitude, String _latitude) => store.runInTransactionAsync( TxMode.write, _addEtabInTx, Etab( id_etab: _id_etab, nom: _nom, mail: _mail, tel: _tel, url_photo_principale: _photo_principale, longitude: _longitude, latitude: _latitude)); static void _addEtabInTx(Store store, _Etab) { // Perform ObjectBox operations that take longer than a few milliseconds // here. To keep it simple, this example just puts a single object. store.box().put(_Etab); } String getEtabPhotoPrincipale(int _id_etab) { final query = etabBox.query(Etab_.id_etab.equals(_id_etab)).build(); var p = query.findFirst(); return p!.url_photo_principale; } int getEtabCount() { return etabBox.count(); } /// /// /ETAB -------------------------------------------------------------------- /// LOG ---------------------------------------------------------------------- /// Future addLog(String type, String module, String libelle, int duree) => store.runInTransactionAsync( TxMode.write, _addLogInTx, Log( type, module, libelle, duree, )); static void _addLogInTx(Store store, _Log) { // Perform ObjectBox operations that take longer than a few milliseconds // here. To keep it simple, this example just puts a single object. store.box().put(_Log); } Stream> getLogs() { // Query for all notes, sorted by their date. // https://docs.objectbox.io/queries final builder = logBox.query().order(Log_.date, flags: Order.descending); // Build and watch the query, // set triggerImmediately to emit the query immediately on listen. return builder .watch(triggerImmediately: true) // Map it to a list of notes to be used by a StreamBuilder. .map((query) => query.find()); } /// /// /LOG --------------------------------------------------------------------- }