flutter_app/lib/views/messageview.dart

217 lines
8.8 KiB
Dart
Raw Normal View History

2021-04-08 05:07:01 +00:00
import 'dart:convert';
import 'dart:io';
2021-04-08 05:07:01 +00:00
2021-06-03 18:32:25 +00:00
import 'package:cwtch/cwtch_icons_icons.dart';
import 'package:cwtch/widgets/profileimage.dart';
2021-05-24 21:14:29 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2021-05-19 21:39:52 +00:00
import 'package:cwtch/views/peersettingsview.dart';
import 'package:cwtch/widgets/DropdownContacts.dart';
2021-03-17 19:54:14 +00:00
import 'package:provider/provider.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2021-03-17 19:54:14 +00:00
import '../main.dart';
import '../model.dart';
import '../settings.dart';
2021-01-21 20:37:35 +00:00
import '../widgets/messagelist.dart';
2021-04-23 19:57:30 +00:00
import 'groupsettingsview.dart';
class MessageView extends StatefulWidget {
@override
_MessageViewState createState() => _MessageViewState();
}
class _MessageViewState extends State<MessageView> {
2021-01-21 20:37:35 +00:00
final ctrlrCompose = TextEditingController();
2021-04-10 02:31:27 +00:00
final focusNode = FocusNode();
String selectedContact = "";
2021-01-14 23:36:09 +00:00
2021-04-27 20:02:27 +00:00
// @override
// void didChangeDependencies() {
// super.didChangeDependencies();
// if (Provider.of<ContactInfoState>(context, listen: false).unreadMessages > 0) {
// Provider.of<ContactInfoState>(context, listen: false).unreadMessages = 0;
// }
// }
2021-04-13 00:04:51 +00:00
2021-01-14 23:36:09 +00:00
@override
2021-01-21 20:37:35 +00:00
void dispose() {
2021-04-10 02:31:27 +00:00
focusNode.dispose();
2021-01-21 20:37:35 +00:00
ctrlrCompose.dispose();
super.dispose();
2021-01-14 23:36:09 +00:00
}
@override
Widget build(BuildContext context) {
2021-06-24 07:36:41 +00:00
var appState = Provider.of<AppState>(context);
2021-05-13 01:22:05 +00:00
return WillPopScope(
2021-05-13 19:58:43 +00:00
onWillPop: _onWillPop,
child: Scaffold(
appBar: AppBar(
2021-06-24 07:36:41 +00:00
// setting leading to null makes it do the default behaviour; container() hides it
leading: Provider.of<Settings>(context).uiColumns(appState.isLandscape(context)).length > 1 ? Container() : null,
title: Row(children: [
ProfileImage(
imagePath: Provider.of<ContactInfoState>(context).imagePath,
diameter: 42,
border: Provider.of<Settings>(context).current().portraitOnlineBorderColor(),
badgeTextColor: Colors.red,
badgeColor: Colors.red,
),
SizedBox(
width: 10,
),Text(Provider.of<ContactInfoState>(context).nickname)]),
2021-05-13 19:58:43 +00:00
actions: [
//IconButton(icon: Icon(Icons.chat), onPressed: _pushContactSettings),
//IconButton(icon: Icon(Icons.list), onPressed: _pushContactSettings),
//IconButton(icon: Icon(Icons.push_pin), onPressed: _pushContactSettings),
2021-06-04 18:13:52 +00:00
IconButton(
icon: Provider.of<ContactInfoState>(context, listen: false).isGroup == true ? Icon(CwtchIcons.group_settings_24px) : Icon(CwtchIcons.peer_settings_24px),
tooltip: AppLocalizations.of(context)!.conversationSettings,
onPressed: _pushContactSettings),
2021-05-13 19:58:43 +00:00
],
),
body: Padding(padding: EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 108.0), child: MessageList()),
bottomSheet: _buildComposeBox(),
));
2021-05-13 01:22:05 +00:00
}
Future<bool> _onWillPop() async {
Provider.of<ContactInfoState>(context, listen: false).unreadMessages = 0;
return true;
}
2021-03-17 19:54:14 +00:00
void _pushContactSettings() {
Navigator.of(context).push(MaterialPageRoute<void>(
builder: (BuildContext bcontext) {
2021-05-05 23:12:30 +00:00
if (Provider.of<ContactInfoState>(context, listen: false).isGroup == true) {
2021-03-17 19:54:14 +00:00
return MultiProvider(
providers: [ChangeNotifierProvider.value(value: Provider.of<ContactInfoState>(context))],
2021-04-23 19:57:30 +00:00
child: GroupSettingsView(),
2021-03-17 19:54:14 +00:00
);
} else {
return MultiProvider(
providers: [ChangeNotifierProvider.value(value: Provider.of<ContactInfoState>(context))],
child: PeerSettingsView(),
);
}
},
));
}
2021-05-25 00:11:39 +00:00
void _sendMessage([String? ignoredParam]) {
2021-04-08 05:07:01 +00:00
ChatMessage cm = new ChatMessage(o: 1, d: ctrlrCompose.value.text);
2021-05-12 23:45:22 +00:00
Provider.of<FlwtchState>(context, listen: false)
.cwtch
.SendMessage(Provider.of<ContactInfoState>(context, listen: false).profileOnion, Provider.of<ContactInfoState>(context, listen: false).onion, jsonEncode(cm));
_sendMessageHelper();
}
2021-05-25 00:11:39 +00:00
void _sendInvitation([String? ignoredParam]) {
2021-05-12 23:45:22 +00:00
Provider.of<FlwtchState>(context, listen: false)
.cwtch
.SendInvitation(Provider.of<ContactInfoState>(context, listen: false).profileOnion, Provider.of<ContactInfoState>(context, listen: false).onion, this.selectedContact);
_sendMessageHelper();
}
void _sendMessageHelper() {
2021-01-21 20:37:35 +00:00
ctrlrCompose.clear();
focusNode.requestFocus();
Future.delayed(const Duration(milliseconds: 80), () {
2021-04-22 21:15:27 +00:00
Provider.of<ContactInfoState>(context, listen: false).totalMessages++;
2021-05-06 23:40:13 +00:00
// Resort the contact list...
Provider.of<ProfileInfoState>(context, listen: false).contactList.updateLastMessageTime(Provider.of<ContactInfoState>(context, listen: false).onion, DateTime.now());
});
}
2021-01-21 20:37:35 +00:00
Widget _buildComposeBox() {
2021-03-10 17:40:14 +00:00
return Container(
color: Provider.of<Settings>(context).theme.backgroundMainColor(),
2021-05-24 21:14:29 +00:00
padding: EdgeInsets.all(2),
margin: EdgeInsets.all(2),
2021-01-21 20:37:35 +00:00
height: 100,
child: Row(
children: <Widget>[
Expanded(
2021-05-24 21:14:29 +00:00
child: Container(
decoration: BoxDecoration(border: Border(top: BorderSide(color: Provider.of<Settings>(context).theme.defaultButtonActiveColor()))),
child: TextFormField(
key: Key('txtCompose'),
controller: ctrlrCompose,
autofocus: !Platform.isAndroid,
2021-05-24 21:14:29 +00:00
focusNode: focusNode,
textInputAction: TextInputAction.send,
2021-05-24 21:24:02 +00:00
onFieldSubmitted: _sendMessage,
2021-05-24 21:14:29 +00:00
decoration: InputDecoration(
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
enabled: true,
prefixIcon: IconButton(
2021-06-04 23:05:45 +00:00
icon: Icon(CwtchIcons.send_invite, size: 24, color: Provider.of<Settings>(context).theme.mainTextColor()),
2021-06-15 00:38:07 +00:00
tooltip: AppLocalizations.of(context)!.sendInvite,
enableFeedback: true,
splashColor: Provider.of<Settings>(context).theme.defaultButtonActiveColor(),
hoverColor: Provider.of<Settings>(context).theme.defaultButtonActiveColor(),
2021-05-24 21:14:29 +00:00
onPressed: () => _modalSendInvitation(context)),
suffixIcon: IconButton(
2021-06-03 18:32:25 +00:00
icon: Icon(CwtchIcons.send_24px, size: 24, color: Provider.of<Settings>(context).theme.mainTextColor()),
2021-06-15 00:38:07 +00:00
tooltip: AppLocalizations.of(context)!.sendMessage,
2021-05-24 21:14:29 +00:00
onPressed: _sendMessage,
2021-05-12 23:45:22 +00:00
),
))),
2021-05-24 21:14:29 +00:00
),
2021-01-21 20:37:35 +00:00
],
),
);
}
2021-03-23 22:42:10 +00:00
void placeHolder() => {};
2021-05-11 22:56:16 +00:00
// explicitly passing BuildContext ctx here is important, change at risk to own health
// otherwise some Providers will become inaccessible to subwidgets...?
// https://stackoverflow.com/a/63818697
void _modalSendInvitation(BuildContext ctx) {
showModalBottomSheet<void>(
context: ctx,
builder: (BuildContext bcontext) {
return Container(
height: 200, // bespoke value courtesy of the [TextField] docs
child: Center(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
2021-05-25 00:11:39 +00:00
Text(AppLocalizations.of(bcontext)!.invitationLabel),
SizedBox(
height: 20,
),
2021-05-12 23:45:22 +00:00
ChangeNotifierProvider.value(
value: Provider.of<ProfileInfoState>(ctx, listen: false),
2021-06-07 21:14:05 +00:00
child: DropdownContacts(filter: (contact) {
return contact.onion != Provider.of<ContactInfoState>(context).onion;
}, onChanged: (newVal) {
2021-05-12 23:45:22 +00:00
setState(() {
this.selectedContact = newVal;
});
})),
SizedBox(
height: 20,
),
ElevatedButton(
2021-05-25 00:11:39 +00:00
child: Text(AppLocalizations.of(bcontext)!.inviteBtn, semanticsLabel: AppLocalizations.of(bcontext)!.inviteBtn),
2021-06-16 23:03:39 +00:00
onPressed: () {
if (this.selectedContact != "") {
this._sendInvitation();
}
Navigator.pop(bcontext);
},
),
],
)),
));
});
}
}