From b4df87b7a6ce03af523ba4172afe343571c83fb0 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Wed, 29 Dec 2021 10:53:56 +0100 Subject: [PATCH] refactor(@desktop/settings-language): language settings added --- src/app/boot/app_controller.nim | 31 +++---------------- src/app/modules/main/module.nim | 2 +- .../main/profile_section/io_interface.nim | 3 ++ .../profile_section/language/controller.nim | 4 +-- .../language/controller_interface.nim | 9 +----- .../profile_section/language/io_interface.nim | 5 +++ .../main/profile_section/language/module.nim | 7 +++-- .../modules/main/profile_section/module.nim | 5 ++- src/app/modules/main/profile_section/view.nim | 5 +++ src/app_service/service/language/dto.nim | 0 src/app_service/service/language/service.nim | 19 +++++++----- .../service/language/service_interface.nim | 4 --- src/app_service/service/token/dto.nim | 2 +- ui/app/AppLayouts/Profile/ProfileLayout.qml | 2 +- .../Profile/popups/LanguageModal.qml | 6 ++-- .../Profile/stores/LanguageStore.qml | 12 +++++++ .../AppLayouts/Profile/stores/RootStore.qml | 10 +++--- .../AppLayouts/Profile/views/LanguageView.qml | 4 +-- 18 files changed, 62 insertions(+), 68 deletions(-) delete mode 100644 src/app_service/service/language/dto.nim create mode 100644 ui/app/AppLayouts/Profile/stores/LanguageStore.qml diff --git a/src/app/boot/app_controller.nim b/src/app/boot/app_controller.nim index e3cafb0f0f..42b77be9b2 100644 --- a/src/app/boot/app_controller.nim +++ b/src/app/boot/app_controller.nim @@ -1,4 +1,4 @@ -import NimQml, os, strformat +import NimQml import ../../app_service/common/utils @@ -39,28 +39,6 @@ import ../global/global_singleton import ../core/[main] -var i18nPath = "" -if defined(development): - i18nPath = joinPath(getAppDir(), "../ui/i18n") -elif (defined(windows)): - i18nPath = joinPath(getAppDir(), "../resources/i18n") -elif (defined(macosx)): - i18nPath = joinPath(getAppDir(), "../i18n") -elif (defined(linux)): - i18nPath = joinPath(getAppDir(), "../i18n") - -proc setLanguage(locale: string) = - let shouldRetranslate = not defined(linux) - singletonInstance.engine.setTranslationPackage(joinPath(i18nPath, fmt"qml_{locale}.qm"), shouldRetranslate) - -proc changeLanguage(locale: string) = - let currentLanguageCode = singletonInstance.localAppSettings.getLocale() - if (locale == currentLanguageCode): - return - - singletonInstance.localAppSettings.setLocale(locale) - setLanguage(locale) - type AppController* = ref object of RootObj statusFoundation: StatusFoundation @@ -230,6 +208,7 @@ proc delete*(self: AppController) = self.startupModule.delete self.mainModule.delete self.ethService.delete + self.languageService.delete self.localAppSettingsVariant.delete self.localAccountSettingsVariant.delete @@ -261,9 +240,8 @@ proc startupDidLoad*(self: AppController) = singletonInstance.engine.setRootContextProperty("localAccountSettings", self.localAccountSettingsVariant) singletonInstance.engine.load(newQUrl("qrc:///main.qml")) - # We need to set a language once qml is loaded - let locale = singletonInstance.localAppSettings.getLocale() - setLanguage(locale) + # We need to init a language service once qml is loaded + self.languageService.init() proc mainDidLoad*(self: AppController) = self.statusFoundation.onLoggedIn() @@ -289,7 +267,6 @@ proc load(self: AppController) = self.providerService.init() self.walletAccountService.init() self.transactionService.init() - self.languageService.init() self.stickersService.init() self.networkService.init() self.activityCenterService.init() diff --git a/src/app/modules/main/module.nim b/src/app/modules/main/module.nim index fc406c225e..fcade7d441 100644 --- a/src/app/modules/main/module.nim +++ b/src/app/modules/main/module.nim @@ -30,7 +30,7 @@ import ../../../app_service/service/accounts/service as accounts_service import ../../../app_service/service/settings/service_interface as settings_service import ../../../app_service/service/contacts/service as contacts_service import ../../../app_service/service/about/service as about_service -import ../../../app_service/service/language/service as language_service +import ../../../app_service/service/language/service_interface as language_service import ../../../app_service/service/mnemonic/service as mnemonic_service import ../../../app_service/service/privacy/service as privacy_service import ../../../app_service/service/stickers/service as stickers_service diff --git a/src/app/modules/main/profile_section/io_interface.nim b/src/app/modules/main/profile_section/io_interface.nim index a7bbbdd68f..4512eca9c3 100644 --- a/src/app/modules/main/profile_section/io_interface.nim +++ b/src/app/modules/main/profile_section/io_interface.nim @@ -29,6 +29,9 @@ method contactsModuleDidLoad*(self: AccessInterface) {.base.} = method languageModuleDidLoad*(self: AccessInterface) {.base.} = raise newException(ValueError, "No implementation available") +method getLanguageModule*(self: AccessInterface): QVariant {.base.} = + raise newException(ValueError, "No implementation available") + method mnemonicModuleDidLoad*(self: AccessInterface) {.base.} = raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/profile_section/language/controller.nim b/src/app/modules/main/profile_section/language/controller.nim index 5060045c0a..a46ba09d84 100644 --- a/src/app/modules/main/profile_section/language/controller.nim +++ b/src/app/modules/main/profile_section/language/controller.nim @@ -1,8 +1,6 @@ import ./controller_interface import io_interface -import ../../../../../app_service/service/language/service as language_service - -# import ./item as item +import ../../../../../app_service/service/language/service_interface as language_service export controller_interface diff --git a/src/app/modules/main/profile_section/language/controller_interface.nim b/src/app/modules/main/profile_section/language/controller_interface.nim index d7a5808ff4..dabc637641 100644 --- a/src/app/modules/main/profile_section/language/controller_interface.nim +++ b/src/app/modules/main/profile_section/language/controller_interface.nim @@ -1,5 +1,3 @@ -import ../../../../../app_service/service/language/service as language_service - type AccessInterface* {.pure inheritable.} = ref object of RootObj ## Abstract class for any input/interaction with this module. @@ -11,9 +9,4 @@ method init*(self: AccessInterface) {.base.} = raise newException(ValueError, "No implementation available") method changeLanguage*(self: AccessInterface, locale: string) {.base.} = - raise newException(ValueError, "No implementation available") - -type - ## Abstract class (concept) which must be implemented by object/s used in this - ## module. - DelegateInterface* = concept c + raise newException(ValueError, "No implementation available") \ No newline at end of file diff --git a/src/app/modules/main/profile_section/language/io_interface.nim b/src/app/modules/main/profile_section/language/io_interface.nim index 699a9dc837..3ffe73c6eb 100644 --- a/src/app/modules/main/profile_section/language/io_interface.nim +++ b/src/app/modules/main/profile_section/language/io_interface.nim @@ -1,3 +1,5 @@ +import NimQml + type AccessInterface* {.pure inheritable.} = ref object of RootObj ## Abstract class for any input/interaction with this module. @@ -11,6 +13,9 @@ method load*(self: AccessInterface) {.base.} = method isLoaded*(self: AccessInterface): bool {.base.} = raise newException(ValueError, "No implementation available") +method getModuleAsVariant*(self: AccessInterface): QVariant {.base.} = + raise newException(ValueError, "No implementation available") + method changeLanguage*(self: AccessInterface, locale: string) {.base.} = raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/profile_section/language/module.nim b/src/app/modules/main/profile_section/language/module.nim index 571493bb57..49f47905eb 100644 --- a/src/app/modules/main/profile_section/language/module.nim +++ b/src/app/modules/main/profile_section/language/module.nim @@ -4,7 +4,7 @@ import ./io_interface, ./view, ./controller import ../io_interface as delegate_interface import ../../../../global/global_singleton -import ../../../../../app_service/service/language/service as language_service +import ../../../../../app_service/service/language/service_interface as language_service export io_interface @@ -24,8 +24,6 @@ proc newModule*(delegate: delegate_interface.AccessInterface, languageService: l result.controller = controller.newController(result, languageService) result.moduleLoaded = false - singletonInstance.engine.setRootContextProperty("languageModule", result.viewVariant) - method delete*(self: Module) = self.view.delete @@ -40,5 +38,8 @@ method viewDidLoad*(self: Module) = self.moduleLoaded = true self.delegate.languageModuleDidLoad() +method getModuleAsVariant*(self: Module): QVariant = + return self.viewVariant + method changeLanguage*(self: Module, locale: string) = self.controller.changeLanguage(locale) diff --git a/src/app/modules/main/profile_section/module.nim b/src/app/modules/main/profile_section/module.nim index 4a60b61f46..03fe2e881c 100644 --- a/src/app/modules/main/profile_section/module.nim +++ b/src/app/modules/main/profile_section/module.nim @@ -7,7 +7,7 @@ import ../../../../app_service/service/accounts/service as accounts_service import ../../../../app_service/service/settings/service_interface as settings_service import ../../../../app_service/service/contacts/service as contacts_service import ../../../../app_service/service/about/service as about_service -import ../../../../app_service/service/language/service as language_service +import ../../../../app_service/service/language/service_interface as language_service import ../../../../app_service/service/mnemonic/service as mnemonic_service import ../../../../app_service/service/privacy/service as privacy_service import ../../../../app_service/service/node_configuration/service_interface as node_configuration_service @@ -162,6 +162,9 @@ method contactsModuleDidLoad*[T](self: Module[T]) = method languageModuleDidLoad*[T](self: Module[T]) = self.checkIfModuleDidLoad() +method getLanguageModule*[T](self: Module[T]): QVariant = + self.languageModule.getModuleAsVariant() + method mnemonicModuleDidLoad*[T](self: Module[T]) = self.checkIfModuleDidLoad() diff --git a/src/app/modules/main/profile_section/view.nim b/src/app/modules/main/profile_section/view.nim index 09fec0f972..afb1bc5bf9 100644 --- a/src/app/modules/main/profile_section/view.nim +++ b/src/app/modules/main/profile_section/view.nim @@ -21,6 +21,11 @@ QtObject: proc load*(self: View) = self.delegate.viewDidLoad() + proc getLanguageModule(self: View): QVariant {.slot.} = + return self.delegate.getLanguageModule() + QtProperty[QVariant] languageModule: + read = getLanguageModule + proc getAdvancedModule(self: View): QVariant {.slot.} = return self.delegate.getAdvancedModule() QtProperty[QVariant] advancedModule: diff --git a/src/app_service/service/language/dto.nim b/src/app_service/service/language/dto.nim deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/app_service/service/language/service.nim b/src/app_service/service/language/service.nim index ed7b40263b..f069158b20 100644 --- a/src/app_service/service/language/service.nim +++ b/src/app_service/service/language/service.nim @@ -1,6 +1,6 @@ import NimQml import json, json_serialization, sequtils, chronicles, os, strformat -import ./service_interface, ./dto +import ./service_interface import ../../../app/global/global_singleton export service_interface @@ -10,14 +10,15 @@ logScope: type Service* = ref object of ServiceInterface - i18nPath*: string - currentLanguageCode*: string + i18nPath: string + shouldRetranslate: bool method delete*(self: Service) = discard proc newService*(): Service = result = Service() + result.shouldRetranslate = not defined(linux) method init*(self: Service) = try: @@ -31,15 +32,17 @@ method init*(self: Service) = elif (defined(linux)): self.i18nPath = joinPath(getAppDir(), "../i18n") + let locale = singletonInstance.localAppSettings.getLocale() + singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{locale}.qm"), self.shouldRetranslate) + except Exception as e: let errDesription = e.msg error "error: ", errDesription return method setLanguage*(self: Service, locale: string) = - if (locale == self.currentLanguageCode): + if (locale == singletonInstance.localAppSettings.getLocale()): return - self.currentLanguageCode = locale - let shouldRetranslate = not defined(linux) - singletonInstance.engine.setTranslationPackage( - joinPath(self.i18nPath, fmt"qml_{locale}.qm"), shouldRetranslate) + + singletonInstance.localAppSettings.setLocale(locale) + singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{locale}.qm"), self.shouldRetranslate) \ No newline at end of file diff --git a/src/app_service/service/language/service_interface.nim b/src/app_service/service/language/service_interface.nim index bea7d86e5f..32499a1d7a 100644 --- a/src/app_service/service/language/service_interface.nim +++ b/src/app_service/service/language/service_interface.nim @@ -1,7 +1,3 @@ -import dto - -export dto - type ServiceInterface* {.pure inheritable.} = ref object of RootObj ## Abstract class for this service access. diff --git a/src/app_service/service/token/dto.nim b/src/app_service/service/token/dto.nim index 0abdd49d21..59ef879439 100644 --- a/src/app_service/service/token/dto.nim +++ b/src/app_service/service/token/dto.nim @@ -1,4 +1,4 @@ -import json, sequtils +import json include ../../common/json_utils diff --git a/ui/app/AppLayouts/Profile/ProfileLayout.qml b/ui/app/AppLayouts/Profile/ProfileLayout.qml index 1b21e78ccb..c445e5ca64 100644 --- a/ui/app/AppLayouts/Profile/ProfileLayout.qml +++ b/ui/app/AppLayouts/Profile/ProfileLayout.qml @@ -78,7 +78,7 @@ StatusAppTwoPanelLayout { } LanguageView { - store: profileView.store + languageStore: profileView.store.languageStore profileContentWidth: _internal.profileContentWidth } diff --git a/ui/app/AppLayouts/Profile/popups/LanguageModal.qml b/ui/app/AppLayouts/Profile/popups/LanguageModal.qml index 30ae072aa5..eea8e0307c 100644 --- a/ui/app/AppLayouts/Profile/popups/LanguageModal.qml +++ b/ui/app/AppLayouts/Profile/popups/LanguageModal.qml @@ -13,7 +13,7 @@ ModalPopup { //% "Language" title: qsTrId("Language") - property var store + property var languageStore onClosed: { destroy() @@ -37,7 +37,7 @@ ModalPopup { confirmButtonLabel: qsTr("Close the app now") cancelButtonLabel: qsTr("I'll do that later") onConfirmButtonClicked: { - root.store.changeLocale(newLocale) + root.languageStore.changeLocale(newLocale) Qt.quit(); } onCancelButtonClicked: { @@ -81,7 +81,7 @@ ModalPopup { languageChangeConfirmationDialog.item.newLocale = modelData.locale languageChangeConfirmationDialog.item.open() } else { - root.store.changeLocale(modelData.locale) + root.languageStore.changeLocale(modelData.locale) } } } diff --git a/ui/app/AppLayouts/Profile/stores/LanguageStore.qml b/ui/app/AppLayouts/Profile/stores/LanguageStore.qml new file mode 100644 index 0000000000..dde915957b --- /dev/null +++ b/ui/app/AppLayouts/Profile/stores/LanguageStore.qml @@ -0,0 +1,12 @@ +import QtQuick 2.13 +import utils 1.0 + +QtObject { + id: root + + property var languageModule + + function changeLocale(locale) { + root.languageModule.changeLocale(locale) + } +} diff --git a/ui/app/AppLayouts/Profile/stores/RootStore.qml b/ui/app/AppLayouts/Profile/stores/RootStore.qml index 7db7bea874..cc0be3a0e4 100644 --- a/ui/app/AppLayouts/Profile/stores/RootStore.qml +++ b/ui/app/AppLayouts/Profile/stores/RootStore.qml @@ -7,7 +7,6 @@ QtObject { property var profile: profileModule.model property var contactsModuleInst: contactsModule property var aboutModuleInst: aboutModule - property var languageModuleInst: languageModule property var mnemonicModuleInst: mnemonicModule property var profileModuleInst: profileSectionModule @@ -28,6 +27,10 @@ QtObject { notificationsModule: profileModuleInst.notificationsModule } + property LanguageStore languageStore: LanguageStore { + languageModule: profileModuleInst.languageModule + } + // Not Refactored Yet // property var chatsModelInst: chatsModel // Not Refactored Yet @@ -307,11 +310,6 @@ QtObject { // return utilsModelInst.getSNTBalance() } - function changeLocale(l) { - localAppSettings.locale = l - languageModuleInst.changeLocale(l) - } - function getNetworkName() { // Not Refactored Yet // return utilsModelInst.getNetworkName() diff --git a/ui/app/AppLayouts/Profile/views/LanguageView.qml b/ui/app/AppLayouts/Profile/views/LanguageView.qml index 1dcb548f56..c5606448c2 100644 --- a/ui/app/AppLayouts/Profile/views/LanguageView.qml +++ b/ui/app/AppLayouts/Profile/views/LanguageView.qml @@ -16,10 +16,10 @@ Item { Layout.fillWidth: true clip: true - property var store + property var languageStore property int profileContentWidth property Component languagePopup: LanguageModal { - store: languageContainer.store + languageStore: languageContainer.languageStore } Item {