refactor(chat-communities): items and model for users list added

This commit is contained in:
Sale Djenic 2021-11-16 10:21:45 +01:00
parent 0def67e783
commit dafb457a49
2 changed files with 114 additions and 24 deletions

View File

@ -1,21 +1,39 @@
import NimQml
QtObject:
type type
Item* = ref object of QObject OnlineStatus* {.pure.} = enum
Online = 0
Idle
DoNotDisturb
Invisible
Offline
proc setup(self: Item) = type
self.QObject.setup Item* = ref object
id: string
name: string
onlineStatus: OnlineStatus
identicon: string
proc delete*(self: Item) = proc initItem*(id: string, name: string, onlineStatus: OnlineStatus, identicon: string): Item =
self.QObject.delete result = Item()
result.id = id
result.name = name
result.onlineStatus = onlineStatus
result.identicon = identicon
proc newItem*(): Item = proc id*(self: Item): string {.inline.} =
new(result, delete)
result.setup()
proc id*(self: Item): string {.slot.} =
self.id self.id
QtProperty[string] id: proc name*(self: Item): string {.inline.} =
read = id self.name
proc `name=`*(self: Item, value: string) {.inline.} =
self.name = value
proc onlineStatus*(self: Item): OnlineStatus {.inline.} =
self.onlineStatus
proc `onlineStatus=`*(self: Item, value: OnlineStatus) {.inline.} =
self.onlineStatus = value
proc identicon*(self: Item): string {.inline.} =
self.identicon

View File

@ -1,17 +1,89 @@
import NimQml import NimQml, Tables
import item import item
type
ModelRole {.pure.} = enum
Id = UserRole + 1
Name
OnlineStatus
Identicon
QtObject: QtObject:
type type
Model* = ref object of QAbstractListModel Model* = ref object of QAbstractListModel
sections: seq[Item] items: seq[Item]
proc delete(self: Model) =
self.items = @[]
self.QAbstractListModel.delete
proc setup(self: Model) = proc setup(self: Model) =
self.QAbstractListModel.setup self.QAbstractListModel.setup
proc delete*(self: Model) =
self.QAbstractListModel.delete
proc newModel*(): Model = proc newModel*(): Model =
new(result, delete) new(result, delete)
result.setup result.setup
method rowCount(self: Model, index: QModelIndex = nil): int =
return self.items.len
method roleNames(self: Model): Table[int, string] =
{
ModelRole.Id.int:"id",
ModelRole.Name.int:"name",
ModelRole.OnlineStatus.int:"onlineStatus",
ModelRole.Identicon.int:"identicon",
}.toTable
method data(self: Model, index: QModelIndex, role: int): QVariant =
if (not index.isValid):
return
if (index.row < 0 or index.row >= self.items.len):
return
let item = self.items[index.row]
let enumRole = role.ModelRole
case enumRole:
of ModelRole.Id:
result = newQVariant(item.id)
of ModelRole.Name:
result = newQVariant(item.name)
of ModelRole.OnlineStatus:
result = newQVariant(item.onlineStatus.int)
of ModelRole.Identicon:
result = newQVariant(item.identicon)
proc setItems*(self: Model, items: seq[Item]) =
self.beginResetModel()
self.items = items
self.endResetModel()
proc findIndexForMessageId(self: Model, id: string): int =
for i in 0 ..< self.items.len:
if(self.items[i].id == id):
return i
return -1
proc setName*(self: Model, id: string, name: string) =
let ind = self.findIndexForMessageId(id)
if(ind == -1):
return
self.items[ind].name = name
let index = self.createIndex(ind, 0, nil)
self.dataChanged(index, index, @[ModelRole.Name.int])
proc setOnlineStatus*(self: Model, id: string, onlineStatus: OnlineStatus) =
let ind = self.findIndexForMessageId(id)
if(ind == -1):
return
self.items[ind].onlineStatus = onlineStatus
let index = self.createIndex(ind, 0, nil)
self.dataChanged(index, index, @[ModelRole.OnlineStatus.int])