[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
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,9 +315,11 @@ Item {
}
function updateModel() {
if (completed && !moving && currentIndex >= 0 && !bookViewWatcher.updatingViewPosition) {
hideViews()
stackModel.currentPage = currentIndex
}
}
ListWatcher {
id: bookViewWatcher

View file

@ -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);
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();
}

View file

@ -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;