-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(@desktop/wallet): Simplify the wallet networks service
fixes #12717
- Loading branch information
1 parent
666ba77
commit 5963be3
Showing
18 changed files
with
624 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/app/modules/main/wallet_section/networks/combined_item.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import strformat | ||
|
||
import ./item | ||
|
||
type | ||
CombinedItem* = object | ||
prod: Item | ||
test: Item | ||
layer: int | ||
|
||
proc initCombinedItem*( | ||
prod: Item, | ||
test: Item, | ||
layer: int | ||
): CombinedItem = | ||
result.prod = prod | ||
result.test = test | ||
result.layer = layer | ||
|
||
proc `$`*(self: CombinedItem): string = | ||
result = fmt"""CombinedItem( | ||
prod: {self.prod}, | ||
test: {self.test}, | ||
layer: {self.layer}, | ||
]""" | ||
|
||
proc getProd*(self: CombinedItem): Item = | ||
return self.prod | ||
|
||
proc getTest*(self: CombinedItem): Item = | ||
return self.test | ||
|
||
proc getLayer*(self: CombinedItem): int = | ||
return self.layer | ||
|
||
proc getShortName*(self: CombinedItem, areTestNetworksEnabled: bool): string = | ||
if areTestNetworksEnabled: | ||
return self.test.getShortName() | ||
else: | ||
return self.prod.getShortName() | ||
|
||
proc getChainId*(self: CombinedItem, areTestNetworksEnabled: bool): int = | ||
if areTestNetworksEnabled: | ||
return self.test.getChainId() | ||
else: | ||
return self.prod.getChainId() |
89 changes: 89 additions & 0 deletions
89
src/app/modules/main/wallet_section/networks/combined_model.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import NimQml, Tables, strutils, strformat | ||
|
||
import ./combined_item | ||
|
||
type | ||
ModelRole* {.pure.} = enum | ||
Prod = UserRole + 1, | ||
Test | ||
Layer | ||
|
||
QtObject: | ||
type | ||
CombinedModel* = ref object of QAbstractListModel | ||
items: seq[CombinedItem] | ||
|
||
proc delete(self: CombinedModel) = | ||
self.items = @[] | ||
self.QAbstractListModel.delete | ||
|
||
proc setup(self: CombinedModel) = | ||
self.QAbstractListModel.setup | ||
|
||
proc newCombinedModel*(): CombinedModel = | ||
new(result, delete) | ||
result.setup | ||
|
||
proc `$`*(self: CombinedModel): string = | ||
for i in 0 ..< self.items.len: | ||
result &= fmt"""[{i}]:({$self.items[i]})""" | ||
|
||
proc countChanged(self: CombinedModel) {.signal.} | ||
|
||
proc getCount(self: CombinedModel): int {.slot.} = | ||
self.items.len | ||
|
||
QtProperty[int] count: | ||
read = getCount | ||
notify = countChanged | ||
|
||
method rowCount*(self: CombinedModel, index: QModelIndex = nil): int = | ||
return self.items.len | ||
|
||
method roleNames(self: CombinedModel): Table[int, string] = | ||
{ | ||
ModelRole.Prod.int:"prod", | ||
ModelRole.Test.int:"test", | ||
ModelRole.Layer.int:"layer", | ||
}.toTable | ||
|
||
method data(self: CombinedModel, 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.Prod: | ||
result = newQVariant(item.getProd()) | ||
of ModelRole.Test: | ||
result = newQVariant(item.getTest()) | ||
of ModelRole.Layer: | ||
result = newQVariant(item.getLayer()) | ||
|
||
proc setItems*(self: CombinedModel, items: seq[CombinedItem]) = | ||
self.beginResetModel() | ||
self.items = items | ||
self.endResetModel() | ||
self.countChanged() | ||
|
||
proc getAllNetworksChainIds*(self: CombinedModel, areTestNetworksEnabled: bool): string = | ||
var networks: seq[int] = @[] | ||
for item in self.items: | ||
networks.add(item.getChainId(areTestNetworksEnabled)) | ||
return networks.join(":") | ||
|
||
proc getNetworkShortNames*(self: CombinedModel, preferredNetworks: string, areTestNetworksEnabled: bool): string = | ||
var networkString = "" | ||
let networks = preferredNetworks.split(":") | ||
for nw in networks: | ||
for item in self.items: | ||
if $item.getChainId(areTestNetworksEnabled) == nw: | ||
networkString = networkString & $item.getShortName(areTestNetworksEnabled) & ':' | ||
break | ||
return networkString | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.