fluffychat/lib/views/new_private_chat.dart

219 lines
7.6 KiB
Dart
Raw Normal View History

2020-01-27 10:14:38 +01:00
import 'dart:async';
2021-01-16 12:46:38 +01:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2020-01-27 10:14:38 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/avatar.dart';
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-01-27 10:14:38 +01:00
import 'package:fluffychat/components/matrix.dart';
2020-10-28 07:23:50 +01:00
import 'package:fluffychat/utils/fluffy_share.dart';
2020-01-27 10:14:38 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-01-27 10:14:38 +01:00
2021-01-16 12:46:38 +01:00
class NewPrivateChat extends StatefulWidget {
2020-01-27 10:14:38 +01:00
@override
_NewPrivateChatState createState() => _NewPrivateChatState();
}
2021-01-16 12:46:38 +01:00
class _NewPrivateChatState extends State<NewPrivateChat> {
2020-01-27 10:14:38 +01:00
TextEditingController controller = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool loading = false;
String currentSearchTerm;
2020-06-10 10:07:01 +02:00
List<Profile> foundProfiles = [];
2020-01-27 10:14:38 +01:00
Timer coolDown;
2020-06-10 10:07:01 +02:00
Profile get foundProfile =>
foundProfiles.firstWhere((user) => user.userId == '@$currentSearchTerm',
orElse: () => null);
2020-01-27 10:14:38 +01:00
bool get correctMxId =>
foundProfiles
2020-06-10 10:07:01 +02:00
.indexWhere((user) => user.userId == '@$currentSearchTerm') !=
2020-01-27 10:14:38 +01:00
-1;
void submitAction(BuildContext context) async {
2021-01-23 10:13:21 +01:00
controller.text = controller.text.replaceAll('@', '').trim();
2020-01-27 10:14:38 +01:00
if (controller.text.isEmpty) return;
if (!_formKey.currentState.validate()) return;
2020-05-13 15:58:59 +02:00
final matrix = Matrix.of(context);
2020-01-27 10:14:38 +01:00
2021-01-23 10:13:21 +01:00
if ('@' + controller.text == matrix.client.userID) return;
2020-01-27 10:14:38 +01:00
2020-05-13 15:58:59 +02:00
final user = User(
2021-01-23 10:13:21 +01:00
'@' + controller.text,
2020-05-13 15:58:59 +02:00
room: Room(id: '', client: matrix.client),
2020-01-27 10:14:38 +01:00
);
2020-12-25 09:58:34 +01:00
final roomID = await showFutureLoadingDialog(
context: context,
future: () => user.startDirectChat(),
);
2020-01-27 10:14:38 +01:00
2020-12-25 09:58:34 +01:00
if (roomID.error == null) {
2021-01-16 12:46:38 +01:00
await AdaptivePageLayout.of(context)
.popAndPushNamed('/rooms/${roomID.result}');
2020-01-27 10:14:38 +01:00
}
}
2021-01-18 08:43:29 +01:00
void searchUserWithCoolDown(BuildContext context) async {
2020-01-27 10:14:38 +01:00
coolDown?.cancel();
coolDown = Timer(
2021-01-23 10:13:21 +01:00
Duration(milliseconds: 500),
2021-01-18 08:43:29 +01:00
() => searchUser(context, controller.text),
2020-01-27 10:14:38 +01:00
);
}
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 15:58:59 +02:00
final matrix = Matrix.of(context);
2021-01-23 10:13:21 +01:00
UserSearchResult response;
try {
response = await matrix.client.searchUser(text, limit: 10);
} catch (_) {}
2020-01-27 10:14:38 +01:00
setState(() => loading = false);
2021-01-23 10:13:21 +01:00
if (response?.results?.isEmpty ?? true) return;
2020-01-27 10:14:38 +01:00
setState(() {
2021-01-23 10:13:21 +01:00
foundProfiles = List<Profile>.from(response.results);
2020-01-27 10:14:38 +01:00
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2021-01-16 14:24:52 +01:00
leading: BackButton(),
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).newPrivateChat),
2020-01-27 10:14:38 +01: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,
2021-01-18 08:43:29 +01:00
onChanged: (String text) => searchUserWithCoolDown(context),
2020-01-27 10:14:38 +01:00
textInputAction: TextInputAction.go,
onFieldSubmitted: (s) => submitAction(context),
validator: (value) {
if (value.isEmpty) {
2020-05-07 07:52:40 +02:00
return L10n.of(context).pleaseEnterAMatrixIdentifier;
2020-01-27 10:14:38 +01:00
}
2020-05-13 15:58:59 +02:00
final matrix = Matrix.of(context);
var mxid = '@' + controller.text.trim();
2020-01-27 10:14:38 +01:00
if (mxid == matrix.client.userID) {
2020-05-07 07:52:40 +02:00
return L10n.of(context).youCannotInviteYourself;
2020-01-27 10:14:38 +01:00
}
2020-05-13 15:58:59 +02:00
if (!mxid.contains('@')) {
2020-05-07 07:52:40 +02:00
return L10n.of(context).makeSureTheIdentifierIsValid;
2020-01-27 10:14:38 +01:00
}
2020-05-13 15:58:59 +02:00
if (!mxid.contains(':')) {
2020-05-07 07:52:40 +02:00
return L10n.of(context).makeSureTheIdentifierIsValid;
2020-01-27 10:14:38 +01:00
}
return null;
},
decoration: InputDecoration(
border: OutlineInputBorder(),
2020-05-07 07:52:40 +02:00
labelText: L10n.of(context).enterAUsername,
2020-01-27 10:14:38 +01: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-06-10 10:07:01 +02:00
foundProfile.avatarUrl,
foundProfile.displayname ?? foundProfile.userId,
2020-01-27 10:14:38 +01:00
size: 12,
),
)
2020-12-06 10:31:35 +01:00
: Icon(Icons.account_circle_outlined),
2020-05-13 15:58:59 +02:00
prefixText: '@',
hintText: '${L10n.of(context).username.toLowerCase()}',
2020-01-27 10:14:38 +01:00
),
),
),
),
Divider(height: 1),
if (foundProfiles.isNotEmpty && !correctMxId)
Expanded(
child: ListView.builder(
itemCount: foundProfiles.length,
itemBuilder: (BuildContext context, int i) {
2020-05-13 15:58:59 +02:00
var foundProfile = foundProfiles[i];
2020-01-27 10:14:38 +01:00
return ListTile(
onTap: () {
setState(() {
controller.text = currentSearchTerm =
2020-06-10 10:07:01 +02:00
foundProfile.userId.substring(1);
2020-01-27 10:14:38 +01:00
});
},
leading: Avatar(
2020-06-10 10:07:01 +02:00
foundProfile.avatarUrl,
foundProfile.displayname ?? foundProfile.userId,
2020-01-27 10:14:38 +01:00
//size: 24,
),
title: Text(
2020-06-10 10:07:01 +02:00
foundProfile.displayname ?? foundProfile.userId.localpart,
2020-01-27 10:14:38 +01:00
style: TextStyle(),
maxLines: 1,
),
subtitle: Text(
2020-06-10 10:07:01 +02:00
foundProfile.userId,
2020-01-27 10:14:38 +01:00
maxLines: 1,
style: TextStyle(
fontSize: 12,
),
),
);
},
),
),
if (foundProfiles.isEmpty || correctMxId)
ListTile(
2020-12-06 10:31:35 +01:00
trailing: Icon(Icons.share_outlined),
2020-10-28 07:23:50 +01:00
onTap: () => FluffyShare.share(
L10n.of(context).inviteText(Matrix.of(context).client.userID,
'https://matrix.to/#/${Matrix.of(context).client.userID}'),
context),
2020-01-27 10:14:38 +01:00
title: Text(
2020-05-13 15:58:59 +02:00
'${L10n.of(context).yourOwnUsername}:',
2020-01-27 10:14:38 +01: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 17:19:07 +01:00
if (foundProfiles.isEmpty || correctMxId)
Expanded(
2020-05-13 15:58:59 +02:00
child: Image.asset('assets/private_chat_wallpaper.png'),
2020-02-09 17:19:07 +01:00
),
2020-01-27 10:14:38 +01:00
],
),
floatingActionButton: FloatingActionButton(
onPressed: () => submitAction(context),
2020-12-06 10:31:35 +01:00
child: Icon(Icons.arrow_forward_outlined),
2020-01-27 10:14:38 +01:00
),
);
}
}