Skip to content

Commit

Permalink
[outputs] refs#295 Add qml ownership to created models
Browse files Browse the repository at this point in the history
  • Loading branch information
stdevMac committed Feb 10, 2020
1 parent 37cec49 commit 30b3243
Showing 1 changed file with 94 additions and 77 deletions.
171 changes: 94 additions & 77 deletions src/models/modelWallets.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package models

import (
//"github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/config"
coin "github.com/fibercrypto/fibercryptowallet/src/coin/skycoin/models"
"github.com/fibercrypto/fibercryptowallet/src/core"
local "github.com/fibercrypto/fibercryptowallet/src/main"
"github.com/fibercrypto/fibercryptowallet/src/util"
"github.com/fibercrypto/fibercryptowallet/src/util/logging"
qtcore "github.com/therecipe/qt/core"
"github.com/therecipe/qt/qml"
//"time"
)

var logWalletModel = logging.MustGetLogger("Wallet Model")
Expand All @@ -18,7 +21,7 @@ const (

type ModelWallets struct {
qtcore.QAbstractListModel
addresses []*ModelAddresses
addresses []*ModelAddresses `property:"addresses"`
WalletEnv core.WalletEnv
_ func() `constructor:"init"`

Expand All @@ -32,7 +35,7 @@ type ModelWallets struct {

func (m *ModelWallets) init() {
m.SetRoles(map[int]*qtcore.QByteArray{
Name: qtcore.NewQByteArray2("name", -1),
QName: qtcore.NewQByteArray2("name", -1),
QAddresses: qtcore.NewQByteArray2("qaddresses", -1),
})

Expand All @@ -51,7 +54,14 @@ func (m *ModelWallets) init() {
}

m.WalletEnv = walletsEnvs[0]

//go func() {
// uptimeTicker := time.NewTicker(100000)

//for {
//<-uptimeTicker.C
m.loadModel()
//}
//}()
}

func (m *ModelWallets) rowCount(*qtcore.QModelIndex) int {
Expand Down Expand Up @@ -101,92 +111,97 @@ func (m *ModelWallets) cleanModel() {
}

func (m *ModelWallets) loadModel() {
m.BeginResetModel()
logWalletModel.Info("Loading Model")
m.SetLoading(true)
fullyLoad := true
aModels := make([]*ModelAddresses, 0)
wallets := m.WalletEnv.GetWalletSet().ListWallets()
if wallets == nil {
logWalletModel.WithError(nil).Warn("Couldn't load wallet")
return
}
for wallets.Next() {

addresses, err := wallets.Value().GetLoadedAddresses()
if err != nil {
logWalletModel.WithError(nil).Warn("Couldn't get loaded address")
go func() {

logWalletModel.Info("Loading Model")
m.SetLoading(true)
fullyLoad := true
aModels := make([]*ModelAddresses, 0)
wallets := m.WalletEnv.GetWalletSet().ListWallets()
if wallets == nil {
logWalletModel.WithError(nil).Warn("Couldn't load wallet")
return
}
ma := NewModelAddresses(nil)
ma.SetName(wallets.Value().GetLabel())
ma.SetId(wallets.Value().GetId())
oModels := make([]*ModelOutputs, 0)

for addresses.Next() {
a := addresses.Value()
outputs := a.GetCryptoAccount().ScanUnspentOutputs()
if outputs == nil {
logWalletModel.WithField("address", a.String()).Warn("Couldn't get unspent outputs")
fullyLoad = false
continue
for wallets.Next() {

addresses, err := wallets.Value().GetLoadedAddresses()
if err != nil {
logWalletModel.WithError(nil).Warn("Couldn't get loaded address")
return
}
mo := NewModelOutputs(nil)
mo.SetAddress(a.String())
qOutputs := make([]*QOutput, 0)

for outputs.Next() {
to := outputs.Value()
qo := NewQOutput(nil)
qo.SetOutputID(to.GetId())
val, err := to.GetCoins(coin.Sky)
if err != nil {
logWalletModel.WithError(nil).Warn("Couldn't get " + coin.Sky + " coins")
fullyLoad = false
continue
}
accuracy, err := util.AltcoinQuotient(coin.Sky)
if err != nil {
logWalletModel.WithError(err).Warn("Couldn't get " + coin.Sky + " coins quotient")
ma := NewModelAddresses(nil)
qml.QQmlEngine_SetObjectOwnership(ma, qml.QQmlEngine__CppOwnership)
ma.SetName(wallets.Value().GetLabel())
ma.SetId(wallets.Value().GetId())
oModels := make([]*ModelOutputs, 0)

for addresses.Next() {
a := addresses.Value()
outputs := a.GetCryptoAccount().ScanUnspentOutputs()
if outputs == nil {
logWalletModel.WithField("address", a.String()).Warn("Couldn't get unspent outputs")
fullyLoad = false
continue
}
coins := util.FormatCoins(val, accuracy)
qo.SetAddressSky(coins)
val, err = to.GetCoins(coin.CoinHoursTicker)
if err != nil {
logWalletModel.WithError(err).Warn("Couldn't get " + coin.CoinHoursTicker + " coins")
fullyLoad = false
continue
mo := NewModelOutputs(nil)
qml.QQmlEngine_SetObjectOwnership(mo, qml.QQmlEngine__CppOwnership)
mo.SetAddress(a.String())
qOutputs := make([]*QOutput, 0)

for outputs.Next() {
to := outputs.Value()
qo := NewQOutput(nil)
qml.QQmlEngine_SetObjectOwnership(qo, qml.QQmlEngine__CppOwnership)
qo.SetOutputID(to.GetId())
val, err := to.GetCoins(coin.Sky)
if err != nil {
logWalletModel.WithError(nil).Warn("Couldn't get " + coin.Sky + " coins")
fullyLoad = false
continue
}
accuracy, err := util.AltcoinQuotient(coin.Sky)
if err != nil {
logWalletModel.WithError(err).Warn("Couldn't get " + coin.Sky + " coins quotient")
fullyLoad = false
continue
}
coins := util.FormatCoins(val, accuracy)
qo.SetAddressSky(coins)
val, err = to.GetCoins(coin.CoinHoursTicker)
if err != nil {
logWalletModel.WithError(err).Warn("Couldn't get " + coin.CoinHoursTicker + " coins")
fullyLoad = false
continue
}
accuracy, err = util.AltcoinQuotient(coin.CoinHoursTicker)
if err != nil {
logWalletModel.WithError(err).Warn("Couldn't get " + coin.CoinHoursTicker + " coins quotient")
fullyLoad = false
continue
}
coinsH := util.FormatCoins(val, accuracy)
qo.SetAddressCoinHours(coinsH)
qOutputs = append(qOutputs, qo)
}
accuracy, err = util.AltcoinQuotient(coin.CoinHoursTicker)
if err != nil {
logWalletModel.WithError(err).Warn("Couldn't get " + coin.CoinHoursTicker + " coins quotient")
fullyLoad = false
continue
if len(qOutputs) != 0 {
mo.addOutputs(qOutputs)
oModels = append(oModels, mo)
}
coinsH := util.FormatCoins(val, accuracy)
qo.SetAddressCoinHours(coinsH)
qOutputs = append(qOutputs, qo)
}
if len(qOutputs) != 0 {
mo.addOutputs(qOutputs)
oModels = append(oModels, mo)
}
ma.addOutputs(oModels)
aModels = append(aModels, ma)
}
ma.addOutputs(oModels)
aModels = append(aModels, ma)
}
logWalletModel.Info("Model loaded")
m.addAddresses(aModels)
if fullyLoad {
m.SetLoading(false)
}
m.EndResetModel()
logWalletModel.Info("Model loaded")
m.addAddresses(aModels)
if fullyLoad {
m.SetLoading(false)
}
}()
}

func (m *ModelWallets) addAddresses(ma []*ModelAddresses) {
for _, modelAddresses := range ma {
for row, modelAddresses := range ma {
m.BeginInsertRows(qtcore.NewQModelIndex(), row, row)
find := false
for _, modelASet := range m.addresses {
if modelAddresses.Id() == modelASet.Id() {
Expand All @@ -198,6 +213,8 @@ func (m *ModelWallets) addAddresses(ma []*ModelAddresses) {
if !find {
m.addresses = append(m.addresses, modelAddresses)
}
//pIndex := m.Index(row, 0, qtcore.NewQModelIndex())
//m.DataChanged(pIndex, pIndex, []int{QName, QAddresses})
m.EndInsertRows()
}
m.insertRows(len(m.addresses), len(ma))
}

0 comments on commit 30b3243

Please sign in to comment.