Skip to content

Commit 1d21b90

Browse files
committed
dart project structure
1 parent 1eb9db1 commit 1d21b90

File tree

10 files changed

+127
-40
lines changed

10 files changed

+127
-40
lines changed

example/lib/main.dart

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import 'package:flutter/material.dart';
2-
import 'dart:async';
3-
4-
import 'package:flutter/services.dart';
5-
import 'package:reflex/reflex.dart';
62

73
void main() {
84
runApp(const MyApp());
@@ -16,34 +12,9 @@ class MyApp extends StatefulWidget {
1612
}
1713

1814
class _MyAppState extends State<MyApp> {
19-
String _platformVersion = 'Unknown';
20-
2115
@override
2216
void initState() {
2317
super.initState();
24-
initPlatformState();
25-
}
26-
27-
// Platform messages are asynchronous, so we initialize in an async method.
28-
Future<void> initPlatformState() async {
29-
String platformVersion;
30-
// Platform messages may fail, so we use a try/catch PlatformException.
31-
// We also handle the message potentially returning null.
32-
try {
33-
platformVersion =
34-
await Reflex.platformVersion ?? 'Unknown platform version';
35-
} on PlatformException {
36-
platformVersion = 'Failed to get platform version.';
37-
}
38-
39-
// If the widget was removed from the tree while the asynchronous platform
40-
// message was in flight, we want to discard the reply rather than calling
41-
// setState to update our non-existent appearance.
42-
if (!mounted) return;
43-
44-
setState(() {
45-
_platformVersion = platformVersion;
46-
});
4718
}
4819

4920
@override
@@ -53,8 +24,8 @@ class _MyAppState extends State<MyApp> {
5324
appBar: AppBar(
5425
title: const Text('Plugin example app'),
5526
),
56-
body: Center(
57-
child: Text('Running on: $_platformVersion\n'),
27+
body: const Center(
28+
child: Text('App Running!!\n'),
5829
),
5930
),
6031
);

example/pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ packages:
102102
url: "https://pub.dartlang.org"
103103
source: hosted
104104
version: "1.8.0"
105+
plugin_platform_interface:
106+
dependency: transitive
107+
description:
108+
name: plugin_platform_interface
109+
url: "https://pub.dartlang.org"
110+
source: hosted
111+
version: "2.0.2"
105112
reflex:
106113
dependency: "direct main"
107114
description:

lib/reflex.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
21
import 'dart:async';
32

4-
import 'package:flutter/services.dart';
3+
import 'package:reflex/src/platform/reflex_platform.dart';
54

6-
class Reflex {
7-
static const MethodChannel _channel = MethodChannel('reflex');
5+
export 'package:reflex/src/helper/helper.dart';
86

9-
static Future<String?> get platformVersion async {
10-
final String? version = await _channel.invokeMethod('getPlatformVersion');
11-
return version;
7+
class Reflex {
8+
/// [notificationStream] returns stream of notifications.
9+
Stream<String> get notificationStream {
10+
return ReflexPlatform.instance.notificationStream;
1211
}
1312
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
import 'dart:io';
12+
13+
import 'package:flutter/services.dart';
14+
import 'package:reflex/src/helper/exception/reflex_exception.dart';
15+
import 'package:reflex/src/platform/reflex_platform.dart';
16+
17+
class ReflexHandler extends ReflexPlatform {
18+
// static const MethodChannel _methodChannel = MethodChannel('reflex');
19+
20+
static const EventChannel _eventChannel =
21+
EventChannel('reflex_event_channel');
22+
23+
// MethodChannel get methodChannel => _methodChannel;
24+
EventChannel get eventChannel => _eventChannel;
25+
26+
static Stream<String>? _notificationStream;
27+
28+
@override
29+
Stream<String> get notificationStream {
30+
if (Platform.isAndroid) {
31+
_notificationStream ??=
32+
_eventChannel.receiveBroadcastStream().map<String>((event) => event);
33+
return _notificationStream!;
34+
}
35+
throw ReflexException(
36+
'Reflex.notificationStream is only supported on Android.');
37+
}
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 ReflexException implements Exception {
12+
String message;
13+
14+
ReflexException(this.message);
15+
16+
@override
17+
String toString() {
18+
return "ReflexException: $message";
19+
}
20+
}

lib/src/helper/helper.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
export 'exception/reflex_exception.dart';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
12+
import 'package:reflex/src/handler/reflex_handler.dart';
13+
14+
abstract class ReflexPlatform extends PlatformInterface {
15+
ReflexPlatform() : super(token: _token);
16+
17+
static final Object _token = Object();
18+
19+
static ReflexPlatform _instance = ReflexHandler();
20+
21+
/// returns the instance of the [ReflexHandler].
22+
static ReflexPlatform get instance => _instance;
23+
24+
static set instance(ReflexPlatform instance) {
25+
PlatformInterface.verifyToken(instance, _token);
26+
_instance = instance;
27+
}
28+
29+
Stream<String> get notificationStream {
30+
throw UnimplementedError(
31+
'Getter notificationStream has not been implemented');
32+
}
33+
}

pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ packages:
9595
url: "https://pub.dartlang.org"
9696
source: hosted
9797
version: "1.8.0"
98+
plugin_platform_interface:
99+
dependency: "direct main"
100+
description:
101+
name: plugin_platform_interface
102+
url: "https://pub.dartlang.org"
103+
source: hosted
104+
version: "2.0.2"
98105
sky_engine:
99106
dependency: transitive
100107
description: flutter

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ environment:
1010
dependencies:
1111
flutter:
1212
sdk: flutter
13+
plugin_platform_interface: ^2.0.2
1314

1415
dev_dependencies:
1516
flutter_test:

test/reflex_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/services.dart';
22
import 'package:flutter_test/flutter_test.dart';
3-
import 'package:reflex/reflex.dart';
3+
// import 'package:reflex/reflex.dart';
44

55
void main() {
66
const MethodChannel channel = MethodChannel('reflex');
@@ -18,6 +18,6 @@ void main() {
1818
});
1919

2020
test('getPlatformVersion', () async {
21-
expect(await Reflex.platformVersion, '42');
21+
// expect(await Reflex.platformVersion, '42');
2222
});
2323
}

0 commit comments

Comments
 (0)