2021-06-19 23:41:19 +08:00
|
|
|
export 'package:async/async.dart';
|
|
|
|
import 'dart:async';
|
2024-04-07 21:36:55 +08:00
|
|
|
import 'dart:convert';
|
2023-01-08 12:10:53 +08:00
|
|
|
import 'package:appflowy_backend/rust_stream.dart';
|
2024-04-07 21:36:55 +08:00
|
|
|
import 'package:flutter/foundation.dart';
|
2021-06-19 23:41:19 +08:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'dart:ffi';
|
2021-07-13 10:59:51 +08:00
|
|
|
import 'ffi.dart' as ffi;
|
2021-06-19 23:41:19 +08:00
|
|
|
import 'package:ffi/ffi.dart';
|
2024-04-07 21:36:55 +08:00
|
|
|
import 'dart:isolate';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:logger/logger.dart';
|
2021-06-19 23:41:19 +08:00
|
|
|
|
2022-01-28 18:34:21 +08:00
|
|
|
enum ExceptionType {
|
|
|
|
AppearanceSettingsIsEmpty,
|
|
|
|
}
|
|
|
|
|
|
|
|
class FlowySDKException implements Exception {
|
|
|
|
ExceptionType type;
|
|
|
|
FlowySDKException(this.type);
|
|
|
|
}
|
|
|
|
|
2021-06-19 23:41:19 +08:00
|
|
|
class FlowySDK {
|
2023-01-08 12:10:53 +08:00
|
|
|
static const MethodChannel _channel = MethodChannel('appflowy_backend');
|
2021-06-19 23:41:19 +08:00
|
|
|
static Future<String> get platformVersion async {
|
|
|
|
final String version = await _channel.invokeMethod('getPlatformVersion');
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2022-12-20 11:14:42 +08:00
|
|
|
FlowySDK();
|
2021-06-19 23:41:19 +08:00
|
|
|
|
2023-12-27 11:42:39 +08:00
|
|
|
Future<void> dispose() async {}
|
2021-06-19 23:41:19 +08:00
|
|
|
|
2023-11-20 20:54:47 +08:00
|
|
|
Future<void> init(String configuration) async {
|
2024-04-07 21:36:55 +08:00
|
|
|
ffi.set_stream_port(RustStreamReceiver.shared.port);
|
2021-06-19 23:41:19 +08:00
|
|
|
ffi.store_dart_post_cobject(NativeApi.postCObject);
|
2024-02-04 05:50:23 +08:00
|
|
|
|
2024-04-07 21:36:55 +08:00
|
|
|
// On iOS, VSCode can't print logs from Rust, so we need to use a different method to print logs.
|
|
|
|
// So we use a shared port to receive logs from Rust and print them using the logger. In release mode, we don't print logs.
|
|
|
|
if (Platform.isIOS && kDebugMode) {
|
|
|
|
ffi.set_log_stream_port(RustLogStreamReceiver.logShared.port);
|
|
|
|
}
|
|
|
|
|
2024-02-04 05:50:23 +08:00
|
|
|
// final completer = Completer<Uint8List>();
|
|
|
|
// // Create a SendPort that accepts only one message.
|
|
|
|
// final sendPort = singleCompletePort(completer);
|
|
|
|
|
|
|
|
final code = ffi.init_sdk(0, configuration.toNativeUtf8());
|
|
|
|
if (code != 0) {
|
|
|
|
throw Exception('Failed to initialize the SDK');
|
|
|
|
}
|
|
|
|
// return completer.future;
|
2021-06-19 23:41:19 +08:00
|
|
|
}
|
|
|
|
}
|
2024-04-07 21:36:55 +08:00
|
|
|
|
|
|
|
class RustLogStreamReceiver {
|
|
|
|
static RustLogStreamReceiver logShared = RustLogStreamReceiver._internal();
|
|
|
|
late RawReceivePort _ffiPort;
|
|
|
|
late StreamController<Uint8List> _streamController;
|
|
|
|
late StreamSubscription<Uint8List> _subscription;
|
|
|
|
int get port => _ffiPort.sendPort.nativePort;
|
|
|
|
late Logger _logger;
|
|
|
|
|
|
|
|
RustLogStreamReceiver._internal() {
|
|
|
|
_ffiPort = RawReceivePort();
|
|
|
|
_streamController = StreamController();
|
|
|
|
_ffiPort.handler = _streamController.add;
|
|
|
|
_logger = Logger(
|
|
|
|
printer: PrettyPrinter(
|
|
|
|
methodCount: 0, // number of method calls to be displayed
|
|
|
|
errorMethodCount: 8, // number of method calls if stacktrace is provided
|
|
|
|
lineLength: 120, // width of the output
|
|
|
|
colors: false, // Colorful log messages
|
|
|
|
printEmojis: false, // Print an emoji for each log message
|
|
|
|
printTime: false, // Should each log print contain a timestamp
|
|
|
|
),
|
|
|
|
level: kDebugMode ? Level.trace : Level.info,
|
|
|
|
);
|
|
|
|
|
|
|
|
_subscription = _streamController.stream.listen((data) {
|
|
|
|
String decodedString = utf8.decode(data);
|
|
|
|
_logger.i(decodedString);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
factory RustLogStreamReceiver() {
|
|
|
|
return logShared;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> dispose() async {
|
|
|
|
await _streamController.close();
|
|
|
|
await _subscription.cancel();
|
|
|
|
_ffiPort.close();
|
|
|
|
}
|
|
|
|
}
|