2024-11-16 05:54:36 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2024-11-13 16:08:09 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-11-16 05:54:36 +00:00
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import 'package:material_symbols_icons/symbols.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2024-11-16 13:15:55 +00:00
|
|
|
import 'package:surface/providers/channel.dart';
|
2024-11-16 08:55:31 +00:00
|
|
|
import 'package:surface/types/chat.dart';
|
|
|
|
import 'package:surface/widgets/account/account_image.dart';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'package:surface/widgets/dialog.dart';
|
2024-11-16 08:55:31 +00:00
|
|
|
import 'package:surface/widgets/loading_indicator.dart';
|
2024-11-13 16:08:09 +00:00
|
|
|
|
2024-11-16 05:54:36 +00:00
|
|
|
class ChatScreen extends StatefulWidget {
|
2024-11-13 16:08:09 +00:00
|
|
|
const ChatScreen({super.key});
|
|
|
|
|
2024-11-16 05:54:36 +00:00
|
|
|
@override
|
|
|
|
State<ChatScreen> createState() => _ChatScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChatScreenState extends State<ChatScreen> {
|
2024-11-16 13:15:55 +00:00
|
|
|
bool _isBusy = true;
|
2024-11-16 08:55:31 +00:00
|
|
|
|
|
|
|
List<SnChannel>? _channels;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-11-16 13:15:55 +00:00
|
|
|
final chan = context.read<ChatChannelProvider>();
|
|
|
|
chan.fetchChannels().listen((channels) {
|
|
|
|
if (mounted) setState(() => _channels = channels);
|
|
|
|
})
|
2024-11-16 17:16:54 +00:00
|
|
|
..onError((err) {
|
|
|
|
if (!mounted) return;
|
|
|
|
context.showErrorDialog(err);
|
2024-11-16 13:15:55 +00:00
|
|
|
setState(() => _isBusy = false);
|
|
|
|
})
|
|
|
|
..onDone(() {
|
2024-11-16 17:16:54 +00:00
|
|
|
if (!mounted) return;
|
2024-11-16 13:15:55 +00:00
|
|
|
setState(() => _isBusy = false);
|
|
|
|
});
|
2024-11-16 05:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 16:08:09 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-11-16 05:54:36 +00:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text('screenChat').tr(),
|
|
|
|
),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
child: const Icon(Symbols.chat_add_on),
|
|
|
|
onPressed: () {
|
|
|
|
GoRouter.of(context).pushNamed('chatManage');
|
|
|
|
},
|
|
|
|
),
|
2024-11-16 08:55:31 +00:00
|
|
|
body: Column(
|
|
|
|
children: [
|
|
|
|
LoadingIndicator(isActive: _isBusy),
|
|
|
|
Expanded(
|
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: _channels?.length ?? 0,
|
|
|
|
itemBuilder: (context, idx) {
|
|
|
|
final channel = _channels![idx];
|
|
|
|
return ListTile(
|
|
|
|
title: Text(channel.name),
|
2024-11-16 13:15:55 +00:00
|
|
|
subtitle: Text(
|
|
|
|
channel.description,
|
|
|
|
maxLines: 1,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
2024-11-16 08:55:31 +00:00
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
|
|
|
|
leading: AccountImage(
|
|
|
|
content: null,
|
|
|
|
fallbackWidget: const Icon(Symbols.chat, size: 20),
|
|
|
|
),
|
2024-11-16 17:16:54 +00:00
|
|
|
onTap: () {
|
|
|
|
GoRouter.of(context).pushNamed(
|
|
|
|
'chatRoom',
|
|
|
|
pathParameters: {
|
|
|
|
'scope': channel.realm?.alias ?? 'global',
|
|
|
|
'alias': channel.alias,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
2024-11-16 08:55:31 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2024-11-16 05:54:36 +00:00
|
|
|
);
|
2024-11-13 16:08:09 +00:00
|
|
|
}
|
|
|
|
}
|