2020-01-19 16:37:37 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2020-01-19 16:37:37 +01:00
|
|
|
import 'package:fluffychat/components/avatar.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
import 'package:fluffychat/views/chat.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2020-01-19 16:37:37 +01:00
|
|
|
import 'package:share/share.dart';
|
2020-01-01 19:10:13 +01:00
|
|
|
|
|
|
|
import '../matrix.dart';
|
|
|
|
|
2020-01-19 16:37:37 +01:00
|
|
|
class NewPrivateChatDialog extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_NewPrivateChatDialogState createState() => _NewPrivateChatDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NewPrivateChatDialogState extends State<NewPrivateChatDialog> {
|
|
|
|
TextEditingController controller = TextEditingController();
|
2020-01-03 20:44:31 +01:00
|
|
|
final _formKey = GlobalKey<FormState>();
|
2020-01-19 16:37:37 +01:00
|
|
|
bool loading = false;
|
|
|
|
String currentSearchTerm;
|
|
|
|
Map<String, dynamic> foundProfile;
|
|
|
|
Timer coolDown;
|
|
|
|
bool get correctMxId =>
|
|
|
|
foundProfile != null && foundProfile["user_id"] == "@$currentSearchTerm";
|
2020-01-01 19:10:13 +01:00
|
|
|
|
|
|
|
void submitAction(BuildContext context) async {
|
|
|
|
if (controller.text.isEmpty) return;
|
2020-01-03 20:44:31 +01:00
|
|
|
if (!_formKey.currentState.validate()) return;
|
2020-01-01 19:10:13 +01:00
|
|
|
final MatrixState matrix = Matrix.of(context);
|
2020-01-03 20:44:31 +01:00
|
|
|
|
|
|
|
if ("@" + controller.text.trim() == matrix.client.userID) return;
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
final User user = User(
|
2020-01-03 20:44:31 +01:00
|
|
|
"@" + controller.text.trim(),
|
2020-01-01 19:10:13 +01:00
|
|
|
room: Room(id: "", client: matrix.client),
|
|
|
|
);
|
|
|
|
final String roomID =
|
|
|
|
await matrix.tryRequestWithLoadingDialog(user.startDirectChat());
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
2020-01-02 22:31:39 +01:00
|
|
|
if (roomID != null) {
|
|
|
|
await Navigator.push(
|
2020-01-01 19:10:13 +01:00
|
|
|
context,
|
|
|
|
MaterialPageRoute(builder: (context) => Chat(roomID)),
|
|
|
|
);
|
2020-01-02 22:31:39 +01:00
|
|
|
}
|
2020-01-01 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2020-01-19 16:37:37 +01:00
|
|
|
void searchUserWithCoolDown(BuildContext context, String text) async {
|
|
|
|
coolDown?.cancel();
|
|
|
|
coolDown = Timer(
|
|
|
|
Duration(seconds: 1),
|
|
|
|
() => searchUser(context, text),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchUser(BuildContext context, String text) async {
|
|
|
|
if (text.isEmpty) {
|
|
|
|
setState(() {
|
|
|
|
foundProfile = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
currentSearchTerm = text;
|
|
|
|
if (currentSearchTerm.isEmpty) return;
|
|
|
|
if (loading) return;
|
|
|
|
setState(() => loading = true);
|
|
|
|
final MatrixState matrix = Matrix.of(context);
|
|
|
|
final response = await matrix.tryRequestWithErrorToast(
|
|
|
|
matrix.client.jsonRequest(
|
|
|
|
type: HTTPType.POST,
|
|
|
|
action: "/client/r0/user_directory/search",
|
|
|
|
data: {
|
|
|
|
"search_term": text,
|
|
|
|
"limit": 1,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
print(response);
|
|
|
|
setState(() => loading = false);
|
|
|
|
if (response == false ||
|
|
|
|
!(response is Map) ||
|
|
|
|
(response["results"]?.isEmpty ?? true)) return;
|
|
|
|
print("Set...");
|
|
|
|
setState(() {
|
|
|
|
foundProfile = response["results"].first;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:10:13 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-01-19 16:37:37 +01:00
|
|
|
final String defaultDomain = Matrix.of(context).client.userID.split(":")[1];
|
2020-01-01 19:10:13 +01:00
|
|
|
return AlertDialog(
|
|
|
|
title: Text("New private chat"),
|
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
2020-01-19 16:37:37 +01:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2020-01-01 19:10:13 +01:00
|
|
|
children: <Widget>[
|
2020-01-03 20:44:31 +01:00
|
|
|
Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: TextFormField(
|
|
|
|
controller: controller,
|
|
|
|
autofocus: true,
|
|
|
|
autocorrect: false,
|
2020-01-19 16:37:37 +01:00
|
|
|
onChanged: (String text) => searchUserWithCoolDown(context, text),
|
2020-01-03 20:44:31 +01:00
|
|
|
textInputAction: TextInputAction.go,
|
|
|
|
onFieldSubmitted: (s) => submitAction(context),
|
|
|
|
validator: (value) {
|
|
|
|
if (value.isEmpty) {
|
|
|
|
return 'Please enter a matrix identifier';
|
|
|
|
}
|
|
|
|
final MatrixState matrix = Matrix.of(context);
|
|
|
|
String mxid = "@" + controller.text.trim();
|
|
|
|
if (mxid == matrix.client.userID) {
|
|
|
|
return "You cannot invite yourself";
|
|
|
|
}
|
2020-01-19 16:37:37 +01:00
|
|
|
if (!mxid.contains("@")) {
|
2020-01-03 20:44:31 +01:00
|
|
|
return "Make sure the identifier is valid";
|
|
|
|
}
|
2020-01-19 16:37:37 +01:00
|
|
|
if (!mxid.contains(":")) {
|
2020-01-03 20:44:31 +01:00
|
|
|
return "Make sure the identifier is valid";
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
decoration: InputDecoration(
|
2020-01-01 19:10:13 +01:00
|
|
|
labelText: "Enter a username",
|
2020-01-19 16:37:37 +01:00
|
|
|
icon: loading
|
|
|
|
? Container(
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 1),
|
|
|
|
)
|
|
|
|
: correctMxId
|
|
|
|
? Avatar(
|
|
|
|
MxContent(foundProfile["avatar_url"] ?? ""),
|
|
|
|
foundProfile["display_name"] ??
|
|
|
|
foundProfile["user_id"],
|
|
|
|
size: 24,
|
|
|
|
)
|
|
|
|
: Icon(Icons.account_circle),
|
2020-01-01 19:10:13 +01:00
|
|
|
prefixText: "@",
|
2020-01-19 16:37:37 +01:00
|
|
|
hintText: "username:$defaultDomain",
|
2020-01-03 20:44:31 +01:00
|
|
|
),
|
|
|
|
),
|
2020-01-01 19:10:13 +01:00
|
|
|
),
|
2020-01-19 16:37:37 +01:00
|
|
|
if (foundProfile != null && !correctMxId)
|
|
|
|
ListTile(
|
|
|
|
onTap: () {
|
|
|
|
setState(() {
|
|
|
|
controller.text =
|
|
|
|
currentSearchTerm = foundProfile["user_id"].substring(1);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
leading: Avatar(
|
|
|
|
MxContent(foundProfile["avatar_url"] ?? ""),
|
|
|
|
foundProfile["display_name"] ?? foundProfile["user_id"],
|
|
|
|
//size: 24,
|
|
|
|
),
|
|
|
|
contentPadding: EdgeInsets.all(0),
|
|
|
|
title: Text(
|
|
|
|
foundProfile["display_name"] ??
|
|
|
|
foundProfile["user_id"].split(":").first.substring(1),
|
|
|
|
style: TextStyle(),
|
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
|
|
|
foundProfile["user_id"],
|
|
|
|
maxLines: 1,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 12,
|
|
|
|
),
|
|
|
|
),
|
2020-01-01 19:10:13 +01:00
|
|
|
),
|
2020-01-19 16:37:37 +01:00
|
|
|
if (foundProfile == null || correctMxId)
|
|
|
|
ListTile(
|
|
|
|
trailing: Icon(
|
|
|
|
Icons.share,
|
|
|
|
size: 16,
|
|
|
|
),
|
|
|
|
contentPadding: EdgeInsets.all(0),
|
|
|
|
onTap: () => Share.share(
|
|
|
|
"https://matrix.to/#/${Matrix.of(context).client.userID}"),
|
|
|
|
title: Text(
|
|
|
|
"Your own username:",
|
|
|
|
style: TextStyle(
|
|
|
|
color: Colors.blueGrey,
|
|
|
|
fontSize: 12,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
|
|
|
Matrix.of(context).client.userID,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Divider(height: 1),
|
2020-01-01 19:10:13 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
|
|
|
child: Text("Close".toUpperCase(),
|
|
|
|
style: TextStyle(color: Colors.blueGrey)),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
FlatButton(
|
|
|
|
child: Text("Continue".toUpperCase()),
|
|
|
|
onPressed: () => submitAction(context),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|