FurryChat/lib/views/status_view.dart

187 lines
6.1 KiB
Dart
Raw Normal View History

2020-10-03 10:07:00 +00:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/avatar.dart';
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
import 'package:fluffychat/components/matrix.dart';
2020-10-03 13:53:08 +00:00
import 'package:fluffychat/utils/url_launcher.dart';
import 'package:fluffychat/utils/user_status.dart';
2020-10-03 10:07:00 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/utils/string_color.dart';
import 'package:flutter/material.dart';
2020-10-03 13:53:08 +00:00
import 'package:matrix_link_text/link_text.dart';
2020-10-03 10:07:00 +00:00
import 'chat.dart';
2020-10-03 13:53:08 +00:00
class StatusView extends StatelessWidget {
2020-10-03 10:07:00 +00:00
final Uri avatarUrl;
final String displayname;
2020-10-03 13:53:08 +00:00
final UserStatus status;
2020-10-03 10:07:00 +00:00
final bool composeMode;
2020-10-03 13:53:08 +00:00
final String composeText;
final TextEditingController _composeController;
2020-10-03 10:07:00 +00:00
2020-10-03 13:53:08 +00:00
StatusView({
2020-10-03 10:07:00 +00:00
this.composeMode = false,
2020-10-03 13:53:08 +00:00
this.status,
2020-10-03 10:07:00 +00:00
this.avatarUrl,
this.displayname,
2020-10-03 13:53:08 +00:00
this.composeText,
2020-10-03 10:07:00 +00:00
Key key,
2020-10-03 13:53:08 +00:00
}) : _composeController = TextEditingController(text: composeText),
super(key: key);
2020-10-03 10:07:00 +00:00
void _sendMessageAction(BuildContext context) async {
final roomId = await User(
2020-10-03 13:53:08 +00:00
status.userId,
2020-10-03 10:07:00 +00:00
room: Room(id: '', client: Matrix.of(context).client),
).startDirectChat();
await Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
ChatView(roomId),
),
(Route r) => r.isFirst);
}
void _setStatusAction(BuildContext context) async {
if (_composeController.text.isEmpty) return;
await SimpleDialogs(context).tryRequestWithLoadingDialog(
Matrix.of(context).client.sendPresence(
Matrix.of(context).client.userID, PresenceType.online,
statusMsg: _composeController.text),
);
await Navigator.of(context).popUntil((Route r) => r.isFirst);
}
2020-10-03 13:53:08 +00:00
void _removeStatusAction(BuildContext context) async {
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
Matrix.of(context).client.sendPresence(
Matrix.of(context).client.userID,
PresenceType.online,
statusMsg:
' ', // Send this empty String make sure that all other devices will get an update
),
);
if (success == false) return;
await Navigator.of(context).popUntil((Route r) => r.isFirst);
}
2020-10-03 10:07:00 +00:00
@override
Widget build(BuildContext context) {
2020-10-03 13:53:08 +00:00
if (composeMode == false && status == null) {
2020-10-03 10:07:00 +00:00
throw ('If composeMode is null then the presence must be not null!');
}
final padding = const EdgeInsets.only(
top: 16.0,
right: 16.0,
left: 16.0,
bottom: 64.0,
);
return Scaffold(
backgroundColor: displayname.color,
extendBody: true,
appBar: AppBar(
2020-10-04 07:16:46 +00:00
titleSpacing: 0.0,
2020-10-03 10:07:00 +00:00
brightness: Brightness.dark,
leading: IconButton(
icon: Icon(
Icons.close,
color: Colors.white,
),
onPressed: Navigator.of(context).pop,
),
backgroundColor: Colors.transparent,
elevation: 1,
title: ListTile(
contentPadding: EdgeInsets.zero,
leading: Avatar(avatarUrl, displayname),
title: Text(
displayname,
style: TextStyle(color: Colors.white),
),
subtitle: Text(
2020-10-03 13:53:08 +00:00
status?.userId ?? Matrix.of(context).client.userID,
2020-10-04 07:16:46 +00:00
maxLines: 1,
2020-10-03 10:07:00 +00:00
style: TextStyle(color: Colors.white),
),
),
2020-10-03 13:53:08 +00:00
actions:
!composeMode && status.userId == Matrix.of(context).client.userID
? [
IconButton(
icon: Icon(Icons.archive),
onPressed: () => _removeStatusAction(context),
color: Colors.white,
),
]
: null,
2020-10-03 10:07:00 +00:00
),
body: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
displayname.color,
Theme.of(context).primaryColor,
displayname.color,
],
),
),
child: composeMode
? Padding(
padding: padding,
child: TextField(
controller: _composeController,
autofocus: true,
minLines: 1,
maxLines: 20,
style: TextStyle(
fontSize: 30,
color: Colors.white,
),
textAlign: TextAlign.center,
decoration: InputDecoration(
border: InputBorder.none,
),
),
)
: ListView(
shrinkWrap: true,
padding: padding,
children: [
2020-10-03 13:53:08 +00:00
LinkText(
text: status.statusMsg,
2020-10-03 10:07:00 +00:00
textAlign: TextAlign.center,
2020-10-03 13:53:08 +00:00
textStyle: TextStyle(
2020-10-03 10:07:00 +00:00
fontSize: 30,
color: Colors.white,
),
2020-10-03 13:53:08 +00:00
linkStyle: TextStyle(
fontSize: 30,
color: Colors.white70,
decoration: TextDecoration.underline,
),
onLinkTap: (url) => UrlLauncher(context, url).launchUrl(),
2020-10-03 10:07:00 +00:00
),
],
),
),
2020-10-03 13:53:08 +00:00
floatingActionButton:
!composeMode && status.userId == Matrix.of(context).client.userID
? null
: FloatingActionButton.extended(
backgroundColor: Theme.of(context).primaryColor,
icon: Icon(composeMode ? Icons.edit : Icons.message_outlined),
label: Text(composeMode
? L10n.of(context).setStatus
: L10n.of(context).sendAMessage),
onPressed: () => composeMode
? _setStatusAction(context)
: _sendMessageAction(context),
),
2020-10-03 10:07:00 +00:00
);
}
}