Solian/lib/screens/chat/index.dart

189 lines
5.8 KiB
Dart
Raw Normal View History

2024-04-16 14:29:58 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/providers/auth.dart';
2024-05-01 17:38:45 +00:00
import 'package:solian/providers/chat.dart';
2024-04-17 15:00:53 +00:00
import 'package:solian/router.dart';
2024-05-01 16:49:38 +00:00
import 'package:solian/screens/chat/chat.dart';
2024-04-16 14:29:58 +00:00
import 'package:solian/utils/service_url.dart';
2024-05-01 17:38:45 +00:00
import 'package:solian/widgets/chat/channel_action.dart';
2024-04-19 11:36:03 +00:00
import 'package:solian/widgets/chat/chat_new.dart';
2024-05-01 16:49:38 +00:00
import 'package:solian/widgets/empty.dart';
2024-04-29 12:22:06 +00:00
import 'package:solian/widgets/exts.dart';
2024-04-16 14:29:58 +00:00
import 'package:solian/widgets/indent_wrapper.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2024-04-25 13:33:53 +00:00
import 'package:solian/widgets/notification_notifier.dart';
2024-04-19 10:02:53 +00:00
import 'package:solian/widgets/signin_required.dart';
2024-04-16 14:29:58 +00:00
class ChatIndexScreen extends StatefulWidget {
const ChatIndexScreen({super.key});
@override
State<ChatIndexScreen> createState() => _ChatIndexScreenState();
}
class _ChatIndexScreenState extends State<ChatIndexScreen> {
2024-05-01 16:49:38 +00:00
Channel? _selectedChannel;
@override
Widget build(BuildContext context) {
2024-05-01 17:38:45 +00:00
final chat = context.watch<ChatProvider>();
2024-05-01 16:49:38 +00:00
final screenWidth = MediaQuery.of(context).size.width;
final isLargeScreen = screenWidth >= 600;
return IndentWrapper(
title: AppLocalizations.of(context)!.chat,
2024-05-01 17:38:45 +00:00
appBarActions: chat.focusChannel != null
? [
ChannelCallAction(
call: chat.ongoingCall,
channel: chat.focusChannel!,
onUpdate: () => chat.fetchChannel(chat.focusChannel!.alias),
),
ChannelManageAction(
channel: chat.focusChannel!,
onUpdate: () => chat.fetchChannel(chat.focusChannel!.alias),
),
]
: [const NotificationButton()],
2024-05-01 16:49:38 +00:00
fixedAppBarColor: isLargeScreen,
child: isLargeScreen
? Row(
children: [
Flexible(
flex: 2,
child: ChatIndexScreenWidget(
onSelect: (item) {
setState(() => _selectedChannel = item);
},
),
),
const VerticalDivider(thickness: 0.3, width: 0.3),
Flexible(
flex: 4,
child: _selectedChannel == null
? const SelectionEmptyWidget()
: ChatScreenWidget(
key: Key('c${_selectedChannel!.id}'),
alias: _selectedChannel!.alias,
),
),
],
)
: ChatIndexScreenWidget(
onSelect: (item) async {
final result = await router.pushNamed(
'chat.channel',
pathParameters: {
'channel': item.alias,
},
);
switch (result) {
case 'refresh':
// fetchChannels();
}
},
),
);
}
}
class ChatIndexScreenWidget extends StatefulWidget {
final Function(Channel item) onSelect;
const ChatIndexScreenWidget({super.key, required this.onSelect});
@override
State<ChatIndexScreenWidget> createState() => _ChatIndexScreenWidgetState();
}
class _ChatIndexScreenWidgetState extends State<ChatIndexScreenWidget> {
2024-04-16 14:29:58 +00:00
List<Channel> _channels = List.empty();
Future<void> fetchChannels() async {
2024-04-16 14:29:58 +00:00
final auth = context.read<AuthProvider>();
if (!await auth.isAuthorized()) return;
var uri = getRequestUri('messaging', '/api/channels/me/available');
var res = await auth.client!.get(uri);
if (res.statusCode == 200) {
final result = jsonDecode(utf8.decode(res.bodyBytes)) as List<dynamic>;
setState(() {
_channels = result.map((x) => Channel.fromJson(x)).toList();
});
} else {
var message = utf8.decode(res.bodyBytes);
2024-04-29 12:22:06 +00:00
context.showErrorDialog(message);
2024-04-16 14:29:58 +00:00
}
}
2024-04-19 11:36:03 +00:00
void viewNewChatAction() {
showModalBottomSheet(
context: context,
builder: (context) => ChatNewAction(onUpdate: () => fetchChannels()),
2024-04-19 11:36:03 +00:00
);
}
2024-04-16 14:29:58 +00:00
@override
void initState() {
Future.delayed(Duration.zero, () {
fetchChannels();
2024-04-16 14:29:58 +00:00
});
super.initState();
}
@override
Widget build(BuildContext context) {
2024-04-19 10:02:53 +00:00
final auth = context.read<AuthProvider>();
2024-05-01 16:49:38 +00:00
return Scaffold(
2024-04-19 11:36:03 +00:00
floatingActionButton: FutureBuilder(
future: auth.isAuthorized(),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data!) {
return FloatingActionButton.extended(
icon: const Icon(Icons.add),
label: Text(AppLocalizations.of(context)!.chatNew),
onPressed: () => viewNewChatAction(),
);
} else {
return Container();
}
},
),
2024-05-01 16:49:38 +00:00
body: FutureBuilder(
future: auth.isAuthorized(),
builder: (context, snapshot) {
if (!snapshot.hasData || !snapshot.data!) {
return const SignInRequiredScreen();
}
return RefreshIndicator(
onRefresh: () => fetchChannels(),
child: ListView.builder(
itemCount: _channels.length,
itemBuilder: (context, index) {
final element = _channels[index];
return ListTile(
leading: const CircleAvatar(
backgroundColor: Colors.indigo,
child: Icon(Icons.tag, color: Colors.white),
),
title: Text(element.name),
subtitle: Text(element.description),
onTap: () => widget.onSelect(element),
);
},
),
);
},
),
2024-04-16 14:29:58 +00:00
);
}
}