2022-09-27 19:49:11 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-27 22:00:12 +00:00
|
|
|
import 'package:localization/localization.dart';
|
2022-09-27 19:49:11 +00:00
|
|
|
import 'package:loris/business_logic/chat/chat.dart';
|
2022-09-30 15:45:07 +00:00
|
|
|
import 'package:loris/dialogues/full_post_view.dart';
|
|
|
|
import 'package:loris/dialogues/profile_view.dart';
|
2022-09-29 13:55:58 +00:00
|
|
|
import 'package:loris/partials/loadingbox.dart';
|
2022-09-29 20:46:16 +00:00
|
|
|
import 'package:loris/partials/post.dart';
|
2022-09-27 22:43:59 +00:00
|
|
|
import 'package:loris/partials/post_text_renderer.dart';
|
2022-09-27 19:49:11 +00:00
|
|
|
import 'package:loris/themes/themes.dart' as themes;
|
|
|
|
import 'package:loris/global.dart' as global;
|
2022-07-02 22:03:10 +00:00
|
|
|
|
2022-09-27 19:49:11 +00:00
|
|
|
class Chat extends StatefulWidget {
|
|
|
|
const Chat({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<Chat> createState() => _ChatState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChatState extends State<Chat> {
|
|
|
|
List<ConversationModel> conversations = [];
|
|
|
|
// map that stores max ids for each identity
|
|
|
|
Map<String, String?> maxIds = {};
|
2022-09-29 13:55:58 +00:00
|
|
|
final scrollController = ScrollController();
|
|
|
|
bool loading = false;
|
2022-09-27 19:49:11 +00:00
|
|
|
|
2022-09-29 13:55:58 +00:00
|
|
|
// loads more conversations and properly stores all ids
|
2022-09-27 22:00:12 +00:00
|
|
|
Future<void> updateConversations() async {
|
2022-09-29 13:55:58 +00:00
|
|
|
if (loading) return;
|
|
|
|
loading = true;
|
2022-09-27 22:00:12 +00:00
|
|
|
final models = await getAllConversationModels(maxIds);
|
2022-09-27 19:49:11 +00:00
|
|
|
setState(() {
|
2022-09-27 22:00:12 +00:00
|
|
|
conversations.addAll(models.models);
|
|
|
|
maxIds = models.maxIds;
|
2022-09-27 19:49:11 +00:00
|
|
|
});
|
2022-09-29 13:55:58 +00:00
|
|
|
loading = false;
|
2022-09-27 19:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
updateConversations();
|
2022-09-29 13:55:58 +00:00
|
|
|
scrollController.addListener(() {
|
|
|
|
if (scrollController.position.maxScrollExtent - scrollController.offset <
|
|
|
|
MediaQuery.of(context).size.height) {
|
|
|
|
updateConversations();
|
|
|
|
}
|
|
|
|
});
|
2022-09-27 19:49:11 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-09-29 13:55:58 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
scrollController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2022-09-27 19:49:11 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: ListView.separated(
|
2022-09-29 13:55:58 +00:00
|
|
|
controller: scrollController,
|
2022-09-27 19:49:11 +00:00
|
|
|
shrinkWrap: true,
|
2022-09-29 13:55:58 +00:00
|
|
|
itemBuilder: ((context, index) {
|
|
|
|
if (index >= conversations.length) return const LoadingBox();
|
|
|
|
return ConversationButton(
|
|
|
|
model: conversations[index],
|
2022-09-29 17:44:01 +00:00
|
|
|
onTap: () {
|
|
|
|
showDialog(
|
2022-09-30 15:45:07 +00:00
|
|
|
barrierColor: Colors.transparent,
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
|
|
|
final con = conversations[index];
|
|
|
|
if (con.lastStatus != null) {
|
|
|
|
return FullPostView(
|
|
|
|
originPostModel: con.lastStatus!);
|
|
|
|
}
|
|
|
|
return ProfileView(model: con.accounts.first);
|
|
|
|
});
|
2022-09-29 17:44:01 +00:00
|
|
|
},
|
2022-09-29 13:55:58 +00:00
|
|
|
);
|
|
|
|
}),
|
2022-09-27 19:49:11 +00:00
|
|
|
separatorBuilder: (context, index) => const Divider(
|
|
|
|
height: themes.defaultSeperatorHeight,
|
|
|
|
color: Colors.transparent,
|
|
|
|
),
|
2022-09-29 13:55:58 +00:00
|
|
|
itemCount: conversations.length + 1),
|
2022-09-27 19:49:11 +00:00
|
|
|
),
|
|
|
|
Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border(
|
|
|
|
top: BorderSide(
|
|
|
|
color: Theme.of(context).colorScheme.primary, width: 2))),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConversationButton extends StatelessWidget {
|
|
|
|
const ConversationButton({
|
|
|
|
super.key,
|
|
|
|
required this.model,
|
2022-09-29 17:44:01 +00:00
|
|
|
required this.onTap,
|
2022-09-27 19:49:11 +00:00
|
|
|
});
|
|
|
|
final ConversationModel model;
|
2022-09-29 17:44:01 +00:00
|
|
|
final void Function() onTap;
|
2022-09-27 19:49:11 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-09-30 15:45:07 +00:00
|
|
|
final List<Widget> people = [];
|
2022-09-29 20:46:16 +00:00
|
|
|
for (var p in model.accounts) {
|
|
|
|
people.add(DisplayName(account: p));
|
|
|
|
}
|
2022-09-29 17:44:01 +00:00
|
|
|
return Align(
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: const BorderRadius.all(themes.defaultRadius),
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
),
|
|
|
|
margin: const EdgeInsets.fromLTRB(themes.defaultSeperatorHeight * 2, 0,
|
|
|
|
themes.defaultSeperatorHeight * 2, 0),
|
|
|
|
width: global.getWidth(context),
|
|
|
|
constraints: global.getConstraints(context),
|
|
|
|
child: Material(
|
|
|
|
borderOnForeground: true,
|
|
|
|
shape: RoundedRectangleBorder(
|
2022-09-29 13:55:58 +00:00
|
|
|
borderRadius: const BorderRadius.all(themes.defaultRadius),
|
2022-09-29 17:44:01 +00:00
|
|
|
side: BorderSide(
|
|
|
|
color: Theme.of(context).colorScheme.secondary, width: 2),
|
2022-09-29 13:55:58 +00:00
|
|
|
),
|
2022-09-29 17:44:01 +00:00
|
|
|
child: InkWell(
|
|
|
|
borderRadius: const BorderRadius.all(themes.defaultRadius),
|
|
|
|
onTap: onTap,
|
|
|
|
child: Padding(
|
|
|
|
padding: themes.defaultInsideMargins,
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
2022-09-30 15:45:07 +00:00
|
|
|
children: people +
|
|
|
|
[
|
|
|
|
Divider(
|
|
|
|
color: Theme.of(context).hintColor,
|
|
|
|
),
|
2022-09-29 20:46:16 +00:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
ElevatedButton.icon(
|
|
|
|
onPressed: onTap,
|
|
|
|
icon: const Icon(Icons.open_in_full),
|
|
|
|
label: Text("show".i18n())),
|
|
|
|
if (model.unread) const UnreadIndicator(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Wrap(
|
|
|
|
alignment: WrapAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
SelectableText(model.getAccountsString()),
|
|
|
|
SelectableText(
|
|
|
|
"${"you-are".i18n()} ${model.identity}"),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
if (model.lastStatus != null)
|
2022-10-01 09:59:43 +00:00
|
|
|
PostTextRenderer(
|
|
|
|
input: model.lastStatus!.content,
|
|
|
|
identityName: model.identity,
|
|
|
|
),
|
2022-09-30 15:45:07 +00:00
|
|
|
],
|
2022-09-29 17:44:01 +00:00
|
|
|
),
|
2022-09-27 22:43:59 +00:00
|
|
|
),
|
2022-09-29 13:55:58 +00:00
|
|
|
),
|
2022-09-27 19:49:11 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-07-02 22:03:10 +00:00
|
|
|
}
|
2022-09-29 17:44:01 +00:00
|
|
|
|
|
|
|
class UnreadIndicator extends StatefulWidget {
|
|
|
|
const UnreadIndicator({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<UnreadIndicator> createState() => _UnreadIndicatorState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UnreadIndicatorState extends State<UnreadIndicator> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
const Icon(Icons.mark_unread_chat_alt),
|
|
|
|
SelectableText("unread".i18n())
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|