2024-04-18 14:33:55 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2024-04-26 17:36:54 +00:00
|
|
|
import 'package:solian/models/call.dart';
|
2024-04-26 15:25:56 +00:00
|
|
|
import 'package:solian/models/channel.dart';
|
2024-04-18 14:33:55 +00:00
|
|
|
import 'package:solian/models/message.dart';
|
|
|
|
import 'package:solian/models/packet.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/providers/chat.dart';
|
2024-04-18 14:43:14 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-04-18 14:33:55 +00:00
|
|
|
|
|
|
|
class ChatMaintainer extends StatefulWidget {
|
|
|
|
final Widget child;
|
2024-04-26 15:25:56 +00:00
|
|
|
final Channel channel;
|
2024-04-18 14:43:14 +00:00
|
|
|
final Function(Message val) onInsertMessage;
|
|
|
|
final Function(Message val) onUpdateMessage;
|
|
|
|
final Function(Message val) onDeleteMessage;
|
2024-04-26 17:36:54 +00:00
|
|
|
final Function(Call val) onCallStarted;
|
|
|
|
final Function() onCallEnded;
|
2024-04-18 14:33:55 +00:00
|
|
|
|
2024-04-18 14:43:14 +00:00
|
|
|
const ChatMaintainer({
|
|
|
|
super.key,
|
|
|
|
required this.child,
|
2024-04-26 15:25:56 +00:00
|
|
|
required this.channel,
|
2024-04-18 14:43:14 +00:00
|
|
|
required this.onInsertMessage,
|
|
|
|
required this.onUpdateMessage,
|
|
|
|
required this.onDeleteMessage,
|
2024-04-26 17:36:54 +00:00
|
|
|
required this.onCallStarted,
|
|
|
|
required this.onCallEnded,
|
2024-04-18 14:43:14 +00:00
|
|
|
});
|
2024-04-18 14:33:55 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<ChatMaintainer> createState() => _ChatMaintainerState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChatMaintainerState extends State<ChatMaintainer> {
|
2024-04-18 14:43:14 +00:00
|
|
|
void connect() {
|
2024-04-25 13:33:53 +00:00
|
|
|
ScaffoldMessenger.of(context).clearSnackBars();
|
|
|
|
|
2024-04-18 14:43:14 +00:00
|
|
|
final notify = ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.connectingServer),
|
2024-04-24 15:19:26 +00:00
|
|
|
duration: const Duration(minutes: 1),
|
2024-04-18 14:43:14 +00:00
|
|
|
),
|
|
|
|
);
|
2024-04-18 14:33:55 +00:00
|
|
|
|
2024-04-18 14:43:14 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
final chat = context.read<ChatProvider>();
|
|
|
|
|
|
|
|
chat.connect(auth).then((snapshot) {
|
|
|
|
snapshot!.stream.listen(
|
|
|
|
(event) {
|
2024-04-18 14:33:55 +00:00
|
|
|
final result = NetworkPackage.fromJson(jsonDecode(event));
|
|
|
|
switch (result.method) {
|
|
|
|
case 'messages.new':
|
2024-04-26 15:25:56 +00:00
|
|
|
final payload = Message.fromJson(result.payload!);
|
2024-05-01 16:49:38 +00:00
|
|
|
if (payload.channelId == widget.channel.id) widget.onInsertMessage(payload);
|
2024-04-18 14:43:14 +00:00
|
|
|
break;
|
|
|
|
case 'messages.update':
|
2024-04-26 15:25:56 +00:00
|
|
|
final payload = Message.fromJson(result.payload!);
|
2024-05-01 16:49:38 +00:00
|
|
|
if (payload.channelId == widget.channel.id) widget.onUpdateMessage(payload);
|
2024-04-18 14:43:14 +00:00
|
|
|
break;
|
|
|
|
case 'messages.burnt':
|
2024-04-26 15:25:56 +00:00
|
|
|
final payload = Message.fromJson(result.payload!);
|
2024-05-01 16:49:38 +00:00
|
|
|
if (payload.channelId == widget.channel.id) widget.onDeleteMessage(payload);
|
2024-04-18 14:43:14 +00:00
|
|
|
break;
|
2024-04-26 17:36:54 +00:00
|
|
|
case 'calls.new':
|
|
|
|
final payload = Call.fromJson(result.payload!);
|
2024-05-01 16:49:38 +00:00
|
|
|
if (payload.channelId == widget.channel.id) widget.onCallStarted(payload);
|
2024-04-26 17:36:54 +00:00
|
|
|
break;
|
|
|
|
case 'calls.end':
|
|
|
|
final payload = Call.fromJson(result.payload!);
|
|
|
|
if (payload.channelId == widget.channel.id) widget.onCallEnded();
|
|
|
|
break;
|
2024-04-18 14:33:55 +00:00
|
|
|
}
|
2024-04-18 14:43:14 +00:00
|
|
|
},
|
|
|
|
onError: (_, __) => connect(),
|
2024-04-24 15:19:26 +00:00
|
|
|
onDone: () => connect(),
|
2024-04-18 14:43:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
notify.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
Future.delayed(Duration.zero, () {
|
|
|
|
connect();
|
2024-04-18 14:33:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return widget.child;
|
|
|
|
}
|
|
|
|
}
|