fix(Syncing): Fixed changing device name (#9921)

This commit is contained in:
Igor Sirotin 2023-03-23 10:21:44 +11:00 committed by GitHub
parent 3b2a8f4253
commit 2d22f2f09a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 85 additions and 31 deletions

View File

@ -34,16 +34,20 @@ proc delete*(self: Controller) =
proc init*(self: Controller) =
self.events.on(SIGNAL_DEVICES_LOADED) do(e: Args):
var args = DevicesArg(e)
let args = DevicesArg(e)
self.delegate.onDevicesLoaded(args.devices)
self.events.on(SIGNAL_ERROR_LOADING_DEVICES) do(e: Args):
self.delegate.onDevicesLoadingErrored()
self.events.on(SIGNAL_UPDATE_DEVICE) do(e: Args):
var args = UpdateInstallationArgs(e)
let args = UpdateInstallationArgs(e)
self.delegate.updateOrAddDevice(args.installation)
self.events.on(SIGNAL_INSTALLATION_NAME_UPDATED) do(e: Args):
let args = UpdateInstallationNameArgs(e)
self.delegate.updateInstallationName(args.installationId, args.name)
self.events.on(SIGNAL_SHARED_KEYCARD_MODULE_USER_AUTHENTICATED) do(e: Args):
let args = SharedKeycarModuleArgs(e)
if args.uniqueIdentifier != UNIQUE_SYNCING_SECTION_ACCOUNTS_MODULE_AUTH_IDENTIFIER:
@ -51,11 +55,11 @@ proc init*(self: Controller) =
self.delegate.onUserAuthenticated(args.pin, args.password, args.keyUid)
self.events.on(SIGNAL_LOCAL_PAIRING_EVENT) do(e: Args):
var args = LocalPairingEventArgs(e)
let args = LocalPairingEventArgs(e)
self.delegate.onLocalPairingEvent(args.eventType, args.action, args.error)
self.events.on(SIGNAL_LOCAL_PAIRING_STATUS_UPDATE) do(e: Args):
var args = LocalPairingStatus(e)
let args = LocalPairingStatus(e)
self.delegate.onLocalPairingStatusUpdate(args)
@ -68,8 +72,8 @@ proc asyncLoadDevices*(self: Controller) =
proc getAllDevices*(self: Controller): seq[InstallationDto] =
return self.devicesService.getAllDevices()
proc setDeviceName*(self: Controller, name: string) =
self.devicesService.setDeviceName(name)
proc setInstallationName*(self: Controller, installationId: string, name: string) =
self.devicesService.setInstallationName(installationId, name)
proc syncAllDevices*(self: Controller) =
self.devicesService.syncAllDevices()

View File

@ -20,6 +20,9 @@ method getModuleAsVariant*(self: AccessInterface): QVariant {.base.} =
method updateOrAddDevice*(self: AccessInterface, installation: InstallationDto) {.base.} =
raise newException(ValueError, "No implementation available")
method updateInstallationName*(self: AccessInterface, installationId: string, name: string) {.base.} =
raise newException(ValueError, "No implementation available")
method viewDidLoad*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
@ -35,7 +38,7 @@ method onDevicesLoadingErrored*(self: AccessInterface) {.base.} =
method loadDevices*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method setDeviceName*(self: AccessInterface, name: string) {.base.} =
method setInstallationName*(self: AccessInterface, installationId: string, name: string) {.base.} =
raise newException(ValueError, "No implementation available")
method syncAllDevices*(self: AccessInterface) {.base.} =

View File

@ -109,10 +109,22 @@ QtObject:
if(i == -1):
return
let first = self.createIndex(i, 0, nil)
let last = self.createIndex(i, 0, nil)
let index = self.createIndex(i, 0, nil)
defer: index.delete
self.items[i].installation = installation
self.dataChanged(first, last, @[])
self.dataChanged(index, index, @[])
proc updateItemName*(self: Model, installationId: string, name: string) =
var i = self.findIndexByInstallationId(installationId)
if(i == -1):
return
let index = self.createIndex(i, 0, nil)
defer: index.delete
self.items[i].installation.metadata.name = name
self.dataChanged(index, index, @[ModelRole.Name.int])
proc getIsDeviceSetup*(self: Model, installationId: string): bool =
return anyIt(self.items, it.installation.id == installationId and it.name != "")

View File

@ -75,11 +75,8 @@ method viewDidLoad*(self: Module) =
method getModuleAsVariant*(self: Module): QVariant =
return self.viewVariant
method setDeviceName*(self: Module, name: string) =
self.controller.setDeviceName(name)
# in future if we start getting more meaningful response form the `status-go` part, we may
# move this call to the `onDeviceNameSet` slot and confirm change on the qml side that way.
self.view.deviceSetupChanged()
method setInstallationName*(self: Module, installationId: string, name: string) =
self.controller.setInstallationName(installationId, name)
method syncAllDevices*(self: Module) =
self.controller.syncAllDevices()
@ -97,6 +94,8 @@ method updateOrAddDevice*(self: Module, installation: InstallationDto) =
let item = initItem(installation, self.isMyDevice(installation.id))
self.view.model().addItem(item)
method updateInstallationName*(self: Module, installationId: string, name: string) =
self.view.model().updateItemName(installationId, name)
method authenticateUser*(self: Module, keyUid: string) =
self.controller.authenticateUser(keyUid)

View File

@ -76,8 +76,8 @@ QtObject:
proc loadDevices*(self: View) {.slot.} =
self.delegate.loadDevices()
proc setName*(self: View, deviceName: string) {.slot.} =
self.delegate.setDeviceName(deviceName)
proc setInstallationName*(self: View, installationId: string, name: string) {.slot.} =
self.delegate.setInstallationName(installationId, name)
proc syncAll*(self: View) {.slot.} =
self.delegate.syncAllDevices()

View File

@ -16,7 +16,7 @@ type InstallationMetadata* = object
deviceType*: string
fcmToken*: string
type InstallationDto* = object
type InstallationDto* = ref object
id*: string
identity*: string
version*: int

View File

@ -33,6 +33,11 @@ type
UpdateInstallationArgs* = ref object of Args
installation*: InstallationDto
type
UpdateInstallationNameArgs* = ref object of Args
installationId*: string
name*: string
type
DevicesArg* = ref object of Args
devices*: seq[InstallationDto]
@ -43,6 +48,7 @@ const SIGNAL_DEVICES_LOADED* = "devicesLoaded"
const SIGNAL_ERROR_LOADING_DEVICES* = "devicesErrorLoading"
const SIGNAL_LOCAL_PAIRING_EVENT* = "localPairingEvent"
const SIGNAL_LOCAL_PAIRING_STATUS_UPDATE* = "localPairingStatusUpdate"
const SIGNAL_INSTALLATION_NAME_UPDATED* = "installationNameUpdated"
QtObject:
type Service* = ref object of QObject
@ -105,8 +111,7 @@ QtObject:
let installations = map(rpcResponse.result.getElems(), proc(x: JsonNode): InstallationDto = x.toInstallationDto())
self.events.emit(SIGNAL_DEVICES_LOADED, DevicesArg(devices: installations))
except Exception as e:
let errDesription = e.msg
error "error loading devices: ", errDesription
error "error loading devices: ", desription = e.msg
self.events.emit(SIGNAL_ERROR_LOADING_DEVICES, Args())
proc getAllDevices*(self: Service): seq[InstallationDto] =
@ -114,13 +119,20 @@ QtObject:
let response = status_installations.getOurInstallations()
return map(response.result.getElems(), proc(x: JsonNode): InstallationDto = x.toInstallationDto())
except Exception as e:
let errDesription = e.msg
error "error: ", errDesription
error "error: ", desription = e.msg
proc setDeviceName*(self: Service, name: string) =
let installationId = self.settingsService.getInstallationId()
proc setInstallationName*(self: Service, installationId: string, name: string) =
# Once we get more info from `status-go` we may emit success/failed signal from here.
discard status_installations.setInstallationMetadata(installationId, name, hostOs)
try:
let response = status_installations.setInstallationName(installationId, name)
if response.error != nil:
let e = Json.decode($response.error, RpcError)
error "error: ", errorDescription = e.message
return
let data = UpdateInstallationNameArgs(installationId: installationId, name: name)
self.events.emit(SIGNAL_INSTALLATION_NAME_UPDATED, data)
except Exception as e:
error "error: ", desription = e.msg
proc syncAllDevices*(self: Service) =
let preferredName = self.settingsService.getPreferredName()

View File

@ -12,6 +12,11 @@ proc setInstallationMetadata*(installationId: string, deviceName: string, device
}]
result = callPrivateRPC("setInstallationMetadata".prefix, payload)
proc setInstallationName*(installationId: string, name: string):
RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* [installationId, name]
result = callPrivateRPC("setInstallationName".prefix, payload)
proc getOurInstallations*(): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* []
result = callPrivateRPC("getOurInstallations".prefix, payload)

View File

@ -6,6 +6,7 @@ import QtQml.Models 2.14
import StatusQ.Core 0.1
import StatusQ.Core.Theme 0.1
import StatusQ.Controls 0.1
import StatusQ.Controls.Validators 0.1
import StatusQ.Components 0.1
import StatusQ.Popups 0.1
import StatusQ.Popups.Dialog 0.1
@ -30,6 +31,13 @@ StatusDialog {
QtObject {
id: d
property string deviceName: ""
function saveNewName() {
if (!nameInput.valid)
return
root.devicesStore.setInstallationName(root.deviceModel.installationId, nameInput.text.trim())
root.close()
}
}
onOpened: {
@ -41,6 +49,18 @@ StatusDialog {
id: nameInput
Layout.fillWidth: true
label: qsTr("Device name")
validators: [
StatusValidator {
errorMessage: qsTr("Device name can not be empty")
validate: (value) => {
return value.trim() !== ""
}
}
]
Keys.onReturnPressed: {
d.saveNewName()
}
}
}
@ -48,10 +68,9 @@ StatusDialog {
rightButtons: ObjectModel {
StatusButton {
text: qsTr("Done")
enabled: nameInput.text !== ""
enabled: nameInput.valid
onClicked : {
root.devicesStore.setName(nameInput.text.trim())
root.close();
d.saveNewName()
}
}
}

View File

@ -18,8 +18,8 @@ QtObject {
return root.devicesModule.loadDevices()
}
function setName(name) {
return root.devicesModule.setName(name)
function setInstallationName(installationId, name) {
return root.devicesModule.setInstallationName(installationId, name)
}
function syncAll() {