Redesign
parent
f2b0cce282
commit
cc61d8e91a
@ -0,0 +1,107 @@
|
||||
import 'package:fluffychat/components/adaptive_page_layout.dart';
|
||||
import 'package:fluffychat/components/matrix.dart';
|
||||
import 'package:fluffychat/i18n/i18n.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pedantic/pedantic.dart';
|
||||
|
||||
import 'chat.dart';
|
||||
import 'chat_list.dart';
|
||||
import 'invitation_selection.dart';
|
||||
|
||||
class NewGroupView extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AdaptivePageLayout(
|
||||
primaryPage: FocusPage.SECOND,
|
||||
firstScaffold: ChatList(),
|
||||
secondScaffold: _NewGroup(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _NewGroup extends StatefulWidget {
|
||||
@override
|
||||
_NewGroupState createState() => _NewGroupState();
|
||||
}
|
||||
|
||||
class _NewGroupState extends State<_NewGroup> {
|
||||
TextEditingController controller = TextEditingController();
|
||||
bool publicGroup = false;
|
||||
|
||||
void submitAction(BuildContext context) async {
|
||||
final MatrixState matrix = Matrix.of(context);
|
||||
Map<String, dynamic> params = {};
|
||||
if (publicGroup) {
|
||||
params["preset"] = "public_chat";
|
||||
params["visibility"] = "public";
|
||||
if (controller.text.isNotEmpty) {
|
||||
params["room_alias_name"] = controller.text;
|
||||
}
|
||||
} else {
|
||||
params["preset"] = "private_chat";
|
||||
}
|
||||
if (controller.text.isNotEmpty) params["name"] = controller.text;
|
||||
final String roomID = await matrix.tryRequestWithLoadingDialog(
|
||||
matrix.client.createRoom(params: params),
|
||||
);
|
||||
Navigator.of(context).pop();
|
||||
if (roomID != null) {
|
||||
unawaited(
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) {
|
||||
return ChatView(roomID);
|
||||
}),
|
||||
),
|
||||
);
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => InvitationSelection(
|
||||
matrix.client.getRoomById(roomID),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(I18n.of(context).createNewGroup),
|
||||
elevation: 0,
|
||||
),
|
||||
body: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: TextField(
|
||||
controller: controller,
|
||||
autofocus: true,
|
||||
autocorrect: false,
|
||||
textInputAction: TextInputAction.go,
|
||||
onSubmitted: (s) => submitAction(context),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: I18n.of(context).optionalGroupName,
|
||||
prefixIcon: Icon(Icons.people),
|
||||
hintText: I18n.of(context).enterAGroupName),
|
||||
),
|
||||
),
|
||||
SwitchListTile(
|
||||
title: Text(I18n.of(context).groupIsPublic),
|
||||
value: publicGroup,
|
||||
onChanged: (bool b) => setState(() => publicGroup = b),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
onPressed: () => submitAction(context),
|
||||
child: Icon(Icons.arrow_forward),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,240 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:fluffychat/components/adaptive_page_layout.dart';
|
||||
import 'package:fluffychat/components/avatar.dart';
|
||||
import 'package:fluffychat/components/matrix.dart';
|
||||
import 'package:fluffychat/i18n/i18n.dart';
|
||||
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(
|
||||
(user) => user["user_id"] == "@$currentSearchTerm",
|
||||
orElse: () => null);
|
||||
bool get correctMxId =>
|
||||
foundProfiles
|
||||
.indexWhere((user) => user["user_id"] == "@$currentSearchTerm") !=
|
||||
-1;
|
||||
|
||||
void submitAction(BuildContext context) async {
|
||||
if (controller.text.isEmpty) return;
|
||||
if (!_formKey.currentState.validate()) return;
|
||||
final MatrixState matrix = Matrix.of(context);
|
||||
|
||||
if ("@" + controller.text.trim() == matrix.client.userID) return;
|
||||
|
||||
final User user = User(
|
||||
"@" + controller.text.trim(),
|
||||
room: Room(id: "", client: matrix.client),
|
||||
);
|
||||
final String roomID =
|
||||
await matrix.tryRequestWithLoadingDialog(user.startDirectChat());
|
||||
Navigator.of(context).pop();
|
||||
|
||||
if (roomID != null) {
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (context) => ChatView(roomID)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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": 10,
|
||||
}),
|
||||
);
|
||||
setState(() => loading = false);
|
||||
if (response == false ||
|
||||
!(response is Map) ||
|
||||
(response["results"]?.isEmpty ?? true)) return;
|
||||
setState(() {
|
||||
foundProfiles = List<Map<String, dynamic>>.from(response["results"]);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final String defaultDomain = Matrix.of(context).client.userID.split(":")[1];
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(I18n.of(context).newPrivateChat),
|
||||
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) {
|
||||
return I18n.of(context).pleaseEnterAMatrixIdentifier;
|
||||
}
|
||||
final MatrixState matrix = Matrix.of(context);
|
||||
String mxid = "@" + controller.text.trim();
|
||||
if (mxid == matrix.client.userID) {
|
||||
return I18n.of(context).youCannotInviteYourself;
|
||||
}
|
||||
if (!mxid.contains("@")) {
|
||||
return I18n.of(context).makeSureTheIdentifierIsValid;
|
||||
}
|
||||
if (!mxid.contains(":")) {
|
||||
return I18n.of(context).makeSureTheIdentifierIsValid;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: I18n.of(context).enterAUsername,
|
||||
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(
|
||||
MxContent(foundProfile["avatar_url"] ?? ""),
|
||||
foundProfile["display_name"] ??
|
||||
foundProfile["user_id"],
|
||||
size: 12,
|
||||
),
|
||||
)
|
||||
: Icon(Icons.account_circle),
|
||||
prefixText: "@",
|
||||
hintText:
|
||||
"${I18n.of(context).username.toLowerCase()}:$defaultDomain",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Divider(height: 1),
|
||||
if (foundProfiles.isNotEmpty && !correctMxId)
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: foundProfiles.length,
|
||||
itemBuilder: (BuildContext context, int i) {
|
||||
Map<String, dynamic> foundProfile = foundProfiles[i];
|
||||
return 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,
|
||||
),
|
||||
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,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
if (foundProfiles.isEmpty || correctMxId)
|
||||
ListTile(
|
||||
trailing: Icon(
|
||||
Icons.share,
|
||||
size: 16,
|
||||
),
|
||||
onTap: () => Share.share(
|
||||
"https://matrix.to/#/${Matrix.of(context).client.userID}"),
|
||||
title: Text(
|
||||
"${I18n.of(context).yourOwnUsername}:",
|
||||
style: TextStyle(
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
Matrix.of(context).client.userID,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
Divider(height: 1),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () => submitAction(context),
|
||||
child: Icon(Icons.arrow_forward),
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue