Skip to content

Commit

Permalink
Merge pull request #128 from Nighty3098/InDev
Browse files Browse the repository at this point in the history
Fixed connection indicator. Optimized the function of getting the total number of stars.
  • Loading branch information
Nighty3098 authored Jul 20, 2024
2 parents 6c7cb6f + bc33255 commit 76a2a15
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 63 deletions.
56 changes: 15 additions & 41 deletions src/CodeKeeper/accountFunc/functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,62 +143,38 @@ void AccountWindow::setFontStyle()
"}");
}

int AccountWindow::getStarsCount(const QString& username)
int AccountWindow::getStarsCount(const QString& username, const QString& token)
{
QNetworkAccessManager manager;
QUrl url("https://api.github.com/users/" + username + "/repos");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::UserAgentHeader, "CodeKeeper");
request.setRawHeader("Authorization", ("Bearer " + git_token).toUtf8());
request.setRawHeader("X-GitHub-Api-Version", "2022-11-28");
request.setRawHeader("Authorization", ("Bearer " + token).toUtf8());
request.setRawHeader("Accept", "application/vnd.github.v3+json");

QNetworkReply* reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

if (reply->error()) {
qDebug() << "Error:" << reply->errorString();
return 0;
}

QJsonDocument json = QJsonDocument::fromJson(reply->readAll());
QJsonArray repos = json.array();

int totalStars = 0;
for (const QJsonValue& repo : repos) {
QJsonObject repoObject = repo.toObject();
QString repoUrl = repoObject["url"].toString();

QNetworkRequest repoRequest(repoUrl);
repoRequest.setHeader(QNetworkRequest::UserAgentHeader, "CodeKeeper");
repoRequest.setRawHeader("Authorization", ("Bearer " + git_token).toUtf8());
repoRequest.setRawHeader("X-GitHub-Api-Version", "2022-11-28");
repoRequest.setRawHeader("Accept", "application/vnd.github.v3+json");
;

QNetworkReply* repoReply = manager.get(repoRequest);
QEventLoop repoLoop;
QObject::connect(repoReply, &QNetworkReply::finished, &repoLoop, &QEventLoop::quit);
repoLoop.exec();

if (repoReply->error()) {
qDebug() << "Error:" << repoReply->errorString();
continue;
}

QJsonDocument repoJson = QJsonDocument::fromJson(repoReply->readAll());
QJsonObject repoJsonObject = repoJson.object();
int stars = repoJsonObject["stargazers_count"].toInt();
if (!reply->error()) {
QJsonDocument json = QJsonDocument::fromJson(reply->readAll());
QJsonArray repos = json.array();

totalStars += stars;
for (const QJsonValue& repo : repos) {
QJsonObject repoObject = repo.toObject();
totalStars += repoObject["stargazers_count"].toInt();
}
} else {
qDebug() << "Error:" << reply->errorString();
}

reply->deleteLater();
return totalStars;
}

void AccountWindow::setUserData(const QString& username)
void AccountWindow::setUserData(const QString& username, QLabel *label)
{
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QUrl url("https://api.github.com/users/" + git_user);
Expand Down Expand Up @@ -238,11 +214,9 @@ void AccountWindow::setUserData(const QString& username)
qDebug() << "Company:" << obj["company"].toString();
qDebug() << "Login:" << obj["login"].toString();

qDebug() << "" << doc;

profileInfo->setText("\n\n" + obj["bio"].toString() + "\nPublic repos: " + QString::number(obj["public_repos"].toInt()) + "\n\nFollowing: "
label->setText("\n\n" + obj["bio"].toString() + "\nPublic repos: " + QString::number(obj["public_repos"].toInt()) + "\n\nFollowing: "
+ QString::number(obj["following"].toInt()) + "\n\nFollowers: " + QString::number(obj["followers"].toInt())
+ "\n\nStars: " + QString::number(getStarsCount(git_user)) + "\n");
+ "\n\nStars: " + QString::number(getStarsCount(git_user, git_token)) + "\n");

reply->deleteLater();
});
Expand Down
33 changes: 17 additions & 16 deletions src/CodeKeeper/accountwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ AccountWindow::AccountWindow(QWidget* parent) : QMainWindow { parent }
codeKeeperStats->setAlignment(Qt::AlignHCenter);

tasksStatsProgress = new CircleProgressBar();
tasksStatsProgress->setFixedSize(50, 50);
tasksStatsProgress->setFixedSize(60, 60);
tasksStatsProgress->setBackgroundColor(QColor(Qt::transparent));
tasksStatsProgress->setLineWidth(font_size.toInt());
tasksStatsProgress->setDisplayMode(CircleProgressBar::Percent); // Percent, CustomText, NoPercent, Hidden
Expand All @@ -67,28 +67,28 @@ AccountWindow::AccountWindow(QWidget* parent) : QMainWindow { parent }
QHBoxLayout* projectsStatsLayout = new QHBoxLayout();

notStartedProjectsProgress = new CircleProgressBar();
notStartedProjectsProgress->setFixedSize(50, 50);
notStartedProjectsProgress->setFixedSize(60, 60);
notStartedProjectsProgress->setBackgroundColor(QColor(Qt::transparent));
notStartedProjectsProgress->setProgressColor(QColor("#c75d5e"));
notStartedProjectsProgress->setLineWidth(font_size.toInt() - 3);
notStartedProjectsProgress->setLineWidth(font_size.toInt());

startedProjectsProgress = new CircleProgressBar();
startedProjectsProgress->setFixedSize(50, 50);
startedProjectsProgress->setFixedSize(60, 60);
startedProjectsProgress->setBackgroundColor(QColor(Qt::transparent));
startedProjectsProgress->setProgressColor(QColor("#e09132"));
startedProjectsProgress->setLineWidth(font_size.toInt() - 3);
startedProjectsProgress->setLineWidth(font_size.toInt());

reviewProjectsProgress = new CircleProgressBar();
reviewProjectsProgress->setFixedSize(50, 50);
reviewProjectsProgress->setFixedSize(60, 60);
reviewProjectsProgress->setBackgroundColor(QColor(Qt::transparent));
reviewProjectsProgress->setProgressColor(QColor("#b1e032"));
reviewProjectsProgress->setLineWidth(font_size.toInt() - 3);
reviewProjectsProgress->setLineWidth(font_size.toInt());

completedProjectsProgress = new CircleProgressBar();
completedProjectsProgress->setFixedSize(50, 50);
completedProjectsProgress->setFixedSize(60, 60);
completedProjectsProgress->setBackgroundColor(QColor(Qt::transparent));
completedProjectsProgress->setProgressColor(QColor("#78b3ba"));
completedProjectsProgress->setLineWidth(font_size.toInt() - 3);
completedProjectsProgress->setLineWidth(font_size.toInt());

projectsStatsLayout->addWidget(notStartedProjectsProgress);
projectsStatsLayout->addWidget(startedProjectsProgress);
Expand All @@ -107,7 +107,7 @@ AccountWindow::AccountWindow(QWidget* parent) : QMainWindow { parent }
QObject::connect(setUserDataThread, &QThread::started, this, [this]() {
qDebug() << "setUserDataThread started";

setUserData(git_user);
setUserData(git_user, profileInfo);
setTasksProgress();
setProjectsStats();
});
Expand All @@ -122,13 +122,14 @@ AccountWindow::AccountWindow(QWidget* parent) : QMainWindow { parent }
setUserImageThread->start();

mainLayout->addWidget(closeWindow, 0, 0, 1, 6, Qt::AlignLeft);
mainLayout->addWidget(profilePicture, 1, 0, 3, 3, Qt::AlignCenter);
mainLayout->addWidget(profilePicture, 2, 0, 3, 3, Qt::AlignCenter);

mainLayout->addWidget(userName, 1, 3, 1, 3, Qt::AlignCenter);
mainLayout->addWidget(tasksTitle, 2, 3, 1, 3, Qt::AlignCenter);
mainLayout->addWidget(tasksStatsProgress, 3, 3, 1, 3, Qt::AlignCenter);
mainLayout->addWidget(projectTitle, 4, 3, 1, 3, Qt::AlignCenter);
mainLayout->addLayout(projectsStatsLayout, 5, 3, 1, 3, Qt::AlignCenter);
mainLayout->addWidget(userName, 2, 3, 1, 3, Qt::AlignCenter);

mainLayout->addWidget(tasksTitle, 3, 3, 1, 3, Qt::AlignCenter);
mainLayout->addWidget(tasksStatsProgress, 4, 3, 1, 3, Qt::AlignCenter);
mainLayout->addWidget(projectTitle, 5, 3, 1, 3, Qt::AlignCenter);
mainLayout->addLayout(projectsStatsLayout, 6, 3, 1, 3, Qt::AlignCenter);

mainLayout->addWidget(profileInfo, 14, 0, 3, 3, Qt::AlignCenter);
mainLayout->addWidget(codeKeeperStats, 14, 3, 3, 3, Qt::AlignCenter);
Expand Down
4 changes: 2 additions & 2 deletions src/CodeKeeper/accountwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class AccountWindow : public QMainWindow
CircleProgressBar *completedProjectsProgress;

private slots:
void setUserData(const QString &username);
void setUserData(const QString &username, QLabel *label);
void closeWindowSlot();
void getSettingsData();
void setImageFromUrl(const QString &url, QLabel *label);
void onOpenRepoClicked();
int getStarsCount(const QString &username);
int getStarsCount(const QString& username, const QString& token);
void setTasksProgress();
void setProjectsStats();
void get_image_url(const QString& username, QLabel *label);
Expand Down
2 changes: 1 addition & 1 deletion src/CodeKeeper/keeperFunc/functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ void MainWindow::setStyle(QFont* selectedFont, int* font_size_int)

tasksProgress->setFont(*selectedFont);
tasksProgress->setStyleSheet("background-color: rgb(211, 102, 107); selection-background-color: "
"rgb(118, 148, 106); color: #222436; font-size: "
"#c3e88d; color: #222436; font-size: "
+ font_size + "pt;");

menu->setStyleSheet("font-size: " + font_size + "pt;");
Expand Down
2 changes: 1 addition & 1 deletion src/CodeKeeper/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)

QTimer* connectionTimer = new QTimer(this);
connect(connectionTimer, &QTimer::timeout, this, &MainWindow::setConnectionStatus);
connectionTimer->start(100); // 1000ms = 1s
connectionTimer->start(1000); // 1000ms = 1s

} else {
sizeGrip2->hide();
Expand Down
4 changes: 2 additions & 2 deletions src/stylesheet/custom_stylesheet.qss
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ QProgressBar {

QProgressBar::chunk {
color: #0a070d;
background-color: #74bbbc;
background-color: #c3e88d;
border-radius: 5px;
}

Expand Down Expand Up @@ -535,4 +535,4 @@ QToolTip {
border: 10px #171b22;
border-radius: 5px;
background-color: #171b22;
}
}

0 comments on commit 76a2a15

Please sign in to comment.