chore(settings): add and populate SIGNAL_LOCALE_UPDATE

task: #6416
This commit is contained in:
Patryk Osmaczko 2022-07-15 18:50:03 +02:00 committed by osmaczko
parent 13a6407a75
commit d68833c7a5
6 changed files with 65 additions and 16 deletions

View File

@ -1,26 +1,34 @@
import io_interface import io_interface
import ../../../../../app_service/service/language/service as language_service import ../../../../../app_service/service/language/service as language_service
import ../../../../../app/core/eventemitter
type type
Controller* = ref object of RootObj Controller* = ref object of RootObj
delegate: io_interface.AccessInterface delegate: io_interface.AccessInterface
events: EventEmitter
languageService: language_service.Service languageService: language_service.Service
proc init*(self: Controller) = proc init*(self: Controller) =
discard self.events.on(SIGNAL_LOCALE_UPDATE) do(e: Args):
let args = LocaleUpdatedArgs(e)
self.delegate.onCurrentLocaleChanged(args.locale)
proc newController*(delegate: io_interface.AccessInterface, languageService: language_service.Service): proc newController*(delegate: io_interface.AccessInterface,
events: EventEmitter, languageService: language_service.Service):
Controller = Controller =
result = Controller() result = Controller()
result.delegate = delegate result.delegate = delegate
result.events = events
result.languageService = languageService result.languageService = languageService
proc delete*(self: Controller) = proc delete*(self: Controller) =
discard discard
proc changeLanguage*(self: Controller, locale: string) = proc changeLocale*(self: Controller, locale: string) =
self.languageService.setLanguage(locale) self.languageService.setLanguage(locale)
proc getLocales*(self: Controller): seq[string] = proc getLocales*(self: Controller): seq[string] =
self.languageService.locales() self.languageService.locales()
proc getCurrentLocale*(self: Controller): string =
language_service.currentLocale()

View File

@ -16,7 +16,7 @@ method isLoaded*(self: AccessInterface): bool {.base.} =
method getModuleAsVariant*(self: AccessInterface): QVariant {.base.} = method getModuleAsVariant*(self: AccessInterface): QVariant {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method changeLanguage*(self: AccessInterface, locale: string) {.base.} = method changeLocale*(self: AccessInterface, locale: string) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method setIsDDMMYYDateFormat*(self: AccessInterface, isDDMMYYDateFormat: bool) {.slot.} = method setIsDDMMYYDateFormat*(self: AccessInterface, isDDMMYYDateFormat: bool) {.slot.} =
@ -25,6 +25,10 @@ method setIsDDMMYYDateFormat*(self: AccessInterface, isDDMMYYDateFormat: bool) {
method setIs24hTimeFormat*(self: AccessInterface, is24hTimeFormat: bool) {.slot.} = method setIs24hTimeFormat*(self: AccessInterface, is24hTimeFormat: bool) {.slot.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method onCurrentLocaleChanged*(self: AccessInterface, locale: string) {.base.} =
raise newException(ValueError, "No implementation available")
# View Delegate Interface # View Delegate Interface
# Delegate for the view must be declared here due to use of QtObject and multi # Delegate for the view must be declared here due to use of QtObject and multi
# inheritance, which is not well supported in Nim. # inheritance, which is not well supported in Nim.

View File

@ -3,6 +3,7 @@ import NimQml, tables, sequtils, sugar, chronicles
import io_interface, view, controller, item, model, locale_table import io_interface, view, controller, item, model, locale_table
import ../io_interface as delegate_interface import ../io_interface as delegate_interface
import ../../../../global/global_singleton import ../../../../global/global_singleton
import ../../../../../app/core/eventemitter
import ../../../../../app_service/service/language/service as language_service import ../../../../../app_service/service/language/service as language_service
@ -17,12 +18,12 @@ type
moduleLoaded: bool moduleLoaded: bool
proc newModule*(delegate: delegate_interface.AccessInterface, proc newModule*(delegate: delegate_interface.AccessInterface,
languageService: language_service.Service): Module = events: EventEmitter, languageService: language_service.Service): Module =
result = Module() result = Module()
result.delegate = delegate result.delegate = delegate
result.view = newView(result) result.view = newView(result)
result.viewVariant = newQVariant(result.view) result.viewVariant = newQVariant(result.view)
result.controller = controller.newController(result, languageService) result.controller = controller.newController(result, events, languageService)
result.moduleLoaded = false result.moduleLoaded = false
proc populateLanguageModel(self: Module) = proc populateLanguageModel(self: Module) =
@ -54,14 +55,19 @@ method isLoaded*(self: Module): bool =
method viewDidLoad*(self: Module) = method viewDidLoad*(self: Module) =
self.populateLanguageModel() self.populateLanguageModel()
self.view.setLocale(self.controller.getCurrentLocale())
self.moduleLoaded = true self.moduleLoaded = true
self.delegate.languageModuleDidLoad() self.delegate.languageModuleDidLoad()
method getModuleAsVariant*(self: Module): QVariant = method getModuleAsVariant*(self: Module): QVariant =
return self.viewVariant return self.viewVariant
method changeLanguage*(self: Module, locale: string) = method changeLocale*(self: Module, locale: string) =
self.controller.changeLanguage(locale) self.controller.changeLocale(locale)
method onCurrentLocaleChanged*(self: Module, locale: string) =
self.view.setLocale(locale)
method setIsDDMMYYDateFormat*(self: Module, isDDMMYYDateFormat: bool) = method setIsDDMMYYDateFormat*(self: Module, isDDMMYYDateFormat: bool) =
if(isDDMMYYDateFormat != singletonInstance.localAccountSensitiveSettings.getIsDDMMYYDateFormat()): if(isDDMMYYDateFormat != singletonInstance.localAccountSensitiveSettings.getIsDDMMYYDateFormat()):
@ -70,3 +76,5 @@ method setIsDDMMYYDateFormat*(self: Module, isDDMMYYDateFormat: bool) =
method setIs24hTimeFormat*(self: Module, is24hTimeFormat: bool) = method setIs24hTimeFormat*(self: Module, is24hTimeFormat: bool) =
if(is24hTimeFormat != singletonInstance.localAccountSensitiveSettings.getIs24hTimeFormat()): if(is24hTimeFormat != singletonInstance.localAccountSensitiveSettings.getIs24hTimeFormat()):
singletonInstance.localAccountSensitiveSettings.setIs24hTimeFormat(is24hTimeFormat) singletonInstance.localAccountSensitiveSettings.setIs24hTimeFormat(is24hTimeFormat)

View File

@ -8,6 +8,7 @@ QtObject:
delegate: io_interface.AccessInterface delegate: io_interface.AccessInterface
model: Model model: Model
modelVariant: QVariant modelVariant: QVariant
currentLocale: string
proc delete*(self: View) = proc delete*(self: View) =
self.QObject.delete self.QObject.delete
@ -33,11 +34,25 @@ QtObject:
QtProperty[QVariant] model: QtProperty[QVariant] model:
read = getModel read = getModel
proc changeLocale*(self: View, locale: string) {.slot.} =
self.delegate.changeLanguage(locale)
proc setIsDDMMYYDateFormat*(self: View, isDDMMYYDateFormat: bool) {.slot.} = proc setIsDDMMYYDateFormat*(self: View, isDDMMYYDateFormat: bool) {.slot.} =
self.delegate.setIsDDMMYYDateFormat(isDDMMYYDateFormat) self.delegate.setIsDDMMYYDateFormat(isDDMMYYDateFormat)
proc setIs24hTimeFormat*(self: View, is24hTimeFormat: bool) {.slot.} = proc setIs24hTimeFormat*(self: View, is24hTimeFormat: bool) {.slot.} =
self.delegate.setIs24hTimeFormat(is24hTimeFormat) self.delegate.setIs24hTimeFormat(is24hTimeFormat)
proc changeLocale*(self: View, locale: string) {.slot.} =
self.delegate.changeLocale(locale)
proc getLocale*(self: View): string {.slot.} =
self.currentLocale
proc localeChanged*(self: View) {.signal.}
QtProperty[string] currentLocale:
read = getLocale
notify = localeChanged
proc setLocale*(self: View, locale: string) =
if locale != self.currentLocale:
self.currentLocale = locale
self.localeChanged()

View File

@ -85,7 +85,7 @@ proc newModule*(delegate: delegate_interface.AccessInterface,
result.profileModule = profile_module.newModule(result, profileService) result.profileModule = profile_module.newModule(result, profileService)
result.contactsModule = contacts_module.newModule(result, events, contactsService, chatService) result.contactsModule = contacts_module.newModule(result, events, contactsService, chatService)
result.languageModule = language_module.newModule(result, languageService) result.languageModule = language_module.newModule(result, events, languageService)
result.privacyModule = privacy_module.newModule(result, events, settingsService, privacyService, generalService) result.privacyModule = privacy_module.newModule(result, events, settingsService, privacyService, generalService)
result.aboutModule = about_module.newModule(result, events, aboutService) result.aboutModule = about_module.newModule(result, events, aboutService)
result.advancedModule = advanced_module.newModule(result, events, settingsService, stickersService, nodeConfigurationService) result.advancedModule = advanced_module.newModule(result, events, settingsService, stickersService, nodeConfigurationService)

View File

@ -1,12 +1,21 @@
import NimQml import NimQml
import json, json_serialization, sequtils, chronicles, os, strformat, re import json, json_serialization, sequtils, chronicles, os, strformat, re
import ../../../app/global/global_singleton import ../../../app/global/global_singleton
import ../../../app/core/eventemitter
import ../../../app/core/signals/types
logScope: logScope:
topics = "language-service" topics = "language-service"
const SIGNAL_LOCALE_UPDATE* = "localeUpdated"
type
LocaleUpdatedArgs* = ref object of Args
locale*: string
type type
Service* = ref object of RootObj Service* = ref object of RootObj
events: EventEmitter
i18nPath: string i18nPath: string
shouldRetranslate: bool shouldRetranslate: bool
locales: seq[string] locales: seq[string]
@ -24,6 +33,12 @@ proc obtainLocales(dir: string): seq[string] =
if file =~ localeRe: if file =~ localeRe:
result.add(matches[0]) result.add(matches[0])
proc currentLocale*(): string =
singletonInstance.localAppSettings.getLocale()
proc locales*(self: Service): seq[string] =
self.locales
proc init*(self: Service) = proc init*(self: Service) =
try: try:
self.i18nPath = "" self.i18nPath = ""
@ -38,7 +53,7 @@ proc init*(self: Service) =
self.locales = obtainLocales(self.i18nPath) self.locales = obtainLocales(self.i18nPath)
let locale = singletonInstance.localAppSettings.getLocale() let locale = currentLocale()
singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{locale}.qm"), self.shouldRetranslate) singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{locale}.qm"), self.shouldRetranslate)
except Exception as e: except Exception as e:
@ -53,5 +68,4 @@ proc setLanguage*(self: Service, locale: string) =
singletonInstance.localAppSettings.setLocale(locale) singletonInstance.localAppSettings.setLocale(locale)
singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{locale}.qm"), self.shouldRetranslate) singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{locale}.qm"), self.shouldRetranslate)
proc locales*(self: Service): seq[string] = self.events.emit(SIGNAL_LOCALE_UPDATE, LocaleUpdatedArgs(locale: locale))
self.locales