Python filtered models for room members

This commit is contained in:
miruka 2020-05-10 14:58:59 -04:00
parent 200f25d23e
commit 37a9332aed
5 changed files with 54 additions and 18 deletions

View File

@ -314,6 +314,9 @@ class Backend:
async def set_substring_filter(self, model_id: SyncId, value: str) -> None: async def set_substring_filter(self, model_id: SyncId, value: str) -> None:
if isinstance(model_id, list): # QML can't pass tuples
model_id = tuple(model_id)
model = Model.proxies[model_id] model = Model.proxies[model_id]
if not isinstance(model, FieldSubstringFilter): if not isinstance(model, FieldSubstringFilter):

View File

@ -2,10 +2,11 @@
from collections import UserDict from collections import UserDict
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Dict from typing import Dict, Type
from . import SyncId from . import SyncId
from .model import Model from .model import Model
from .special_models import FilteredMembers
@dataclass(frozen=True) @dataclass(frozen=True)
@ -23,7 +24,16 @@ class ModelStore(UserDict):
def __missing__(self, key: SyncId) -> Model: def __missing__(self, key: SyncId) -> Model:
"""When accessing a non-existent model, create and return it.""" """When accessing a non-existent model, create and return it."""
is_tuple = isinstance(key, tuple)
model: Model
if is_tuple and len(key) == 3 and key[2] == "filtered_members":
model = FilteredMembers(user_id=key[0], room_id=key[1])
else:
model = Model(sync_id=key) model = Model(sync_id=key)
print( key, model)
self.data[key] = model self.data[key] = model
return model return model
@ -35,3 +45,10 @@ class ModelStore(UserDict):
type(self).__name__, type(self).__name__,
"\n ".join(sorted(str(v) for v in self.values())), "\n ".join(sorted(str(v) for v in self.values())),
) )
async def ensure_exists_from_qml(self, sync_id: SyncId) -> None:
if isinstance(sync_id, list): # QML can't pass tuples
sync_id = tuple(sync_id)
self[sync_id] # will call __missing__ if needed

View File

@ -37,3 +37,16 @@ class MatchingAccounts(ModelFilter):
(r for r in self.all_rooms.values() if r.for_account == item.id), (r for r in self.all_rooms.values() if r.for_account == item.id),
False, False,
) )
class FilteredMembers(FieldSubstringFilter):
def __init__(self, user_id: str, room_id: str) -> None:
self.user_id = user_id
self.room_id = room_id
sync_id = (user_id, room_id, "filtered_members")
super().__init__(sync_id=sync_id, fields=("display_name",))
def accept_source(self, source: Model) -> bool:
return source.sync_id == (self.user_id, self.room_id, "members")

View File

@ -38,9 +38,12 @@ QtObject {
function get(...modelId) { function get(...modelId) {
if (modelId.length === 1) modelId = modelId[0] if (modelId.length === 1) modelId = modelId[0]
if (! privates.store[modelId]) if (! privates.store[modelId]) {
privates.py.callCoro("models.ensure_exists_from_qml", [modelId])
privates.store[modelId] = privates.store[modelId] =
privates.model.createObject(this, {modelId}) privates.model.createObject(this, {modelId})
}
return privates.store[modelId] return privates.store[modelId]
} }

View File

@ -7,17 +7,15 @@ import "../../../Base"
HColumnLayout { HColumnLayout {
readonly property alias keybindFocusItem: filterField readonly property alias keybindFocusItem: filterField
readonly property var modelSyncId:
[chat.userId, chat.roomId, "filtered_members"]
HListView { HListView {
id: memberList id: memberList
clip: true clip: true
add: null // See the XXX comment in HListView.qml add: null // See the XXX comment in HListView.qml
model: HStringFilterModel { model: ModelStore.get(modelSyncId)
sourceModel: ModelStore.get(chat.userId, chat.roomId, "members")
field: "display_name"
filter: filterField.text
delegate: MemberDelegate { delegate: MemberDelegate {
id: member id: member
@ -31,7 +29,6 @@ HColumnLayout {
} }
} }
} }
}
Layout.fillWidth: true Layout.fillWidth: true
Layout.fillHeight: true Layout.fillHeight: true
@ -70,6 +67,9 @@ HColumnLayout {
// declared normally // declared normally
Component.onCompleted: placeholderText = qsTr("Filter members") Component.onCompleted: placeholderText = qsTr("Filter members")
onTextChanged:
py.callCoro("set_substring_filter", [modelSyncId, text])
Behavior on opacity { HNumberAnimation {} } Behavior on opacity { HNumberAnimation {} }
} }