Skip to content

Commit

Permalink
Added ApplicationConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Mar 28, 2022
1 parent b255c3a commit db37e05
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 2 deletions.
51 changes: 51 additions & 0 deletions lib/src/application_configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
import 'dart:io';

import 'package:meta/meta.dart';
import 'package:pub_semver/pub_semver.dart';

import 'cli/common/utils.dart';
import 'native/realm_core.dart';

@immutable
class ApplicationConfiguration {
final RealmAppConfigHandle _handle;

final String appId;
final Uri? baseUrl;
final Duration? defaultRequestTimeout;
final String? localAppName;
final Version? localAppVersion;

ApplicationConfiguration(
this.appId, {
this.baseUrl,
this.defaultRequestTimeout,
this.localAppName,
this.localAppVersion,
}) : _handle = realmCore.createAppConfig(appId, realmCore.createHttpTransport(HttpClient())) {
if (baseUrl != null) realmCore.setAppConfigBaseUrl(_handle, baseUrl!);
if (defaultRequestTimeout != null) realmCore.setAppConfigDefaultRequestTimeout(_handle, defaultRequestTimeout!);
if (localAppName != null) realmCore.setAppConfigLocalAppName(_handle, localAppName!);
if (localAppVersion != null) realmCore.setAppConfigLocalAppVersion(_handle, localAppVersion!);
realmCore.setAppConfigPlatform(_handle, Platform.operatingSystem);
realmCore.setAppConfigPlatformVersion(_handle, Platform.operatingSystemVersion);
realmCore.setAppConfigSdkVersion(_handle, Version.parse(Platform.version.takeUntil(' ')));
}
}
4 changes: 2 additions & 2 deletions lib/src/cli/metrics/metrics_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ Future<FlutterInfo?> getInfo(Options options) async {
// Sanity check full info, if we have it
if (info != null && (version == null || version == info.frameworkVersion) && flutterVersionConstraints.allows(info.frameworkVersion)) {
// The returned info match both the projects constraints and the
// flutter version of the lastest flutter command run on the project
// flutter version of the latest flutter command run on the project
return info;
}

// Fallback to simplified info build from the version read from .dart_tool/version,
// secondly the min constraint of the flutter SDK used
return FlutterInfo(
frameworkVersion: version ?? (await safe(() => (flutterVersionConstraints as VersionRange).min!)) ?? Version.none,
dartSdkVersion: Version.parse(Platform.version.toString().takeUntil(' ')),
dartSdkVersion: Version.parse(Platform.version.takeUntil(' ')),
);
}
54 changes: 54 additions & 0 deletions lib/src/native/realm_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'dart:typed_data';

// Hide StringUtf8Pointer.toNativeUtf8 and StringUtf16Pointer since these allows silently allocating memory. Use toUtf8Ptr instead
import 'package:ffi/ffi.dart' hide StringUtf8Pointer, StringUtf16Pointer;
import 'package:pub_semver/pub_semver.dart';

import '../collections.dart';
import '../configuration.dart';
Expand Down Expand Up @@ -752,6 +753,55 @@ class _RealmCore {
return out_modified.asTypedList(count).toList();
});
}

RealmAppConfigHandle createAppConfig(String appId, RealmHttpTransportHandle httpTransport) {
return using((arena) {
final app_id = appId.toUtf8Ptr(arena);
return RealmAppConfigHandle._(_realmLib.realm_app_config_new(app_id, httpTransport._pointer));
});
}

void setAppConfigBaseUrl(RealmAppConfigHandle handle, Uri baseUrl) {
using((arena) {
_realmLib.realm_app_config_set_base_url(handle._pointer, baseUrl.toString().toUtf8Ptr(arena));
});
}

void setAppConfigDefaultRequestTimeout(RealmAppConfigHandle handle, Duration defaultRequestTimeout) {
_realmLib.realm_app_config_set_default_request_timeout(handle._pointer, defaultRequestTimeout.inMilliseconds);
}

void setAppConfigLocalAppName(RealmAppConfigHandle handle, String localAppName) {
using((arena) {
_realmLib.realm_app_config_set_local_app_name(handle._pointer, localAppName.toUtf8Ptr(arena));
});
}

void setAppConfigLocalAppVersion(RealmAppConfigHandle handle, Version localAppVersion) {
using((arena) {
final versionString = localAppVersion.toString();
_realmLib.realm_app_config_set_local_app_version(handle._pointer, versionString.toUtf8Ptr(arena));
});
}

void setAppConfigPlatform(RealmAppConfigHandle handle, String platform) {
using((arena) {
_realmLib.realm_app_config_set_platform(handle._pointer, platform.toUtf8Ptr(arena));
});
}

void setAppConfigPlatformVersion(RealmAppConfigHandle handle, String platformVersion) {
using((arena) {
_realmLib.realm_app_config_set_platform_version(handle._pointer, platformVersion.toUtf8Ptr(arena));
});
}

void setAppConfigSdkVersion(RealmAppConfigHandle handle, Version sdkVersion) {
using((arena) {
final versionString = sdkVersion.toString();
_realmLib.realm_app_config_set_sdk_version(handle._pointer, versionString.toUtf8Ptr(arena));
});
}
}

class LastError {
Expand Down Expand Up @@ -848,6 +898,10 @@ class RealmHttpTransportHandle extends Handle<realm_http_transport> {
RealmHttpTransportHandle._(Pointer<realm_http_transport> pointer) : super(pointer, 256); // TODO; What should hint be?
}

class RealmAppConfigHandle extends Handle<realm_app_config> {
RealmAppConfigHandle._(Pointer<realm_app_config> pointer) : super(pointer, 256); // TODO: What should hint be?
}

extension on List<int> {
Pointer<Int8> toInt8Ptr(Allocator allocator) {
final nativeStringSize = length + 1;
Expand Down
1 change: 1 addition & 0 deletions src/realm_dart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void dummy(void) {
realm_results_add_notification_callback(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
realm_results_snapshot(nullptr);
realm_http_transport_new(nullptr, nullptr, nullptr);
realm_app_config_new(nullptr, nullptr);
#if (ANDROID)
realm_android_dummy();
#endif
Expand Down
38 changes: 38 additions & 0 deletions test/application_configuration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
import 'dart:io';
import 'package:pub_semver/pub_semver.dart';
import 'package:realm_dart/src/application_configuration.dart';
import 'test.dart';

Future<void> main([List<String>? args]) async {
print("Current PID $pid");

setupTests(args);

test('ApplicationConfiguration can be created', () {
ApplicationConfiguration(
'foo',
baseUrl: Uri.parse('https://not_re.al'),
defaultRequestTimeout: const Duration(seconds: 2),
localAppName: 'bar',
localAppVersion: Version(1, 0, 0),
);
});
}

0 comments on commit db37e05

Please sign in to comment.