From 5762a959f185f3cb429639ef6c6dd80dcbc728c1 Mon Sep 17 00:00:00 2001 From: Tommy Chow Date: Sat, 9 Oct 2021 03:49:12 -0400 Subject: [PATCH] Refactor chat widget to use dependency injection --- lib/screens/video_chat.dart | 3 ++- lib/widgets/chat.dart | 26 +++++++++++--------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/screens/video_chat.dart b/lib/screens/video_chat.dart index 589d892e..d625e93b 100644 --- a/lib/screens/video_chat.dart +++ b/lib/screens/video_chat.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:frosty/models/channel.dart'; import 'package:frosty/stores/auth_store.dart'; +import 'package:frosty/stores/chat_store.dart'; import 'package:frosty/stores/settings_store.dart'; import 'package:frosty/widgets/chat.dart'; import 'package:frosty/widgets/video.dart'; @@ -26,7 +27,7 @@ class VideoChat extends StatelessWidget { ), ), Expanded( - child: Chat(auth: context.read(), channelInfo: channelInfo), + child: Chat(chatStore: ChatStore(auth: context.read(), channelInfo: channelInfo), channelInfo: channelInfo), ), const TextField(), ], diff --git a/lib/widgets/chat.dart b/lib/widgets/chat.dart index 2a8fdde0..684eb4b6 100644 --- a/lib/widgets/chat.dart +++ b/lib/widgets/chat.dart @@ -1,53 +1,49 @@ import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:frosty/models/channel.dart'; -import 'package:frosty/stores/auth_store.dart'; import 'package:frosty/stores/chat_store.dart'; class Chat extends StatefulWidget { - final AuthStore auth; + final ChatStore chatStore; final Channel channelInfo; - const Chat({Key? key, required this.auth, required this.channelInfo}) : super(key: key); + const Chat({Key? key, required this.chatStore, required this.channelInfo}) : super(key: key); @override _ChatState createState() => _ChatState(); } class _ChatState extends State { - late final ChatStore chatStore = ChatStore(auth: widget.auth, channelInfo: widget.channelInfo); - @override Widget build(BuildContext context) { return FutureBuilder( - future: chatStore.getAssets(), + future: widget.chatStore.getAssets(), builder: (_, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return StreamBuilder( - stream: chatStore.channel.stream, + stream: widget.chatStore.channel.stream, builder: (context, snapshot) { - chatStore.handleWebsocketData(snapshot.data); + widget.chatStore.handleWebsocketData(snapshot.data); return Stack( alignment: AlignmentDirectional.bottomCenter, children: [ ListView.builder( - physics: const ClampingScrollPhysics(), - itemCount: chatStore.messages.length, - controller: chatStore.scrollController, + itemCount: widget.chatStore.messages.length, + controller: widget.chatStore.scrollController, padding: const EdgeInsets.all(5.0), itemBuilder: (context, index) { - return chatStore.parseIrcMessage(chatStore.messages[index]); + return widget.chatStore.parseIrcMessage(widget.chatStore.messages[index]); }, ), Observer( builder: (_) { return Visibility( - visible: !chatStore.autoScroll, + visible: !widget.chatStore.autoScroll, child: Container( padding: const EdgeInsets.symmetric(horizontal: 4.0), width: double.infinity, child: ElevatedButton( - onPressed: () => chatStore.resumeScroll(), + onPressed: () => widget.chatStore.resumeScroll(), child: const Text('Resume Scroll'), ), ), @@ -68,7 +64,7 @@ class _ChatState extends State { @override void dispose() { - chatStore.channel.sink.close(); + widget.chatStore.channel.sink.close(); super.dispose(); } }