Compare commits
2 Commits
32f39471b8
...
c7e8621c94
Author | SHA1 | Date |
---|---|---|
Pascal Abresch | c7e8621c94 | |
Pascal Abresch | b1073b1b4a |
|
@ -1,12 +1,13 @@
|
|||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
class Matrix {
|
||||
|
||||
// TODO Error handeling
|
||||
static Future<String> request({@required Uri uri, @required final String method, final String accessToken, final String jsonPayload}) async {
|
||||
final client = new HttpClient();
|
||||
print(uri);
|
||||
if (method == 'GET') {
|
||||
requestMethod(Uri uri) => client.getUrl(uri);
|
||||
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
||||
|
@ -20,7 +21,7 @@ class Matrix {
|
|||
requestMethod(Uri uri) => client.deleteUrl(uri);
|
||||
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
||||
} else {
|
||||
throw 'bicycles at dartlang';
|
||||
throw method + ' aint no method i ever heard off, they speak english in ' + method + '?';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,6 +40,18 @@ class Matrix {
|
|||
});
|
||||
}
|
||||
|
||||
static Future<void> sendMessage({@required Uri uri, @required final String accessToken, @required final String roomid, @required final String message}) async {
|
||||
var rand = Random();
|
||||
//String txid = (String)rand.nextInt(2^32);
|
||||
String txid = '3';
|
||||
var json = new JsonCodec();
|
||||
String payload = '{"msgtype":"m.text","body":' + json.encode(message) + '}';
|
||||
print(payload);
|
||||
request(uri: uri.replace(path: '/_matrix/client/r0/rooms/' + Uri.encodeComponent(roomid) + '/send/m.room.message/' + txid), method: 'PUT', accessToken: accessToken, jsonPayload: payload).then((cont) {
|
||||
print(cont);
|
||||
});
|
||||
}
|
||||
|
||||
static Future<List<String>> joinedRooms(final String server, final String accessToken) async {
|
||||
return await request(uri: Uri.parse(server).replace(path: "/_matrix/client/r0/joined_rooms"), method: 'GET', accessToken: accessToken).then ((cont) {
|
||||
var json = new JsonDecoder().convert(cont);
|
||||
|
@ -72,4 +85,18 @@ class Matrix {
|
|||
request(uri: uri.replace(path:"/_matrix/client/r0/logout"), method: "POST", accessToken: accessToken);
|
||||
}
|
||||
|
||||
static Future<void> sync({@required Uri uri, @required final String accessToken}) async {
|
||||
request(uri: uri.replace(path:"/_matrix/client/r0/sync"), method: 'GET', accessToken: accessToken).then((cont) {
|
||||
var json = new JsonDecoder().convert(cont);
|
||||
print(json);
|
||||
});
|
||||
}
|
||||
static Future<List<dynamic>> roomsync({@required Uri uri, @required final String accessToken, @required final String roomid, final String paginationToken}) async {
|
||||
return await request(uri: uri.replace(path:"/_matrix/client/r0/rooms/" + Uri.encodeComponent(roomid) + "/initialSync"), method: 'GET', accessToken: accessToken).then((cont) {
|
||||
var json = new JsonDecoder().convert(cont);
|
||||
var john = json["messages"]["chunk"];
|
||||
print("done");
|
||||
return john;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,20 +2,70 @@
|
|||
//import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../matrix/cs-r0.5.0';
|
||||
|
||||
class RoomPage extends StatelessWidget {
|
||||
class RoomPage extends StatefulWidget {
|
||||
final String roomid;
|
||||
final String roomname;
|
||||
final SharedPreferences settings;
|
||||
RoomPage({Key key, @required this.settings, @required this.roomid, @required this.roomname}) : super(key: key);
|
||||
|
||||
@override
|
||||
_RoomPageState createState() => _RoomPageState();
|
||||
}
|
||||
|
||||
class _RoomPageState extends State<RoomPage> {
|
||||
List<Widget> _messages = List<Widget>();
|
||||
|
||||
final _inputController = TextEditingController();
|
||||
|
||||
void initState() {
|
||||
_messages = [ ];
|
||||
_updateMessages();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void _sendMessage(@required String str) {
|
||||
var set = widget.settings;
|
||||
Matrix.sendMessage(uri: Uri.parse(set.getString("home_server")), accessToken: set.getString("access_token"), roomid: widget.roomid, message: str);
|
||||
_inputController.text = "";
|
||||
}
|
||||
|
||||
void _updateMessages() {
|
||||
var set = widget.settings;
|
||||
var cont = Matrix.roomsync(uri: Uri.parse(set.getString("home_server")), roomid: widget.roomid, accessToken: set.getString("access_token")).then((cont) {
|
||||
_messages= [ ];
|
||||
for (var neko in cont) {
|
||||
if (neko["type"] == "m.room.message") {
|
||||
_messages.add(Text(neko["sender"] + ": " + neko["content"]["body"], textAlign: TextAlign.left));
|
||||
}
|
||||
}
|
||||
setState(() {
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(roomname),
|
||||
title: Text(widget.roomname),
|
||||
),
|
||||
body: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: Column(
|
||||
children: _messages
|
||||
),
|
||||
),
|
||||
TextField(
|
||||
onSubmitted: _sendMessage,
|
||||
controller: _inputController,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Center( child:Text(roomid))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue