/
cbl_flutter.dart
56 lines (46 loc) · 1.85 KB
/
cbl_flutter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ignore_for_file: implementation_imports
import 'dart:io';
import 'package:cbl/cbl.dart';
import 'package:cbl/src/support/isolate.dart';
import 'package:cbl/src/support/tracing.dart';
import 'package:cbl_flutter_platform_interface/cbl_flutter_platform_interface.dart';
import 'package:path_provider/path_provider.dart';
/// Initializes global resources and configures global settings, such as
/// logging, for usage of Couchbase Lite in Flutter apps.
class CouchbaseLiteFlutter {
/// Private constructor to allow control over instance creation.
CouchbaseLiteFlutter._();
/// Initializes the `cbl` package, for the main isolate.
static Future<void> init() =>
asyncOperationTracePoint(InitializeOp.new, () async {
await initPrimaryIsolate(IsolateContext(
libraries: CblFlutterPlatform.instance.libraries(),
initContext: await _context(),
));
_setupLogging();
});
}
Future<InitContext> _context() async {
final directories = await Future.wait([
getApplicationSupportDirectory(),
getExternalStorageDirectory().onError<UnsupportedError>((_, __) => null),
]);
final filesDir = directories[0]!;
// For temporary files, we try to use the apps external storage directory
// and fallback to the application support directory, if it's not available.
final tempDir = directories[1] ?? filesDir;
final clbTempDir = Directory.fromUri(tempDir.uri.resolve('CBLTemp'));
await clbTempDir.create();
return InitContext(
filesDir: filesDir.path,
tempDir: clbTempDir.path,
);
}
void _setupLogging() {
Database.log
// stdout and stderr is not visible to Flutter developers, usually. That is
// why the console logger is disabled and a custom logger which logs to
// Dart's `print` functions is installed.
..console.level = LogLevel.none
..custom = DartConsoleLogger(LogLevel.warning);
}