You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fluffychat/lib/views/new_private_chat.dart

221 lines
7.7 KiB
Dart

5 years ago
import 'dart:async';
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
5 years ago
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/avatar.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
5 years ago
import 'package:fluffychat/components/matrix.dart';
import 'package:fluffychat/utils/fluffy_share.dart';
5 years ago
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
5 years ago
class NewPrivateChat extends StatefulWidget {
5 years ago
@override
_NewPrivateChatState createState() => _NewPrivateChatState();
}
class _NewPrivateChatState extends State<NewPrivateChat> {
5 years ago
TextEditingController controller = TextEditingController();
final _formKey = GlobalKey<FormState>();
bool loading = false;
String currentSearchTerm;
5 years ago
List<Profile> foundProfiles = [];
5 years ago
Timer coolDown;
5 years ago
Profile get foundProfile =>
foundProfiles.firstWhere((user) => user.userId == '@$currentSearchTerm',
orElse: () => null);
5 years ago
bool get correctMxId =>
foundProfiles
5 years ago
.indexWhere((user) => user.userId == '@$currentSearchTerm') !=
5 years ago
-1;
void submitAction(BuildContext context) async {
if (controller.text.isEmpty) return;
if (!_formKey.currentState.validate()) return;
5 years ago
final matrix = Matrix.of(context);
5 years ago
5 years ago
if ('@' + controller.text.trim() == matrix.client.userID) return;
5 years ago
5 years ago
final user = User(
'@' + controller.text.trim(),
room: Room(id: '', client: matrix.client),
5 years ago
);
final roomID = await showFutureLoadingDialog(
context: context,
future: () => user.startDirectChat(),
);
5 years ago
if (roomID.error == null) {
await AdaptivePageLayout.of(context)
.popAndPushNamed('/rooms/${roomID.result}');
5 years ago
}
}
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);
5 years ago
final matrix = Matrix.of(context);
final response = await showFutureLoadingDialog(
context: context,
future: () => matrix.client.searchUser(text, limit: 10),
5 years ago
);
setState(() => loading = false);
if (response.result?.results?.isEmpty ?? true) return;
5 years ago
setState(() {
foundProfiles = List<Profile>.from(response.result.results);
5 years ago
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: BackButton(),
5 years ago
title: Text(L10n.of(context).newPrivateChat),
5 years ago
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) {
5 years ago
return L10n.of(context).pleaseEnterAMatrixIdentifier;
5 years ago
}
5 years ago
final matrix = Matrix.of(context);
var mxid = '@' + controller.text.trim();
5 years ago
if (mxid == matrix.client.userID) {
5 years ago
return L10n.of(context).youCannotInviteYourself;
5 years ago
}
5 years ago
if (!mxid.contains('@')) {
5 years ago
return L10n.of(context).makeSureTheIdentifierIsValid;
5 years ago
}
5 years ago
if (!mxid.contains(':')) {
5 years ago
return L10n.of(context).makeSureTheIdentifierIsValid;
5 years ago
}
return null;
},
decoration: InputDecoration(
border: OutlineInputBorder(),
5 years ago
labelText: L10n.of(context).enterAUsername,
5 years ago
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(
5 years ago
foundProfile.avatarUrl,
foundProfile.displayname ?? foundProfile.userId,
5 years ago
size: 12,
),
)
: Icon(Icons.account_circle_outlined),
5 years ago
prefixText: '@',
hintText: '${L10n.of(context).username.toLowerCase()}',
5 years ago
),
),
),
),
Divider(height: 1),
if (foundProfiles.isNotEmpty && !correctMxId)
Expanded(
child: ListView.builder(
itemCount: foundProfiles.length,
itemBuilder: (BuildContext context, int i) {
5 years ago
var foundProfile = foundProfiles[i];
5 years ago
return ListTile(
onTap: () {
setState(() {
controller.text = currentSearchTerm =
5 years ago
foundProfile.userId.substring(1);
5 years ago
});
},
leading: Avatar(
5 years ago
foundProfile.avatarUrl,
foundProfile.displayname ?? foundProfile.userId,
5 years ago
//size: 24,
),
title: Text(
5 years ago
foundProfile.displayname ?? foundProfile.userId.localpart,
5 years ago
style: TextStyle(),
maxLines: 1,
),
subtitle: Text(
5 years ago
foundProfile.userId,
5 years ago
maxLines: 1,
style: TextStyle(
fontSize: 12,
),
),
);
},
),
),
if (foundProfiles.isEmpty || correctMxId)
ListTile(
trailing: Icon(Icons.share_outlined),
onTap: () => FluffyShare.share(
L10n.of(context).inviteText(Matrix.of(context).client.userID,
'https://matrix.to/#/${Matrix.of(context).client.userID}'),
context),
5 years ago
title: Text(
5 years ago
'${L10n.of(context).yourOwnUsername}:',
5 years ago
style: TextStyle(
fontStyle: FontStyle.italic,
),
),
subtitle: Text(
Matrix.of(context).client.userID,
style: TextStyle(
fontSize: 16,
color: Theme.of(context).primaryColor,
),
),
),
Divider(height: 1),
if (foundProfiles.isEmpty || correctMxId)
Expanded(
5 years ago
child: Image.asset('assets/private_chat_wallpaper.png'),
),
5 years ago
],
),
floatingActionButton: FloatingActionButton(
onPressed: () => submitAction(context),
child: Icon(Icons.arrow_forward_outlined),
5 years ago
foregroundColor: Colors.white,
5 years ago
backgroundColor: Theme.of(context).primaryColor,
),
);
}
}