143 lines
4.3 KiB
Dart
143 lines
4.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:localization/localization.dart';
|
|
import 'package:loris/business_logic/chat/chat.dart';
|
|
import 'package:loris/partials/loadingbox.dart';
|
|
import 'package:loris/partials/post_text_renderer.dart';
|
|
import 'package:loris/themes/themes.dart' as themes;
|
|
import 'package:loris/global.dart' as global;
|
|
|
|
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 = {};
|
|
final scrollController = ScrollController();
|
|
bool loading = false;
|
|
|
|
// loads more conversations and properly stores all ids
|
|
Future<void> updateConversations() async {
|
|
if (loading) return;
|
|
loading = true;
|
|
final models = await getAllConversationModels(maxIds);
|
|
setState(() {
|
|
conversations.addAll(models.models);
|
|
maxIds = models.maxIds;
|
|
});
|
|
loading = false;
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
updateConversations();
|
|
scrollController.addListener(() {
|
|
if (scrollController.position.maxScrollExtent - scrollController.offset <
|
|
MediaQuery.of(context).size.height) {
|
|
updateConversations();
|
|
}
|
|
});
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
children: [
|
|
Expanded(
|
|
child: ListView.separated(
|
|
controller: scrollController,
|
|
shrinkWrap: true,
|
|
itemBuilder: ((context, index) {
|
|
if (index >= conversations.length) return const LoadingBox();
|
|
return ConversationButton(
|
|
model: conversations[index],
|
|
onPressed: () {},
|
|
);
|
|
}),
|
|
separatorBuilder: (context, index) => const Divider(
|
|
height: themes.defaultSeperatorHeight,
|
|
color: Colors.transparent,
|
|
),
|
|
itemCount: conversations.length + 1),
|
|
),
|
|
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,
|
|
required this.onPressed,
|
|
});
|
|
final ConversationModel model;
|
|
final void Function() onPressed;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
borderRadius: const BorderRadius.all(themes.defaultRadius),
|
|
border: Border.fromBorderSide(BorderSide(
|
|
color: Theme.of(context).colorScheme.secondary, width: 2)),
|
|
),
|
|
margin: const EdgeInsets.fromLTRB(themes.defaultSeperatorHeight * 2, 0,
|
|
themes.defaultSeperatorHeight * 2, 0),
|
|
width: global.getWidth(context),
|
|
constraints: global.getConstraints(context),
|
|
child: InkWell(
|
|
borderRadius: const BorderRadius.all(themes.defaultRadius),
|
|
onTap: onPressed,
|
|
child: Ink(
|
|
decoration: BoxDecoration(
|
|
borderRadius: const BorderRadius.all(themes.defaultRadius),
|
|
color: Theme.of(context).colorScheme.surface,
|
|
),
|
|
child: Padding(
|
|
padding: themes.defaultInsideMargins,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
if (model.unread)
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.mark_chat_unread),
|
|
SelectableText("unread".i18n())
|
|
],
|
|
),
|
|
Wrap(
|
|
alignment: WrapAlignment.spaceBetween,
|
|
children: [
|
|
if (model.unread) SelectableText(model.getAccountsString()),
|
|
SelectableText("${"you-are".i18n()} ${model.identity}"),
|
|
],
|
|
),
|
|
if (model.lastStatus != null)
|
|
PostTextRenderer(input: model.lastStatus!.content),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|