Skip to content

Commit

Permalink
REL-4262: migrate to Federated
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kononov committed Apr 12, 2024
1 parent 6e6203e commit f144497
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 70 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
##
## 0.1.1
* Fix plugin crashes in background on Android.
## 0.1.0
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 11 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.1.1"
version: "0.1.2"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -114,6 +114,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.3"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -176,5 +184,5 @@ packages:
source: hosted
version: "2.1.4"
sdks:
dart: ">=3.0.0-0 <4.0.0"
flutter: ">=1.20.0"
dart: ">=3.0.0 <4.0.0"
flutter: ">=3.10.6"
68 changes: 10 additions & 58 deletions lib/managed_configurations.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/services.dart';

const String getManagedConfiguration = "getManagedConfigurations";
const String reportKeyedAppState = "reportKeyedAppState";
import 'managed_configurations_platform_interface.dart';

enum Severity { SEVERITY_INFO, SEVERITY_ERROR }

Expand All @@ -21,48 +14,16 @@ extension SeverityExtensions on Severity {
}

class ManagedConfigurations {
static const MethodChannel _managedConfigurationMethodChannel =
const MethodChannel('managed_configurations_method');
static const EventChannel _managedConfigurationEventChannel =
const EventChannel('managed_configurations_event');

static StreamController<Map<String, dynamic>?>
_mangedConfigurationsController =
StreamController<Map<String, dynamic>?>.broadcast();

static Stream<Map<String, dynamic>?> _managedConfigurationsStream =
_mangedConfigurationsController.stream.asBroadcastStream();
Future<String?> getPlatformVersion() {
return ManagedConfigurationsPlatform.instance.getPlatformVersion();
}

/// Returns a broadcast stream which calls on managed app configuration changes
/// Json will be returned
/// Call [dispose] when stream is not more necessary
static Stream<Map<String, dynamic>?> get mangedConfigurationsStream {
if (_actionApplicationRestrictionsChangedSubscription == null) {
_actionApplicationRestrictionsChangedSubscription =
_managedConfigurationEventChannel
.receiveBroadcastStream()
.listen((newManagedConfigurations) {
if (newManagedConfigurations != null) {
_mangedConfigurationsController
.add(json.decode(newManagedConfigurations));
}
});
}
return _managedConfigurationsStream;
return ManagedConfigurationsPlatform.mangedConfigurationsStream;
}

static StreamSubscription<dynamic>?
_actionApplicationRestrictionsChangedSubscription;

/// Returns managed app configurations as Json
static Future<Map<String, dynamic>?> get getManagedConfigurations async {
final String? rawJson = await _managedConfigurationMethodChannel
.invokeMethod(getManagedConfiguration);
if (rawJson != null) {
return json.decode(rawJson);
} else {
return null;
}
return ManagedConfigurationsPlatform.getManagedConfigurations;
}

/// This method is only supported on Android Platform
Expand All @@ -72,20 +33,11 @@ class ManagedConfigurations {
String? message,
String? data,
) async {
if (Platform.isAndroid) {
await _managedConfigurationMethodChannel.invokeMethod(
reportKeyedAppState,
{
'key': key,
'severity': severity.toInteger(),
'message': message,
'data': data,
},
);
}
return ManagedConfigurationsPlatform.reportKeyedAppStates(
key, severity, message, data);
}

static dispose() {
_actionApplicationRestrictionsChangedSubscription?.cancel();
static void dispose() {
return ManagedConfigurationsPlatform.dispose();
}
}
86 changes: 86 additions & 0 deletions lib/managed_configurations_method_channel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:managed_configurations/managed_configurations.dart';
import 'package:managed_configurations/managed_configurations_platform_interface.dart';

const String getManagedConfiguration = "getManagedConfigurations";
const String reportKeyedAppState = "reportKeyedAppState";

/// An implementation of [ManagedConfigurationsPlatform] that uses method channels.
class MethodChannelManagedConfigurations extends ManagedConfigurationsPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel('managed_configurations');

static const MethodChannel _managedConfigurationMethodChannel =
const MethodChannel('managed_configurations_method');
static const EventChannel _managedConfigurationEventChannel =
const EventChannel('managed_configurations_event');

static StreamController<Map<String, dynamic>?>
_mangedConfigurationsController =
StreamController<Map<String, dynamic>?>.broadcast();

static Stream<Map<String, dynamic>?> _managedConfigurationsStream =
_mangedConfigurationsController.stream.asBroadcastStream();

/// Returns a broadcast stream which calls on managed app configuration changes
/// Json will be returned
/// Call [dispose] when stream is not more necessary
static Stream<Map<String, dynamic>?> get mangedConfigurationsStream {
if (_actionApplicationRestrictionsChangedSubscription == null) {
_actionApplicationRestrictionsChangedSubscription =
_managedConfigurationEventChannel
.receiveBroadcastStream()
.listen((newManagedConfigurations) {
if (newManagedConfigurations != null) {
_mangedConfigurationsController
.add(json.decode(newManagedConfigurations));
}
});
}
return _managedConfigurationsStream;
}

static StreamSubscription<dynamic>?
_actionApplicationRestrictionsChangedSubscription;

/// Returns managed app configurations as Json
static Future<Map<String, dynamic>?> get getManagedConfigurations async {
final String? rawJson = await _managedConfigurationMethodChannel
.invokeMethod(getManagedConfiguration);
if (rawJson != null) {
return json.decode(rawJson);
} else {
return null;
}
}

/// This method is only supported on Android Platform
static Future<void> reportKeyedAppStates(
String key,
Severity severity,
String? message,
String? data,
) async {
if (Platform.isAndroid) {
await _managedConfigurationMethodChannel.invokeMethod(
reportKeyedAppState,
{
'key': key,
'severity': severity.toInteger(),
'message': message,
'data': data,
},
);
}
}

static dispose() {
_actionApplicationRestrictionsChangedSubscription?.cancel();
}
}
53 changes: 53 additions & 0 deletions lib/managed_configurations_platform_interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:managed_configurations/managed_configurations.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import 'managed_configurations_method_channel.dart';

abstract class ManagedConfigurationsPlatform extends PlatformInterface {
/// Constructs a ManagedConfigurationsPlatform.
ManagedConfigurationsPlatform() : super(token: _token);

static final Object _token = Object();

static ManagedConfigurationsPlatform _instance =
MethodChannelManagedConfigurations();

/// The default instance of [ManagedConfigurationsPlatform] to use.
///
/// Defaults to [MethodChannelManagedConfigurations].
static ManagedConfigurationsPlatform get instance => _instance;

/// Platform-specific implementations should set this with their own
/// platform-specific class that extends [ManagedConfigurationsPlatform] when
/// they register themselves.
static set instance(ManagedConfigurationsPlatform instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}

Future<String?> getPlatformVersion() {
throw UnimplementedError('platformVersion() has not been implemented.');
}

static Stream<Map<String, dynamic>?> get mangedConfigurationsStream {
throw UnimplementedError('platformVersion() has not been implemented.');
}

static Future<Map<String, dynamic>?> get getManagedConfigurations async {
throw UnimplementedError('platformVersion() has not been implemented.');
}

/// This method is only supported on Android Platform
static Future<void> reportKeyedAppStates(
String key,
Severity severity,
String? message,
String? data,
) async {
throw UnimplementedError('platformVersion() has not been implemented.');
}

static void dispose() {
throw UnimplementedError('platformVersion() has not been implemented.');
}
}
28 changes: 26 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_lints:
dependency: "direct dev"
description:
name: flutter_lints
sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04
url: "https://pub.dev"
source: hosted
version: "2.0.3"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -67,6 +75,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.7"
lints:
dependency: transitive
description:
name: lints
sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -99,6 +115,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.3"
plugin_platform_interface:
dependency: "direct main"
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -161,5 +185,5 @@ packages:
source: hosted
version: "2.1.4"
sdks:
dart: ">=3.0.0-0 <4.0.0"
flutter: ">=1.20.0"
dart: ">=3.0.0 <4.0.0"
flutter: ">=3.10.6"
28 changes: 21 additions & 7 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
name: managed_configurations
description: Plugin to support managed app configuration provided by a Mobile device management (MDM).
version: 0.1.1
version: 0.1.2
homepage: https://github.com/mwaylabs/flutter-managed-configuration


environment:
sdk: ">=2.12.0"
flutter: ">=1.20.0"
sdk: ^3.0.0
# Flutter versions prior to 3.7 did not support the
# sharedDarwinSource option.
flutter: ">=3.10.6"

dependencies:
flutter:
sdk: flutter
plugin_platform_interface: ^2.0.2

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
# The following section is specific to Flutter packages.
flutter:
# This section identifies this Flutter project as a plugin project.
# The 'pluginClass' and Android 'package' identifiers should not ordinarily
# be modified. They are used by the tooling to maintain consistency when
# The 'pluginClass' specifies the class (in Java, Kotlin, Swift, Objective-C, etc.)
# which should be registered in the plugin registry. This is required for
# using method channels.
# The Android 'package' specifies package in which the registered class is.
# This is required for using method channels on Android.
# The 'ffiPlugin' specifies that native code should be built and bundled.
# This is required for using `dart:ffi`.
# All these are used by the tooling to maintain consistency when
# adding or updating assets for this project.
plugin:
platforms:
Expand All @@ -31,4 +42,7 @@ flutter:
pluginClass: ManagedConfigurationsPlugin
ios:
pluginClass: ManagedConfigurationsPlugin

sharedDarwinSource: true
macos:
pluginClass: ManagedConfigurationsPlugin
sharedDarwinSource: true

0 comments on commit f144497

Please sign in to comment.