Merge Backend and QMLModels

Backend.models.accounts → Backend.accounts
Backend.models.roomEvents → Backend.roomEvents
This commit is contained in:
miruka 2019-05-02 14:31:47 -04:00
parent 047225fded
commit 52d538e995
11 changed files with 35 additions and 56 deletions

View File

@ -2,13 +2,12 @@
# This file is part of harmonyqml, licensed under GPLv3. # This file is part of harmonyqml, licensed under GPLv3.
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from typing import Dict, Sequence, Set from typing import Deque, Dict, Sequence, Set
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSlot from PyQt5.QtCore import QObject, pyqtProperty, pyqtSlot
from .html_filter import HtmlFilter from .html_filter import HtmlFilter
from .model.items import RoomEvent from .model import ListModel, ListModelMap
from .model.qml_models import QMLModels
from .pyqt_future import futurize from .pyqt_future import futurize
@ -22,10 +21,13 @@ class Backend(QObject):
self.past_tokens: Dict[str, str] = {} self.past_tokens: Dict[str, str] = {}
self.fully_loaded_rooms: Set[str] = set() self.fully_loaded_rooms: Set[str] = set()
self._html_filter: HtmlFilter = HtmlFilter(self)
from .client_manager import ClientManager from .client_manager import ClientManager
self._client_manager: ClientManager = ClientManager(self) self._client_manager: ClientManager = ClientManager(self)
self._models: QMLModels = QMLModels(self)
self._html_filter: HtmlFilter = HtmlFilter(self) self._accounts: ListModel = ListModel(parent=parent)
self._room_events: ListModelMap = ListModelMap(Deque, parent)
from .signal_manager import SignalManager from .signal_manager import SignalManager
self._signal_manager: SignalManager = SignalManager(self) self._signal_manager: SignalManager = SignalManager(self)
@ -33,17 +35,21 @@ class Backend(QObject):
self.clientManager.configLoad() self.clientManager.configLoad()
@pyqtProperty("QVariant", constant=True)
def htmlFilter(self):
return self._html_filter
@pyqtProperty("QVariant", constant=True) @pyqtProperty("QVariant", constant=True)
def clientManager(self): def clientManager(self):
return self._client_manager return self._client_manager
@pyqtProperty("QVariant", constant=True) @pyqtProperty("QVariant", constant=True)
def models(self): def accounts(self):
return self._models return self._accounts
@pyqtProperty("QVariant", constant=True) @pyqtProperty("QVariant", constant=True)
def htmlFilter(self): def roomEvents(self):
return self._html_filter return self._room_events
@pyqtSlot(str, result="QVariant") @pyqtSlot(str, result="QVariant")

View File

@ -4,4 +4,3 @@
from . import items from . import items
from .list_model import ListModel from .list_model import ListModel
from .list_model_map import ListModelMap from .list_model_map import ListModelMap
from .qml_models import QMLModels

View File

@ -1,26 +0,0 @@
# Copyright 2019 miruka
# This file is part of harmonyqml, licensed under GPLv3.
from typing import Deque
from PyQt5.QtCore import QObject, pyqtProperty
from .list_model import ListModel
from .list_model_map import ListModelMap
class QMLModels(QObject):
def __init__(self, parent: QObject) -> None:
super().__init__(parent)
self._accounts: ListModel = ListModel(parent=parent)
self._room_events: ListModelMap = ListModelMap(Deque, parent)
@pyqtProperty(ListModel, constant=True)
def accounts(self):
return self._accounts
@pyqtProperty("QVariant", constant=True)
def roomEvents(self):
return self._room_events

View File

@ -11,8 +11,7 @@ from nio.rooms import MatrixRoom
from .backend import Backend from .backend import Backend
from .client import Client from .client import Client
from .model.items import Account, Room, RoomCategory, RoomEvent from .model.items import Account, ListModel, Room, RoomCategory, RoomEvent
from .model.list_model import ListModel
Inviter = Optional[Dict[str, str]] Inviter = Optional[Dict[str, str]]
LeftEvent = Optional[Dict[str, str]] LeftEvent = Optional[Dict[str, str]]
@ -36,7 +35,7 @@ class SignalManager(QObject):
def onClientAdded(self, client: Client) -> None: def onClientAdded(self, client: Client) -> None:
self.connectClient(client) self.connectClient(client)
self.backend.models.accounts.append(Account( self.backend.accounts.append(Account(
userId = client.userId, userId = client.userId,
roomCategories = ListModel([ roomCategories = ListModel([
RoomCategory("Invites", ListModel()), RoomCategory("Invites", ListModel()),
@ -48,7 +47,7 @@ class SignalManager(QObject):
def onClientDeleted(self, user_id: str) -> None: def onClientDeleted(self, user_id: str) -> None:
del self.backend.models.accounts[user_id] del self.backend.accounts[user_id]
def connectClient(self, client: Client) -> None: def connectClient(self, client: Client) -> None:
@ -79,7 +78,7 @@ class SignalManager(QObject):
inviter: Inviter = None) -> None: inviter: Inviter = None) -> None:
nio_room = client.nio.invited_rooms[room_id] nio_room = client.nio.invited_rooms[room_id]
categories = self.backend.models.accounts[client.userId].roomCategories categories = self.backend.accounts[client.userId].roomCategories
categories["Rooms"].rooms.pop(room_id, None) categories["Rooms"].rooms.pop(room_id, None)
categories["Left"].rooms.pop(room_id, None) categories["Left"].rooms.pop(room_id, None)
@ -94,7 +93,7 @@ class SignalManager(QObject):
def onRoomJoined(self, client: Client, room_id: str) -> None: def onRoomJoined(self, client: Client, room_id: str) -> None:
nio_room = client.nio.rooms[room_id] nio_room = client.nio.rooms[room_id]
categories = self.backend.models.accounts[client.userId].roomCategories categories = self.backend.accounts[client.userId].roomCategories
categories["Invites"].rooms.pop(room_id, None) categories["Invites"].rooms.pop(room_id, None)
categories["Left"].rooms.pop(room_id, None) categories["Left"].rooms.pop(room_id, None)
@ -110,7 +109,7 @@ class SignalManager(QObject):
client: Client, client: Client,
room_id: str, room_id: str,
left_event: LeftEvent = None) -> None: left_event: LeftEvent = None) -> None:
categories = self.backend.models.accounts[client.userId].roomCategories categories = self.backend.accounts[client.userId].roomCategories
previous = categories["Rooms"].rooms.pop(room_id, None) previous = categories["Rooms"].rooms.pop(room_id, None)
previous = previous or categories["Invites"].rooms.pop(room_id, None) previous = previous or categories["Invites"].rooms.pop(room_id, None)
@ -155,7 +154,7 @@ class SignalManager(QObject):
self.last_room_events.appendleft(edict["event_id"]) self.last_room_events.appendleft(edict["event_id"])
model = self.backend.models.roomEvents[room_id] model = self.backend.roomEvents[room_id]
date_time = QDateTime\ date_time = QDateTime\
.fromMSecsSinceEpoch(edict["server_timestamp"]) .fromMSecsSinceEpoch(edict["server_timestamp"])
new_event = RoomEvent(type=etype, dateTime=date_time, dict=edict) new_event = RoomEvent(type=etype, dateTime=date_time, dict=edict)
@ -218,7 +217,7 @@ class SignalManager(QObject):
client: Client, client: Client,
room_id: str, room_id: str,
users: List[str]) -> None: users: List[str]) -> None:
categories = self.backend.models.accounts[client.userId].roomCategories categories = self.backend.accounts[client.userId].roomCategories
for categ in categories: for categ in categories:
try: try:
categ.rooms[room_id].typingUsers = users categ.rooms[room_id].typingUsers = users
@ -233,7 +232,7 @@ class SignalManager(QObject):
content: Dict[str, str]) -> None: content: Dict[str, str]) -> None:
with self._lock: with self._lock:
model = self.backend.models.roomEvents[room_id] model = self.backend.roomEvents[room_id]
nio_event = nio.events.RoomMessage.parse_event({ nio_event = nio.events.RoomMessage.parse_event({
"event_id": "", "event_id": "",
"sender": client.userId, "sender": client.userId,
@ -252,9 +251,9 @@ class SignalManager(QObject):
def onRoomAboutToBeForgotten(self, client: Client, room_id: str) -> None: def onRoomAboutToBeForgotten(self, client: Client, room_id: str) -> None:
categories = self.backend.models.accounts[client.userId].roomCategories categories = self.backend.accounts[client.userId].roomCategories
for categ in categories: for categ in categories:
categ.rooms.pop(room_id, None) categ.rooms.pop(room_id, None)
self.backend.models.roomEvents[room_id].clear() self.backend.roomEvents[room_id].clear()

View File

@ -10,8 +10,9 @@ HColumnLayout {
property string roomId: "" property string roomId: ""
readonly property var roomInfo: readonly property var roomInfo:
Backend.models.accounts.get(userId) Backend.accounts.get(userId)
.roomCategories.get(category).rooms.get(roomId) .roomCategories.get(category)
.rooms.get(roomId)
property bool canLoadPastEvents: true property bool canLoadPastEvents: true

View File

@ -10,7 +10,7 @@ HGlassRectangle {
ListView { ListView {
id: roomEventListView id: roomEventListView
delegate: RoomEventDelegate {} delegate: RoomEventDelegate {}
model: Backend.models.roomEvents.get(chatPage.roomId) model: Backend.roomEvents.get(chatPage.roomId)
anchors.fill: parent anchors.fill: parent
anchors.leftMargin: space anchors.leftMargin: space

View File

@ -9,6 +9,6 @@ ListView {
topMargin: spacing topMargin: spacing
bottomMargin: topMargin bottomMargin: topMargin
model: Backend.models.accounts model: Backend.accounts
delegate: AccountDelegate {} delegate: AccountDelegate {}
} }

View File

@ -6,6 +6,6 @@ ListView {
property string userId: "" property string userId: ""
id: roomCategoriesList id: roomCategoriesList
model: Backend.models.accounts.get(userId).roomCategories model: Backend.accounts.get(userId).roomCategories
delegate: RoomCategoryDelegate {} delegate: RoomCategoryDelegate {}
} }

View File

@ -43,7 +43,7 @@ MouseArea {
} }
Connections { Connections {
target: Backend.models.roomEvents.get(roomId) target: Backend.roomEvents.get(roomId)
onChanged: subtitleLabel.text = subtitleLabel.getText() onChanged: subtitleLabel.text = subtitleLabel.getText()
} }

View File

@ -8,6 +8,6 @@ ListView {
id: roomList id: roomList
spacing: accountList.spacing spacing: accountList.spacing
model: Backend.models.accounts.get(userId).roomCategories.get(category).rooms model: Backend.accounts.get(userId).roomCategories.get(category).rooms
delegate: RoomDelegate {} delegate: RoomDelegate {}
} }

View File

@ -2,7 +2,7 @@
function getLastRoomEventText(roomId, accountId) { function getLastRoomEventText(roomId, accountId) {
var eventsModel = Backend.models.roomEvents.get(roomId) var eventsModel = Backend.roomEvents.get(roomId)
if (eventsModel.count < 1) { return "" } if (eventsModel.count < 1) { return "" }
var ev = eventsModel.get(0) var ev = eventsModel.get(0)