From a22d4f383ada37b560b2c48e14476f7173c798e0 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Mon, 2 Nov 2020 05:19:46 +0200 Subject: [PATCH] [app] Fixing issues with saving/restoring book position --- app/qml/BooksBookView.qml | 20 +++++------ app/src/BooksListWatcher.cpp | 68 ++++++++++++++++++++++++------------ app/src/BooksListWatcher.h | 7 +++- 3 files changed, 59 insertions(+), 36 deletions(-) diff --git a/app/qml/BooksBookView.qml b/app/qml/BooksBookView.qml index 98ecd06..f9f78bd 100644 --- a/app/qml/BooksBookView.qml +++ b/app/qml/BooksBookView.qml @@ -199,6 +199,8 @@ Item { flickDeceleration: maximumFlickVelocity orientation: ListView.Horizontal snapMode: ListView.SnapOneItem + preferredHighlightBegin: 0 + preferredHighlightEnd: width highlightRangeMode: ListView.StrictlyEnforceRange spacing: Theme.paddingMedium opacity: loading ? 0 : 1 @@ -225,17 +227,9 @@ Item { pager.setPage(currentPage) } - onCurrentIndexChanged: { - if (completed && !moving && currentIndex >= 0) { - updateModel() - } - } + onCurrentIndexChanged: updateModel() - onMovingChanged: { - if (!moving && currentIndex >= 0) { - updateModel() - } - } + onMovingChanged: updateModel() delegate: BooksPageView { id: pageView @@ -321,8 +315,10 @@ Item { } function updateModel() { - hideViews() - stackModel.currentPage = currentIndex + if (completed && !moving && currentIndex >= 0 && !bookViewWatcher.updatingViewPosition) { + hideViews() + stackModel.currentPage = currentIndex + } } ListWatcher { diff --git a/app/src/BooksListWatcher.cpp b/app/src/BooksListWatcher.cpp index 8b65e44..390bfb6 100644 --- a/app/src/BooksListWatcher.cpp +++ b/app/src/BooksListWatcher.cpp @@ -35,12 +35,16 @@ #include "HarbourDebug.h" -#define LISTVIEW_CONTENT_X "contentX" -#define LISTVIEW_CONTENT_Y "contentY" -#define LISTVIEW_CONTENT_WIDTH "contentWidth" -#define LISTVIEW_CONTENT_HEIGHT "contentHeight" -#define LISTVIEW_INDEX_AT "indexAt" -#define LISTVIEW_POSITION_VIEW_AT_INDEX "positionViewAtIndex" +// Properties +static const char LISTVIEW_CONTENT_X[] = "contentX"; +static const char LISTVIEW_CONTENT_Y[] = "contentY"; +static const char LISTVIEW_CONTENT_WIDTH[] = "contentWidth"; +static const char LISTVIEW_CONTENT_HEIGHT[] = "contentHeight"; +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) : 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); - bool ok = false; - if (value.isValid()) { - ok = false; - qreal r = value.toReal(&ok); - if (ok) return r; + if (iListView) { + QVariant value = iListView->property(aName); + bool ok = false; + if (value.isValid()) { + ok = false; + qreal r = value.toReal(&ok); + if (ok) return r; + } } - return defaultValue; + return aDefaultValue; } void BooksListWatcher::positionViewAtIndex(int aIndex) @@ -147,7 +153,10 @@ void BooksListWatcher::doPositionViewAtIndex(int aIndex) 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); const int currentIndex = getCurrentIndex(); if (currentIndex != aIndex) { @@ -155,7 +164,8 @@ void BooksListWatcher::doPositionViewAtIndex(int aIndex) HDEBUG("still" << currentIndex << ", retrying..."); positionViewAtIndex(aIndex, iCenterMode); } - iPositionIsChanging--; + positionUpdatedFinished(); + updateCurrentIndex(); } 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() { return getRealProperty(LISTVIEW_CONTENT_X); @@ -202,8 +226,7 @@ int BooksListWatcher::getCurrentIndex() void BooksListWatcher::tryToRestoreCurrentIndex() { - HASSERT(!iPositionIsChanging); - if (iCurrentIndex >= 0 && !iPositionIsChanging) { + if (iCurrentIndex >= 0 && !updatingViewPosition()) { const int index = getCurrentIndex(); if (iCurrentIndex != index) { HDEBUG(index << "->" << iCurrentIndex); @@ -215,8 +238,7 @@ void BooksListWatcher::tryToRestoreCurrentIndex() void BooksListWatcher::updateCurrentIndex() { - HASSERT(!iPositionIsChanging); - if (!iPositionIsChanging && (contentWidth() > 0 || contentHeight() > 0)) { + if (!updatingViewPosition() && (contentWidth() > 0 || contentHeight() > 0)) { const int index = getCurrentIndex(); if (iCurrentIndex != index && index >= 0) { iCurrentIndex = index; @@ -279,7 +301,7 @@ void BooksListWatcher::onContentXChanged() { HASSERT(sender() == iListView); iContentX = contentX(); - if (!iPositionIsChanging && !iResizeTimer->isActive()) { + if (!updatingViewPosition() && !iResizeTimer->isActive()) { updateCurrentIndex(); } } @@ -288,7 +310,7 @@ void BooksListWatcher::onContentYChanged() { HASSERT(sender() == iListView); iContentY = contentY(); - if (!iPositionIsChanging && !iResizeTimer->isActive()) { + if (!updatingViewPosition() && !iResizeTimer->isActive()) { updateCurrentIndex(); } } @@ -296,7 +318,7 @@ void BooksListWatcher::onContentYChanged() void BooksListWatcher::onContentSizeChanged() { HASSERT(sender() == iListView); - if (!iPositionIsChanging && !iResizeTimer->isActive()) { + if (!updatingViewPosition() && !iResizeTimer->isActive()) { tryToRestoreCurrentIndex(); updateCurrentIndex(); } diff --git a/app/src/BooksListWatcher.h b/app/src/BooksListWatcher.h index e4ed8ae..76a5de7 100644 --- a/app/src/BooksListWatcher.h +++ b/app/src/BooksListWatcher.h @@ -43,6 +43,7 @@ class BooksListWatcher: public QObject { Q_OBJECT 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(qreal width READ width NOTIFY widthChanged) Q_PROPERTY(qreal height READ height NOTIFY heightChanged) @@ -51,6 +52,7 @@ class BooksListWatcher: public QObject public: explicit BooksListWatcher(QObject* aParent = NULL); + bool updatingViewPosition() const { return iPositionIsChanging > 0; } int currentIndex() const { return iCurrentIndex; } QSize size() const { return iSize; } qreal width() const { return iSize.width(); } @@ -66,10 +68,12 @@ private: qreal contentY(); qreal contentWidth(); qreal contentHeight(); - qreal getRealProperty(const char *name, qreal defaultValue = 0.0); + qreal getRealProperty(const char* aName, qreal aDefaultValue = 0.0); int getCurrentIndex(); void doPositionViewAtIndex(int aIndex); void positionViewAtIndex(int aIndex, int aMode); + void positionUpdatedStarted(); + void positionUpdatedFinished(); void updateCurrentIndex(); void tryToRestoreCurrentIndex(); void updateSize(); @@ -88,6 +92,7 @@ Q_SIGNALS: void widthChanged(); void heightChanged(); void currentIndexChanged(); + void updatingViewPositionChanged(); private: QSize iSize;