Minor refactor

This commit is contained in:
tommyxchow 2021-11-14 04:25:33 -05:00
parent 0bbf7f80d4
commit f4d30a710e
4 changed files with 19 additions and 22 deletions

View file

@ -8,7 +8,6 @@ import 'package:frosty/api/seventv_api.dart';
import 'package:frosty/api/twitch_api.dart';
import 'package:frosty/models/irc.dart';
import 'package:frosty/stores/auth_store.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:mobx/mobx.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
@ -21,19 +20,17 @@ abstract class _ChatStoreBase with Store {
/// The Twitch IRC WebSocket channel.
final _channel = WebSocketChannel.connect(Uri.parse('wss://irc-ws.chat.twitch.tv:443'));
/// The list of chat messages to render and display.
final messages = ObservableList<IRCMessage>();
/// The map of badges and emote words to their image or GIF URL.
final _assetToUrl = <String, String>{};
/// The list of chat messages to render and display.
final messages = ObservableList<IRCMessage>();
/// The scroll controller that controls auto-scroll and resume-scroll behavior.
final _scrollController = ScrollController();
ScrollController get scrollController => _scrollController;
final scrollController = ScrollController();
/// The text controller that handles the TextField inputs and sending of messages.
final _textController = TextEditingController();
TextEditingController get textController => _textController;
final textController = TextEditingController();
/// The name of the channel to connect to.
final String channelName;
@ -81,12 +78,12 @@ abstract class _ChatStoreBase with Store {
}
// Tell the scrollController to determine when auto-scroll should be enabled or disabled.
_scrollController.addListener(() {
scrollController.addListener(() {
// If the user scrolls up, auto-scroll will stop, allowing them to freely scroll back to previous messages.
// Else if the user scrolls back to the bottom edge (latest message), auto-scroll will resume.
if (!_scrollController.position.atEdge && _scrollController.position.pixels < _scrollController.position.maxScrollExtent) {
if (!scrollController.position.atEdge && scrollController.position.pixels < scrollController.position.maxScrollExtent) {
_autoScroll = false;
} else if (_scrollController.position.atEdge && _scrollController.position.pixels != _scrollController.position.minScrollExtent) {
} else if (scrollController.position.atEdge && scrollController.position.pixels != scrollController.position.minScrollExtent) {
_autoScroll = true;
}
});
@ -158,7 +155,7 @@ abstract class _ChatStoreBase with Store {
messages.add(userChatMessage);
// Clear the previous input in the TextField.
_textController.clear();
textController.clear();
}
/// If [_autoScroll] is enabled, removes messages if [messages] is too large and scrolls to the latest message.
@ -173,8 +170,8 @@ abstract class _ChatStoreBase with Store {
// After the end of the frame, scroll to the bottom of the chat.
// This is a postFrameCallback because the chat should scroll after the widget is built and rendered.
SchedulerBinding.instance?.addPostFrameCallback((_) {
if (_scrollController.hasClients) {
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
if (scrollController.hasClients) {
scrollController.jumpTo(scrollController.position.maxScrollExtent);
}
});
}
@ -186,11 +183,11 @@ abstract class _ChatStoreBase with Store {
_autoScroll = true;
// Jump to the latest message (bottom of the list/chat).
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
scrollController.jumpTo(scrollController.position.maxScrollExtent);
// Schedule a postFrameCallback in the event a new message is added at the same time.
SchedulerBinding.instance?.addPostFrameCallback((_) {
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
scrollController.jumpTo(scrollController.position.maxScrollExtent);
});
}
@ -251,7 +248,7 @@ abstract class _ChatStoreBase with Store {
: const Text('Permanently Banned.')
: Text(
'Timed out for $banDuration second(s).',
style: GoogleFonts.inter(fontWeight: FontWeight.bold),
style: const TextStyle(fontWeight: FontWeight.bold),
),
],
),
@ -292,7 +289,7 @@ abstract class _ChatStoreBase with Store {
/// Closes and disposes all the channels and controllers used by the store.
void dispose() {
_channel.sink.close();
_textController.dispose();
_scrollController.dispose();
textController.dispose();
scrollController.dispose();
}
}

View file

@ -37,7 +37,7 @@ abstract class _StreamListBase with Store {
}
}
/// Returns the appropriate streams the [category].
/// Returns the appropriate streams given the [category].
ObservableList<Stream> streams({required StreamCategory category}) {
switch (category) {
case StreamCategory.top:

View file

@ -48,7 +48,7 @@ abstract class _VideoStoreBase with Store {
}
}
void removeOverlay() {
void initVideo() {
controller.runJavascript('document.getElementsByClassName("video-player__overlay")[0].innerHTML = "";');
}

View file

@ -33,7 +33,7 @@ class Video extends StatelessWidget {
videoStore.controller = controller;
},
onPageFinished: (string) {
videoStore.removeOverlay();
videoStore.initVideo();
},
),
GestureDetector(