Skip to content

Commit 1ae4d09

Browse files
committed
整理,修改readme,例子
1 parent 09c6b8a commit 1ae4d09

20 files changed

+1219
-51
lines changed

.vscode/launch.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
"type": "dart",
1010
"request": "launch",
1111
"program": "example/lib/main.dart"
12-
},
13-
{
14-
"name": "Flutter",
15-
"request": "launch",
16-
"type": "dart"
1712
}
1813
]
1914
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Parse Server Dart
2+
23
A rewrite of a library hosted on GitHub. This is not my own content but based on a library already created and looks to be abandoned.
34

45
https://github.com/lotux/parse_server_dart
56

6-
## Join in!
7-
Want to get involved? Join our Slack channel and help out! FlutterParseSDK.Slack.com
7+
## example 目录有完整测试
88

99
## Getting Started
1010

example/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# flutter_plugin_example
1+
# flutter_parse_client_example
22

3-
Demonstrates how to use the flutter_plugin plugin.
3+
Demonstrates how to use the flutter parse client.
44

5-
## Getting Started
5+
## html ok
66

7-
For help getting started with Flutter, view our online
8-
[documentation](https://flutter.io/).
7+
## websoket ok
8+
9+
## bloc

example/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
FlutterApplication and put your custom class here. -->
1515
<application
1616
android:name="io.flutter.app.FlutterApplication"
17-
android:label="flutter_plugin_example"
17+
android:label="parse_example"
1818
android:icon="@mipmap/ic_launcher">
1919
<activity
2020
android:name=".MainActivity"

example/lib/ApplicationBloc.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'dart:async';
2+
3+
import 'package:Parse_example/blocProvider.dart';
4+
import 'package:parse_server_sdk/network/parse_livequery.dart';
5+
6+
class ApplicationBloc implements BlocBase {
7+
///
8+
/// Synchronous Stream to handle the provision of the movie genres
9+
///
10+
StreamController<List> _syncController = StreamController<List>.broadcast();
11+
Stream<List> get outParseStream => _syncController.stream;
12+
13+
///
14+
StreamController<List> _cmdController = StreamController<List>.broadcast();
15+
StreamSink get getMovieGenres => _cmdController.sink;
16+
17+
List _genresList;
18+
19+
ApplicationBloc(LiveQuery liveQuery) {
20+
// _genresList = liveQuery.channel as List;
21+
// Read all genres from Internet
22+
// api.movieGenres().then((list) {
23+
// _genresList = list;
24+
// });
25+
_syncController.stream.listen((_) {
26+
_syncController.sink.addStream(liveQuery.channel.stream);
27+
});
28+
// _cmdController.stream.listen((_) {
29+
// _syncController.sink
30+
// .add(UnmodifiableListView<MovieGenre>(_genresList.genres));
31+
// });
32+
}
33+
34+
void dispose() {
35+
_syncController.close();
36+
_cmdController.close();
37+
}
38+
}

example/lib/IncrementBloc.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'dart:async';
2+
3+
import 'package:Parse_example/blocProvider.dart';
4+
5+
class IncrementBloc implements BlocBase {
6+
int _counter;
7+
8+
// 处理counter的stream
9+
StreamController<int> _counterController = StreamController<int>();
10+
StreamSink<int> get _inAdd => _counterController.sink;
11+
Stream<int> get outCounter => _counterController.stream;
12+
13+
// 处理业务逻辑的stream
14+
StreamController _actionController = StreamController();
15+
StreamSink get incrementCounter => _actionController.sink;
16+
17+
// 构造器
18+
IncrementBloc() {
19+
_counter = 0;
20+
_actionController.stream.listen(_handleLogic);
21+
}
22+
23+
void dispose() {
24+
_actionController.close();
25+
_counterController.close();
26+
}
27+
28+
void _handleLogic(data) {
29+
_counter = _counter + 1;
30+
_inAdd.add(_counter);
31+
}
32+
}

example/lib/application_constants.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ abstract class ApplicationConstants {
33
static const String PARSE_APPLICATION_ID = "myAppId";
44
static const String PARSE_MASTER_KEY = "123456";
55
static const String PARSE_SERVER_URL = "http://118.24.162.252:2018/parse";
6+
static const String PARSE_LIVE_SERVER_URL = "ws://118.24.162.252:2018/parse";
67
}

example/lib/blocProvider.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:flutter/material.dart';
2+
3+
abstract class BlocBase {
4+
void dispose();
5+
}
6+
7+
class BlocProvider<T extends BlocBase> extends StatefulWidget {
8+
BlocProvider({
9+
Key key,
10+
@required this.child,
11+
@required this.bloc,
12+
}) : super(key: key);
13+
14+
final T bloc;
15+
final Widget child;
16+
17+
@override
18+
_BlocProviderState<T> createState() => _BlocProviderState<T>();
19+
20+
static T of<T extends BlocBase>(BuildContext context) {
21+
final type = _typeOf<BlocProvider<T>>();
22+
BlocProvider<T> provider = context.ancestorWidgetOfExactType(type);
23+
return provider.bloc;
24+
}
25+
26+
static Type _typeOf<T>() => T;
27+
}
28+
29+
class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> {
30+
@override
31+
void dispose() {
32+
widget.bloc.dispose();
33+
super.dispose();
34+
}
35+
36+
@override
37+
Widget build(BuildContext context) {
38+
return widget.child;
39+
}
40+
}

example/lib/count_bloc.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'dart:async';
2+
import 'package:Parse_example/blocProvider.dart';
3+
import 'package:rxdart/rxdart.dart';
4+
5+
class CountBLoC implements BlocBase {
6+
int _count = 0;
7+
var _countController = StreamController<int>.broadcast();
8+
var _subject = BehaviorSubject<int>();
9+
Stream<int> get stream => _subject.stream;
10+
int get value => _count;
11+
12+
increment() {
13+
_countController.sink.add(++_count);
14+
}
15+
16+
dispose() {
17+
_countController.close();
18+
}
19+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import 'dart:convert';
2+
3+
import 'package:flutter/foundation.dart';
4+
import 'WebSocketsNotifications.dart';
5+
6+
///
7+
/// Again, application-level global variable
8+
///
9+
GameCommunication game = new GameCommunication();
10+
11+
class GameCommunication {
12+
static final GameCommunication _game = new GameCommunication._internal();
13+
14+
///
15+
/// At first initialization, the player has not yet provided any name
16+
///
17+
String _playerName = "";
18+
19+
///
20+
/// Before the "join" action, the player has no unique ID
21+
///
22+
String _playerID = "";
23+
24+
factory GameCommunication() {
25+
return _game;
26+
}
27+
28+
GameCommunication._internal() {
29+
///
30+
/// Let's initialize the WebSockets communication
31+
///
32+
sockets.initCommunication();
33+
34+
///
35+
/// and ask to be notified as soon as a message comes in
36+
///
37+
sockets.addListener(_onMessageReceived);
38+
}
39+
40+
///
41+
/// Getter to return the player's name
42+
///
43+
String get playerName => _playerName;
44+
45+
/// ----------------------------------------------------------
46+
/// Common handler for all received messages, from the server
47+
/// ----------------------------------------------------------
48+
_onMessageReceived(serverMessage) {
49+
///
50+
/// As messages are sent as a String
51+
/// let's deserialize it to get the corresponding
52+
/// JSON object
53+
///
54+
Map message = json.decode(serverMessage);
55+
56+
switch (message["action"]) {
57+
58+
///
59+
/// When the communication is established, the server
60+
/// returns the unique identifier of the player.
61+
/// Let's record it
62+
///
63+
case 'connect':
64+
_playerID = message["data"];
65+
break;
66+
67+
///
68+
/// For any other incoming message, we need to
69+
/// dispatch it to all the listeners
70+
///
71+
default:
72+
_listeners.forEach((Function callback) {
73+
callback(message);
74+
});
75+
break;
76+
}
77+
}
78+
79+
/// ----------------------------------------------------------
80+
/// Common method to send requests to the server
81+
/// ----------------------------------------------------------
82+
send(String action, String data) {
83+
///
84+
/// When a player joins, we need to record the name
85+
/// he provides
86+
///
87+
if (action == 'join') {
88+
_playerName = data;
89+
}
90+
91+
///
92+
/// Send the action to the server
93+
/// To send the message, we need to serialize the JSON
94+
///
95+
sockets.send(json.encode({"action": action, "data": data}));
96+
}
97+
98+
/// ==========================================================
99+
///
100+
/// Listeners to allow the different pages to be notified
101+
/// when messages come in
102+
///
103+
ObserverList<Function> _listeners = new ObserverList<Function>();
104+
105+
/// ---------------------------------------------------------
106+
/// Adds a callback to be invoked in case of incoming
107+
/// notification
108+
/// ---------------------------------------------------------
109+
addListener(Function callback) {
110+
_listeners.add(callback);
111+
}
112+
113+
removeListener(Function callback) {
114+
_listeners.remove(callback);
115+
}
116+
}

0 commit comments

Comments
 (0)