From bea62ad818c53c565cdd751949d4a92982cb3df4 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Tue, 14 Mar 2023 14:54:00 -0700 Subject: [PATCH] Ensure offline support works in linux --- src/SDK/Language/Flutter.php | 14 ++++++++++++-- .../lib/src/client_offline_mixin.dart.twig | 4 +++- ...ine_db.dart.twig => offline_db_io.dart.twig} | 17 ++++++++++------- .../flutter/lib/src/offline_db_stub.dart.twig | 11 +++++++++++ .../flutter/lib/src/offline_db_web.dart.twig | 17 +++++++++++++++++ templates/flutter/pubspec.yaml.twig | 1 + 6 files changed, 54 insertions(+), 10 deletions(-) rename templates/flutter/lib/src/{offline_db.dart.twig => offline_db_io.dart.twig} (53%) create mode 100644 templates/flutter/lib/src/offline_db_stub.dart.twig create mode 100644 templates/flutter/lib/src/offline_db_web.dart.twig diff --git a/src/SDK/Language/Flutter.php b/src/SDK/Language/Flutter.php index eab733fb9..4e398f576 100644 --- a/src/SDK/Language/Flutter.php +++ b/src/SDK/Language/Flutter.php @@ -207,8 +207,18 @@ public function getFiles(): array ], [ 'scope' => 'default', - 'destination' => '/lib/src/offline_db.dart', - 'template' => 'flutter/lib/src/offline_db.dart.twig', + 'destination' => '/lib/src/offline_db_io.dart', + 'template' => 'flutter/lib/src/offline_db_io.dart.twig', + ], + [ + 'scope' => 'default', + 'destination' => '/lib/src/offline_db_stub.dart', + 'template' => 'flutter/lib/src/offline_db_stub.dart.twig', + ], + [ + 'scope' => 'default', + 'destination' => '/lib/src/offline_db_web.dart', + 'template' => 'flutter/lib/src/offline_db_web.dart.twig', ], [ 'scope' => 'default', diff --git a/templates/flutter/lib/src/client_offline_mixin.dart.twig b/templates/flutter/lib/src/client_offline_mixin.dart.twig index 6568cbc10..4a3d7d42c 100644 --- a/templates/flutter/lib/src/client_offline_mixin.dart.twig +++ b/templates/flutter/lib/src/client_offline_mixin.dart.twig @@ -10,7 +10,9 @@ import 'package:sembast/utils/value_utils.dart'; import 'enums.dart'; import 'exception.dart'; -import 'offline_db.dart'; +import 'offline_db_stub.dart' + if (dart.library.html) 'offline_db_web.dart' + if (dart.library.io) 'offline_db_io.dart'; import 'response.dart'; class AccessTimestamp { diff --git a/templates/flutter/lib/src/offline_db.dart.twig b/templates/flutter/lib/src/offline_db_io.dart.twig similarity index 53% rename from templates/flutter/lib/src/offline_db.dart.twig rename to templates/flutter/lib/src/offline_db_io.dart.twig index 075d6a8ae..2aa3a7674 100644 --- a/templates/flutter/lib/src/offline_db.dart.twig +++ b/templates/flutter/lib/src/offline_db_io.dart.twig @@ -1,8 +1,9 @@ -import 'package:flutter/foundation.dart'; +import 'dart:io'; + import 'package:sembast/sembast.dart'; import 'package:sembast_sqflite/sembast_sqflite.dart'; -import 'package:sembast_web/sembast_web.dart'; import 'package:sqflite/sqflite.dart' as sqflite; +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; class OfflineDatabase { static final OfflineDatabase instance = OfflineDatabase._internal(); @@ -12,11 +13,13 @@ class OfflineDatabase { Future db() async { if (_db == null) { - final factory = kIsWeb - ? databaseFactoryWeb - : getDatabaseFactorySqflite(sqflite.databaseFactory); - _db = await factory.openDatabase('{{spec.title | caseLower}}.db'); + final factory = getDatabaseFactorySqflite( + Platform.isLinux || Platform.isWindows + ? databaseFactoryFfi + : sqflite.databaseFactory, + ); + _db = await factory.openDatabase('{{ spec.title | caseLower }}.db'); } return _db!; } -} \ No newline at end of file +} diff --git a/templates/flutter/lib/src/offline_db_stub.dart.twig b/templates/flutter/lib/src/offline_db_stub.dart.twig new file mode 100644 index 000000000..fe0994265 --- /dev/null +++ b/templates/flutter/lib/src/offline_db_stub.dart.twig @@ -0,0 +1,11 @@ +import 'package:sembast/sembast.dart'; + +class OfflineDatabase { + static final OfflineDatabase instance = OfflineDatabase._internal(); + + OfflineDatabase._internal(); + + Future db() async { + throw UnimplementedError(); + } +} diff --git a/templates/flutter/lib/src/offline_db_web.dart.twig b/templates/flutter/lib/src/offline_db_web.dart.twig new file mode 100644 index 000000000..320d6b2d0 --- /dev/null +++ b/templates/flutter/lib/src/offline_db_web.dart.twig @@ -0,0 +1,17 @@ +import 'package:sembast/sembast.dart'; +import 'package:sembast_web/sembast_web.dart'; + +class OfflineDatabase { + static final OfflineDatabase instance = OfflineDatabase._internal(); + Database? _db; + + OfflineDatabase._internal(); + + Future db() async { + if (_db == null) { + final factory = databaseFactoryWeb; + _db = await factory.openDatabase('{{ spec.title | caseLower }}.db'); + } + return _db!; + } +} diff --git a/templates/flutter/pubspec.yaml.twig b/templates/flutter/pubspec.yaml.twig index 7998da8dd..08a26cbd8 100644 --- a/templates/flutter/pubspec.yaml.twig +++ b/templates/flutter/pubspec.yaml.twig @@ -23,6 +23,7 @@ dependencies: path: ^1.8.2 sembast: ^3.4.0+6 sembast_sqflite: ^2.1.0+1 + sqflite_common_ffi: ^2.2.2 sembast_web: ^2.1.0+4 sqflite: ^2.2.2