Implement ROOMSTATE

- Added ROOMSTATE observable
- Fixed ROOMSTATE not registering in handleWebsocketData due to returnining in the for loop
This commit is contained in:
tommyxchow 2021-10-22 02:46:06 -04:00
parent bb74b7bd6e
commit dc47812a6b
2 changed files with 48 additions and 15 deletions

View file

@ -6,7 +6,7 @@ import 'package:frosty/api/ffz_api.dart';
import 'package:frosty/api/irc_api.dart';
import 'package:frosty/api/seventv_api.dart';
import 'package:frosty/api/twitch_api.dart';
import 'package:frosty/models/irc_message.dart';
import 'package:frosty/models/irc.dart';
import 'package:frosty/stores/auth_store.dart';
import 'package:frosty/widgets/chat_message.dart';
import 'package:mobx/mobx.dart';
@ -20,6 +20,13 @@ abstract class _ChatStoreBase with Store {
@readonly
var _autoScroll = true;
@readonly
ROOMSTATE? _roomState;
String? _userState;
// String? _globalUserState;
final messages = ObservableList<IRCMessage>();
final _assetToUrl = <String, String>{};
@ -35,10 +42,6 @@ abstract class _ChatStoreBase with Store {
final _channel = WebSocketChannel.connect(Uri.parse('wss://irc-ws.chat.twitch.tv:443'));
WebSocketChannel get channel => _channel;
String? userState;
String? globalUserState;
String? roomState;
final String channelName;
final AuthStore auth;
@ -51,7 +54,6 @@ abstract class _ChatStoreBase with Store {
final commands = [
'CAP REQ :twitch.tv/tags twitch.tv/commands',
'CAP END',
'PASS oauth:${auth.token}',
'NICK ${auth.isLoggedIn ? auth.user!.login : 'justinfan888'}',
'JOIN #$channelName',
@ -95,11 +97,12 @@ abstract class _ChatStoreBase with Store {
}
}
@action
void _handleWebsocketData(String data) {
for (final message in data.substring(0, data.length - 2).split('\r\n')) {
// debugPrint(message);
if (message.startsWith('@')) {
final parsedIRCMessage = IRC.parse(message);
final parsedIRCMessage = IRCMessage.fromString(message);
switch (parsedIRCMessage.command) {
case 'CLEARCHAT':
@ -110,24 +113,24 @@ abstract class _ChatStoreBase with Store {
break;
case 'GLOBALUSERSTATE':
// Updates the current global user state data (it includes user-id)
globalUserState = message;
return;
// _globalUserState = message;
break;
case 'PRIVMSG':
messages.add(parsedIRCMessage);
break;
case 'ROOMSTATE':
roomState = message;
return;
_roomState = ROOMSTATE.fromMessage(parsedIRCMessage);
break;
case 'USERNOTICE':
debugPrint(message);
// _messages = IRC.USERNOTICE(messages: _messages, ircMessage: parsedIRCMessage);
break;
case 'USERSTATE':
// Updates the current user-state data
userState = message;
return;
_userState = message;
break;
}
} else if (message.startsWith('P')) {
} else if (message == 'PING :tmi.twitch.tv') {
channel.sink.add('PONG :tmi.twitch.tv');
return;
}
@ -276,7 +279,9 @@ abstract class _ChatStoreBase with Store {
_channel.sink.add('PRIVMSG #$channelName :$message');
IRC.addMessage(messages: messages, userState: userState!, sentMessage: message);
final userChatMessage = IRCMessage.fromString(_userState!);
userChatMessage.message = message;
messages.add(userChatMessage);
_textController.clear();
}

View file

@ -26,9 +26,37 @@ mixin _$ChatStore on _ChatStoreBase, Store {
});
}
final _$_roomStateAtom = Atom(name: '_ChatStoreBase._roomState');
ROOMSTATE? get roomState {
_$_roomStateAtom.reportRead();
return super._roomState;
}
@override
ROOMSTATE? get _roomState => roomState;
@override
set _roomState(ROOMSTATE? value) {
_$_roomStateAtom.reportWrite(value, super._roomState, () {
super._roomState = value;
});
}
final _$_ChatStoreBaseActionController =
ActionController(name: '_ChatStoreBase');
@override
void _handleWebsocketData(String data) {
final _$actionInfo = _$_ChatStoreBaseActionController.startAction(
name: '_ChatStoreBase._handleWebsocketData');
try {
return super._handleWebsocketData(data);
} finally {
_$_ChatStoreBaseActionController.endAction(_$actionInfo);
}
}
@override
void resumeScroll() {
final _$actionInfo = _$_ChatStoreBaseActionController.startAction(