Moved online status checking logic from ChatWidgetHeader to TwitchChannel
- Channel now needs to be initialized with a name. Special cases like the emote window just sends an empty string. - ChatWidget now has a signal which is called whenever the widgets channel is changed - Changed roomID from an std::string to a QString
This commit is contained in:
@@ -119,10 +119,7 @@ void ChatWidget::setChannel(std::shared_ptr<Channel> _newChannel)
|
||||
|
||||
this->channel = _newChannel;
|
||||
|
||||
twitch::TwitchChannel *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(_newChannel.get());
|
||||
if (twitchChannel != nullptr) {
|
||||
twitchChannel->roomIDchanged.connect([this]() { this->header.checkLive(); });
|
||||
}
|
||||
this->channelChanged();
|
||||
}
|
||||
|
||||
void ChatWidget::channelNameUpdated(const std::string &newChannelName)
|
||||
@@ -224,9 +221,6 @@ void ChatWidget::doCloseSplit()
|
||||
{
|
||||
NotebookPage *page = static_cast<NotebookPage *>(this->parentWidget());
|
||||
page->removeFromLayout(this);
|
||||
QTimer *timer = this->header.findChild<QTimer *>();
|
||||
timer->stop();
|
||||
timer->deleteLater();
|
||||
}
|
||||
|
||||
void ChatWidget::doChangeChannel()
|
||||
|
||||
@@ -59,6 +59,8 @@ public:
|
||||
void layoutMessages();
|
||||
void updateGifEmotes();
|
||||
|
||||
boost::signals2::signal<void()> channelChanged;
|
||||
|
||||
protected:
|
||||
virtual void paintEvent(QPaintEvent *) override;
|
||||
|
||||
|
||||
@@ -69,9 +69,26 @@ ChatWidgetHeader::ChatWidgetHeader(ChatWidget *_chatWidget)
|
||||
this->rightLabel.getLabel().setTextFormat(Qt::RichText);
|
||||
this->rightLabel.getLabel().setText("ayy");
|
||||
|
||||
QTimer *timer = new QTimer(this);
|
||||
connect(timer, &QTimer::timeout, this, &ChatWidgetHeader::checkLive);
|
||||
timer->start(60000);
|
||||
this->initializeChannelSignals();
|
||||
|
||||
this->chatWidget->channelChanged.connect([this]() {
|
||||
this->initializeChannelSignals(); //
|
||||
});
|
||||
}
|
||||
|
||||
void ChatWidgetHeader::initializeChannelSignals()
|
||||
{
|
||||
// Disconnect any previous signal first
|
||||
this->onlineStatusChangedConnection.disconnect();
|
||||
|
||||
auto channel = this->chatWidget->getChannel();
|
||||
twitch::TwitchChannel *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get());
|
||||
|
||||
if (twitchChannel) {
|
||||
twitchChannel->onlineStatusChanged.connect([this]() {
|
||||
this->updateChannelText(); //
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ChatWidgetHeader::resizeEvent(QResizeEvent *event)
|
||||
@@ -202,43 +219,5 @@ void ChatWidgetHeader::menuShowChangelog()
|
||||
{
|
||||
}
|
||||
|
||||
// TODO: this needs to be moved out of here
|
||||
void ChatWidgetHeader::checkLive()
|
||||
{
|
||||
twitch::TwitchChannel *channel =
|
||||
dynamic_cast<twitch::TwitchChannel *>(this->chatWidget->getChannel().get());
|
||||
|
||||
if (channel == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto id = QString::fromStdString(channel->roomID);
|
||||
|
||||
if (id.isEmpty()) {
|
||||
channel->isLive = false;
|
||||
this->updateChannelText();
|
||||
return;
|
||||
}
|
||||
|
||||
util::twitch::get("https://api.twitch.tv/kraken/streams/" + id, this, [=](QJsonObject obj) {
|
||||
if (obj.value("stream").isNull()) {
|
||||
channel->isLive = false;
|
||||
this->updateChannelText();
|
||||
} else {
|
||||
channel->isLive = true;
|
||||
auto stream = obj.value("stream").toObject();
|
||||
channel->streamViewerCount = QString::number(stream.value("viewers").toDouble());
|
||||
channel->streamGame = stream.value("game").toString();
|
||||
channel->streamStatus = stream.value("channel").toObject().value("status").toString();
|
||||
QDateTime since =
|
||||
QDateTime::fromString(stream.value("created_at").toString(), Qt::ISODate);
|
||||
auto diff = since.secsTo(QDateTime::currentDateTime());
|
||||
channel->streamUptime =
|
||||
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
|
||||
this->updateChannelText();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace widgets
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <QPaintEvent>
|
||||
#include <QPoint>
|
||||
#include <QWidget>
|
||||
#include <boost/signals2/connection.hpp>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
@@ -29,7 +30,6 @@ public:
|
||||
explicit ChatWidgetHeader(ChatWidget *_chatWidget);
|
||||
// Update channel text from chat widget
|
||||
void updateChannelText();
|
||||
void checkLive();
|
||||
|
||||
protected:
|
||||
virtual void paintEvent(QPaintEvent *) override;
|
||||
@@ -44,6 +44,8 @@ private:
|
||||
QPoint dragStart;
|
||||
bool dragging = false;
|
||||
|
||||
boost::signals2::connection onlineStatusChangedConnection;
|
||||
|
||||
QHBoxLayout hbox;
|
||||
|
||||
// top left
|
||||
@@ -62,6 +64,8 @@ private:
|
||||
|
||||
virtual void refreshTheme() override;
|
||||
|
||||
void initializeChannelSignals();
|
||||
|
||||
public slots:
|
||||
void menuMoveSplit();
|
||||
void menuReloadChannelEmotes();
|
||||
|
||||
@@ -34,7 +34,7 @@ void EmotePopup::loadChannel(std::shared_ptr<Channel> _channel)
|
||||
return;
|
||||
}
|
||||
|
||||
std::shared_ptr<Channel> emoteChannel(new Channel);
|
||||
std::shared_ptr<Channel> emoteChannel(new Channel(""));
|
||||
|
||||
auto addEmotes = [&](EmoteMap &map, const QString &title, const QString &emoteDesc) {
|
||||
// TITLE
|
||||
|
||||
Reference in New Issue
Block a user