Skip to content

Commit

Permalink
Prepare timesheet history rework
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePTJ committed Jun 10, 2024
1 parent f710b3a commit 1f9357f
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(SRCS
models/loggerTreeModel.cpp
models/taskFilterProxyModel.cpp
models/taskListModel.cpp
models/timeSheetModel.cpp
models/timeSheetParamsModel.cpp
monitor/desktopEventsMonitor.cpp
monitor/kimaiEventsMonitor.cpp
Expand Down Expand Up @@ -62,6 +63,7 @@ set(HDRS
models/loggerTreeModel.h
models/taskFilterProxyModel.h
models/taskListModel.h
models/timeSheetModel.h
models/timeSheetParamsModel.h
monitor/desktopEventsMonitor.h
monitor/kimaiEventsMonitor.h
Expand Down
7 changes: 7 additions & 0 deletions src/client/kimaiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ TimeSheetsResult KimaiClient::requestRecentTimeSheets()
return mD->processApiNetworkReplyArray<TimeSheet>(ApiMethod::RecentTimeSheets, reply);
}

TimeSheetsResult KimaiClient::requestTimeSheets(const QString& term, int page)
{
auto request = mD->prepareRequest(ApiMethod::TimeSheets, {{"page", QString::number(page)}, {"full", "1"}, {"term", term}});
auto reply = mD->sendGetRequest(request);
return mD->processApiNetworkReplyArray<TimeSheet>(ApiMethod::TimeSheets, reply);
}

ProjectsResult KimaiClient::requestProjects(std::optional<int> customerId)
{
std::map<QString, QString> parameters;
Expand Down
1 change: 1 addition & 0 deletions src/client/kimaiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class KimaiClient : public QObject
CustomersResult requestCustomers();
TimeSheetsResult requestActiveTimeSheets();
TimeSheetsResult requestRecentTimeSheets();
TimeSheetsResult requestTimeSheets(const QString& term = "", int page = 1);
ProjectsResult requestProjects(std::optional<int> customerId = std::nullopt);
ActivitiesResult requestActivities(std::optional<int> projectId = std::nullopt);

Expand Down
36 changes: 19 additions & 17 deletions src/gui/activityWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ ActivityWidget::ActivityWidget(QWidget* parent) : QWidget(parent), mUi(std::make
mUi->cbTimeSheetParams->setModel(&mTimeSheetParamsModel);
mUi->cbTimeSheetParams->setItemDelegate(new TimeSheetParamsItemDelegate);

mUi->lwHistory->setModel(&mTimeSheetModel);

connect(mUi->btStartStop, &QPushButton::clicked, this, &ActivityWidget::onBtStartStopClicked);
connect(mUi->cbTimeSheetParams, &QComboBox::currentIndexChanged, this, &ActivityWidget::onTimeSheetParamsIndexChanged);
connect(&mSecondTimer, &QTimer::timeout, this, &ActivityWidget::onSecondTimeout);
Expand All @@ -36,11 +38,11 @@ ActivityWidget::~ActivityWidget()
mSecondTimer.stop();
}

void ActivityWidget::setKemaiSession(std::shared_ptr<KemaiSession> kemaiSession)
void ActivityWidget::setKemaiSession(const std::shared_ptr<KemaiSession>& kemaiSession)
{
mSession = std::move(kemaiSession);
mSession = kemaiSession;

mUi->lwHistory->clear();
mTimeSheetModel.setKemaiSession(kemaiSession);

if (mSession)
{
Expand Down Expand Up @@ -198,20 +200,20 @@ void ActivityWidget::updateControls()

void ActivityWidget::updateRecentTimeSheetsView()
{
mUi->lwHistory->clear();
for (const auto& timeSheet : mSession->cache().recentTimeSheets())
{
auto timeSheetListWidgetItem = new TimeSheetListWidgetItem(timeSheet);

auto item = new QListWidgetItem;
item->setSizeHint(timeSheetListWidgetItem->sizeHint());

mUi->lwHistory->addItem(item);
mUi->lwHistory->setItemWidget(item, timeSheetListWidgetItem);

connect(timeSheetListWidgetItem, &TimeSheetListWidgetItem::timeSheetStartRequested, this, &ActivityWidget::onHistoryTimeSheetStartRequested);
connect(timeSheetListWidgetItem, &TimeSheetListWidgetItem::timeSheetFillRequested, this, &ActivityWidget::onHistoryTimeSheetFillRequested);
}
// mUi->lwHistory->clear();
// for (const auto& timeSheet : mSession->cache().recentTimeSheets())
// {
// auto timeSheetListWidgetItem = new TimeSheetListWidgetItem(timeSheet);
//
// auto item = new QListWidgetItem;
// item->setSizeHint(timeSheetListWidgetItem->sizeHint());
//
// mUi->lwHistory->addItem(item);
// mUi->lwHistory->setItemWidget(item, timeSheetListWidgetItem);
//
// connect(timeSheetListWidgetItem, &TimeSheetListWidgetItem::timeSheetStartRequested, this, &ActivityWidget::onHistoryTimeSheetStartRequested);
// connect(timeSheetListWidgetItem, &TimeSheetListWidgetItem::timeSheetFillRequested, this, &ActivityWidget::onHistoryTimeSheetFillRequested);
// }
}

void ActivityWidget::startPendingTimeSheet()
Expand Down
4 changes: 3 additions & 1 deletion src/gui/activityWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "client/kimaiClient.h"
#include "context/kemaiSession.h"
#include "models/timeSheetModel.h"
#include "models/timeSheetParamsModel.h"

namespace Ui {
Expand All @@ -21,7 +22,7 @@ class ActivityWidget : public QWidget
ActivityWidget(QWidget* parent = nullptr);
~ActivityWidget() override;

void setKemaiSession(std::shared_ptr<KemaiSession> kemaiSession);
void setKemaiSession(const std::shared_ptr<KemaiSession>& kemaiSession);
void stopCurrentTimeSheet();

private:
Expand Down Expand Up @@ -51,6 +52,7 @@ class ActivityWidget : public QWidget
std::shared_ptr<KemaiSession> mSession;
std::optional<TimeSheet> mPendingStartRequest;
TimeSheetParamsModel mTimeSheetParamsModel;
TimeSheetModel mTimeSheetModel;
};

} // namespace kemai
2 changes: 1 addition & 1 deletion src/gui/activityWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
</widget>
</item>
<item>
<widget class="QListWidget" name="lwHistory"/>
<widget class="QListView" name="lwHistory"/>
</item>
</layout>
</widget>
Expand Down
89 changes: 89 additions & 0 deletions src/models/timeSheetModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "timeSheetModel.h"

#include <QCoreApplication>

using namespace kemai;

static const auto gMaxTimeSheetCount = 1000;

void TimeSheetModel::setKemaiSession(const std::shared_ptr<KemaiSession>& kemaiSession)
{
mSession = kemaiSession;
resetModel();
}

void TimeSheetModel::setFilterTerm(const QString& term)
{
mFilterTerm = term;
resetModel();
}

QVariant TimeSheetModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || (index.row() > mTimeSheets.size()))
{
return {};
}

const auto& timeSheet = mTimeSheets.at(index.row());
switch (role)
{
case Qt::DisplayRole:
return QString("%1 / %2 / %3").arg(timeSheet.project.customer.name, timeSheet.project.name, timeSheet.activity.name);

default:
return {};
}
}

int TimeSheetModel::rowCount(const QModelIndex& /*parent*/) const
{
return static_cast<int>(mTimeSheets.size());
}

void TimeSheetModel::fetchMore(const QModelIndex& /*parent*/)
{
if (mIsFetching || mEndReached || !mSession)
{
return;
}

mIsFetching = true;
auto timeSheetsResult = mSession->client()->requestTimeSheets(mFilterTerm, mLastPageFetched + 1);

connect(timeSheetsResult, &KimaiApiBaseResult::ready, this, [this, timeSheetsResult] {
auto timeSheets = timeSheetsResult->takeResult();

beginInsertRows({}, static_cast<int>(mTimeSheets.size()), static_cast<int>(mTimeSheets.size() + timeSheets.size() - 1));
mTimeSheets.insert(mTimeSheets.end(), timeSheets.begin(), timeSheets.end());
endInsertRows();

++mLastPageFetched;
mEndReached = mTimeSheets.size() >= gMaxTimeSheetCount;
mIsFetching = false;
timeSheetsResult->deleteLater();
});
connect(timeSheetsResult, &KimaiApiBaseResult::error, [this, timeSheetsResult]() {
mEndReached = true;
mIsFetching = false;
timeSheetsResult->deleteLater();
});
}

bool TimeSheetModel::canFetchMore(const QModelIndex& /*parent*/) const
{
while (mIsFetching)
{
qApp->processEvents();
}
return !mEndReached;
}

void TimeSheetModel::resetModel()
{
beginResetModel();
mTimeSheets.clear();
mEndReached = mSession == nullptr;
mLastPageFetched = 0;
endResetModel();
}
31 changes: 31 additions & 0 deletions src/models/timeSheetModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <QAbstractListModel>

#include <context/kemaiSession.h>

namespace kemai {

class TimeSheetModel : public QAbstractListModel
{
public:
void setKemaiSession(const std::shared_ptr<KemaiSession>& kemaiSession);
void setFilterTerm(const QString& term);

QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
void fetchMore(const QModelIndex& parent) override;
bool canFetchMore(const QModelIndex& parent) const override;

private:
void resetModel();

std::shared_ptr<KemaiSession> mSession;
std::vector<TimeSheet> mTimeSheets;
bool mEndReached = false;
bool mIsFetching = false;
int mLastPageFetched = 0;
QString mFilterTerm;
};

} // namespace kemai

0 comments on commit 1f9357f

Please sign in to comment.