Python filtered models for room members
This commit is contained in:
parent
200f25d23e
commit
37a9332aed
|
@ -314,6 +314,9 @@ class Backend:
|
|||
|
||||
|
||||
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]
|
||||
|
||||
if not isinstance(model, FieldSubstringFilter):
|
||||
|
|
|
@ -2,10 +2,11 @@
|
|||
|
||||
from collections import UserDict
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict
|
||||
from typing import Dict, Type
|
||||
|
||||
from . import SyncId
|
||||
from .model import Model
|
||||
from .special_models import FilteredMembers
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
@ -23,7 +24,16 @@ class ModelStore(UserDict):
|
|||
def __missing__(self, key: SyncId) -> Model:
|
||||
"""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)
|
||||
print( key, model)
|
||||
|
||||
self.data[key] = model
|
||||
return model
|
||||
|
||||
|
@ -35,3 +45,10 @@ class ModelStore(UserDict):
|
|||
type(self).__name__,
|
||||
"\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
|
||||
|
|
|
@ -37,3 +37,16 @@ class MatchingAccounts(ModelFilter):
|
|||
(r for r in self.all_rooms.values() if r.for_account == item.id),
|
||||
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")
|
||||
|
|
|
@ -38,9 +38,12 @@ QtObject {
|
|||
function get(...modelId) {
|
||||
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.model.createObject(this, {modelId})
|
||||
}
|
||||
|
||||
return privates.store[modelId]
|
||||
}
|
||||
|
|
|
@ -7,17 +7,15 @@ import "../../../Base"
|
|||
|
||||
HColumnLayout {
|
||||
readonly property alias keybindFocusItem: filterField
|
||||
|
||||
readonly property var modelSyncId:
|
||||
[chat.userId, chat.roomId, "filtered_members"]
|
||||
|
||||
HListView {
|
||||
id: memberList
|
||||
clip: true
|
||||
add: null // See the XXX comment in HListView.qml
|
||||
|
||||
model: HStringFilterModel {
|
||||
sourceModel: ModelStore.get(chat.userId, chat.roomId, "members")
|
||||
field: "display_name"
|
||||
filter: filterField.text
|
||||
model: ModelStore.get(modelSyncId)
|
||||
|
||||
delegate: MemberDelegate {
|
||||
id: member
|
||||
|
@ -31,7 +29,6 @@ HColumnLayout {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
|
@ -70,6 +67,9 @@ HColumnLayout {
|
|||
// declared normally
|
||||
Component.onCompleted: placeholderText = qsTr("Filter members")
|
||||
|
||||
onTextChanged:
|
||||
py.callCoro("set_substring_filter", [modelSyncId, text])
|
||||
|
||||
Behavior on opacity { HNumberAnimation {} }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user