Skip to content

Commit

Permalink
Merge pull request #629 from appwrite/fix-offline-linux
Browse files Browse the repository at this point in the history
Ensure offline support works in linux
  • Loading branch information
lohanidamodar authored Mar 15, 2023
2 parents 6a3e452 + bea62ad commit c82e686
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
14 changes: 12 additions & 2 deletions src/SDK/Language/Flutter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion templates/flutter/lib/src/client_offline_mixin.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -12,11 +13,13 @@ class OfflineDatabase {

Future<Database> 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!;
}
}
}
11 changes: 11 additions & 0 deletions templates/flutter/lib/src/offline_db_stub.dart.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:sembast/sembast.dart';

class OfflineDatabase {
static final OfflineDatabase instance = OfflineDatabase._internal();

OfflineDatabase._internal();

Future<Database> db() async {
throw UnimplementedError();
}
}
17 changes: 17 additions & 0 deletions templates/flutter/lib/src/offline_db_web.dart.twig
Original file line number Diff line number Diff line change
@@ -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<Database> db() async {
if (_db == null) {
final factory = databaseFactoryWeb;
_db = await factory.openDatabase('{{ spec.title | caseLower }}.db');
}
return _db!;
}
}
1 change: 1 addition & 0 deletions templates/flutter/pubspec.yaml.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c82e686

Please sign in to comment.