Fix empty stamp browser crashing upon an attempt to change pages

This commit is contained in:
Tamás Bálint Misius 2023-08-28 20:27:38 +02:00
parent 98cf5b302d
commit f0a447bcd5
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
4 changed files with 14 additions and 19 deletions

View File

@ -24,7 +24,7 @@ LocalBrowserController::LocalBrowserController(std::function<void ()> onDone_):
onDone = onDone_;
browserModel->UpdateSavesList(1);
browserModel->UpdateSavesList(0);
}
void LocalBrowserController::OpenSave(int index)
@ -94,13 +94,13 @@ void LocalBrowserController::ClearSelection()
void LocalBrowserController::SetPage(int page)
{
if (page != browserModel->GetPageNum() && page > 0 && page <= browserModel->GetPageCount())
if (page != browserModel->GetPageNum() && page >= 0 && page < browserModel->GetPageCount())
browserModel->UpdateSavesList(page);
}
void LocalBrowserController::SetPageRelative(int offset)
{
int page = std::min(std::max(browserModel->GetPageNum() + offset, 1), browserModel->GetPageCount());
int page = std::max(std::min(browserModel->GetPageNum() + offset, browserModel->GetPageCount() - 1), 0);
if (page != browserModel->GetPageNum())
browserModel->UpdateSavesList(page);
}

View File

@ -3,19 +3,15 @@
#include "client/Client.h"
#include "client/SaveFile.h"
#include "client/GameSave.h"
#include "common/tpt-minmax.h"
#include <algorithm>
constexpr auto pageSize = 20;
LocalBrowserModel::LocalBrowserModel():
currentPage(1),
stampToFront(1)
LocalBrowserModel::LocalBrowserModel()
{
stampIDs = Client::Ref().GetStamps();
}
std::vector<SaveFile *> LocalBrowserModel::GetSavesList() // non-owning
{
std::vector<SaveFile *> nonOwningSaveList;
@ -79,12 +75,10 @@ void LocalBrowserModel::UpdateSavesList(int pageNumber)
{
savesList.clear();
currentPage = pageNumber;
notifyPageChanged();
notifySavesListChanged();
stampIDs = Client::Ref().GetStamps();
auto size = int(stampIDs.size());
for (int i = (currentPage - 1) * pageSize; i < size && i < currentPage * pageSize; i++)
for (int i = currentPage * pageSize; i < size && i < (currentPage + 1) * pageSize; i++)
{
auto tempSave = Client::Ref().GetStamp(stampIDs[i]);
if (tempSave)
@ -92,6 +86,7 @@ void LocalBrowserModel::UpdateSavesList(int pageNumber)
savesList.push_back(std::move(tempSave));
}
}
notifyPageChanged();
notifySavesListChanged();
}
@ -103,7 +98,8 @@ void LocalBrowserModel::RescanStamps()
int LocalBrowserModel::GetPageCount()
{
auto size = int(stampIDs.size());
return size / pageSize + ((size % pageSize) ? 1 : 0);
auto count = size / pageSize + ((size % pageSize) ? 1 : 0);
return count ? count : 1; // there is always at least one page; there may not be anything on it though
}
void LocalBrowserModel::SelectSave(ByteString stampID)

View File

@ -12,8 +12,8 @@ class LocalBrowserModel {
std::vector<ByteString> stampIDs;
std::vector<std::unique_ptr<SaveFile>> savesList;
std::vector<LocalBrowserView*> observers;
int currentPage;
bool stampToFront;
int currentPage = 0;
bool stampToFront = true;
void notifySavesListChanged();
void notifyPageChanged();
void notifySelectedChanged();

View File

@ -68,7 +68,7 @@ void LocalBrowserView::OnTick(float dt)
if (changed && lastChanged < GetTicks())
{
changed = false;
c->SetPage(std::max(pageTextbox->GetText().ToNumber<int>(true), 0));
c->SetPage(std::max(pageTextbox->GetText().ToNumber<int>(true) - 1, 0));
}
}
@ -91,11 +91,10 @@ void LocalBrowserView::NotifyPageChanged(LocalBrowserModel * sender)
//pageCountLabel->Position.X = WINDOWW/2+6;
pageLabel->Visible = pageCountLabel->Visible = pageTextbox->Visible = true;
pageInfo = String::Build(sender->GetPageNum());
pageTextbox->SetText(pageInfo);
pageTextbox->SetText(String::Build(sender->GetPageNum() + 1));
}
if(sender->GetPageNum() == 1)
if(sender->GetPageNum() == 0)
{
previousButton->Visible = false;
}
@ -103,7 +102,7 @@ void LocalBrowserView::NotifyPageChanged(LocalBrowserModel * sender)
{
previousButton->Visible = true;
}
if(sender->GetPageNum() == sender->GetPageCount())
if(sender->GetPageNum() == sender->GetPageCount() - 1)
{
nextButton->Visible = false;
}