Compare commits
6 Commits
733b3ff4ab
...
51d96153bb
Author | SHA1 | Date |
---|---|---|
Pascal Abresch | 51d96153bb | |
Pascal Abresch | d6d173653a | |
Pascal Abresch | b94862ef06 | |
Pascal Abresch | 03bcc0e0fb | |
Pascal Abresch | 9ae2aef8e4 | |
Pascal Abresch | 3de56870a0 |
|
@ -2,6 +2,8 @@ import 'dart:io';
|
|||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'main.dart';
|
||||
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
LoginPage({Key key, this.settings}) : super(key: key);
|
||||
|
@ -13,25 +15,29 @@ class LoginPage extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _LoginPageState extends State<LoginPage> {
|
||||
final String def_home_server = "https://matrix.org";
|
||||
static const String def_home_server = "https://pink.packageloss.eu:8448";
|
||||
|
||||
String _matrix_id;
|
||||
String _matrixId;
|
||||
String _password;
|
||||
String _home_server;
|
||||
String _identity_server;
|
||||
String _homeServer = "https://pink.packageloss.eu:8448";
|
||||
|
||||
String _failure_message = "";
|
||||
String _failureMessage = "";
|
||||
|
||||
void _showMainView() {
|
||||
|
||||
Navigator.pushReplacement(
|
||||
context, MaterialPageRoute(builder: (BuildContext context) => MyHomePage(settings: widget.settings)));
|
||||
}
|
||||
void _doLogin() async {
|
||||
var client = new HttpClient();
|
||||
await client.postUrl(Uri.parse(_home_server)
|
||||
await client.postUrl(Uri.parse(_homeServer)
|
||||
.replace(path: "/_matrix/client/r0/login")).then((req) {
|
||||
req.headers.contentType = new ContentType("application", "json", charset: "utf-8");
|
||||
var data = new JsonEncoder().convert(<String,dynamic>{
|
||||
"type": "m.login.password",
|
||||
"identifier": <String,String>{
|
||||
"type": "m.id.user",
|
||||
"user": _matrix_id,
|
||||
"user": _matrixId,
|
||||
},
|
||||
"password": _password,
|
||||
"initial_device_display_name": "Hermes alpha",
|
||||
|
@ -43,14 +49,18 @@ class _LoginPageState extends State<LoginPage> {
|
|||
var json = new JsonDecoder().convert(cont);
|
||||
// I hate switch statements
|
||||
if (resp.statusCode == 403) {
|
||||
_failure_message = json["error"];
|
||||
_failureMessage = json["error"];
|
||||
} else if (resp.statusCode == 200) {
|
||||
var s = widget.settings;
|
||||
print(json);
|
||||
s.setString("access_token", json["access_token"]);
|
||||
s.setString("matrix_id", json["matrix_id"]);
|
||||
s.setString("home_server", json["well_known"]["m.homeserver"]["base_url"]);
|
||||
s.setString("matrix_id", json["user_id"]);
|
||||
//this means the hs uri was correct, but not neccesarily what the server gives us
|
||||
s.setString("home_server", Uri.parse(_homeServer).toString()); // jep, stealing that again
|
||||
//s.setString("home_server", json["home_server"]);
|
||||
s.setString("device_id", json["device_id"]);
|
||||
s.setBool("logged_in", true);
|
||||
_showMainView();
|
||||
} else {
|
||||
print(resp.statusCode);
|
||||
print(json);
|
||||
|
@ -68,25 +78,22 @@ class _LoginPageState extends State<LoginPage> {
|
|||
body: Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
child: ListView(
|
||||
children: <Widget>[
|
||||
TextField(autocorrect: false, onChanged: (s) { _matrixId = s; }),
|
||||
Text("Matrix ID"),
|
||||
TextField(autocorrect: false, onChanged: (s) { _matrix_id = s; }),
|
||||
Text("Password"),
|
||||
TextField(autocorrect: false, obscureText: true, onChanged: (s) { _password = s; }),
|
||||
Text("Home server"),
|
||||
Text("Password"),
|
||||
TextField(
|
||||
autocorrect: false,
|
||||
controller: TextEditingController(text: def_home_server),
|
||||
onChanged: (s) { _home_server = s; }
|
||||
onChanged: (s) { _homeServer = s; }
|
||||
),
|
||||
Text("Identity server"),
|
||||
TextField(autocorrect: false, onChanged: (s) { _identity_server = s; }),
|
||||
Text("Home server"),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: <Widget>[
|
||||
Text(_failure_message),
|
||||
Text(_failureMessage),
|
||||
RaisedButton(
|
||||
child: Text("Login"),
|
||||
onPressed: () { setState(() { _doLogin(); }); },
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'login.dart';
|
||||
import 'matrix.dart';
|
||||
import 'matrix/cs-r0.5.0';
|
||||
import 'page/room.dart';
|
||||
|
||||
void main() async {
|
||||
var settings = await SharedPreferences.getInstance();
|
||||
|
@ -26,7 +27,7 @@ void main() async {
|
|||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
MyHomePage({Key key, this.settings}) : super(key: key);
|
||||
MyHomePage({Key key, @required this.settings}) : super(key: key);
|
||||
|
||||
final SharedPreferences settings;
|
||||
|
||||
|
@ -35,15 +36,15 @@ class MyHomePage extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
Map<String, String> room_map = Map<String, String>();
|
||||
Map<String, String> roomMap = Map<String, String>();
|
||||
|
||||
void _updateRooms() {
|
||||
Matrix.get_joined_rooms(
|
||||
Matrix.joinedRooms(
|
||||
widget.settings.getString("home_server"),
|
||||
widget.settings.getString("access_token")
|
||||
).then((r) {
|
||||
for (String roomid in r) {
|
||||
Matrix.get_room_attr(
|
||||
Matrix.roomAtribute(
|
||||
widget.settings.getString("home_server"),
|
||||
widget.settings.getString("access_token"),
|
||||
roomid,
|
||||
|
@ -51,51 +52,70 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
"name",
|
||||
).then((roomname) {
|
||||
setState(() {
|
||||
room_map[roomid] = roomname;
|
||||
roomMap[roomid] = roomname;
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _logout() {
|
||||
var set = widget.settings;
|
||||
set.setBool("logged_in", false);
|
||||
// TODO Actually invalidate access_token
|
||||
set.setString("access_token", "");
|
||||
}
|
||||
|
||||
void initState() {
|
||||
_updateRooms();
|
||||
|
||||
print(Matrix.supportedVersions(widget.settings.getString("home_server")));
|
||||
super.initState();
|
||||
}
|
||||
List<Widget> room_entries(room_map) {
|
||||
List<Widget> new_widgets = new List<Widget>();
|
||||
room_map.forEach((roomid, roomname) {
|
||||
|
||||
List<Widget> roomEntries(roomMap) {
|
||||
List<Widget> newWidgets = new List<Widget>();
|
||||
roomMap.forEach((roomid, roomname) {
|
||||
if (roomname != null && roomname != "") {
|
||||
new_widgets.add(
|
||||
GestureDetector(
|
||||
child: Container(
|
||||
height: 30,
|
||||
alignment: Alignment.centerLeft,
|
||||
child : Text(roomname)
|
||||
),
|
||||
onTap: () {
|
||||
print(roomid); // call other activity here!!
|
||||
// TODO: addd avatar or something
|
||||
},
|
||||
newWidgets.add(
|
||||
ListTile(
|
||||
title: Text(roomname),
|
||||
onTap: () {
|
||||
print(roomid);
|
||||
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => RoomPage(settings: widget.settings, roomid: roomid, roomname: roomname),
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
)
|
||||
);
|
||||
} else {
|
||||
new_widgets.add(GestureDetector( child: Text(roomid), onTap: () { print(roomid); }));
|
||||
newWidgets.add(ListTile( title: Text(roomid), onTap: () { print(roomid); }));
|
||||
}
|
||||
});
|
||||
return new_widgets;
|
||||
return newWidgets;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Hermes"),
|
||||
actions: <Widget>[
|
||||
IconButton(
|
||||
icon: Icon(Icons.link_off),
|
||||
tooltip: "Logout",
|
||||
onPressed: _logout,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Center(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.all(8.0),
|
||||
children: room_entries(room_map)
|
||||
children: roomEntries(roomMap)
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
|
||||
class Matrix {
|
||||
// TODO Error handeling
|
||||
static Future<List<String>> get_joined_rooms(String server, String access_token) async {
|
||||
print("fetching room list");
|
||||
var client = new HttpClient();
|
||||
return await client.getUrl(Uri.parse(server)
|
||||
.replace(path: "/_matrix/client/r0/joined_rooms")).then((req) {
|
||||
req.headers.contentType = new ContentType("application", "json", charset: "utf-8");
|
||||
req.headers.add("Authorization", "Bearer " + access_token);
|
||||
return req.close();
|
||||
}).then((resp) {
|
||||
return resp.transform(utf8.decoder).join();
|
||||
}).then((cont) {
|
||||
var json = new JsonDecoder().convert(cont);
|
||||
return json["joined_rooms"].cast<String>();
|
||||
}).catchError((error) {
|
||||
print(error);
|
||||
});
|
||||
}
|
||||
static Future<String> get_room_attr(String server, String access_token, String roomid, String mx_type, String json_type) async {
|
||||
print("fetching " + mx_type + " for " + roomid);
|
||||
var client = new HttpClient();
|
||||
return await client.getUrl(Uri.parse(server)
|
||||
.replace(path: "/_matrix/client/r0/rooms/" + roomid + "/state/m.room." + mx_type)).then((req) {
|
||||
req.headers.contentType = new ContentType("application", "json", charset: "utf-8");
|
||||
req.headers.add("Authorization", "Bearer " + access_token);
|
||||
return req.close();
|
||||
}).then((resp) {
|
||||
return resp.transform(utf8.decoder).join();
|
||||
}).then((cont) {
|
||||
var json = new JsonDecoder().convert(cont);
|
||||
return json[json_type];
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
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();
|
||||
if (method == 'GET') {
|
||||
requestMethod(Uri uri) => client.getUrl(uri);
|
||||
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
||||
} else if (method == 'POST') {
|
||||
requestMethod(Uri uri) => client.postUrl(uri);
|
||||
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
||||
} else if (method == 'PUT') {
|
||||
requestMethod(Uri uri) => client.putUrl(uri);
|
||||
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
||||
} else if (method == 'DELETE') {
|
||||
requestMethod(Uri uri) => client.deleteUrl(uri);
|
||||
return await requestsend(uri: uri, function: requestMethod, accessToken: accessToken, jsonPayload: jsonPayload);
|
||||
} else {
|
||||
throw 'bicycles at dartlang';
|
||||
}
|
||||
}
|
||||
|
||||
static Future<String> requestsend({@required Uri uri, @required final function, final String accessToken, final String jsonPayload}) async {
|
||||
return await function(uri).then((req) {
|
||||
if (accessToken != null) {
|
||||
req.headers.add("Authorization", "Bearer " + accessToken);
|
||||
}
|
||||
if (jsonPayload != null) {
|
||||
req.headers.contentType = new ContentType("application", "json", charset: "utf-8");
|
||||
req.write(jsonPayload);
|
||||
}
|
||||
return req.close();
|
||||
}).then((response) {
|
||||
return response.transform(utf8.decoder).join();
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
return json["joined_rooms"].cast<String>();
|
||||
});
|
||||
}
|
||||
|
||||
static Future<String> roomAtribute(final String server, final String accessToken, final String roomId, final String mxType, final String jsonType) async {
|
||||
return await request(
|
||||
uri: Uri.parse(server)
|
||||
.replace(path: "/_matrix/client/r0/rooms/" + roomId + "/state/m.room." + mxType),
|
||||
method: 'GET',
|
||||
accessToken: accessToken).then((cont) {
|
||||
var json = new JsonDecoder().convert(cont);
|
||||
return json[jsonType];
|
||||
});
|
||||
}
|
||||
|
||||
static Future<List<String>> supportedVersions(String server) async {
|
||||
return await request(
|
||||
uri: Uri.parse(server)
|
||||
.replace(path: "/_matrix/client/versions"),
|
||||
method: 'GET',
|
||||
).then((cont) {
|
||||
var json = new JsonDecoder().convert(cont);
|
||||
return json["versions"].cast<String>();
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
//import 'dart:io';
|
||||
//import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class RoomPage extends StatelessWidget {
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(roomname),
|
||||
),
|
||||
body: Center( child:Text(roomid))
|
||||
);
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ description: A new Flutter project.
|
|||
version: 1.0.0+1
|
||||
|
||||
environment:
|
||||
sdk: ">=2.0.0-dev.68.0 <3.0.0"
|
||||
sdk: ">=2.1.0 <3.0.0"
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
|
|
Reference in New Issue