move 'models' to status lib for clarity

move 'models' to status lib for clarity

move chat model

update login controller

update wallet controller

update onboarding and profile

update
This commit is contained in:
Iuri Matias 2020-05-29 15:54:35 -04:00
parent 73069255cd
commit 2bc165bbc2
16 changed files with 140 additions and 133 deletions

View File

@ -6,21 +6,20 @@ import ../../status/libstatus/types as status_types
import views/channels_list
import view
import chronicles
import ../../status/status
logScope:
topics = "chat-controller"
type ChatController* = ref object of SignalSubscriber
view*: ChatsView
model*: ChatModel
status*: Status
variant*: QVariant
appEvents*: EventEmitter
proc newController*(appEvents: EventEmitter): ChatController =
proc newController*(status: Status): ChatController =
result = ChatController()
result.appEvents = appEvents
result.model = newChatModel()
result.view = newChatsView(result.model)
result.status = status
result.view = newChatsView(status)
result.variant = newQVariant(result.view)
proc delete*(self: ChatController) =
@ -28,25 +27,25 @@ proc delete*(self: ChatController) =
delete self.variant
proc init*(self: ChatController) =
self.model.events.on("messageSent") do(e: Args):
self.status.events.on("messageSent") do(e: Args):
var sentMessage = MsgArgs(e)
var chatMessage = sentMessage.payload.toChatMessage()
chatMessage.message = sentMessage.message
chatMessage.isCurrentUser = true
self.view.pushMessage(sentMessage.chatId, chatMessage)
self.model.events.on("channelJoined") do(e: Args):
self.status.events.on("channelJoined") do(e: Args):
var channelMessage = ChannelArgs(e)
let chatItem = newChatItem(id = channelMessage.channel, channelMessage.chatTypeInt)
discard self.view.chats.addChatItemToList(chatItem)
self.model.events.on("channelLeft") do(e: Args):
self.status.events.on("channelLeft") do(e: Args):
discard self.view.chats.removeChatItemFromList(self.view.activeChannel)
self.model.events.on("activeChannelChanged") do(e: Args):
self.status.events.on("activeChannelChanged") do(e: Args):
self.view.setActiveChannel(ChannelArgs(e).channel)
self.model.load()
self.status.chat.load()
self.view.setActiveChannelByIndex(0)
proc handleMessage(self: ChatController, data: Signal) =

View File

@ -1,14 +1,17 @@
import NimQml
import Tables
import views/channels_list
import views/message_list
import ../../signals/types
import ../../status/chat
import ../../status/status
import views/channels_list
import views/message_list
QtObject:
type
ChatsView* = ref object of QAbstractListModel
model: ChatModel
status: Status
chats*: ChannelsList
callResult: string
messageList: Table[string, ChatMessageList]
@ -18,10 +21,10 @@ QtObject:
proc delete(self: ChatsView) = self.QAbstractListModel.delete
proc newChatsView*(model: ChatModel): ChatsView =
proc newChatsView*(status: Status): ChatsView =
new(result, delete)
result.model = model
result.chats = newChannelsList(result.model)
result.status = status
result.chats = newChannelsList()
result.activeChannel = ""
result.messageList = initTable[string, ChatMessageList]()
result.setup()
@ -84,13 +87,13 @@ QtObject:
discard self.chats.addChatItemToList(chatItem)
proc sendMessage*(self: ChatsView, message: string) {.slot.} =
discard self.model.sendMessage(self.activeChannel, message)
discard self.status.chat.sendMessage(self.activeChannel, message)
proc joinChat*(self: ChatsView, channel: string, chatTypeInt: int): int {.slot.} =
self.model.join(channel, ChatType(chatTypeInt))
self.status.chat.join(channel, ChatType(chatTypeInt))
proc leaveActiveChat*(self: ChatsView) {.slot.} =
self.model.leave(self.activeChannel)
self.status.chat.leave(self.activeChannel)
proc updateChat*(self: ChatsView, chat: ChatItem) =
self.chats.updateChat(chat)

View File

@ -36,17 +36,15 @@ type
QtObject:
type
ChannelsList* = ref object of QAbstractListModel
model*: ChatModel
chats*: seq[ChatItem]
proc setup(self: ChannelsList) = self.QAbstractListModel.setup
proc delete(self: ChannelsList) = self.QAbstractListModel.delete
proc newChannelsList*(model: ChatModel): ChannelsList =
proc newChannelsList*(): ChannelsList =
new(result, delete)
result.chats = @[]
result.model = model
result.setup()
method rowCount(self: ChannelsList, index: QModelIndex = nil): int = self.chats.len

View File

@ -1,24 +1,25 @@
import NimQml
import ../../status/libstatus/types as status_types
import ../../signals/types
import eventemitter
import view
import ../../status/accounts as AccountModel
import chronicles
import options
import std/wrapnils
import eventemitter
import ../../status/libstatus/types as status_types
import ../../signals/types
# import ../../status/accounts as AccountModel
import ../../status/status
import view
type LoginController* = ref object of SignalSubscriber
status*: Status
view*: LoginView
variant*: QVariant
appEvents*: EventEmitter
model: AccountModel
proc newController*(appEvents: EventEmitter): LoginController =
proc newController*(status: Status): LoginController =
result = LoginController()
result.appEvents = appEvents
result.model = newAccountModel()
result.view = newLoginView(result.model)
result.status = status
result.view = newLoginView(status)
result.variant = newQVariant(result.view)
proc delete*(self: LoginController) =
@ -26,18 +27,18 @@ proc delete*(self: LoginController) =
delete self.variant
proc init*(self: LoginController, nodeAccounts: seq[NodeAccount]) =
self.model.nodeAccounts = nodeAccounts
self.status.accounts.nodeAccounts = nodeAccounts
for nodeAccount in nodeAccounts:
self.view.addAccountToList(nodeAccount)
proc handleNodeLogin(self: LoginController, data: Signal) =
var response = NodeSignal(data)
self.view.setLastLoginResponse($response.event.toJson)
if ?.response.event.error == "" and self.model.currentAccount != nil:
self.appEvents.emit("login", AccountArgs(account: self.model.currentAccount))
if ?.response.event.error == "" and self.status.accounts.currentAccount != nil:
self.status.events.emit("login", AccountArgs(account: self.status.accounts.currentAccount))
method onSignal(self: LoginController, data: Signal) =
if data.signalType == SignalType.NodeLogin:
self.handleNodeLogin(data)
else:
discard
discard

View File

@ -10,6 +10,8 @@ import json_serialization
import core
import ../../status/accounts as AccountModel
import ../../status/status
type
AccountRoles {.pure.} = enum
Username = UserRole + 1,
@ -18,9 +20,9 @@ type
QtObject:
type LoginView* = ref object of QAbstractListModel
status: Status
accounts: seq[NodeAccount]
lastLoginResponse: string
model*: AccountModel
proc setup(self: LoginView) =
self.QAbstractListModel.setup
@ -29,11 +31,11 @@ QtObject:
self.QAbstractListModel.delete
self.accounts = @[]
proc newLoginView*(model: AccountModel): LoginView =
proc newLoginView*(status: Status): LoginView =
new(result, delete)
result.accounts = @[]
result.lastLoginResponse = ""
result.model = model
result.status = status
result.setup
proc addAccountToList*(self: LoginView, account: NodeAccount) =
@ -64,7 +66,7 @@ QtObject:
proc login(self: LoginView, selectedAccountIndex: int, password: string): string {.slot.} =
try:
result = self.model.login(selectedAccountIndex, password).toJson
result = self.status.accounts.login(selectedAccountIndex, password).toJson
except:
let
e = getCurrentException()

View File

@ -5,20 +5,20 @@ import ../../signals/types
import ../../status/node
import view
import ../../status/status
logScope:
topics = "node"
type NodeController* = ref object of SignalSubscriber
model*: NodeModel
status*: Status
view*: NodeView
variant*: QVariant
appEvents*: EventEmitter
proc newController*(appEvents: EventEmitter): NodeController =
proc newController*(status: Status): NodeController =
result = NodeController()
result.appEvents = appEvents
result.model = newNodeModel()
result.view = newNodeView(result.model)
result.status = status
result.view = newNodeView(status)
result.variant = newQVariant(result.view)
proc delete*(self: NodeController) =

View File

@ -1,18 +1,19 @@
import NimQml
import ../../status/node
import ../../status/status
QtObject:
type NodeView* = ref object of QObject
model: NodeModel
status*: Status
callResult: string
lastMessage*: string
proc setup(self: NodeView) =
self.QObject.setup
proc newNodeView*(model: NodeModel): NodeView =
proc newNodeView*(status: Status): NodeView =
new(result)
result.model = model
result.status = status
result.callResult = "Use this tool to call JSONRPC methods"
result.lastMessage = ""
result.setup
@ -38,7 +39,7 @@ QtObject:
notify = callResultChanged
proc onSend*(self: NodeView, inputJSON: string) {.slot.} =
self.setCallResult(self.model.sendRPCMessageRaw(inputJSON))
self.setCallResult(self.status.node.sendRPCMessageRaw(inputJSON))
echo "Done!: ", self.callResult
proc onMessage*(self: NodeView, message: string) {.slot.} =

View File

@ -8,17 +8,17 @@ import chronicles
import ../../signals/types
import std/wrapnils
import ../../status/status
type OnboardingController* = ref object of SignalSubscriber
view*: OnboardingView
variant*: QVariant
appEvents*: EventEmitter
model: AccountModel
status: Status
proc newController*(appEvents: EventEmitter): OnboardingController =
proc newController*(status: Status): OnboardingController =
result = OnboardingController()
result.appEvents = appEvents
result.model = newAccountModel()
result.view = newOnboardingView(result.model)
result.status = status
result.view = newOnboardingView(status)
result.variant = newQVariant(result.view)
proc delete*(self: OnboardingController) =
@ -26,15 +26,15 @@ proc delete*(self: OnboardingController) =
delete self.variant
proc init*(self: OnboardingController) =
let accounts = self.model.generateAddresses()
let accounts = self.status.accounts.generateAddresses()
for account in accounts:
self.view.addAccountToList(account)
proc handleNodeLogin(self: OnboardingController, data: Signal) =
var response = NodeSignal(data)
self.view.setLastLoginResponse($response.event.toJson)
if ?.response.event.error == "" and self.model.currentAccount != nil:
self.appEvents.emit("login", AccountArgs(account: self.model.currentAccount))
if ?.response.event.error == "" and self.status.accounts.currentAccount != nil:
self.status.events.emit("login", AccountArgs(account: self.status.accounts.currentAccount))
method onSignal(self: OnboardingController, data: Signal) =
if data.signalType == SignalType.NodeLogin:

View File

@ -7,6 +7,7 @@ import ../../signals/types
import strformat
import json_serialization
import ../../status/accounts as AccountModel
import ../../status/status
type
AccountRoles {.pure.} = enum
@ -18,7 +19,7 @@ QtObject:
type OnboardingView* = ref object of QAbstractListModel
accounts*: seq[GeneratedAccount]
lastLoginResponse: string
model*: AccountModel
status*: Status
proc setup(self: OnboardingView) =
self.QAbstractListModel.setup
@ -27,11 +28,11 @@ QtObject:
self.QAbstractListModel.delete
self.accounts = @[]
proc newOnboardingView*(model: AccountModel): OnboardingView =
proc newOnboardingView*(status: Status): OnboardingView =
new(result, delete)
result.accounts = @[]
result.lastLoginResponse = ""
result.model = model
result.status = status
result.setup
proc addAccountToList*(self: OnboardingView, account: GeneratedAccount) =
@ -62,7 +63,7 @@ QtObject:
proc storeAccountAndLogin(self: OnboardingView, selectedAccountIndex: int, password: string): string {.slot.} =
try:
result = self.model.storeAccountAndLogin(selectedAccountIndex, password).toJson
result = self.status.accounts.storeAccountAndLogin(selectedAccountIndex, password).toJson
except:
let
e = getCurrentException()

View File

@ -2,21 +2,23 @@ import NimQml
import eventemitter
import strformat
import json
import "../../status/libstatus/core" as status
import ../../status/libstatus/mailservers as status_mailservers
import ../../signals/types
import view
import "../../status/libstatus/types" as status_types
import ../../status/profile
import ../../status/status
type ProfileController* = object
view*: ProfileView
variant*: QVariant
appEvents*: EventEmitter
status*: Status
proc newController*(appEvents: EventEmitter): ProfileController =
proc newController*(status: Status): ProfileController =
result = ProfileController()
result.appEvents = appEvents
result.status = status
result.view = newProfileView()
result.variant = newQVariant(result.view)

View File

@ -9,17 +9,17 @@ import ../../status/libstatus/wallet as status_wallet
import ../../status/wallet
import ../../signals/types
import ../../status/status
type WalletController* = ref object of SignalSubscriber
model: WalletModel
status: Status
view*: WalletView
variant*: QVariant
appEvents*: EventEmitter
proc newController*(appEvents: EventEmitter): WalletController =
proc newController*(status: Status): WalletController =
result = WalletController()
result.appEvents = appEvents
result.model = newWalletModel()
result.view = newWalletView(result.model)
result.status = status
result.view = newWalletView(status)
result.variant = newQVariant(result.view)
proc delete*(self: WalletController) =
@ -28,8 +28,8 @@ proc delete*(self: WalletController) =
proc init*(self: WalletController) =
var symbol = "ETH"
var eth_balance = self.model.getEthBalance("0x0000000000000000000000000000000000000000")
var usd_balance = self.model.getFiatValue(eth_balance, symbol, "USD")
var eth_balance = self.status.wallet.getEthBalance("0x0000000000000000000000000000000000000000")
var usd_balance = self.status.wallet.getFiatValue(eth_balance, symbol, "USD")
var asset = Asset(name:"Ethereum", symbol: symbol, value: fmt"{eth_balance:.6}", fiatValue: "$" & fmt"{usd_balance:.6}", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg")
self.view.addAssetToList(asset)

View File

@ -2,13 +2,14 @@ import NimQml
import Tables
import views/asset_list
import ../../status/wallet
import ../../status/status
QtObject:
type
WalletView* = ref object of QAbstractListModel
assets*: AssetsList
defaultAccount: string
model: WalletModel
status: Status
proc delete(self: WalletView) =
self.QAbstractListModel.delete
@ -16,9 +17,9 @@ QtObject:
proc setup(self: WalletView) =
self.QAbstractListModel.setup
proc newWalletView*(model: WalletModel): WalletView =
proc newWalletView*(status: Status): WalletView =
new(result, delete)
result.model = model
result.status = status
result.assets = newAssetsList()
result.setup
@ -32,7 +33,7 @@ QtObject:
read = getAssetsList
proc onSendTransaction*(self: WalletView, from_value: string, to: string, value: string, password: string): string {.slot.} =
result = self.model.sendTransaction(from_value, to, value, password)
result = self.status.wallet.sendTransaction(from_value, to, value, password)
proc setDefaultAccount*(self: WalletView, account: string) =
self.defaultAccount = account

View File

@ -1,21 +1,19 @@
import NimQml
import eventemitter
import chronicles
import json_serialization
import app/chat/core as chat
import app/wallet/core as wallet
import app/node/core as node
import app/profile/core as profile
import signals/core as signals
import app/onboarding/core as onboarding
import app/login/core as login
import state
import status/libstatus/accounts as status_accounts
import status/libstatus/core as status_core
import status/libstatus/types as types
import status/libstatus/libstatus
import signals/core as signals
import status/libstatus/types
import state
import eventemitter
import json_serialization
import status/libstatus/libstatus
import status/status as statuslib
var signalsQObjPointer: pointer
@ -23,11 +21,12 @@ logScope:
topics = "main"
proc mainProc() =
let nodeAccounts = status_accounts.initNodeAccounts()
let status = statuslib.newStatusInstance()
let nodeAccounts = status.initNodeAccounts()
let app = newQApplication()
let engine = newQQmlApplicationEngine()
let signalController = signals.newController(app)
let appEvents = createEventEmitter()
defer: # Defer will run this just before mainProc() function ends
app.delete()
@ -38,31 +37,28 @@ proc mainProc() =
# from the non-closure callback passed to `libstatus.setSignalEventCallback`
signalsQObjPointer = cast[pointer](signalController.vptr)
var appState = state.newAppState()
debug "Application State", title=appState.title
var wallet = wallet.newController(appEvents)
var wallet = wallet.newController(status)
engine.setRootContextProperty("assetsModel", wallet.variant)
var chat = chat.newController(appEvents)
var chat = chat.newController(status)
engine.setRootContextProperty("chatsModel", chat.variant)
var node = node.newController(appEvents)
var node = node.newController(status)
node.init()
engine.setRootContextProperty("nodeModel", node.variant)
var profile = profile.newController(appEvents)
var profile = profile.newController(status)
engine.setRootContextProperty("profileModel", profile.variant)
appEvents.once("login") do(a: Args):
status.events.once("login") do(a: Args):
var args = AccountArgs(a)
status_core.startMessenger()
status.startMessenger()
chat.init()
wallet.init()
profile.init(args.account)
var login = login.newController(appEvents)
var onboarding = onboarding.newController(appEvents)
var login = login.newController(status)
var onboarding = onboarding.newController(status)
# TODO: replace this with routing
let showLogin = nodeAccounts.len > 0

View File

@ -1,28 +0,0 @@
##########################################################
## warning: this file is still very much in flux
##########################################################
type
ChatChannel = object
name*: string
Subscriber* = proc ()
type AppState* = ref object
title*: string
channels*: seq[ChatChannel]
subscribers*: seq[Subscriber]
proc newAppState*(): AppState =
result = AppState(title: "hello")
proc subscribe*(self: AppState, subscriber: Subscriber) =
self.subscribers.add(subscriber)
proc dispatch*(self: AppState) =
for subscriber in self.subscribers:
subscriber()
proc addChannel*(self: AppState, name: string) =
self.channels.add(ChatChannel(name: name))
self.dispatch()

View File

@ -28,9 +28,9 @@ type
channels*: HashSet[string]
filters*: Table[string, string]
proc newChatModel*(): ChatModel =
proc newChatModel*(events: EventEmitter): ChatModel =
result = ChatModel()
result.events = createEventEmitter()
result.events = events
result.channels = initHashSet[string]()
result.filters = initTable[string, string]()

31
src/status/status.nim Normal file
View File

@ -0,0 +1,31 @@
import eventemitter
import libstatus/types
import libstatus/accounts as libstatus_accounts
import libstatus/core as libstatus_core
import chat as chat
import accounts as accounts
import wallet as wallet
import node as node
type Status* = ref object
events*: EventEmitter
chat*: ChatModel
accounts*: AccountModel
wallet*: WalletModel
node*: NodeModel
proc newStatusInstance*(): Status =
result = Status()
result.events = createEventEmitter()
result.chat = chat.newChatModel(result.events)
result.accounts = accounts.newAccountModel()
result.wallet = wallet.newWalletModel()
result.node = node.newNodeModel()
proc initNodeAccounts*(self: Status): seq[NodeAccount] =
libstatus_accounts.initNodeAccounts()
proc startMessenger*(self: Status) =
libstatus_core.startMessenger()