Skip to content

Commit

Permalink
Removed dependency on rxdart
Browse files Browse the repository at this point in the history
  • Loading branch information
nrubin29 committed Jul 1, 2021
1 parent 6bccf0b commit 4970875
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 89 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
63 changes: 33 additions & 30 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,47 +40,50 @@ class _MyAppState extends State<MyApp> {
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'),
],
],
),
),
Expand Down
15 changes: 4 additions & 11 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions lib/flutter_acrcloud.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
library flutter_acrcloud;

export 'src/acrcloud_response.dart';
export 'src/flutter_acrcloud_impl.dart';
42 changes: 23 additions & 19 deletions lib/src/flutter_acrcloud_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,21 +17,19 @@ class ACRCloudConfig {

/// A recording session.
class ACRCloudSession {
final BehaviorSubject<ACRCloudResponse?> _result;

/// A Stream of volume values.
final BehaviorSubject<double> volume;
final Completer<ACRCloudResponse?> _result;
final StreamController<double> _volume;

/// A Future which resolves to null if the session is [cancel]led, or an
/// [ACRCloudResponse] otherwise.
late final Future<ACRCloudResponse?> result;
Future<ACRCloudResponse?> get result => _result.future;

/// A Stream of volume values.
Stream<double> get volumeStream => _volume.stream;

ACRCloudSession()
: _result = BehaviorSubject<ACRCloudResponse?>(),
volume = BehaviorSubject<double>() {
result = _result.first.catchError((_, __) => null,
test: (error) => error is StateError && error.message == 'No element');
}
: _result = Completer<ACRCloudResponse?>(),
_volume = StreamController<double>();

/// Cancel this session.
void cancel() {
Expand All @@ -41,8 +38,11 @@ class ACRCloudSession {
}

void dispose() {
volume.close();
_result.close();
_volume.close();

if (!_result.isCompleted) {
_result.complete();
}
}
}

Expand All @@ -53,19 +53,21 @@ 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<void> setUp(ACRCloudConfig config) async {
if (isSetUp) {
return;
}

_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)));
}
});

Expand All @@ -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(
Expand Down
Loading

0 comments on commit 4970875

Please sign in to comment.