refactor(@desktop/wallet): Add transaction model/item
This commit is contained in:
parent
1088669a6e
commit
32cfe03880
|
@ -0,0 +1,116 @@
|
|||
import strformat
|
||||
|
||||
type
|
||||
Item* = object
|
||||
id: string
|
||||
typ: string
|
||||
address: string
|
||||
blockNumber: string
|
||||
blockHash: string
|
||||
timestamp: string
|
||||
gasPrice: string
|
||||
gasLimit: string
|
||||
gasUsed: string
|
||||
nonce: string
|
||||
txStatus: string
|
||||
value: string
|
||||
fro: string
|
||||
to: string
|
||||
contract: string
|
||||
|
||||
proc initItem*(
|
||||
id,
|
||||
typ,
|
||||
address,
|
||||
blockNumber,
|
||||
blockHash,
|
||||
timestamp,
|
||||
gasPrice,
|
||||
gasLimit,
|
||||
gasUsed,
|
||||
nonce,
|
||||
txStatus,
|
||||
value,
|
||||
fro,
|
||||
to,
|
||||
contract: string
|
||||
): Item =
|
||||
result.id = id
|
||||
result.typ = typ
|
||||
result.address = address
|
||||
result.blockNumber = blockNumber
|
||||
result.blockHash = blockHash
|
||||
result.timestamp = timestamp
|
||||
result.gasPrice = gasPrice
|
||||
result.gasLimit = gasLimit
|
||||
result.gasUsed = gasUsed
|
||||
result.nonce = nonce
|
||||
result.txStatus = txStatus
|
||||
result.value = value
|
||||
result.fro = fro
|
||||
result.to = to
|
||||
result.contract = contract
|
||||
|
||||
proc `$`*(self: Item): string =
|
||||
result = fmt"""AllTokensItem(
|
||||
id: {self.id},
|
||||
type: {self.typ},
|
||||
address: {self.address},
|
||||
blockNumber: {self.blockNumber},
|
||||
blockHash: {self.blockHash},
|
||||
timestamp: {self.timestamp},
|
||||
gasPrice: {self.gasPrice},
|
||||
gasLimit: {self.gasLimit},
|
||||
gasUsed: {self.gasUsed},
|
||||
nonce: {self.nonce},
|
||||
txStatus: {self.txStatus},
|
||||
value: {self.value},
|
||||
fro: {self.fro},
|
||||
to: {self.to},
|
||||
contract: {self.contract},
|
||||
]"""
|
||||
|
||||
proc getId*(self: Item): string =
|
||||
return self.id
|
||||
|
||||
proc getType*(self: Item): string =
|
||||
return self.typ
|
||||
|
||||
proc getAddress*(self: Item): string =
|
||||
return self.address
|
||||
|
||||
proc getBlockNumber*(self: Item): string =
|
||||
return self.blockNumber
|
||||
|
||||
proc getBlockHash*(self: Item): string =
|
||||
return self.blockHash
|
||||
|
||||
proc getTimestamp*(self: Item): string =
|
||||
return self.timestamp
|
||||
|
||||
proc getGasPrice*(self: Item): string =
|
||||
return self.gasPrice
|
||||
|
||||
proc getGasLimit*(self: Item): string =
|
||||
return self.gasLimit
|
||||
|
||||
proc getGasUsed*(self: Item): string =
|
||||
return self.gasUsed
|
||||
|
||||
proc getNonce*(self: Item): string =
|
||||
return self.nonce
|
||||
|
||||
proc getTxStatus*(self: Item): string =
|
||||
return self.txStatus
|
||||
|
||||
proc getValue*(self: Item): string =
|
||||
return self.value
|
||||
|
||||
proc getfrom*(self: Item): string =
|
||||
return self.fro
|
||||
|
||||
proc getTo*(self: Item): string =
|
||||
return self.to
|
||||
|
||||
proc getContract*(self: Item): string =
|
||||
return self.contract
|
|
@ -0,0 +1,117 @@
|
|||
import NimQml, Tables, strutils, strformat
|
||||
|
||||
import ./item
|
||||
|
||||
type
|
||||
ModelRole {.pure.} = enum
|
||||
Id = UserRole + 1,
|
||||
Type
|
||||
Address
|
||||
BlockNumber
|
||||
BlockHash
|
||||
Timestamp
|
||||
GasPrice
|
||||
GasLimit
|
||||
GasUsed
|
||||
Nonce
|
||||
TxStatus
|
||||
Value
|
||||
From
|
||||
To
|
||||
Contract
|
||||
|
||||
QtObject:
|
||||
type
|
||||
Model* = ref object of QAbstractListModel
|
||||
items: seq[Item]
|
||||
|
||||
proc delete(self: Model) =
|
||||
self.items = @[]
|
||||
self.QAbstractListModel.delete
|
||||
|
||||
proc setup(self: Model) =
|
||||
self.QAbstractListModel.setup
|
||||
|
||||
proc newModel*(): Model =
|
||||
new(result, delete)
|
||||
result.setup
|
||||
|
||||
proc `$`*(self: Model): string =
|
||||
for i in 0 ..< self.items.len:
|
||||
result &= fmt"""[{i}]:({$self.items[i]})"""
|
||||
|
||||
proc countChanged(self: Model) {.signal.}
|
||||
|
||||
proc getCount(self: Model): int {.slot.} =
|
||||
self.items.len
|
||||
|
||||
QtProperty[int] count:
|
||||
read = getCount
|
||||
notify = countChanged
|
||||
|
||||
method rowCount(self: Model, index: QModelIndex = nil): int =
|
||||
return self.items.len
|
||||
|
||||
method roleNames(self: Model): Table[int, string] =
|
||||
{
|
||||
ModelRole.Id.int:"id",
|
||||
ModelRole.Type.int:"type",
|
||||
ModelRole.Address.int:"address",
|
||||
ModelRole.BlockNumber.int:"blockNumber",
|
||||
ModelRole.BlockHash.int:"blockHash",
|
||||
ModelRole.Timestamp.int:"timestamp",
|
||||
ModelRole.GasPrice.int:"gasPrice",
|
||||
ModelRole.GasLimit.int:"gasLimit",
|
||||
ModelRole.GasUsed.int:"gasUsed",
|
||||
ModelRole.Nonce.int:"nonce",
|
||||
ModelRole.TxStatus.int:"txStatus",
|
||||
ModelRole.Value.int:"value",
|
||||
ModelRole.From.int:"from",
|
||||
ModelRole.To.int:"to",
|
||||
ModelRole.Contract.int:"contract",
|
||||
}.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.getId())
|
||||
of ModelRole.Type:
|
||||
result = newQVariant(item.getType())
|
||||
of ModelRole.Address:
|
||||
result = newQVariant(item.getAddress())
|
||||
of ModelRole.BlockNumber:
|
||||
result = newQVariant(item.getBlockNumber())
|
||||
of ModelRole.BlockHash:
|
||||
result = newQVariant(item.getBlockHash())
|
||||
of ModelRole.Timestamp:
|
||||
result = newQVariant(item.getTimestamp())
|
||||
of ModelRole.GasPrice:
|
||||
result = newQVariant(item.getGasPrice())
|
||||
of ModelRole.GasLimit:
|
||||
result = newQVariant(item.getGasLimit())
|
||||
of ModelRole.GasUsed:
|
||||
result = newQVariant(item.getGasUsed())
|
||||
of ModelRole.Nonce:
|
||||
result = newQVariant(item.getNonce())
|
||||
of ModelRole.TxStatus:
|
||||
result = newQVariant(item.getTxStatus())
|
||||
of ModelRole.Value:
|
||||
result = newQVariant(item.getValue())
|
||||
of ModelRole.From:
|
||||
result = newQVariant(item.getFrom())
|
||||
of ModelRole.To:
|
||||
result = newQVariant(item.getTo())
|
||||
of ModelRole.Contract:
|
||||
result = newQVariant(item.getContract())
|
||||
|
||||
proc setData*(self: Model, item: seq[Item]) =
|
||||
self.countChanged()
|
|
@ -11,7 +11,7 @@ type
|
|||
proc newModule*[T](delegate: T): Module[T] =
|
||||
result = Module[T]()
|
||||
result.delegate = delegate
|
||||
result.view = newView()
|
||||
result.view = newView(result)
|
||||
result.moduleLoaded = false
|
||||
|
||||
method delete*[T](self: Module[T]) =
|
||||
|
|
|
@ -1,15 +1,32 @@
|
|||
import NimQml
|
||||
|
||||
import ./model
|
||||
import ./io_interface
|
||||
|
||||
QtObject:
|
||||
type
|
||||
View* = ref object of QObject
|
||||
|
||||
proc setup(self: View) =
|
||||
self.QObject.setup
|
||||
delegate: io_interface.AccessInterface
|
||||
model: Model
|
||||
modelVariant: QVariant
|
||||
|
||||
proc delete*(self: View) =
|
||||
self.model.delete
|
||||
self.modelVariant.delete
|
||||
self.QObject.delete
|
||||
|
||||
proc newView*(): View =
|
||||
proc newView*(delegate: io_interface.AccessInterface): View =
|
||||
new(result, delete)
|
||||
result.setup()
|
||||
result.QObject.setup
|
||||
result.delegate = delegate
|
||||
result.model = newModel()
|
||||
result.modelVariant = newQVariant(result.model)
|
||||
|
||||
proc modelChanged*(self: View) {.signal.}
|
||||
|
||||
proc getModel(self: View): QVariant {.slot.} =
|
||||
return self.modelVariant
|
||||
|
||||
QtProperty[QVariant] model:
|
||||
read = getModel
|
||||
notify = modelChanged
|
Loading…
Reference in New Issue