Skip to content
Open
70 changes: 70 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# Visual Studio Code related
.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.packages
.pub-cache/
.pub/
/build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ android {

dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.2-4'
compile 'com.pusher:pusher-java-client:1.5.0'
compile 'com.pusher:pusher-java-client:1.8.2'
}
16 changes: 8 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class _MyAppState extends State<MyApp> {
Map _latestMessage;
PusherError _lastError;
PusherConnectionState _connectionState;
PusherFlutter pusher = new PusherFlutter("<your_key>");

// Ao instanciar passar o
PusherFlutter pusher = new PusherFlutter("<api-key>", cluster: "<cluster>");

@override
initState() {
Expand All @@ -27,7 +29,7 @@ class _MyAppState extends State<MyApp> {
}
});
});
pusher.onError.listen((err) => _lastError = err);
//pusher.onError.listen((err) => _lastError = err);
_connectionState = PusherConnectionState.disconnected;
}

Expand Down Expand Up @@ -117,19 +119,17 @@ class _MyAppState extends State<MyApp> {
void connect() {
pusher.connect();

pusher.subscribe("test_channel", "test_event");
pusher.subscribe("test_channel", "test_event2");
pusher.subscribe("my-channel", "my-event");

pusher.subscribeAll("test_channel", ["test_event3", "test_event4"]);
//pusher.subscribeAll("test_channel", ["test_event3", "test_event4"]);

pusher.onMessage.listen((pusher) {
setState(() => _latestMessage = pusher.body);
setState(() => _latestMessage = pusher.jsonBody);
});
}

void disconnect() {
pusher.unsubscribe("test_channel");
pusher.unsubscribe("test_channel2");
pusher.unsubscribe("my-channel");
pusher.disconnect();
}
}
39 changes: 15 additions & 24 deletions lib/pusher_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ class PusherFlutter {
///
/// The [apiKey] may not be null.
PusherFlutter(String apiKey, {String cluster}) {
_channel = new MethodChannel('plugins.apptreesoftware.com/pusher');
_channel = MethodChannel('plugins.apptreesoftware.com/pusher');
var args = {"api_key": apiKey};
if (cluster != null) {
args["cluster"] = cluster;
}
_channel.invokeMethod('create', args);
_connectivityEventChannel =
new EventChannel('plugins.apptreesoftware.com/pusher_connection');
_messageChannel =
new EventChannel('plugins.apptreesoftware.com/pusher_message');
_errorChannel =
new EventChannel('plugins.apptreesoftware.com/pusher_error');
_connectivityEventChannel = EventChannel('plugins.apptreesoftware.com/pusher_connection');
_messageChannel = EventChannel('plugins.apptreesoftware.com/pusher_message');
_errorChannel = EventChannel('plugins.apptreesoftware.com/pusher_error');
}

/// Connect to the pusher service.
Expand All @@ -51,8 +48,7 @@ class PusherFlutter {
/// provided to be delivered to the [onMessage] method. After calling this you
/// must listen to the [Stream] returned from [onMessage].
void subscribe(String channelName, String event) {
_channel
.invokeMethod('subscribe', {"channel": channelName, "event": event});
_channel.invokeMethod('subscribe', {"channel": channelName, "event": event});
}

/// Subscribe to the channel [channelName] for each [eventName] in [events]
Expand All @@ -74,21 +70,17 @@ class PusherFlutter {
/// Get the [Stream] of [PusherMessage] for the channels and events you've
/// signed up for.
///
Stream<PusherMessage> get onMessage =>
_messageChannel.receiveBroadcastStream().map(_toPusherMessage);
Stream<PusherMessage> get onMessage => _messageChannel.receiveBroadcastStream().map(_toPusherMessage);

Stream<PusherError> get onError =>
_errorChannel.receiveBroadcastStream().map(_toPusherError);
Stream<PusherError> get onError => _errorChannel.receiveBroadcastStream().map(_toPusherError);

/// Get a [Stream] of [PusherConnectionState] events.
/// Use this method to get notified about connection-related information.
///
Stream<PusherConnectionState> get onConnectivityChanged =>
_connectivityEventChannel
.receiveBroadcastStream()
.map(_connectivityStringToState);
Stream<PusherConnectionState> get onConnectivityChanged =>
_connectivityEventChannel.receiveBroadcastStream().map(_connectivityStringToState);

PusherConnectionState _connectivityStringToState(String string) {
PusherConnectionState _connectivityStringToState(dynamic string) {
switch (string) {
case 'connecting':
return PusherConnectionState.connecting;
Expand All @@ -108,23 +100,22 @@ class PusherFlutter {

PusherMessage _toPusherMessage(dynamic map) {
if (map is Map) {
var body = new Map<String, dynamic>.from(map['body']);
return new PusherMessage(map['channel'], map['event'], body);
return PusherMessage(map['channel'], map['event'], map['body']);
}
return null;
}

PusherError _toPusherError(Map map) {
return new PusherError(map['code'], map['message']);
PusherError _toPusherError(dynamic map) {
return PusherError(map['code'], map['message']);
}
}

class PusherMessage {
final String channelName;
final String eventName;
final Map<String, dynamic> body;
final dynamic jsonBody; // This body can be either JSON object or a list of JSON objects

PusherMessage(this.channelName, this.eventName, this.body);
PusherMessage(this.channelName, this.eventName, this.jsonBody);
}

class PusherError {
Expand Down
43 changes: 43 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
flutter:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
sdks:
dart: ">=2.2.2 <3.0.0"