Skip to content

Commit bb526ab

Browse files
committed
passing data to java
1 parent 24ae3b3 commit bb526ab

File tree

8 files changed

+116
-12
lines changed

8 files changed

+116
-12
lines changed

android/src/main/java/com/devsonflutter/reflex/EventCallHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import com.devsonflutter.reflex.notification.ReflexNotification;
2323
import com.devsonflutter.reflex.permission.NotificationPermission;
2424

25+
import java.util.HashMap;
26+
import java.util.List;
27+
2528
import io.flutter.Log;
2629
import io.flutter.plugin.common.EventChannel;
2730

@@ -41,6 +44,7 @@ public class EventCallHandler implements EventChannel.StreamHandler {
4144
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
4245
@Override
4346
public void onListen(Object arguments, EventChannel.EventSink events) {
47+
Log.w(TAG,arguments.toString());
4448
mEventSink = events;
4549
listenNotification(mEventSink);
4650
Log.w(TAG,"Listening Reflex Stream...");

example/lib/main.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ class MyApp extends StatefulWidget {
1717
}
1818

1919
class _MyAppState extends State<MyApp> {
20-
Reflex? _reflex;
2120
StreamSubscription<NotificationEvent>? _subscription;
2221
final List<NotificationEvent> _log = [];
2322
bool started = false;
2423

24+
Reflex reflex = Reflex(
25+
debug: true,
26+
autoReply: AutoReply(
27+
packageName: "com.whatsapp",
28+
message: "Hello",
29+
),
30+
);
31+
2532
@override
2633
void initState() {
2734
super.initState();
@@ -41,9 +48,8 @@ class _MyAppState extends State<MyApp> {
4148
}
4249

4350
void startListening() {
44-
_reflex = Reflex();
4551
try {
46-
_subscription = _reflex!.notificationStream!.listen(onData);
52+
_subscription = reflex.notificationStream!.listen(onData);
4753
setState(() {
4854
started = true;
4955
});

lib/reflex.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,32 @@ for more details.
1111
import 'dart:async';
1212

1313
import 'package:reflex/src/helper/events/notification_event.dart';
14+
import 'package:reflex/src/helper/helper.dart';
1415
import 'package:reflex/src/platform/reflex_platform.dart';
1516

1617
export 'package:reflex/src/helper/helper.dart';
1718

1819
class Reflex {
20+
/// [Reflex] Constructor
21+
Reflex({
22+
this.debug = false,
23+
this.autoReply,
24+
}) {
25+
init();
26+
}
27+
28+
final bool debug;
29+
final AutoReply? autoReply;
30+
late final ReflexPlatform reflexPlatform;
31+
32+
// Initialize [ReflexPlatform] with [debug] and [autoReply]
33+
void init() {
34+
reflexPlatform = ReflexPlatform.instance;
35+
reflexPlatform.init(debug: debug, autoReply: autoReply);
36+
}
37+
1938
/// [notificationStream] returns stream of notifications.
2039
Stream<NotificationEvent>? get notificationStream {
21-
return ReflexPlatform.instance.notificationStream;
40+
return reflexPlatform.notificationStream;
2241
}
2342
}

lib/src/handler/reflex_handler.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'dart:io';
1313
import 'package:flutter/services.dart';
1414
import 'package:reflex/src/helper/events/notification_event.dart';
1515
import 'package:reflex/src/helper/exception/reflex_exception.dart';
16+
import 'package:reflex/src/helper/helper.dart';
1617
import 'package:reflex/src/platform/reflex_platform.dart';
1718

1819
class ReflexHandler extends ReflexPlatform {
@@ -25,13 +26,28 @@ class ReflexHandler extends ReflexPlatform {
2526

2627
Stream<NotificationEvent>? _notificationStream;
2728

29+
late final List<dynamic> arguments;
30+
31+
@override
32+
void init({
33+
required bool debug,
34+
AutoReply? autoReply,
35+
}) {
36+
Map<String, dynamic> map = {
37+
"debug": debug,
38+
"autoReply": autoReply?.deserialize(),
39+
};
40+
arguments = [map];
41+
}
42+
2843
@override
2944
Stream<NotificationEvent>? get notificationStream {
3045
if (Platform.isAndroid) {
31-
_notificationStream ??=
32-
_eventChannel.receiveBroadcastStream().map<NotificationEvent>(
33-
(event) => NotificationEvent.getNotificationEventFromMap(event),
34-
);
46+
_notificationStream ??= _eventChannel
47+
.receiveBroadcastStream(arguments)
48+
.map<NotificationEvent>(
49+
(event) => NotificationEvent.getNotificationEventFromMap(event),
50+
);
3551
return _notificationStream;
3652
}
3753
throw ReflexException('notificationStream is only supported on Android!');

lib/src/helper/exception/reflex_exception.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ class ReflexException implements Exception {
1818
return "ReflexException: $message";
1919
}
2020
}
21+
22+
class ReflexNullException implements Exception {
23+
String message;
24+
25+
ReflexNullException(this.message);
26+
27+
@override
28+
String toString() {
29+
return "ReflexNullException: $message";
30+
}
31+
}

lib/src/helper/helper.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ for more details.
1010

1111
export 'exception/reflex_exception.dart';
1212
export 'events/notification_event.dart';
13+
export 'model/auto_reply.dart';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
3+
Copyright (c) 2022 DevsOnFlutter (Devs On Flutter)
4+
All rights reserved.
5+
6+
The plugin is governed by the BSD-3-clause License. Please see the LICENSE file
7+
for more details.
8+
9+
*/
10+
11+
class AutoReply {
12+
AutoReply({
13+
required this.packageName,
14+
required this.message,
15+
});
16+
17+
/// Package Name for [AutoReply]
18+
String packageName;
19+
20+
/// Notification Message
21+
String message;
22+
23+
factory AutoReply.fromMap(Map<dynamic, dynamic> map) {
24+
// Error Handling if map does not contain package name or message
25+
if (!map.containsKey('packageName') || !map.containsKey('message')) {
26+
throw Exception('Map does not contain package name or message');
27+
}
28+
29+
String packageName = map['packageName'];
30+
String message = map['message'];
31+
32+
return AutoReply(
33+
packageName: packageName,
34+
message: message,
35+
);
36+
}
37+
38+
Map<String, dynamic> deserialize() {
39+
return <String, dynamic>{
40+
'packageName': packageName,
41+
'message': message,
42+
};
43+
}
44+
}

lib/src/platform/reflex_platform.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@ for more details.
1111
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
1212
import 'package:reflex/src/handler/reflex_handler.dart';
1313
import 'package:reflex/src/helper/events/notification_event.dart';
14+
import 'package:reflex/src/helper/helper.dart';
1415

1516
abstract class ReflexPlatform extends PlatformInterface {
1617
ReflexPlatform() : super(token: _token);
1718

1819
static final Object _token = Object();
1920

20-
static ReflexPlatform _instance = ReflexHandler();
21+
static final ReflexPlatform _instance = ReflexHandler();
2122

2223
/// returns the instance of the [ReflexHandler].
2324
static ReflexPlatform get instance => _instance;
2425

25-
static set instance(ReflexPlatform instance) {
26-
PlatformInterface.verifyToken(instance, _token);
27-
_instance = instance;
26+
void init({
27+
required bool debug,
28+
AutoReply? autoReply,
29+
}) {
30+
throw UnimplementedError('init has not been implemented for reflex.');
2831
}
2932

3033
Stream<NotificationEvent>? get notificationStream {

0 commit comments

Comments
 (0)