2020-01-27 09:14:38 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
|
|
import 'package:fluffychat/components/adaptive_page_layout.dart';
|
|
|
|
import 'package:fluffychat/components/avatar.dart';
|
2020-04-27 11:36:39 +00:00
|
|
|
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
|
2020-01-27 09:14:38 +00:00
|
|
|
import 'package:fluffychat/components/matrix.dart';
|
2020-05-07 05:52:40 +00:00
|
|
|
import 'package:fluffychat/l10n/l10n.dart';
|
2020-02-23 07:49:58 +00:00
|
|
|
import 'package:fluffychat/utils/app_route.dart';
|
2020-01-27 09:14:38 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:share/share.dart';
|
|
|
|
|
|
|
|
import 'chat.dart';
|
|
|
|
import 'chat_list.dart';
|
|
|
|
|
|
|
|
class NewPrivateChatView extends StatelessWidget {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AdaptivePageLayout(
|
|
|
|
primaryPage: FocusPage.SECOND,
|
|
|
|
firstScaffold: ChatList(),
|
|
|
|
secondScaffold: _NewPrivateChat(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NewPrivateChat extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_NewPrivateChatState createState() => _NewPrivateChatState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NewPrivateChatState extends State<_NewPrivateChat> {
|
|
|
|
TextEditingController controller = TextEditingController();
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
bool loading = false;
|
|
|
|
String currentSearchTerm;
|
|
|
|
List<Map<String, dynamic>> foundProfiles = [];
|
|
|
|
Timer coolDown;
|
|
|
|
Map<String, dynamic> get foundProfile => foundProfiles.firstWhere(
|
2020-05-13 13:58:59 +00:00
|
|
|
(user) => user['user_id'] == '@$currentSearchTerm',
|
2020-01-27 09:14:38 +00:00
|
|
|
orElse: () => null);
|
|
|
|
bool get correctMxId =>
|
|
|
|
foundProfiles
|
2020-05-13 13:58:59 +00:00
|
|
|
.indexWhere((user) => user['user_id'] == '@$currentSearchTerm') !=
|
2020-01-27 09:14:38 +00:00
|
|
|
-1;
|
|
|
|
|
|
|
|
void submitAction(BuildContext context) async {
|
|
|
|
if (controller.text.isEmpty) return;
|
|
|
|
if (!_formKey.currentState.validate()) return;
|
2020-05-13 13:58:59 +00:00
|
|
|
final matrix = Matrix.of(context);
|
2020-01-27 09:14:38 +00:00
|
|
|
|
2020-05-13 13:58:59 +00:00
|
|
|
if ('@' + controller.text.trim() == matrix.client.userID) return;
|
2020-01-27 09:14:38 +00:00
|
|
|
|
2020-05-13 13:58:59 +00:00
|
|
|
final user = User(
|
|
|
|
'@' + controller.text.trim(),
|
|
|
|
room: Room(id: '', client: matrix.client),
|
2020-01-27 09:14:38 +00:00
|
|
|
);
|
2020-04-27 11:36:39 +00:00
|
|
|
final String roomID = await SimpleDialogs(context)
|
|
|
|
.tryRequestWithLoadingDialog(user.startDirectChat());
|
2020-01-27 09:14:38 +00:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
|
|
|
if (roomID != null) {
|
2020-02-23 07:49:58 +00:00
|
|
|
await Navigator.of(context).push(
|
|
|
|
AppRoute.defaultRoute(
|
|
|
|
context,
|
|
|
|
ChatView(roomID),
|
|
|
|
),
|
2020-01-27 09:14:38 +00: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(() {
|
|
|
|
foundProfiles = [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
currentSearchTerm = text;
|
|
|
|
if (currentSearchTerm.isEmpty) return;
|
|
|
|
if (loading) return;
|
|
|
|
setState(() => loading = true);
|
2020-05-13 13:58:59 +00:00
|
|
|
final matrix = Matrix.of(context);
|
2020-04-27 11:36:39 +00:00
|
|
|
final response = await SimpleDialogs(context).tryRequestWithErrorToast(
|
2020-01-27 09:14:38 +00:00
|
|
|
matrix.client.jsonRequest(
|
|
|
|
type: HTTPType.POST,
|
2020-05-13 13:58:59 +00:00
|
|
|
action: '/client/r0/user_directory/search',
|
2020-01-27 09:14:38 +00:00
|
|
|
data: {
|
2020-05-13 13:58:59 +00:00
|
|
|
'search_term': text,
|
|
|
|
'limit': 10,
|
2020-01-27 09:14:38 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
setState(() => loading = false);
|
|
|
|
if (response == false ||
|
|
|
|
!(response is Map) ||
|
2020-05-13 13:58:59 +00:00
|
|
|
(response['results']?.isEmpty ?? true)) return;
|
2020-01-27 09:14:38 +00:00
|
|
|
setState(() {
|
2020-05-13 13:58:59 +00:00
|
|
|
foundProfiles = List<Map<String, dynamic>>.from(response['results']);
|
2020-01-27 09:14:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2020-05-07 05:52:40 +00:00
|
|
|
title: Text(L10n.of(context).newPrivateChat),
|
2020-01-27 09:14:38 +00:00
|
|
|
elevation: 0,
|
|
|
|
),
|
|
|
|
body: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
child: Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: TextFormField(
|
|
|
|
controller: controller,
|
|
|
|
autofocus: true,
|
|
|
|
autocorrect: false,
|
|
|
|
onChanged: (String text) =>
|
|
|
|
searchUserWithCoolDown(context, text),
|
|
|
|
textInputAction: TextInputAction.go,
|
|
|
|
onFieldSubmitted: (s) => submitAction(context),
|
|
|
|
validator: (value) {
|
|
|
|
if (value.isEmpty) {
|
2020-05-07 05:52:40 +00:00
|
|
|
return L10n.of(context).pleaseEnterAMatrixIdentifier;
|
2020-01-27 09:14:38 +00:00
|
|
|
}
|
2020-05-13 13:58:59 +00:00
|
|
|
final matrix = Matrix.of(context);
|
|
|
|
var mxid = '@' + controller.text.trim();
|
2020-01-27 09:14:38 +00:00
|
|
|
if (mxid == matrix.client.userID) {
|
2020-05-07 05:52:40 +00:00
|
|
|
return L10n.of(context).youCannotInviteYourself;
|
2020-01-27 09:14:38 +00:00
|
|
|
}
|
2020-05-13 13:58:59 +00:00
|
|
|
if (!mxid.contains('@')) {
|
2020-05-07 05:52:40 +00:00
|
|
|
return L10n.of(context).makeSureTheIdentifierIsValid;
|
2020-01-27 09:14:38 +00:00
|
|
|
}
|
2020-05-13 13:58:59 +00:00
|
|
|
if (!mxid.contains(':')) {
|
2020-05-07 05:52:40 +00:00
|
|
|
return L10n.of(context).makeSureTheIdentifierIsValid;
|
2020-01-27 09:14:38 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
2020-05-07 05:52:40 +00:00
|
|
|
labelText: L10n.of(context).enterAUsername,
|
2020-01-27 09:14:38 +00:00
|
|
|
prefixIcon: loading
|
|
|
|
? Container(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
width: 12,
|
|
|
|
height: 12,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
)
|
|
|
|
: correctMxId
|
|
|
|
? Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Avatar(
|
2020-05-13 13:58:59 +00:00
|
|
|
foundProfile['avatar_url'] == null
|
2020-04-28 12:11:56 +00:00
|
|
|
? null
|
2020-05-13 13:58:59 +00:00
|
|
|
: Uri.parse(foundProfile['avatar_url']),
|
|
|
|
foundProfile['display_name'] ??
|
|
|
|
foundProfile['user_id'],
|
2020-01-27 09:14:38 +00:00
|
|
|
size: 12,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: Icon(Icons.account_circle),
|
2020-05-13 13:58:59 +00:00
|
|
|
prefixText: '@',
|
|
|
|
hintText: '${L10n.of(context).username.toLowerCase()}',
|
2020-01-27 09:14:38 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Divider(height: 1),
|
|
|
|
if (foundProfiles.isNotEmpty && !correctMxId)
|
|
|
|
Expanded(
|
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: foundProfiles.length,
|
|
|
|
itemBuilder: (BuildContext context, int i) {
|
2020-05-13 13:58:59 +00:00
|
|
|
var foundProfile = foundProfiles[i];
|
2020-01-27 09:14:38 +00:00
|
|
|
return ListTile(
|
|
|
|
onTap: () {
|
|
|
|
setState(() {
|
|
|
|
controller.text = currentSearchTerm =
|
2020-05-13 13:58:59 +00:00
|
|
|
foundProfile['user_id'].substring(1);
|
2020-01-27 09:14:38 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
leading: Avatar(
|
2020-05-13 13:58:59 +00:00
|
|
|
foundProfile['avatar_url'] == null
|
2020-04-28 12:11:56 +00:00
|
|
|
? null
|
2020-05-13 13:58:59 +00:00
|
|
|
: Uri.parse(foundProfile['avatar_url']),
|
|
|
|
foundProfile['display_name'] ?? foundProfile['user_id'],
|
2020-01-27 09:14:38 +00:00
|
|
|
//size: 24,
|
|
|
|
),
|
|
|
|
title: Text(
|
2020-05-13 13:58:59 +00:00
|
|
|
foundProfile['display_name'] ??
|
|
|
|
(foundProfile['user_id'] as String).localpart,
|
2020-01-27 09:14:38 +00:00
|
|
|
style: TextStyle(),
|
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
2020-05-13 13:58:59 +00:00
|
|
|
foundProfile['user_id'],
|
2020-01-27 09:14:38 +00:00
|
|
|
maxLines: 1,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 12,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (foundProfiles.isEmpty || correctMxId)
|
|
|
|
ListTile(
|
|
|
|
trailing: Icon(
|
|
|
|
Icons.share,
|
|
|
|
size: 16,
|
|
|
|
),
|
2020-05-07 05:52:40 +00:00
|
|
|
onTap: () => Share.share(L10n.of(context).inviteText(
|
2020-02-10 17:41:43 +00:00
|
|
|
Matrix.of(context).client.userID,
|
2020-05-13 13:58:59 +00:00
|
|
|
'https://matrix.to/#/${Matrix.of(context).client.userID}')),
|
2020-01-27 09:14:38 +00:00
|
|
|
title: Text(
|
2020-05-13 13:58:59 +00:00
|
|
|
'${L10n.of(context).yourOwnUsername}:',
|
2020-01-27 09:14:38 +00:00
|
|
|
style: TextStyle(
|
|
|
|
fontStyle: FontStyle.italic,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
|
|
|
Matrix.of(context).client.userID,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16,
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Divider(height: 1),
|
2020-02-09 16:19:07 +00:00
|
|
|
if (foundProfiles.isEmpty || correctMxId)
|
|
|
|
Expanded(
|
2020-05-13 13:58:59 +00:00
|
|
|
child: Image.asset('assets/private_chat_wallpaper.png'),
|
2020-02-09 16:19:07 +00:00
|
|
|
),
|
2020-01-27 09:14:38 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
onPressed: () => submitAction(context),
|
|
|
|
child: Icon(Icons.arrow_forward),
|
2020-02-16 19:13:55 +00:00
|
|
|
foregroundColor: Colors.white,
|
2020-01-27 09:14:38 +00:00
|
|
|
backgroundColor: Theme.of(context).primaryColor,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|