refactor(@desktop/general): identified user profile as new instance

This commit is contained in:
Sale Djenic 2021-10-29 19:27:14 +02:00 committed by Jakub
parent db45130a1e
commit 188d38be39
3 changed files with 193 additions and 12 deletions

View File

@ -33,7 +33,7 @@ import ../core/global_singleton
# provider/subscriber principe, similar we should have SettingsService.
import ../../app_service/[main]
import eventemitter
import status/[fleet, settings]
import status/[fleet]
import ../profile/core as profile
import status/types/[account, setting]
#################################################
@ -90,6 +90,7 @@ type
localAppSettingsVariant: QVariant
localAccountSettingsVariant: QVariant
localAccountSensitiveSettingsVariant: QVariant
userProfileVariant: QVariant
# Modules
startupModule: startup_module.AccessInterface
@ -103,7 +104,9 @@ type
#################################################
# Forward declaration section
proc load*(self: AppController)
proc load(self: AppController)
proc buildAndRegisterLocalAccountSensitiveSettings(self: AppController)
proc buildAndRegisterUserProfile(self: AppController)
# Startup Module Delegate Interface
proc startupDidLoad*(self: AppController)
@ -153,6 +156,8 @@ proc newAppController*(appService: AppService): AppController =
result.localAppSettingsVariant = newQVariant(singletonInstance.localAppSettings)
result.localAccountSettingsVariant = newQVariant(singletonInstance.localAccountSettings)
result.localAccountSensitiveSettingsVariant = newQVariant(singletonInstance.localAccountSensitiveSettings)
result.userProfileVariant = newQVariant(singletonInstance.userProfile)
# Modules
result.startupModule = startup_module.newModule[AppController](
result,
@ -221,6 +226,7 @@ proc delete*(self: AppController) =
self.localAppSettingsVariant.delete
self.localAccountSettingsVariant.delete
self.localAccountSensitiveSettingsVariant.delete
self.userProfileVariant.delete
self.accountsService.delete
self.chatService.delete
@ -261,15 +267,8 @@ proc start*(self: AppController) =
self.startupModule.load()
proc load*(self: AppController) =
#################################################
# Once SettingService gets added, `pubKey` should be fetched from there, instead the following line:
let pubKey = self.appService.status.settings.getSetting[:string](Setting.PublicKey, "0x0")
singletonInstance.localAccountSensitiveSettings.setFileName(pubKey)
singletonInstance.engine.setRootContextProperty("localAccountSensitiveSettings", self.localAccountSensitiveSettingsVariant)
#################################################
self.languageService.init()
proc load(self: AppController) =
# init services which are available only if a user is logged in
self.settingService.init()
self.contactsService.init()
self.chatService.init()
@ -282,6 +281,12 @@ proc load*(self: AppController) =
self.providerService.init()
self.walletAccountService.init()
self.transactionService.init()
# other global instances
self.buildAndRegisterLocalAccountSensitiveSettings()
self.buildAndRegisterUserProfile()
# load main module
self.mainModule.load()
proc userLoggedIn*(self: AppController) =
@ -292,3 +297,26 @@ proc userLoggedIn*(self: AppController) =
self.appService.status.events.emit("loginCompleted", AccountArgs(account: account))
#################################################
self.load()
proc buildAndRegisterLocalAccountSensitiveSettings(self: AppController) =
var pubKey = self.settingsService.getPubKey()
singletonInstance.localAccountSensitiveSettings.setFileName(pubKey)
singletonInstance.engine.setRootContextProperty("localAccountSensitiveSettings", self.localAccountSensitiveSettingsVariant)
proc buildAndRegisterUserProfile(self: AppController) =
let loggedInAccount = self.accountsService.getLoggedInAccount()
let pubKey = self.settingsService.getPubKey()
let sendUserStatus = self.settingsService.getSendUserStatus()
let currentUserStatus = self.settingsService.getCurrentUserStatus()
let obj = self.settingsService.getIdentityImage(loggedInAccount.keyUid)
singletonInstance.userProfile.setFixedData(loggedInAccount.name, loggedInAccount.keyUid, loggedInAccount.identicon,
pubKey)
singletonInstance.userProfile.setEnsName("") # in this moment we don't know ens name
singletonInstance.userProfile.setThumbnailImage(obj.thumbnail)
singletonInstance.userProfile.setLargeImage(obj.large)
singletonInstance.userProfile.setSendUserStatus(sendUserStatus)
singletonInstance.userProfile.setCurrentUserStatus(currentUserStatus)
singletonInstance.engine.setRootContextProperty("userProfile", self.userProfileVariant)

View File

@ -3,10 +3,12 @@ import NimQml
import local_account_settings
import local_account_sensitive_settings
import local_app_settings
import user_profile
export local_account_settings
export local_account_sensitive_settings
export local_app_settings
export user_profile
type
GlobalSingleton = object
@ -43,8 +45,16 @@ proc localAppSettings*(self: GlobalSingleton): LocalAppSettings =
return localAppSettings
proc userProfile*(self: GlobalSingleton): UserProfile =
var userProfile {.global.}: UserProfile
if (userProfile.isNil):
userProfile = newUserProfile()
return userProfile
proc delete*(self: GlobalSingleton) =
self.engine.delete()
self.localAccountSettings.delete()
self.localAccountSensitiveSettings.delete()
self.localAppSettings.delete()
self.localAppSettings.delete()
self.userProfile.delete()

View File

@ -0,0 +1,143 @@
import NimQml
QtObject:
type UserProfile* = ref object of QObject
# fields which cannot change
username: string
address: string
identicon: string
pubKey: string
# fields which may change during runtime
ensName: string
thumbnailImage: string
largeImage: string
sendUserStatus: bool
currentUserStatus: int
proc setup(self: UserProfile) =
self.QObject.setup
proc delete*(self: UserProfile) =
self.QObject.delete
proc newUserProfile*(): UserProfile =
new(result, delete)
result.setup
proc setFixedData*(self: UserProfile, username: string, address: string, identicon: string, pubKey: string) =
self.username = username
self.address = address
self.identicon = identicon
self.pubKey = pubKey
proc getUsername*(self: UserProfile): string {.slot.} =
self.username
QtProperty[string] username:
read = getUsername
proc getAddress*(self: UserProfile): string {.slot.} =
self.address
QtProperty[string] address:
read = getAddress
proc getPubKey*(self: UserProfile): string {.slot.} =
self.pubKey
QtProperty[string] pubKey:
read = getPubKey
proc ensNameChanged*(self: UserProfile) {.signal.}
proc getEnsName*(self: UserProfile): string {.slot.} =
self.ensName
# this is not a slot
proc setEnsName*(self: UserProfile, name: string) =
if(self.ensName == name):
return
self.ensName = name
self.ensNameChanged()
QtProperty[string] ensName:
read = getEnsName
notify = ensNameChanged
proc thumbnailImageChanged*(self: UserProfile) {.signal.}
proc getThumbnailImage*(self: UserProfile): string {.slot.} =
if(self.thumbnailImage.len > 0):
return self.thumbnailImage
return self.identicon
# this is not a slot
proc setThumbnailImage*(self: UserProfile, image: string) =
if(self.thumbnailImage == image):
return
self.thumbnailImage = image
self.thumbnailImageChanged()
QtProperty[string] thumbnailImage:
read = getThumbnailImage
notify = thumbnailImageChanged
proc largeImageChanged*(self: UserProfile) {.signal.}
proc getLargeImage*(self: UserProfile): string {.slot.} =
if(self.largeImage.len > 0):
return self.largeImage
return self.identicon
# this is not a slot
proc setLargeImage*(self: UserProfile, image: string) =
if(self.largeImage == image):
return
self.largeImage = image
self.largeImageChanged()
QtProperty[string] largeImage:
read = getLargeImage
notify = largeImageChanged
proc sendUserStatusChanged*(self: UserProfile) {.signal.}
proc getSendUserStatus*(self: UserProfile): bool {.slot.} =
self.sendUserStatus
# this is not a slot
proc setSendUserStatus*(self: UserProfile, status: bool) =
if(self.sendUserStatus == status):
return
self.sendUserStatus = status
self.sendUserStatusChanged()
QtProperty[bool] sendUserStatus:
read = getSendUserStatus
notify = sendUserStatusChanged
proc currentUserStatusChanged*(self: UserProfile) {.signal.}
proc getCurrentUserStatus*(self: UserProfile): int {.slot.} =
self.currentUserStatus
# this is not a slot
proc setCurrentUserStatus*(self: UserProfile, status: int) =
if(self.currentUserStatus == status):
return
self.currentUserStatus = status
self.currentUserStatusChanged()
QtProperty[int] currentUserStatus:
read = getCurrentUserStatus
notify = currentUserStatusChanged