Skip to content

Commit e2f7da7

Browse files
committed
refactor(search): move search window state derivation into the model
Expose isFetchMoreInProgress, hasSearchTerm, hasSearchError, canEditSearch and a computed SearchState enum as model properties, replacing the derived booleans in SearchWindow.qml. Assisted-by: Claude:claude-opus-4-8 Signed-off-by: Camila Ayres <hello@camilasan.com>
1 parent c1ed8df commit e2f7da7

3 files changed

Lines changed: 88 additions & 36 deletions

File tree

src/gui/SearchWindow.qml

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import QtQuick.Controls.Basic
88
import QtQuick.Layouts
99

1010
import Style
11+
import com.nextcloud.desktopclient
1112
import "./tray"
1213

1314
WizardStyledWindow {
@@ -17,37 +18,11 @@ WizardStyledWindow {
1718
property var currentUser: null
1819
property var searchModel: null
1920
readonly property string headline: qsTr("Search")
20-
readonly property bool isFetchMoreInProgress: searchModel !== null
21-
&& searchModel.currentFetchMoreInProgressProviderId.length > 0
22-
readonly property bool isSearchInProgress: searchModel !== null
23-
&& searchModel.isSearchInProgress
24-
readonly property bool waitingForSearchTermEditEnd: searchModel !== null
25-
&& searchModel.waitingForSearchTermEditEnd
26-
readonly property bool hasSearchTerm: searchModel !== null
27-
&& searchModel.searchTerm.length > 0
28-
readonly property bool hasSearchError: searchModel !== null
29-
&& searchModel.errorString.length > 0
30-
readonly property bool canEditSearch: currentUser !== null
31-
&& currentUser.isConnected
32-
&& searchModel !== null
33-
&& !isFetchMoreInProgress
34-
readonly property bool showResults: searchModel !== null
35-
&& searchResultsListView.count > 0
36-
readonly property bool showNothingFound: hasSearchTerm
37-
&& !isSearchInProgress
38-
&& !waitingForSearchTermEditEnd
39-
&& !hasSearchError
40-
&& searchResultsListView.count === 0
41-
readonly property bool showPlaceholder: !hasSearchTerm
42-
&& !hasSearchError
43-
readonly property bool showSearchError: hasSearchError
44-
&& !showResults
45-
&& !isSearchInProgress
46-
&& !isFetchMoreInProgress
47-
readonly property bool showSkeleton: hasSearchTerm
48-
&& !showNothingFound
49-
&& !showResults
50-
&& !hasSearchError
21+
readonly property int searchState: searchModel
22+
? searchModel.searchState
23+
: UnifiedSearchResultsListModel.Placeholder
24+
readonly property bool isSearchInProgress: searchModel !== null && searchModel.isSearchInProgress
25+
readonly property bool canEditSearch: searchModel !== null && searchModel.canEditSearch
5126

5227
title: ""
5328
width: Style.searchWindowWidth
@@ -125,25 +100,25 @@ WizardStyledWindow {
125100
anchors.left: parent.left
126101
anchors.right: parent.right
127102
anchors.top: parent.top
128-
visible: root.showSearchError
103+
visible: root.searchState === UnifiedSearchResultsListModel.SearchError
129104
text: root.searchModel ? root.searchModel.errorString : ""
130105
}
131106

132107
UnifiedSearchPlaceholderView {
133108
anchors.fill: parent
134-
visible: root.showPlaceholder
109+
visible: root.searchState === UnifiedSearchResultsListModel.Placeholder
135110
}
136111

137112
UnifiedSearchResultNothingFound {
138113
anchors.fill: parent
139-
visible: root.showNothingFound
114+
visible: root.searchState === UnifiedSearchResultsListModel.NothingFound
140115
text: root.searchModel ? root.searchModel.searchTerm : ""
141116
}
142117

143118
Loader {
144119
anchors.fill: parent
145120
anchors.margins: Style.smallSpacing
146-
active: root.showSkeleton
121+
active: root.searchState === UnifiedSearchResultsListModel.Skeleton
147122
asynchronous: true
148123

149124
sourceComponent: UnifiedSearchResultItemSkeletonContainer {
@@ -158,7 +133,7 @@ WizardStyledWindow {
158133

159134
anchors.fill: parent
160135
contentWidth: availableWidth
161-
visible: root.showResults
136+
visible: root.searchState === UnifiedSearchResultsListModel.Results
162137

163138
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
164139

src/gui/tray/unifiedsearchresultslistmodel.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ UnifiedSearchResultsListModel::UnifiedSearchResultsListModel(AccountState *accou
189189
: QAbstractListModel(parent)
190190
, _accountState(accountState)
191191
{
192+
connect(this, &UnifiedSearchResultsListModel::isSearchInProgressChanged, this, &UnifiedSearchResultsListModel::searchStateChanged);
193+
connect(this, &UnifiedSearchResultsListModel::currentFetchMoreInProgressProviderIdChanged, this, &UnifiedSearchResultsListModel::searchStateChanged);
194+
connect(this, &UnifiedSearchResultsListModel::searchTermChanged, this, &UnifiedSearchResultsListModel::searchStateChanged);
195+
connect(this, &UnifiedSearchResultsListModel::errorStringChanged, this, &UnifiedSearchResultsListModel::searchStateChanged);
196+
connect(this, &UnifiedSearchResultsListModel::waitingForSearchTermEditEndChanged, this, &UnifiedSearchResultsListModel::searchStateChanged);
197+
connect(this, &QAbstractListModel::rowsInserted, this, &UnifiedSearchResultsListModel::searchStateChanged);
198+
connect(this, &QAbstractListModel::rowsRemoved, this, &UnifiedSearchResultsListModel::searchStateChanged);
199+
connect(this, &QAbstractListModel::modelReset, this, &UnifiedSearchResultsListModel::searchStateChanged);
200+
201+
connect(this, &UnifiedSearchResultsListModel::currentFetchMoreInProgressProviderIdChanged, this, &UnifiedSearchResultsListModel::canEditSearchChanged);
202+
if (_accountState) {
203+
connect(_accountState, &AccountState::isConnectedChanged, this, &UnifiedSearchResultsListModel::canEditSearchChanged);
204+
}
192205
}
193206

194207
QVariant UnifiedSearchResultsListModel::data(const QModelIndex &index, int role) const
@@ -326,6 +339,47 @@ bool UnifiedSearchResultsListModel::isSearchInProgress() const
326339
return !_searchJobConnections.isEmpty();
327340
}
328341

342+
bool UnifiedSearchResultsListModel::isFetchMoreInProgress() const
343+
{
344+
return !_currentFetchMoreInProgressProviderId.isEmpty();
345+
}
346+
347+
bool UnifiedSearchResultsListModel::hasSearchTerm() const
348+
{
349+
return !_searchTerm.isEmpty();
350+
}
351+
352+
bool UnifiedSearchResultsListModel::hasSearchError() const
353+
{
354+
return !_errorString.isEmpty();
355+
}
356+
357+
bool UnifiedSearchResultsListModel::canEditSearch() const
358+
{
359+
return _accountState && _accountState->isConnected() && !isFetchMoreInProgress();
360+
}
361+
362+
UnifiedSearchResultsListModel::SearchState UnifiedSearchResultsListModel::searchState() const
363+
{
364+
if (rowCount() > 0) {
365+
return SearchState::Results;
366+
}
367+
368+
if (hasSearchError()) {
369+
return (!isSearchInProgress() && !isFetchMoreInProgress()) ? SearchState::SearchError : SearchState::None;
370+
}
371+
372+
if (!hasSearchTerm()) {
373+
return SearchState::Placeholder;
374+
}
375+
376+
if (!isSearchInProgress() && !waitingForSearchTermEditEnd()) {
377+
return SearchState::NothingFound;
378+
}
379+
380+
return SearchState::Skeleton;
381+
}
382+
329383
void UnifiedSearchResultsListModel::resultClicked(const QString &providerId, const QUrl &resourceUrl) const
330384
{
331385
const QUrlQuery urlQuery{resourceUrl};

src/gui/tray/unifiedsearchresultslistmodel.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class UnifiedSearchResultsListModel : public QAbstractListModel
3030
Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged)
3131
Q_PROPERTY(QString searchTerm READ searchTerm WRITE setSearchTerm NOTIFY searchTermChanged)
3232
Q_PROPERTY(bool waitingForSearchTermEditEnd READ waitingForSearchTermEditEnd NOTIFY waitingForSearchTermEditEndChanged)
33+
Q_PROPERTY(bool isFetchMoreInProgress READ isFetchMoreInProgress NOTIFY currentFetchMoreInProgressProviderIdChanged)
34+
Q_PROPERTY(bool hasSearchTerm READ hasSearchTerm NOTIFY searchTermChanged)
35+
Q_PROPERTY(bool hasSearchError READ hasSearchError NOTIFY errorStringChanged)
36+
Q_PROPERTY(bool canEditSearch READ canEditSearch NOTIFY canEditSearchChanged)
37+
Q_PROPERTY(SearchState searchState READ searchState NOTIFY searchStateChanged)
3338

3439
struct UnifiedSearchProvider
3540
{
@@ -42,6 +47,16 @@ class UnifiedSearchResultsListModel : public QAbstractListModel
4247
};
4348

4449
public:
50+
enum class SearchState {
51+
None,
52+
Placeholder,
53+
Skeleton,
54+
NothingFound,
55+
Results,
56+
SearchError,
57+
};
58+
Q_ENUM(SearchState)
59+
4560
enum DataRole {
4661
ProviderNameRole = Qt::UserRole + 1,
4762
ProviderIdRole,
@@ -71,6 +86,12 @@ class UnifiedSearchResultsListModel : public QAbstractListModel
7186
[[nodiscard]] QString errorString() const;
7287
[[nodiscard]] bool waitingForSearchTermEditEnd() const;
7388

89+
[[nodiscard]] bool isFetchMoreInProgress() const;
90+
[[nodiscard]] bool hasSearchTerm() const;
91+
[[nodiscard]] bool hasSearchError() const;
92+
[[nodiscard]] bool canEditSearch() const;
93+
[[nodiscard]] SearchState searchState() const;
94+
7495
Q_INVOKABLE void resultClicked(const QString &providerId, const QUrl &resourceUrl) const;
7596
Q_INVOKABLE void fetchMoreTriggerClicked(const QString &providerId);
7697

@@ -100,6 +121,8 @@ class UnifiedSearchResultsListModel : public QAbstractListModel
100121
void errorStringChanged();
101122
void searchTermChanged();
102123
void waitingForSearchTermEditEndChanged();
124+
void canEditSearchChanged();
125+
void searchStateChanged();
103126

104127
public slots:
105128
void setSearchTerm(const QString &term);

0 commit comments

Comments
 (0)