Move models to their own files

This commit is contained in:
miruka 2019-07-04 16:01:44 -04:00
parent 990770dd38
commit fde7f4aee6
21 changed files with 107 additions and 93 deletions

View File

@ -6,7 +6,7 @@ Banner {
color: HStyle.chat.inviteBanner.background color: HStyle.chat.inviteBanner.background
// TODO: get disp name from models.users, inviter = userid now // TODO: get disp name from users, inviter = userid now
avatar.name: inviter ? inviter.displayname : "" avatar.name: inviter ? inviter.displayname : ""
//avatar.imageUrl: inviter ? inviter.avatar_url : "" //avatar.imageUrl: inviter ? inviter.avatar_url : ""

View File

@ -10,12 +10,12 @@ HColumnLayout {
property string category: "" property string category: ""
property string roomId: "" property string roomId: ""
readonly property var roomInfo: models.rooms.getWhere( readonly property var roomInfo: rooms.getWhere(
{"userId": userId, "roomId": roomId, "category": category}, 1 {"userId": userId, "roomId": roomId, "category": category}, 1
)[0] )[0]
readonly property var sender: readonly property var sender:
models.users.getWhere({"userId": userId}, 1)[0] users.getWhere({"userId": userId}, 1)[0]
readonly property bool hasUnknownDevices: false readonly property bool hasUnknownDevices: false
//category == "Rooms" ? //category == "Rooms" ?

View File

@ -22,7 +22,7 @@ Column {
onReloadPreviousItem: previousItem = getPreviousItem() onReloadPreviousItem: previousItem = getPreviousItem()
property var senderInfo: null property var senderInfo: null
Component.onCompleted: senderInfo = models.users.getUser(model.senderId) Component.onCompleted: senderInfo = users.getUser(model.senderId)
readonly property bool isOwn: chatPage.userId === model.senderId readonly property bool isOwn: chatPage.userId === model.senderId

View File

@ -12,7 +12,7 @@ HRectangle {
clip: true clip: true
model: HListModel { model: HListModel {
sourceModel: models.timelines sourceModel: timelines
filters: ValueFilter { filters: ValueFilter {
roleName: "roomId" roleName: "roomId"

View File

@ -1,13 +1,11 @@
function onRoomUpdated(user_id, category, room_id, display_name, avatar_url, function onRoomUpdated(user_id, category, room_id, display_name, avatar_url,
topic, inviter, left_event) { topic, inviter, left_event) {
models.roomCategories.upsert({"userId": user_id, "name": category}, { roomCategories.upsert({"userId": user_id, "name": category}, {
"userId": user_id, "userId": user_id,
"name": category "name": category
}) })
var rooms = models.rooms
function roles(for_category) { function roles(for_category) {
return {"userId": user_id, "roomId": room_id, "category": for_category} return {"userId": user_id, "roomId": room_id, "category": for_category}
} }
@ -47,7 +45,7 @@ function onRoomUpdated(user_id, category, room_id, display_name, avatar_url,
function onRoomDeleted(user_id, category, room_id) { function onRoomDeleted(user_id, category, room_id) {
var roles = {"userId": user_id, "roomId": room_id, "category": category} var roles = {"userId": user_id, "roomId": room_id, "category": category}
models.rooms.popWhere(roles, 1) rooms.popWhere(roles, 1)
} }
@ -78,19 +76,19 @@ function onTimelineEventReceived(
} }
// Replace any matching local echo // Replace any matching local echo
var found = models.timelines.getIndices({ var found = timelines.getIndices({
"roomId": room_id, "roomId": room_id,
"senderId": sender_id, "senderId": sender_id,
"content": content, "content": content,
"isLocalEcho": true "isLocalEcho": true
}, 1, 500) }, 1, 500)
if (found.length > 0) { if (found.length > 0) {
models.timelines.set(found[0], item) timelines.set(found[0], item)
return return
} }
// Multiple clients will emit duplicate events with the same eventId // Multiple clients will emit duplicate events with the same eventId
models.timelines.upsert({"eventId": event_id}, item, true, 500) timelines.upsert({"eventId": event_id}, item, true, 500)
} }

View File

@ -1,14 +1,14 @@
function onAccountUpdated(user_id) { function onAccountUpdated(user_id) {
models.accounts.append({"userId": user_id}) accounts.append({"userId": user_id})
} }
function onAccountDeleted(user_id) { function onAccountDeleted(user_id) {
models.accounts.popWhere({"userId": user_id}, 1) accounts.popWhere({"userId": user_id}, 1)
} }
// TODO: get updated from nio rooms // TODO: get updated from nio rooms
function onUserUpdated(user_id, display_name, avatar_url, status_message) { function onUserUpdated(user_id, display_name, avatar_url, status_message) {
models.users.upsert({"userId": user_id}, { users.upsert({"userId": user_id}, {
"userId": user_id, "userId": user_id,
"displayName": display_name, "displayName": display_name,
"avatarUrl": avatar_url, "avatarUrl": avatar_url,

View File

@ -1,58 +0,0 @@
import QtQuick 2.7
import SortFilterProxyModel 0.2
import "Base"
QtObject {
property HListModel accounts: HListModel {
sorters: StringSorter {
roleName: "userId"
numericMode: true // human numeric sort
}
}
property HListModel users: HListModel {
function getUser(user_id) {
var found = users.getWhere({"userId": user_id}, 1)
if (found.length > 0) { return found[0] }
users.append({
"userId": user_id,
"displayName": "",
"avatarUrl": "",
"statusMessage": ""
})
py.callCoro("request_user_update_event", [user_id])
return users.getWhere({"userId": user_id}, 1)[0]
}
}
property HListModel devices: HListModel {}
property HListModel roomCategories: HListModel {
sorters: FilterSorter {
ValueFilter { roleName: "name"; value: "Invites" }
ValueFilter { roleName: "name"; value: "Rooms" }
ValueFilter { roleName: "name"; value: "Left" }
}
}
property HListModel rooms: HListModel {}
property HListModel timelines: HListModel {
function lastEventOf(room_id) {
// Return an event item or undefined if none found
for (var i = 0; i < timelines.count; i++) {
var item = timelines.get(i) // TODO: standardize
if (item.roomId == room_id) { return item }
}
}
sorters: RoleSorter {
roleName: "date"
sortOrder: Qt.DescendingOrder
}
}
}

View File

@ -0,0 +1,10 @@
import QtQuick 2.7
import SortFilterProxyModel 0.2
import "../Base"
HListModel {
sorters: StringSorter {
roleName: "userId"
numericMode: true // human numeric sort
}
}

View File

@ -0,0 +1,5 @@
import QtQuick 2.7
import SortFilterProxyModel 0.2
import "../Base"
HListModel {}

View File

@ -0,0 +1,11 @@
import QtQuick 2.7
import SortFilterProxyModel 0.2
import "../Base"
HListModel {
sorters: FilterSorter {
ValueFilter { roleName: "name"; value: "Invites" }
ValueFilter { roleName: "name"; value: "Rooms" }
ValueFilter { roleName: "name"; value: "Left" }
}
}

5
src/qml/Models/Rooms.qml Normal file
View File

@ -0,0 +1,5 @@
import QtQuick 2.7
import SortFilterProxyModel 0.2
import "../Base"
HListModel {}

View File

@ -0,0 +1,19 @@
import QtQuick 2.7
import SortFilterProxyModel 0.2
import "../Base"
HListModel {
function lastEventOf(room_id) {
// Return an event item or undefined if none found
for (var i = 0; i < count; i++) {
var item = get(i) // TODO: standardize
if (item.roomId == room_id) { return item }
}
}
sorters: RoleSorter {
roleName: "date"
sortOrder: Qt.DescendingOrder
}
}

21
src/qml/Models/Users.qml Normal file
View File

@ -0,0 +1,21 @@
import QtQuick 2.7
import SortFilterProxyModel 0.2
import "../Base"
HListModel {
function getUser(user_id) {
var found = getWhere({"userId": user_id}, 1)
if (found.length > 0) { return found[0] }
append({
"userId": user_id,
"displayName": "",
"avatarUrl": "",
"statusMessage": ""
})
py.callCoro("request_user_update_event", [user_id])
return getWhere({"userId": user_id}, 1)[0]
}
}

View File

@ -9,7 +9,7 @@ Column {
// Avoid binding loop by using Component.onCompleted // Avoid binding loop by using Component.onCompleted
property var user: null property var user: null
Component.onCompleted: user = models.users.getUser(userId) Component.onCompleted: user = users.getUser(userId)
property string roomCategoriesListUserId: userId property string roomCategoriesListUserId: userId
property bool expanded: true property bool expanded: true

View File

@ -6,6 +6,6 @@ HListView {
id: accountList id: accountList
clip: true clip: true
model: models.accounts model: accounts
delegate: AccountDelegate {} delegate: AccountDelegate {}
} }

View File

@ -9,7 +9,7 @@ HListView {
id: roomCategoriesList id: roomCategoriesList
model: SortFilterProxyModel { model: SortFilterProxyModel {
sourceModel: models.roomCategories sourceModel: roomCategories
filters: ValueFilter { filters: ValueFilter {
roleName: "userId" roleName: "userId"
value: userId value: userId

View File

@ -37,11 +37,7 @@ MouseArea {
} }
HRichLabel { HRichLabel {
id: subtitleLabel function getText(ev) {
visible: Boolean(text)
textFormat: Text.StyledText
text: {
var ev = models.timelines.lastEventOf(model.roomId)
if (! ev) { return "" } if (! ev) { return "" }
if (! Utils.eventIsMessage(ev)) { if (! Utils.eventIsMessage(ev)) {
@ -49,11 +45,16 @@ MouseArea {
} }
return Utils.coloredNameHtml( return Utils.coloredNameHtml(
models.users.getUser(ev.senderId).displayName, users.getUser(ev.senderId).displayName,
ev.senderId ev.senderId
) + ": " + py.callSync("inlinify", [ev.content]) ) + ": " + py.callSync("inlinify", [ev.content])
} }
id: subtitleLabel
visible: Boolean(text)
textFormat: Text.StyledText
text: getText(timelines.lastEventOf(model.roomId))
font.pixelSize: HStyle.fontSize.small font.pixelSize: HStyle.fontSize.small
elide: Text.ElideRight elide: Text.ElideRight
maximumLineCount: 1 maximumLineCount: 1

View File

@ -11,7 +11,7 @@ HListView {
spacing: sidePane.normalSpacing spacing: sidePane.normalSpacing
model: SortFilterProxyModel { model: SortFilterProxyModel {
sourceModel: models.rooms sourceModel: rooms
filters: AllOf { filters: AllOf {
ValueFilter { ValueFilter {
roleName: "category" roleName: "category"

View File

@ -16,7 +16,7 @@ Item {
} }
property bool accountsPresent: property bool accountsPresent:
models.accounts.count > 0 || py.loadingAccounts accounts.count > 0 || py.loadingAccounts
HImage { HImage {
id: mainUIBackground id: mainUIBackground

View File

@ -1,6 +1,7 @@
import QtQuick 2.7 import QtQuick 2.7
import QtQuick.Controls 2.2 import QtQuick.Controls 2.2
import "Base" import "Base"
import "Models"
ApplicationWindow { ApplicationWindow {
id: window id: window
@ -20,13 +21,14 @@ ApplicationWindow {
window.ready = true window.ready = true
} }
Python { Python { id: py }
id: py
}
Models { Accounts { id: accounts }
id: models Devices { id: devices }
} RoomCategories { id: roomCategories }
Rooms { id: rooms }
Timelines { id: timelines }
Users { id: users }
LoadingScreen { LoadingScreen {
id: loadingScreen id: loadingScreen

View File

@ -63,16 +63,16 @@ function eventIsMessage(ev) {
function translatedEventContent(ev) { function translatedEventContent(ev) {
// ev: models.timelines item // ev: timelines item
if (ev.translatable == false) { return ev.content } if (ev.translatable == false) { return ev.content }
// %S → sender display name // %S → sender display name
var name = models.users.getUser(ev.senderId).displayName var name = users.getUser(ev.senderId).displayName
var text = ev.content.replace("%S", coloredNameHtml(name, ev.senderId)) var text = ev.content.replace("%S", coloredNameHtml(name, ev.senderId))
// %T → target (event state_key) display name // %T → target (event state_key) display name
if (ev.targetUserId) { if (ev.targetUserId) {
var tname = models.users.getUser(ev.targetUserId).displayName var tname = users.getUser(ev.targetUserId).displayName
text = text.replace("%T", coloredNameHtml(tname, ev.targetUserId)) text = text.replace("%T", coloredNameHtml(tname, ev.targetUserId))
} }