From 4970875f5940c04496edd7a65426d6420dd1962d Mon Sep 17 00:00:00 2001 From: nrubin29 Date: Wed, 30 Jun 2021 23:27:32 -0400 Subject: [PATCH] Removed dependency on rxdart --- CHANGELOG.md | 6 +++ example/lib/main.dart | 63 ++++++++++++++++-------------- example/pubspec.lock | 15 ++----- lib/flutter_acrcloud.dart | 2 + lib/src/flutter_acrcloud_impl.dart | 42 +++++++++++--------- pubspec.lock | 43 +++++++++----------- pubspec.yaml | 7 ++-- 7 files changed, 89 insertions(+), 89 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef3c299..fad004b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.1.3 + +* **BREAKING**: Renamed `volume` to `volumeStream`. It is now just a normal `Stream` instead of a `BehaviorSubject`. +* Removed dependency on `rxdart`. +* Upgraded dependencies. + ## 0.1.2 * Made all JSON classes const. diff --git a/example/lib/main.dart b/example/lib/main.dart index 6a8bcfa..601c014 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -40,47 +40,50 @@ class _MyAppState extends State { final session = ACRCloud.startSession(); showDialog( - context: context, - barrierDismissible: false, - builder: (context) => AlertDialog( - title: Text('Listening...'), - content: StreamBuilder( - stream: session.volume, - initialData: 0, - builder: (context, snapshot) => - Text(snapshot.data.toString()), - ), - actions: [ - TextButton( - child: Text('Cancel'), - onPressed: session.cancel, - ) - ], - )); + context: context, + barrierDismissible: false, + builder: (context) => AlertDialog( + title: Text('Listening...'), + content: StreamBuilder( + stream: session.volumeStream, + initialData: 0, + builder: (_, snapshot) => + Text(snapshot.data.toString()), + ), + actions: [ + TextButton( + child: Text('Cancel'), + onPressed: session.cancel, + ) + ], + ), + ); final result = await session.result; Navigator.pop(context); - setState(() { - if (result == null) { - // Cancelled - return; - } else if (result.metadata == null) { - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text('No result'), - )); - return; - } + if (result == null) { + // Cancelled. + return; + } else if (result.metadata == null) { + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text('No result.'), + )); + return; + } + setState(() { music = result.metadata!.music.first; }); }, child: Text('Listen'), ), ), - if (music != null) Text('Track: ${music!.title}\n'), - if (music != null) Text('Album: ${music!.album.name}\n'), - if (music != null) Text('Artist: ${music!.artists.first.name}\n'), + if (music != null) ...[ + Text('Track: ${music!.title}\n'), + Text('Album: ${music!.album.name}\n'), + Text('Artist: ${music!.artists.first.name}\n'), + ], ], ), ), diff --git a/example/pubspec.lock b/example/pubspec.lock index ccae1e9..f1ede19 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0" + version: "2.6.1" boolean_selector: dependency: transitive description: @@ -61,7 +61,7 @@ packages: path: ".." relative: true source: path - version: "0.1.1" + version: "0.1.3" flutter_test: dependency: "direct dev" description: flutter @@ -95,13 +95,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.0" - rxdart: - dependency: transitive - description: - name: rxdart - url: "https://pub.dartlang.org" - source: hosted - version: "0.27.0" sky_engine: dependency: transitive description: flutter @@ -113,7 +106,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" stack_trace: dependency: transitive description: @@ -148,7 +141,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19" + version: "0.3.0" typed_data: dependency: transitive description: diff --git a/lib/flutter_acrcloud.dart b/lib/flutter_acrcloud.dart index 8eb27ff..36e59a2 100644 --- a/lib/flutter_acrcloud.dart +++ b/lib/flutter_acrcloud.dart @@ -1,2 +1,4 @@ +library flutter_acrcloud; + export 'src/acrcloud_response.dart'; export 'src/flutter_acrcloud_impl.dart'; diff --git a/lib/src/flutter_acrcloud_impl.dart b/lib/src/flutter_acrcloud_impl.dart index 87d69af..e516500 100644 --- a/lib/src/flutter_acrcloud_impl.dart +++ b/lib/src/flutter_acrcloud_impl.dart @@ -5,7 +5,6 @@ import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:flutter_acrcloud/src/acrcloud_response.dart'; -import 'package:rxdart/rxdart.dart'; /// A configuration object with the values necessary to access the ACRCloud API. class ACRCloudConfig { @@ -18,21 +17,19 @@ class ACRCloudConfig { /// A recording session. class ACRCloudSession { - final BehaviorSubject _result; - - /// A Stream of volume values. - final BehaviorSubject volume; + final Completer _result; + final StreamController _volume; /// A Future which resolves to null if the session is [cancel]led, or an /// [ACRCloudResponse] otherwise. - late final Future result; + Future get result => _result.future; + + /// A Stream of volume values. + Stream get volumeStream => _volume.stream; ACRCloudSession() - : _result = BehaviorSubject(), - volume = BehaviorSubject() { - result = _result.first.catchError((_, __) => null, - test: (error) => error is StateError && error.message == 'No element'); - } + : _result = Completer(), + _volume = StreamController(); /// Cancel this session. void cancel() { @@ -41,8 +38,11 @@ class ACRCloudSession { } void dispose() { - volume.close(); - _result.close(); + _volume.close(); + + if (!_result.isCompleted) { + _result.complete(); + } } } @@ -53,8 +53,10 @@ class ACRCloud { static var isSetUp = false; static ACRCloudSession? _session; - /// Set up ACRCloud according to the [ACRCloudConfig] passed. You should only - /// call this function once, but subsequent calls will simply be ignored. + /// Set up ACRCloud according to the [ACRCloudConfig] passed. + /// + /// You should only call this function once, but subsequent calls will simply + /// be ignored. static Future setUp(ACRCloudConfig config) async { if (isSetUp) { return; @@ -62,10 +64,10 @@ class ACRCloud { _channel.setMethodCallHandler((call) async { if (call.method == 'volume') { - _session?.volume.add(call.arguments); + _session?._volume.add(call.arguments); } else if (call.method == 'result') { _session?._result - .add(ACRCloudResponse.fromJson(json.decode(call.arguments))); + .complete(ACRCloudResponse.fromJson(json.decode(call.arguments))); } }); @@ -78,8 +80,10 @@ class ACRCloud { isSetUp = true; } - /// Begin recognizing a track. Returns an [ACRCloudSession] instance that can - /// be used to control the session. + /// Begin recognizing a track. + /// + /// Returns an [ACRCloudSession] instance that can be used to control the + /// session. static ACRCloudSession startSession() { if (!isSetUp) { throw StateError( diff --git a/pubspec.lock b/pubspec.lock index 11f3dcb..572d898 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,28 +7,28 @@ packages: name: _fe_analyzer_shared url: "https://pub.dartlang.org" source: hosted - version: "21.0.0" + version: "22.0.0" analyzer: dependency: transitive description: name: analyzer url: "https://pub.dartlang.org" source: hosted - version: "1.5.0" + version: "1.7.1" args: dependency: transitive description: name: args url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.1" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0" + version: "2.6.1" boolean_selector: dependency: transitive description: @@ -42,7 +42,7 @@ packages: name: build url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "2.0.2" build_config: dependency: transitive description: @@ -63,14 +63,14 @@ packages: name: build_resolvers url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "2.0.3" build_runner: dependency: "direct dev" description: name: build_runner url: "https://pub.dartlang.org" source: hosted - version: "2.0.2" + version: "2.0.5" build_runner_core: dependency: transitive description: @@ -84,14 +84,14 @@ packages: name: built_collection url: "https://pub.dartlang.org" source: hosted - version: "5.0.0" + version: "5.1.0" built_value: dependency: transitive description: name: built_value url: "https://pub.dartlang.org" source: hosted - version: "8.0.6" + version: "8.1.0" characters: dependency: transitive description: @@ -119,7 +119,7 @@ packages: name: cli_util url: "https://pub.dartlang.org" source: hosted - version: "0.3.0" + version: "0.3.1" clock: dependency: transitive description: @@ -147,7 +147,7 @@ packages: name: convert url: "https://pub.dartlang.org" source: hosted - version: "3.0.0" + version: "3.0.1" crypto: dependency: transitive description: @@ -175,7 +175,7 @@ packages: name: file url: "https://pub.dartlang.org" source: hosted - version: "6.1.0" + version: "6.1.2" fixnum: dependency: transitive description: @@ -255,7 +255,7 @@ packages: name: json_serializable url: "https://pub.dartlang.org" source: hosted - version: "4.1.1" + version: "4.1.3" logging: dependency: transitive description: @@ -304,7 +304,7 @@ packages: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.11.0" + version: "1.11.1" pool: dependency: transitive description: @@ -326,20 +326,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.0" - rxdart: - dependency: "direct main" - description: - name: rxdart - url: "https://pub.dartlang.org" - source: hosted - version: "0.27.0" shelf: dependency: transitive description: name: shelf url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" + version: "1.1.4" shelf_web_socket: dependency: transitive description: @@ -358,14 +351,14 @@ packages: name: source_gen url: "https://pub.dartlang.org" source: hosted - version: "1.0.0" + version: "1.0.2" source_span: dependency: transitive description: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" stack_trace: dependency: transitive description: @@ -407,7 +400,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19" + version: "0.3.0" timing: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 40ee5a3..db1f812 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_acrcloud description: A Flutter plugin for the ACRCloud music recognition API. This plugin provides a simple interface for using ACRCloud to recognize music from the device's microphone -version: 0.1.2 +version: 0.1.3 homepage: https://github.com/nrubin29/flutter_acrcloud environment: @@ -11,13 +11,12 @@ dependencies: flutter: sdk: flutter json_annotation: ^4.0.1 - rxdart: ^0.27.0 dev_dependencies: - build_runner: ^2.0.2 + build_runner: ^2.0.5 flutter_test: sdk: flutter - json_serializable: ^4.1.1 + json_serializable: ^4.1.3 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec