From 14b40a80c5bcf258935024a0a75da4a4a39c698b Mon Sep 17 00:00:00 2001 From: David J Kordsmeier Date: Thu, 8 Feb 2024 00:10:16 -0800 Subject: [PATCH] refs #26 Cleanup handling of couchbase integration "getting started" docs are lacking on the flutter docs for couchbase Need to initialize the DB inside of main before you do anything. This currently just captures whatever gets scanned in terms of UUIDs. Will figure out what to do with this later. --- magnet_app/lib/app_state_model.dart | 28 +++++++++++++--- magnet_app/lib/main.dart | 8 +++-- .../flutter/generated_plugin_registrant.cc | 4 +++ .../linux/flutter/generated_plugins.cmake | 1 + .../Flutter/GeneratedPluginRegistrant.swift | 2 ++ magnet_app/pubspec.lock | 32 ++++++++++++++++--- magnet_app/pubspec.yaml | 10 ++++-- .../flutter/generated_plugin_registrant.cc | 3 ++ .../windows/flutter/generated_plugins.cmake | 1 + 9 files changed, 77 insertions(+), 12 deletions(-) diff --git a/magnet_app/lib/app_state_model.dart b/magnet_app/lib/app_state_model.dart index f5d455b..2f8b106 100644 --- a/magnet_app/lib/app_state_model.dart +++ b/magnet_app/lib/app_state_model.dart @@ -1,6 +1,8 @@ import 'dart:io' show Platform, sleep; // import 'dart:js_interop'; import 'dart:math'; +import 'dart:async'; + import 'package:flutter_neumorphic/flutter_neumorphic.dart'; import 'package:provider/provider.dart'; @@ -9,7 +11,8 @@ import 'package:magnet_app/provider/ble_provider.dart'; import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'; // import 'package:location_permissions/location_permissions.dart'; import 'package:permission_handler/permission_handler.dart'; -import 'dart:async'; +import 'package:cbl/cbl.dart'; + /** * @@ -23,7 +26,7 @@ class AppState extends BLEProvider { String? app_screen = "splash"; String? prev_screen = null; bool timerStatus = false; - + AsyncDatabase? _db; Timer? btnStatusTimer; @@ -45,8 +48,11 @@ class AppState extends BLEProvider { return scanStarted; } + Future initDB() async { + _db = await Database.openAsync('magnetdb-schema1'); + } + AppState() { - Timer(const Duration(seconds: 5), () { // scanAndConnectToDevice(); print("Move to home screen"); @@ -58,6 +64,9 @@ class AppState extends BLEProvider { //Scan And Connect Methods void startScan() async { + if (_db == null) { + initDB(); + } bool permGranted = false; scanStarted = true; notifyListeners(); @@ -98,13 +107,24 @@ class AppState extends BLEProvider { print("Scanning ..."); scanDeviceStream = ble.scanForDevices( withServices: [], // [serviceUuid] - scanMode: ScanMode.lowLatency).listen((device) { + scanMode: ScanMode.lowLatency).listen((device) async { print("examining new device"); print("---------------------"); if (device.id == deviceId) { print("--------------> Found match!!!!"); } else { + // Create a new document. + final adoc = await _db!.document(device.id); + if (adoc == null) { + final mutableDocumemt = MutableDocument.withId('hotel::1') + ..setInteger(1, key: 'scans') + ..setString('todo', key: 'name') + ..setDate(DateTime.now(), key: 'createdAt'); + await _db?.saveDocument(mutableDocumemt); + } + + device.serviceData.forEach((k, v) { print('{ key: $k, value: $v }'); }); diff --git a/magnet_app/lib/main.dart b/magnet_app/lib/main.dart index 05ee5ff..d23b261 100644 --- a/magnet_app/lib/main.dart +++ b/magnet_app/lib/main.dart @@ -1,8 +1,9 @@ + import 'package:flutter_neumorphic/flutter_neumorphic.dart'; import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; - +import 'package:cbl_flutter/cbl_flutter.dart'; import 'package:magnet_app/splash_page.dart'; import 'package:magnet_app/about.dart'; @@ -10,8 +11,11 @@ import 'package:magnet_app/app_state_model.dart'; import 'package:magnet_app/provider/ble_provider.dart'; -void main() { +Future main() async { + WidgetsFlutterBinding.ensureInitialized(); + // Now initialize Couchbase Lite. + await CouchbaseLiteFlutter.init(); SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, diff --git a/magnet_app/linux/flutter/generated_plugin_registrant.cc b/magnet_app/linux/flutter/generated_plugin_registrant.cc index 1830e5c..bc97b72 100644 --- a/magnet_app/linux/flutter/generated_plugin_registrant.cc +++ b/magnet_app/linux/flutter/generated_plugin_registrant.cc @@ -7,9 +7,13 @@ #include "generated_plugin_registrant.h" #include +#include void fl_register_plugins(FlPluginRegistry* registry) { g_autoptr(FlPluginRegistrar) audioplayers_linux_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "AudioplayersLinuxPlugin"); audioplayers_linux_plugin_register_with_registrar(audioplayers_linux_registrar); + g_autoptr(FlPluginRegistrar) cbl_flutter_ce_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "CblFlutterCe"); + cbl_flutter_ce_register_with_registrar(cbl_flutter_ce_registrar); } diff --git a/magnet_app/linux/flutter/generated_plugins.cmake b/magnet_app/linux/flutter/generated_plugins.cmake index e9abb91..8885478 100644 --- a/magnet_app/linux/flutter/generated_plugins.cmake +++ b/magnet_app/linux/flutter/generated_plugins.cmake @@ -4,6 +4,7 @@ list(APPEND FLUTTER_PLUGIN_LIST audioplayers_linux + cbl_flutter_ce ) list(APPEND FLUTTER_FFI_PLUGIN_LIST diff --git a/magnet_app/macos/Flutter/GeneratedPluginRegistrant.swift b/magnet_app/macos/Flutter/GeneratedPluginRegistrant.swift index 3165782..cdb223e 100644 --- a/magnet_app/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/magnet_app/macos/Flutter/GeneratedPluginRegistrant.swift @@ -6,11 +6,13 @@ import FlutterMacOS import Foundation import audioplayers_darwin +import cbl_flutter_ce import path_provider_foundation import shared_preferences_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin")) + CblFlutterCe.register(with: registry.registrar(forPlugin: "CblFlutterCe")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/magnet_app/pubspec.lock b/magnet_app/pubspec.lock index 40c9447..009e232 100644 --- a/magnet_app/pubspec.lock +++ b/magnet_app/pubspec.lock @@ -105,6 +105,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.2.2" + cbl_dart: + dependency: "direct dev" + description: + name: cbl_dart + sha256: b7ac781c2b01297849b2740db8fc051b25f323d4516b51a66295c9280319cd36 + url: "https://pub.dev" + source: hosted + version: "2.2.2" cbl_ffi: dependency: transitive description: @@ -121,6 +129,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.9" + cbl_flutter_ce: + dependency: "direct main" + description: + name: cbl_flutter_ce + sha256: "14a6035bf95e284f48b1c49c92b4122bb06b74197b82c1eae9014204294a9880" + url: "https://pub.dev" + source: hosted + version: "2.2.2" cbl_flutter_platform_interface: dependency: transitive description: @@ -133,10 +149,10 @@ packages: dependency: transitive description: name: cbl_libcblite_api - sha256: b2cfb36c883b2b14029f47710f00908a26fcea0ef5182b053fe9d7738413920e + sha256: "839f16022fca3417acc171b683d8b542344277deab5df497736adb2ed803a60a" url: "https://pub.dev" source: hosted - version: "3.1.3" + version: "3.1.1" cbl_libcblitedart_api: dependency: transitive description: @@ -301,10 +317,10 @@ packages: dependency: transitive description: name: http - sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525" + sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2" url: "https://pub.dev" source: hosted - version: "1.1.0" + version: "0.13.6" http_parser: dependency: transitive description: @@ -345,6 +361,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.1" + logging: + dependency: transitive + description: + name: logging + sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340" + url: "https://pub.dev" + source: hosted + version: "1.2.0" matcher: dependency: transitive description: diff --git a/magnet_app/pubspec.yaml b/magnet_app/pubspec.yaml index b7f81bb..4790646 100644 --- a/magnet_app/pubspec.yaml +++ b/magnet_app/pubspec.yaml @@ -44,6 +44,9 @@ dependencies: permission_handler: ^11.0.1 audioplayers: ^5.2.0 cbl_flutter: ^2.0.9 + # This dependency selects the Community Edition of Couchbase Lite. + # For the Enterprise Edition add `cbl_flutter_ee` instead. + cbl_flutter_ce: ^2.2.2 flutter_neumorphic: git: url: https://github.com/truedat101/Flutter-Neumorphic @@ -51,7 +54,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - + flutter_launcher_icons: "^0.13.1" # The "flutter_lints" package below contains a set of recommended lints to @@ -61,9 +64,12 @@ dev_dependencies: # rules and activating additional ones. flutter_lints: ^2.0.0 + # This dependency allows you to use Couchbase Lite in Flutter + # unit tests and not just in integration tests. + cbl_dart: ^2.2.2 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec - + flutter_launcher_icons: image_path: "assets/images/magnet_icon.png" android: true diff --git a/magnet_app/windows/flutter/generated_plugin_registrant.cc b/magnet_app/windows/flutter/generated_plugin_registrant.cc index 3d58508..2326b03 100644 --- a/magnet_app/windows/flutter/generated_plugin_registrant.cc +++ b/magnet_app/windows/flutter/generated_plugin_registrant.cc @@ -7,11 +7,14 @@ #include "generated_plugin_registrant.h" #include +#include #include void RegisterPlugins(flutter::PluginRegistry* registry) { AudioplayersWindowsPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("AudioplayersWindowsPlugin")); + CblFlutterCeRegisterWithRegistrar( + registry->GetRegistrarForPlugin("CblFlutterCe")); PermissionHandlerWindowsPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin")); } diff --git a/magnet_app/windows/flutter/generated_plugins.cmake b/magnet_app/windows/flutter/generated_plugins.cmake index c984fc8..20cbd76 100644 --- a/magnet_app/windows/flutter/generated_plugins.cmake +++ b/magnet_app/windows/flutter/generated_plugins.cmake @@ -4,6 +4,7 @@ list(APPEND FLUTTER_PLUGIN_LIST audioplayers_windows + cbl_flutter_ce permission_handler_windows )