Compare commits

...

2 Commits

2 changed files with 97 additions and 64 deletions

View File

@ -34,52 +34,46 @@ class _LoginPageState extends State<LoginPage> {
.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": _matrixId,
},
"password": _password,
"initial_device_display_name": "Hermes alpha",
"type": "m.login.password",
"identifier": <String,String>{
"type": "m.id.user",
"user": _matrixId,
},
"password": _password,
"initial_device_display_name": "Hermes alpha",
});
req.write(data);
return req.close();
}).then((resp) {
return resp.transform(utf8.decoder).join().then((cont) {
if (resp.statusCode != 200) {
_failureMessage = "HTTP ${resp.statusCode}";
}
print("StatusCode: ${resp.statusCode}");
var json;
try {
json = new JsonDecoder().convert(cont);
}
catch(e) {
print(cont);
_failureMessage = "Invalid JSON!";
return;
}
// I hate switch statements
if (resp.statusCode == 403) {
print(cont);
_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["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 {
_failureMessage = "{$resp.statusCode}: ${json}";
print(resp.statusCode);
print(json);
}
});
return resp.transform(utf8.decoder).join().then((cont) {
if (resp.statusCode != 200) {
_failureMessage = "HTTP ${resp.statusCode}";
}
var json;
try {
json = new JsonDecoder().convert(cont);
}
catch(e) {
_failureMessage = "Invalid JSON!";
return;
}
// I hate switch statements
if (resp.statusCode == 403) {
_failureMessage = json["error"];
} else if (resp.statusCode == 200) {
var s = widget.settings;
s.setString("access_token", json["access_token"]);
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 {
_failureMessage = "{$resp.statusCode}: ${json}";
}
});
});
}
@ -94,7 +88,7 @@ class _LoginPageState extends State<LoginPage> {
padding: EdgeInsets.all(8.0),
child: ListView(
children: <Widget>[
TextField(autocorrect: false, onChanged: (s) { _matrixId = s; }),
TextField(autocorrect: false, onChanged: (s) { _matrixId = s; }, autofocus: true),
Text("Matrix ID"),
TextField(autocorrect: false, obscureText: true, onChanged: (s) { _password = s; }),
Text("Password"),
@ -105,8 +99,8 @@ class _LoginPageState extends State<LoginPage> {
),
Text("Home server"),
RaisedButton(
child: Text("Login"),
onPressed: () { setState(() { _doLogin(); }); },
child: Text("Login"),
onPressed: () { setState(() { _doLogin(); }); },
),
Text(_failureMessage),
],

View File

@ -38,17 +38,48 @@ class _RoomPageState extends State<RoomPage> {
List<Widget> messages = [ ];
for (var neko in cont) {
if (neko["type"] == "m.room.message") {
messages.add(
ListTile(
leading: Text(neko["sender"]),
title: Text(neko["content"]["body"], textAlign: TextAlign.left)
)
);
if (neko["content"]["msgtype"] == "m.text") {
messages.add(
ListTile(
title: Column(
children: <Widget>[
Row( children: <Widget>[ Text(neko["sender"], style: TextStyle(fontWeight: FontWeight.bold), textAlign: TextAlign.start), Spacer()]),
Row( children: <Widget>[ Expanded(child: Text(neko["content"]["body"], textAlign: TextAlign.start))]),
]
),
),
);
} else if (neko["content"]["msgtype"] == "m.notice") {
messages.add(
ListTile(
title: Column(
children: <Widget>[
Text(neko["sender"], textAlign: TextAlign.center),
Text(neko["content"]["body"], textAlign: TextAlign.center),
]
),
),
);
} else if (neko["content"]["msgtype"] == "m.emote") {
messages.add(
ListTile(
title: Column(
children: <Widget>[
Row( children: <Widget> [
Text(neko["sender"], style: TextStyle(fontStyle: FontStyle.italic, fontWeight: FontWeight.bold), textAlign: TextAlign.left)
]),
Row( children: <Widget> [
Text(neko["content"]["body"], style: TextStyle(fontStyle: FontStyle.italic), textAlign: TextAlign.left),
]),
]
),
),
);
}
}
}
_messages= messages;
setState(() {
_messages= messages;
});
});
}
@ -58,6 +89,14 @@ class _RoomPageState extends State<RoomPage> {
return Scaffold(
appBar: AppBar(
title: Text(widget.roomname),
actions: <Widget>[
IconButton(
icon: Icon(Icons.autorenew, color: Colors.purple),
onPressed: () {
_updateMessages();
}
)
]
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
@ -68,22 +107,22 @@ class _RoomPageState extends State<RoomPage> {
),
),
Row(
children: [
Expanded(
child: TextField(
children: [
Expanded(
child: TextField(
onSubmitted: _sendMessage,
controller: _inputController,
minLines: 1,
maxLines: 4
minLines: 1,
maxLines: 4
),
),
IconButton(
),
IconButton(
icon: Icon(Icons.send),
color: Colors.pink,
onPressed: () { _sendMessage(_inputController.text); }
),
color: Colors.pink,
onPressed: () { _sendMessage(_inputController.text); }
),
]
),
),
],
),
);