Apply polymorphism to signals, instead of using json strings
This commit is contained in:
parent
110bd25c06
commit
df1199060d
|
@ -1,7 +1,7 @@
|
|||
import NimQml
|
||||
import ../../status/chat as status_chat
|
||||
import chatView
|
||||
import ../signals/signalSubscriber
|
||||
import ../signals/types
|
||||
|
||||
var sendMessage = proc (msg: string): string =
|
||||
echo "sending public message"
|
||||
|
@ -40,5 +40,6 @@ proc load*(self: ChatController): seq[string] =
|
|||
result = @["test"]
|
||||
|
||||
method onSignal(self: ChatController, data: Signal) =
|
||||
echo "Received a signal in the chat module"
|
||||
# result.view.pushMessage(signal)
|
||||
var chatSignal = cast[ChatSignal](data)
|
||||
for message in chatSignal.messages:
|
||||
self.view.pushMessage(message)
|
||||
|
|
|
@ -2,7 +2,7 @@ import NimQml
|
|||
import ../../status/types as status_types
|
||||
import tables
|
||||
import json
|
||||
import signalSubscriber
|
||||
import types
|
||||
import messages
|
||||
|
||||
QtObject:
|
||||
|
@ -21,7 +21,6 @@ QtObject:
|
|||
result.setup()
|
||||
result.variant = newQVariant(result)
|
||||
|
||||
|
||||
proc setup(self: SignalsController) =
|
||||
self.QObject.setup
|
||||
|
||||
|
@ -37,15 +36,12 @@ QtObject:
|
|||
proc processSignal(self: SignalsController) =
|
||||
let jsonSignal = (self.statusSignal).parseJson
|
||||
let signalType = $jsonSignal["type"].getStr
|
||||
# TODO: ideally the signal would receive an object
|
||||
# formatted for easier usage so the controllers dont
|
||||
# have to parse the signal themselves
|
||||
|
||||
case signalType:
|
||||
of "messages.new":
|
||||
self.signalSubscribers[SignalType.Message].onSignal(messages.fromEvent(jsonSignal))
|
||||
of "wallet":
|
||||
var wMsg = WalletMessage(content: $jsonSignal)
|
||||
self.signalSubscribers[SignalType.Wallet].onSignal(wMsg)
|
||||
self.signalSubscribers[SignalType.Wallet].onSignal(WalletSignal(content: $jsonSignal))
|
||||
else:
|
||||
# TODO: log error?
|
||||
discard
|
||||
|
|
|
@ -1,5 +1,31 @@
|
|||
import json
|
||||
import signalSubscriber
|
||||
import types
|
||||
|
||||
proc fromEvent*(event: JsonNode): Signal =
|
||||
result = Message()
|
||||
var signal:ChatSignal = ChatSignal()
|
||||
signal.messages = @[]
|
||||
for jsonMsg in event["event"]["messages"]:
|
||||
let msg = Message(
|
||||
alias: jsonMsg["alias"].getStr,
|
||||
chatId: jsonMsg["chatId"].getStr,
|
||||
clock: $jsonMsg["clock"].getInt,
|
||||
contentType: jsonMsg["contentType"].getInt,
|
||||
ensName: jsonMsg["ensName"].getStr,
|
||||
fromAuthor: jsonMsg["from"].getStr,
|
||||
id: jsonMsg["identicon"].getStr,
|
||||
identicon: jsonMsg["identicon"].getStr,
|
||||
lineCount: jsonMsg["lineCount"].getInt,
|
||||
localChatId: jsonMsg["localChatId"].getStr,
|
||||
messageType: jsonMsg["messageType"].getStr,
|
||||
replace: jsonMsg["replace"].getStr,
|
||||
responseTo: jsonMsg["responseTo"].getStr,
|
||||
rtl: jsonMsg["rtl"].getBool,
|
||||
seen: jsonMsg["seen"].getBool,
|
||||
text: jsonMsg["text"].getStr,
|
||||
timestamp: $jsonMsg["timestamp"].getInt,
|
||||
whisperTimestamp: $jsonMsg["whisperTimestamp"].getInt
|
||||
)
|
||||
|
||||
signal.messages.add(msg)
|
||||
|
||||
result = signal
|
|
@ -1,35 +0,0 @@
|
|||
type SignalSubscriber* = ref object of RootObj
|
||||
|
||||
type Signal* = ref object of RootObj
|
||||
|
||||
type WalletMessage* = ref object of Signal
|
||||
content*: string
|
||||
|
||||
type Message* = ref object of Signal
|
||||
alias: string
|
||||
chatId: string
|
||||
clock: uint
|
||||
# commandParameters: # ???
|
||||
contentType: uint # ???
|
||||
ensName: string # ???
|
||||
fromAuthor: string
|
||||
id: string
|
||||
identicon: string
|
||||
lineCount: uint
|
||||
localChatId: string
|
||||
messageType: string # ???
|
||||
# parsedText: # ???
|
||||
# quotedMessage: # ???
|
||||
replace: string # ???
|
||||
responseTo: string # ???
|
||||
rtl: bool # ???
|
||||
seen: bool
|
||||
# sticker: # ???
|
||||
text: string
|
||||
timestamp: uint
|
||||
whisperTimestamp: uint
|
||||
|
||||
|
||||
# Override this method
|
||||
method onSignal*(self: SignalSubscriber, data: Signal) {.base.} =
|
||||
echo "Received a signal" # TODO: log signal received
|
|
@ -0,0 +1,38 @@
|
|||
type SignalSubscriber* = ref object of RootObj
|
||||
|
||||
type Signal* = ref object of RootObj
|
||||
|
||||
type WalletSignal* = ref object of Signal
|
||||
content*: string
|
||||
|
||||
type Message* = object
|
||||
alias*: string
|
||||
chatId*: string
|
||||
clock*: string
|
||||
# commandParameters*: # ???
|
||||
contentType*: int # ???
|
||||
ensName*: string # ???
|
||||
fromAuthor*: string
|
||||
id*: string
|
||||
identicon*: string
|
||||
lineCount*: int
|
||||
localChatId*: string
|
||||
messageType*: string # ???
|
||||
# parsedText: # ???
|
||||
# quotedMessage: # ???
|
||||
replace*: string # ???
|
||||
responseTo*: string # ???
|
||||
rtl*: bool # ???
|
||||
seen*: bool
|
||||
# sticker: # ???
|
||||
text*: string
|
||||
timestamp*: string
|
||||
whisperTimestamp*: string
|
||||
|
||||
|
||||
type ChatSignal* = ref object of Signal
|
||||
messages*: seq[Message]
|
||||
|
||||
# Override this method
|
||||
method onSignal*(self: SignalSubscriber, data: Signal) {.base.} =
|
||||
echo "Received a signal" # TODO: log signal received
|
|
@ -4,7 +4,7 @@ import strutils
|
|||
|
||||
import walletView
|
||||
import ../../status/wallet as status_wallet
|
||||
import ../signals/signalSubscriber
|
||||
import ../signals/types
|
||||
|
||||
type WalletController* = ref object of SignalSubscriber
|
||||
view*: WalletView
|
||||
|
@ -41,5 +41,5 @@ proc init*(self: WalletController) =
|
|||
self.view.addAssetToList("Ethereum", symbol, fmt"{eth_value:.6}", "$" & fmt"{usd_balance:.6}", fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg")
|
||||
|
||||
method onSignal(self: WalletController, data: Signal) =
|
||||
var msg = cast[WalletMessage](data)
|
||||
var msg = cast[WalletSignal](data)
|
||||
self.view.setLastMessage(msg.content)
|
Loading…
Reference in New Issue