[app] Fixing issues with saving/restoring book position

This commit is contained in:
Slava Monich 2020-11-02 05:19:46 +02:00
parent 6c2354d2e5
commit a22d4f383a
3 changed files with 59 additions and 36 deletions

View file

@ -199,6 +199,8 @@ Item {
flickDeceleration: maximumFlickVelocity flickDeceleration: maximumFlickVelocity
orientation: ListView.Horizontal orientation: ListView.Horizontal
snapMode: ListView.SnapOneItem snapMode: ListView.SnapOneItem
preferredHighlightBegin: 0
preferredHighlightEnd: width
highlightRangeMode: ListView.StrictlyEnforceRange highlightRangeMode: ListView.StrictlyEnforceRange
spacing: Theme.paddingMedium spacing: Theme.paddingMedium
opacity: loading ? 0 : 1 opacity: loading ? 0 : 1
@ -225,17 +227,9 @@ Item {
pager.setPage(currentPage) pager.setPage(currentPage)
} }
onCurrentIndexChanged: { onCurrentIndexChanged: updateModel()
if (completed && !moving && currentIndex >= 0) {
updateModel()
}
}
onMovingChanged: { onMovingChanged: updateModel()
if (!moving && currentIndex >= 0) {
updateModel()
}
}
delegate: BooksPageView { delegate: BooksPageView {
id: pageView id: pageView
@ -321,8 +315,10 @@ Item {
} }
function updateModel() { function updateModel() {
hideViews() if (completed && !moving && currentIndex >= 0 && !bookViewWatcher.updatingViewPosition) {
stackModel.currentPage = currentIndex hideViews()
stackModel.currentPage = currentIndex
}
} }
ListWatcher { ListWatcher {

View file

@ -35,12 +35,16 @@
#include "HarbourDebug.h" #include "HarbourDebug.h"
#define LISTVIEW_CONTENT_X "contentX" // Properties
#define LISTVIEW_CONTENT_Y "contentY" static const char LISTVIEW_CONTENT_X[] = "contentX";
#define LISTVIEW_CONTENT_WIDTH "contentWidth" static const char LISTVIEW_CONTENT_Y[] = "contentY";
#define LISTVIEW_CONTENT_HEIGHT "contentHeight" static const char LISTVIEW_CONTENT_WIDTH[] = "contentWidth";
#define LISTVIEW_INDEX_AT "indexAt" static const char LISTVIEW_CONTENT_HEIGHT[] = "contentHeight";
#define LISTVIEW_POSITION_VIEW_AT_INDEX "positionViewAtIndex" static const char LISTVIEW_CURRENT_INDEX[] = "currentIndex";
// Methods
static const char LISTVIEW_INDEX_AT[] = "indexAt";
static const char LISTVIEW_POSITION_VIEW_AT_INDEX[] = "positionViewAtIndex";
BooksListWatcher::BooksListWatcher(QObject* aParent) : BooksListWatcher::BooksListWatcher(QObject* aParent) :
QObject(aParent), QObject(aParent),
@ -104,16 +108,18 @@ void BooksListWatcher::setListView(QQuickItem* aView)
} }
} }
qreal BooksListWatcher::getRealProperty(const char *name, qreal defaultValue) qreal BooksListWatcher::getRealProperty(const char* aName, qreal aDefaultValue)
{ {
QVariant value = iListView->property(name); if (iListView) {
bool ok = false; QVariant value = iListView->property(aName);
if (value.isValid()) { bool ok = false;
ok = false; if (value.isValid()) {
qreal r = value.toReal(&ok); ok = false;
if (ok) return r; qreal r = value.toReal(&ok);
if (ok) return r;
}
} }
return defaultValue; return aDefaultValue;
} }
void BooksListWatcher::positionViewAtIndex(int aIndex) void BooksListWatcher::positionViewAtIndex(int aIndex)
@ -147,7 +153,10 @@ void BooksListWatcher::doPositionViewAtIndex(int aIndex)
iCenterMode = 1; iCenterMode = 1;
} }
} }
iPositionIsChanging++; positionUpdatedStarted();
HDEBUG(iListView->property(LISTVIEW_CURRENT_INDEX).toInt());
iListView->setProperty(LISTVIEW_CURRENT_INDEX, aIndex);
HDEBUG(iListView->property(LISTVIEW_CURRENT_INDEX).toInt());
positionViewAtIndex(aIndex, iCenterMode); positionViewAtIndex(aIndex, iCenterMode);
const int currentIndex = getCurrentIndex(); const int currentIndex = getCurrentIndex();
if (currentIndex != aIndex) { if (currentIndex != aIndex) {
@ -155,7 +164,8 @@ void BooksListWatcher::doPositionViewAtIndex(int aIndex)
HDEBUG("still" << currentIndex << ", retrying..."); HDEBUG("still" << currentIndex << ", retrying...");
positionViewAtIndex(aIndex, iCenterMode); positionViewAtIndex(aIndex, iCenterMode);
} }
iPositionIsChanging--; positionUpdatedFinished();
updateCurrentIndex();
} }
void BooksListWatcher::positionViewAtIndex(int aIndex, int aMode) void BooksListWatcher::positionViewAtIndex(int aIndex, int aMode)
@ -167,6 +177,20 @@ void BooksListWatcher::positionViewAtIndex(int aIndex, int aMode)
} }
} }
void BooksListWatcher::positionUpdatedStarted()
{
if (!iPositionIsChanging++) {
Q_EMIT updatingViewPositionChanged();
}
}
void BooksListWatcher::positionUpdatedFinished()
{
if (!--iPositionIsChanging) {
Q_EMIT updatingViewPositionChanged();
}
}
qreal BooksListWatcher::contentX() qreal BooksListWatcher::contentX()
{ {
return getRealProperty(LISTVIEW_CONTENT_X); return getRealProperty(LISTVIEW_CONTENT_X);
@ -202,8 +226,7 @@ int BooksListWatcher::getCurrentIndex()
void BooksListWatcher::tryToRestoreCurrentIndex() void BooksListWatcher::tryToRestoreCurrentIndex()
{ {
HASSERT(!iPositionIsChanging); if (iCurrentIndex >= 0 && !updatingViewPosition()) {
if (iCurrentIndex >= 0 && !iPositionIsChanging) {
const int index = getCurrentIndex(); const int index = getCurrentIndex();
if (iCurrentIndex != index) { if (iCurrentIndex != index) {
HDEBUG(index << "->" << iCurrentIndex); HDEBUG(index << "->" << iCurrentIndex);
@ -215,8 +238,7 @@ void BooksListWatcher::tryToRestoreCurrentIndex()
void BooksListWatcher::updateCurrentIndex() void BooksListWatcher::updateCurrentIndex()
{ {
HASSERT(!iPositionIsChanging); if (!updatingViewPosition() && (contentWidth() > 0 || contentHeight() > 0)) {
if (!iPositionIsChanging && (contentWidth() > 0 || contentHeight() > 0)) {
const int index = getCurrentIndex(); const int index = getCurrentIndex();
if (iCurrentIndex != index && index >= 0) { if (iCurrentIndex != index && index >= 0) {
iCurrentIndex = index; iCurrentIndex = index;
@ -279,7 +301,7 @@ void BooksListWatcher::onContentXChanged()
{ {
HASSERT(sender() == iListView); HASSERT(sender() == iListView);
iContentX = contentX(); iContentX = contentX();
if (!iPositionIsChanging && !iResizeTimer->isActive()) { if (!updatingViewPosition() && !iResizeTimer->isActive()) {
updateCurrentIndex(); updateCurrentIndex();
} }
} }
@ -288,7 +310,7 @@ void BooksListWatcher::onContentYChanged()
{ {
HASSERT(sender() == iListView); HASSERT(sender() == iListView);
iContentY = contentY(); iContentY = contentY();
if (!iPositionIsChanging && !iResizeTimer->isActive()) { if (!updatingViewPosition() && !iResizeTimer->isActive()) {
updateCurrentIndex(); updateCurrentIndex();
} }
} }
@ -296,7 +318,7 @@ void BooksListWatcher::onContentYChanged()
void BooksListWatcher::onContentSizeChanged() void BooksListWatcher::onContentSizeChanged()
{ {
HASSERT(sender() == iListView); HASSERT(sender() == iListView);
if (!iPositionIsChanging && !iResizeTimer->isActive()) { if (!updatingViewPosition() && !iResizeTimer->isActive()) {
tryToRestoreCurrentIndex(); tryToRestoreCurrentIndex();
updateCurrentIndex(); updateCurrentIndex();
} }

View file

@ -43,6 +43,7 @@ class BooksListWatcher: public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged) Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged)
Q_PROPERTY(bool updatingViewPosition READ updatingViewPosition NOTIFY updatingViewPositionChanged)
Q_PROPERTY(QQuickItem* listView READ listView WRITE setListView NOTIFY listViewChanged) Q_PROPERTY(QQuickItem* listView READ listView WRITE setListView NOTIFY listViewChanged)
Q_PROPERTY(qreal width READ width NOTIFY widthChanged) Q_PROPERTY(qreal width READ width NOTIFY widthChanged)
Q_PROPERTY(qreal height READ height NOTIFY heightChanged) Q_PROPERTY(qreal height READ height NOTIFY heightChanged)
@ -51,6 +52,7 @@ class BooksListWatcher: public QObject
public: public:
explicit BooksListWatcher(QObject* aParent = NULL); explicit BooksListWatcher(QObject* aParent = NULL);
bool updatingViewPosition() const { return iPositionIsChanging > 0; }
int currentIndex() const { return iCurrentIndex; } int currentIndex() const { return iCurrentIndex; }
QSize size() const { return iSize; } QSize size() const { return iSize; }
qreal width() const { return iSize.width(); } qreal width() const { return iSize.width(); }
@ -66,10 +68,12 @@ private:
qreal contentY(); qreal contentY();
qreal contentWidth(); qreal contentWidth();
qreal contentHeight(); qreal contentHeight();
qreal getRealProperty(const char *name, qreal defaultValue = 0.0); qreal getRealProperty(const char* aName, qreal aDefaultValue = 0.0);
int getCurrentIndex(); int getCurrentIndex();
void doPositionViewAtIndex(int aIndex); void doPositionViewAtIndex(int aIndex);
void positionViewAtIndex(int aIndex, int aMode); void positionViewAtIndex(int aIndex, int aMode);
void positionUpdatedStarted();
void positionUpdatedFinished();
void updateCurrentIndex(); void updateCurrentIndex();
void tryToRestoreCurrentIndex(); void tryToRestoreCurrentIndex();
void updateSize(); void updateSize();
@ -88,6 +92,7 @@ Q_SIGNALS:
void widthChanged(); void widthChanged();
void heightChanged(); void heightChanged();
void currentIndexChanged(); void currentIndexChanged();
void updatingViewPositionChanged();
private: private:
QSize iSize; QSize iSize;