refactor(chat-communities): items and model for users list added
This commit is contained in:
parent
0def67e783
commit
dafb457a49
|
@ -1,21 +1,39 @@
|
|||
import NimQml
|
||||
|
||||
QtObject:
|
||||
type
|
||||
Item* = ref object of QObject
|
||||
OnlineStatus* {.pure.} = enum
|
||||
Online = 0
|
||||
Idle
|
||||
DoNotDisturb
|
||||
Invisible
|
||||
Offline
|
||||
|
||||
proc setup(self: Item) =
|
||||
self.QObject.setup
|
||||
type
|
||||
Item* = ref object
|
||||
id: string
|
||||
name: string
|
||||
onlineStatus: OnlineStatus
|
||||
identicon: string
|
||||
|
||||
proc delete*(self: Item) =
|
||||
self.QObject.delete
|
||||
proc initItem*(id: string, name: string, onlineStatus: OnlineStatus, identicon: string): Item =
|
||||
result = Item()
|
||||
result.id = id
|
||||
result.name = name
|
||||
result.onlineStatus = onlineStatus
|
||||
result.identicon = identicon
|
||||
|
||||
proc newItem*(): Item =
|
||||
new(result, delete)
|
||||
result.setup()
|
||||
|
||||
proc id*(self: Item): string {.slot.} =
|
||||
proc id*(self: Item): string {.inline.} =
|
||||
self.id
|
||||
|
||||
QtProperty[string] id:
|
||||
read = id
|
||||
proc name*(self: Item): string {.inline.} =
|
||||
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
|
|
@ -1,17 +1,89 @@
|
|||
import NimQml
|
||||
import NimQml, Tables
|
||||
|
||||
import item
|
||||
|
||||
type
|
||||
ModelRole {.pure.} = enum
|
||||
Id = UserRole + 1
|
||||
Name
|
||||
OnlineStatus
|
||||
Identicon
|
||||
|
||||
QtObject:
|
||||
type
|
||||
Model* = ref object of QAbstractListModel
|
||||
sections: seq[Item]
|
||||
items: seq[Item]
|
||||
|
||||
proc delete(self: Model) =
|
||||
self.items = @[]
|
||||
self.QAbstractListModel.delete
|
||||
|
||||
proc setup(self: Model) =
|
||||
self.QAbstractListModel.setup
|
||||
|
||||
proc delete*(self: Model) =
|
||||
self.QAbstractListModel.delete
|
||||
|
||||
proc newModel*(): Model =
|
||||
new(result, delete)
|
||||
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])
|
Loading…
Reference in New Issue