Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-order widgets in the SourceList on update. #1013

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,22 @@ def show_no_source_selected_message(self):
self.no_source_selected.show()


class SourceListWidgetItem(QListWidgetItem):

def __lt__(self, other):
"""
Used for ordering widgets by timestamp of last interaction.
"""
lw = self.listWidget()
me = lw.itemWidget(self)
them = lw.itemWidget(other)
if me and them:
my_ts = arrow.get(me.source.last_updated)
other_ts = arrow.get(them.source.last_updated)
return my_ts < other_ts
return True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice implementation!

cc @creviera for your awareness this impacts #967 - I think merging this and then as part of #967 modifying SourceWidget.update to also update a last_updated field makes sense. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds good to me @redshiftzero



class SourceList(QListWidget):
"""
Displays the list of sources.
Expand Down Expand Up @@ -921,6 +937,9 @@ def __init__(self):
layout = QVBoxLayout(self)
self.setLayout(layout)

# Enable ordering.
self.setSortingEnabled(True)

# To hold references to SourceWidget instances indexed by source UUID.
self.source_widgets = {}

Expand Down Expand Up @@ -975,11 +994,13 @@ def update(self, sources: List[Source]) -> List[str]:
new_source = SourceWidget(self.controller, source)
self.source_widgets[source.uuid] = new_source

list_item = QListWidgetItem()
list_item = SourceListWidgetItem()
self.insertItem(0, list_item)
list_item.setSizeHint(new_source.sizeHint())
self.setItemWidget(list_item, new_source)

# Sort..!
self.sortItems(Qt.DescendingOrder)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're resorting the entire source list for every sync, we'll need to test this against 400 and 1000 sources to make sure things don't freeze up

return deleted_uuids

def initial_update(self, sources: List[Source]):
Expand All @@ -1003,7 +1024,7 @@ def schedule_source_management(slice_size=slice_size):
for source in sources_slice:
new_source = SourceWidget(self.controller, source)
self.source_widgets[source.uuid] = new_source
list_item = QListWidgetItem(self)
list_item = SourceListWidgetItem(self)
list_item.setSizeHint(new_source.sizeHint())

self.insertItem(0, list_item)
Expand Down
6 changes: 3 additions & 3 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ def test_SourceList_update_adds_new_sources(mocker):
mock_sw = mocker.MagicMock()
mock_lwi = mocker.MagicMock()
mocker.patch('securedrop_client.gui.widgets.SourceWidget', mock_sw)
mocker.patch('securedrop_client.gui.widgets.QListWidgetItem', mock_lwi)
mocker.patch('securedrop_client.gui.widgets.SourceListWidgetItem', mock_lwi)

sources = [mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), ]
sl.update(sources)
Expand Down Expand Up @@ -1009,7 +1009,7 @@ def test_SourceList_add_source_closure_adds_sources(mocker):
mock_sw = mocker.MagicMock()
mock_lwi = mocker.MagicMock()
mocker.patch('securedrop_client.gui.widgets.SourceWidget', mock_sw)
mocker.patch('securedrop_client.gui.widgets.QListWidgetItem', mock_lwi)
mocker.patch('securedrop_client.gui.widgets.SourceListWidgetItem', mock_lwi)
sources = [mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), ]
mock_timer = mocker.MagicMock()
with mocker.patch("securedrop_client.gui.widgets.QTimer", mock_timer):
Expand Down Expand Up @@ -1044,7 +1044,7 @@ def test_SourceList_add_source_closure_exits_on_no_more_sources(mocker):
mock_sw = mocker.MagicMock()
mock_lwi = mocker.MagicMock()
mocker.patch('securedrop_client.gui.widgets.SourceWidget', mock_sw)
mocker.patch('securedrop_client.gui.widgets.QListWidgetItem', mock_lwi)
mocker.patch('securedrop_client.gui.widgets.SourceListWidgetItem', mock_lwi)
sources = []
mock_timer = mocker.MagicMock()
with mocker.patch("securedrop_client.gui.widgets.QTimer", mock_timer):
Expand Down