Python filter for AccountsBar

This commit is contained in:
miruka 2020-05-10 14:26:26 -04:00
parent 0e49b5f972
commit 200f25d23e
5 changed files with 81 additions and 49 deletions

View File

@ -24,6 +24,8 @@
- rooms without messages on first sync
- avatar loading performance problem?
- docstrings
## Refactoring
- Rewrite account settings using `HTabbedContainer`

View File

@ -20,7 +20,7 @@ from .models.filters import FieldSubstringFilter
from .models.items import Account
from .models.model import Model
from .models.model_store import ModelStore
from .models.special_models import AllRooms
from .models.special_models import AllRooms, MatchingAccounts
from .user_files import Accounts, History, Theme, UISettings, UIState
# Logging configuration
@ -81,8 +81,10 @@ class Backend:
self.ui_state: UIState = UIState(self)
self.history: History = History(self)
self.all_rooms: AllRooms = AllRooms()
self.models: ModelStore = ModelStore()
self.all_rooms = AllRooms()
self.matching_accounts = MatchingAccounts(self.all_rooms)
self.models = ModelStore()
self.clients: Dict[str, MatrixClient] = {}
self.profile_cache: Dict[str, nio.ProfileGetResponse] = {}

View File

@ -1,6 +1,8 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from typing import TYPE_CHECKING, Any, Collection, Dict, Optional, Tuple
from typing import (
TYPE_CHECKING, Any, Callable, Collection, Dict, List, Optional, Tuple,
)
from . import SyncId
from .model import Model
@ -13,6 +15,7 @@ if TYPE_CHECKING:
class ModelFilter(ModelProxy):
def __init__(self, sync_id: SyncId) -> None:
self.filtered_out: Dict[Tuple[Optional[SyncId], str], "ModelItem"] = {}
self.items_changed_callbacks: List[Callable[[], None]] = []
super().__init__(sync_id)
@ -35,6 +38,9 @@ class ModelFilter(ModelProxy):
self.filtered_out[source.sync_id, key] = value
self.pop((source.sync_id, key), None)
for callback in self.items_changed_callbacks:
callback()
def source_item_deleted(self, source: Model, key) -> None:
if self.accept_source(source):
@ -43,6 +49,9 @@ class ModelFilter(ModelProxy):
except KeyError:
del self.filtered_out[source.sync_id, key]
for callback in self.items_changed_callbacks:
callback()
def source_cleared(self, source: Model) -> None:
if self.accept_source(source):
@ -53,6 +62,9 @@ class ModelFilter(ModelProxy):
except KeyError:
del self.filtered_out[source.sync_id, key]
for callback in self.items_changed_callbacks:
callback()
def refilter(self) -> None:
with self._write_lock:
@ -74,6 +86,9 @@ class ModelFilter(ModelProxy):
for key in bring_back:
self[key] = self.filtered_out.pop(key)
for callback in self.items_changed_callbacks:
callback()
class FieldSubstringFilter(ModelFilter):
def __init__(self, sync_id: SyncId, fields: Collection[str]) -> None:

View File

@ -1,7 +1,8 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from .filters import FieldSubstringFilter
from .filters import FieldSubstringFilter, ModelFilter
from .model import Model
from .model_item import ModelItem
class AllRooms(FieldSubstringFilter):
@ -15,3 +16,24 @@ class AllRooms(FieldSubstringFilter):
len(source.sync_id) == 2 and
source.sync_id[1] == "rooms" # type: ignore
)
class MatchingAccounts(ModelFilter):
def __init__(self, all_rooms: AllRooms) -> None:
super().__init__(sync_id="matching_accounts")
self.all_rooms = all_rooms
self.all_rooms.items_changed_callbacks.append(self.refilter)
def accept_source(self, source: Model) -> bool:
return source.sync_id == "accounts"
def accept_item(self, item: ModelItem) -> bool:
if not self.all_rooms.filter:
return True
return next(
(r for r in self.all_rooms.values() if r.for_account == item.id),
False,
)

View File

@ -39,10 +39,7 @@ HColumnLayout {
-1,
)
model: HFilterModel {
model: ModelStore.get("accounts")
acceptItem: item =>
! roomFilter || item.id in roomList.sectionIndice
model: ModelStore.get("matching_accounts")
delegate: HTileDelegate {
id: tile
@ -90,12 +87,6 @@ HColumnLayout {
}
}
onRoomFilterChanged: refilterAll()
readonly property string roomFilter: roomList.filter
}
highlight: Item {
Rectangle {
anchors.fill: parent