From 1eb7ea4a93c2b088c2e756a4ca3704322a3729d7 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Tue, 5 Mar 2024 20:13:58 +0500 Subject: [PATCH] Refactoring LocalizerContract. Instead of AddListener I made AddChangeCallback. And removed unnecessary dependencies. --- handler/menu.go | 67 +++++++++++++---------------------------- kernel/layout.go | 73 ++++++++++++--------------------------------- kernel/localizer.go | 30 ++++++++++--------- main.go | 8 ++--- 4 files changed, 57 insertions(+), 121 deletions(-) diff --git a/handler/menu.go b/handler/menu.go index ca38d52..5ec2ebd 100644 --- a/handler/menu.go +++ b/handler/menu.go @@ -19,7 +19,6 @@ type MenuHandler struct { menuView menu.ViewContract localizerView localizer.ViewContract localizerRepository localizer.RepositoryContract - localizerListener localizerListenerContract } func NewMenuHandler( @@ -28,7 +27,6 @@ func NewMenuHandler( menuView menu.ViewContract, localizerView localizer.ViewContract, localizerRepository localizer.RepositoryContract, - localizerListener localizerListenerContract, ) *MenuHandler { return &MenuHandler{ app: app, @@ -36,7 +34,6 @@ func NewMenuHandler( menuView: menuView, localizerView: localizerView, localizerRepository: localizerRepository, - localizerListener: localizerListener, } } @@ -52,22 +49,31 @@ func (h MenuHandler) getMenuSettings() *fyne.Menu { MessageID: "exit", }), nil) quit.IsQuit = true - h.localizerListener.AddMenuItem("exit", quit) + h.app.GetLocalizerService().AddChangeCallback("exit", func(text string) { + quit.Label = text + }) languageSelection := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ MessageID: "changeLanguage", }), h.LanguageSelection) - h.localizerListener.AddMenuItem("changeLanguage", languageSelection) + h.app.GetLocalizerService().AddChangeCallback("changeLanguage", func(text string) { + languageSelection.Label = text + }) ffPathSelection := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ MessageID: "changeFFPath", }), h.convertorHandler.FfPathSelection) - h.localizerListener.AddMenuItem("changeFFPath", ffPathSelection) + h.app.GetLocalizerService().AddChangeCallback("changeFFPath", func(text string) { + ffPathSelection.Label = text + }) settings := fyne.NewMenu(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ MessageID: "settings", }), languageSelection, ffPathSelection, quit) - h.localizerListener.AddMenu("settings", settings) + h.app.GetLocalizerService().AddChangeCallback("settings", func(text string) { + settings.Label = text + settings.Refresh() + }) return settings } @@ -76,12 +82,17 @@ func (h MenuHandler) getMenuHelp() *fyne.Menu { about := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ MessageID: "about", }), h.openAbout) - h.localizerListener.AddMenuItem("about", about) + h.app.GetLocalizerService().AddChangeCallback("about", func(text string) { + about.Label = text + }) help := fyne.NewMenu(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ MessageID: "help", }), about) - h.localizerListener.AddMenu("help", help) + h.app.GetLocalizerService().AddChangeCallback("help", func(text string) { + help.Label = text + help.Refresh() + }) return help } @@ -109,41 +120,3 @@ func (h MenuHandler) LanguageSelection() { h.convertorHandler.MainConvertor() }) } - -type menuItems struct { - menuItem map[string]*fyne.MenuItem - menu map[string]*fyne.Menu -} - -type LocalizerListener struct { - menuItems *menuItems -} - -type localizerListenerContract interface { - AddMenu(messageID string, menu *fyne.Menu) - AddMenuItem(messageID string, menuItem *fyne.MenuItem) -} - -func NewLocalizerListener() *LocalizerListener { - return &LocalizerListener{ - &menuItems{menuItem: map[string]*fyne.MenuItem{}, menu: map[string]*fyne.Menu{}}, - } -} - -func (l LocalizerListener) AddMenu(messageID string, menu *fyne.Menu) { - l.menuItems.menu[messageID] = menu -} - -func (l LocalizerListener) AddMenuItem(messageID string, menuItem *fyne.MenuItem) { - l.menuItems.menuItem[messageID] = menuItem -} - -func (l LocalizerListener) Change(localizerService kernel.LocalizerContract) { - for messageID, menu := range l.menuItems.menuItem { - menu.Label = localizerService.GetMessage(&i18n.LocalizeConfig{MessageID: messageID}) - } - for messageID, menu := range l.menuItems.menu { - menu.Label = localizerService.GetMessage(&i18n.LocalizeConfig{MessageID: messageID}) - menu.Refresh() - } -} diff --git a/kernel/layout.go b/kernel/layout.go index a917312..db691cf 100644 --- a/kernel/layout.go +++ b/kernel/layout.go @@ -61,12 +61,11 @@ type QueueLayoutObjectContract interface { type QueueLayoutObject struct { QueueListContract QueueListContract - queue QueueListContract - container *fyne.Container - items map[int]QueueLayoutItem - localizerService LocalizerContract - layoutLocalizerListener LayoutLocalizerListenerContract - queueStatisticsFormat *queueStatisticsFormat + queue QueueListContract + container *fyne.Container + items map[int]QueueLayoutItem + localizerService LocalizerContract + queueStatisticsFormat *queueStatisticsFormat } type QueueLayoutItem struct { @@ -78,17 +77,17 @@ type QueueLayoutItem struct { status *StatusContract } -func NewQueueLayoutObject(queue QueueListContract, localizerService LocalizerContract, layoutLocalizerListener LayoutLocalizerListenerContract) *QueueLayoutObject { +func NewQueueLayoutObject(queue QueueListContract, localizerService LocalizerContract) *QueueLayoutObject { title := widget.NewLabel(localizerService.GetMessage(&i18n.LocalizeConfig{MessageID: "queue"})) title.TextStyle.Bold = true - layoutLocalizerListener.AddItem("queue", func(text string) { + localizerService.AddChangeCallback("queue", func(text string) { title.Text = text title.Refresh() }) items := map[int]QueueLayoutItem{} - queueStatisticsFormat := newQueueStatisticsFormat(localizerService, layoutLocalizerListener, &items) + queueStatisticsFormat := newQueueStatisticsFormat(localizerService, &items) queueLayoutObject := &QueueLayoutObject{ queue: queue, @@ -96,10 +95,9 @@ func NewQueueLayoutObject(queue QueueListContract, localizerService LocalizerCon container.NewHBox(title, queueStatisticsFormat.completed.widget, queueStatisticsFormat.error.widget), container.NewHBox(queueStatisticsFormat.inProgress.widget, queueStatisticsFormat.waiting.widget, queueStatisticsFormat.total.widget), ), - items: items, - localizerService: localizerService, - layoutLocalizerListener: layoutLocalizerListener, - queueStatisticsFormat: queueStatisticsFormat, + items: items, + localizerService: localizerService, + queueStatisticsFormat: queueStatisticsFormat, } queue.AddListener(queueLayoutObject) @@ -281,39 +279,6 @@ func (p Progress) Run(stdOut io.ReadCloser, stdErr io.ReadCloser) error { return nil } -type LayoutLocalizerItem struct { - messageID string - callback func(text string) -} - -type LayoutLocalizerListener struct { - itemCurrentId *int - items map[int]*LayoutLocalizerItem -} - -type LayoutLocalizerListenerContract interface { - AddItem(messageID string, callback func(text string)) -} - -func NewLayoutLocalizerListener() *LayoutLocalizerListener { - id := 0 - return &LayoutLocalizerListener{ - itemCurrentId: &id, - items: map[int]*LayoutLocalizerItem{}, - } -} - -func (l LayoutLocalizerListener) AddItem(messageID string, callback func(text string)) { - *l.itemCurrentId += 1 - l.items[*l.itemCurrentId] = &LayoutLocalizerItem{messageID: messageID, callback: callback} -} - -func (l LayoutLocalizerListener) Change(localizerService LocalizerContract) { - for _, item := range l.items { - item.callback(localizerService.GetMessage(&i18n.LocalizeConfig{MessageID: item.messageID})) - } -} - type queueStatistics struct { widget *widget.Check title string @@ -327,12 +292,12 @@ type queueStatisticsFormat struct { total *queueStatistics } -func newQueueStatisticsFormat(localizerService LocalizerContract, layoutLocalizerListener LayoutLocalizerListenerContract, queueItems *map[int]QueueLayoutItem) *queueStatisticsFormat { - checkWaiting := newQueueStatistics("waitingQueue", localizerService, layoutLocalizerListener) - checkInProgress := newQueueStatistics("inProgressQueue", localizerService, layoutLocalizerListener) - checkCompleted := newQueueStatistics("completedQueue", localizerService, layoutLocalizerListener) - checkError := newQueueStatistics("errorQueue", localizerService, layoutLocalizerListener) - checkTotal := newQueueStatistics("total", localizerService, layoutLocalizerListener) +func newQueueStatisticsFormat(localizerService LocalizerContract, queueItems *map[int]QueueLayoutItem) *queueStatisticsFormat { + checkWaiting := newQueueStatistics("waitingQueue", localizerService) + checkInProgress := newQueueStatistics("inProgressQueue", localizerService) + checkCompleted := newQueueStatistics("completedQueue", localizerService) + checkError := newQueueStatistics("errorQueue", localizerService) + checkTotal := newQueueStatistics("total", localizerService) queueStatisticsFormat := &queueStatisticsFormat{ waiting: checkWaiting, @@ -524,7 +489,7 @@ func (f queueStatisticsFormat) allUnCheckboxChecked() { f.error.widget.Refresh() } -func newQueueStatistics(messaigeID string, localizerService LocalizerContract, layoutLocalizerListener LayoutLocalizerListenerContract) *queueStatistics { +func newQueueStatistics(messaigeID string, localizerService LocalizerContract) *queueStatistics { checkbox := widget.NewCheck("", nil) checkbox.Checked = true @@ -539,7 +504,7 @@ func newQueueStatistics(messaigeID string, localizerService LocalizerContract, l queueStatistics.formatText(false) - layoutLocalizerListener.AddItem(messaigeID, func(text string) { + localizerService.AddChangeCallback(messaigeID, func(text string) { queueStatistics.title = strings.ToLower(text) queueStatistics.formatText(true) queueStatistics.widget.Refresh() diff --git a/kernel/localizer.go b/kernel/localizer.go index e002125..3823c7d 100644 --- a/kernel/localizer.go +++ b/kernel/localizer.go @@ -16,11 +16,7 @@ type LocalizerContract interface { SetCurrentLanguage(lang Lang) error SetCurrentLanguageByCode(code string) error GetCurrentLanguage() *CurrentLanguage - AddListener(listener LocalizerListenerContract) -} - -type LocalizerListenerContract interface { - Change(localizerService LocalizerContract) + AddChangeCallback(messageID string, callback func(text string)) } type Lang struct { @@ -34,11 +30,16 @@ type CurrentLanguage struct { localizerDefault *i18n.Localizer } +type changeCallback struct { + messageID string + callback func(text string) +} + type Localizer struct { - bundle *i18n.Bundle - languages []Lang - currentLanguage *CurrentLanguage - localizerListener map[int]LocalizerListenerContract + bundle *i18n.Bundle + languages []Lang + currentLanguage *CurrentLanguage + changeCallbacks map[int]*changeCallback } func NewLocalizer(directory string, languageDefault language.Tag) (*Localizer, error) { @@ -63,7 +64,7 @@ func NewLocalizer(directory string, languageDefault language.Tag) (*Localizer, e localizer: localizerDefault, localizerDefault: localizerDefault, }, - localizerListener: map[int]LocalizerListenerContract{}, + changeCallbacks: map[int]*changeCallback{}, }, nil } @@ -123,13 +124,14 @@ func (l Localizer) GetCurrentLanguage() *CurrentLanguage { return l.currentLanguage } -func (l Localizer) AddListener(listener LocalizerListenerContract) { - l.localizerListener[len(l.localizerListener)] = listener +func (l Localizer) AddChangeCallback(messageID string, callback func(text string)) { + l.changeCallbacks[len(l.changeCallbacks)] = &changeCallback{messageID: messageID, callback: callback} } func (l Localizer) eventSetCurrentLanguage() { - for _, listener := range l.localizerListener { - listener.Change(l) + for _, changeCallback := range l.changeCallbacks { + text := l.GetMessage(&i18n.LocalizeConfig{MessageID: changeCallback.messageID}) + changeCallback.callback(text) } } diff --git a/main.go b/main.go index 5b98be8..0c21671 100644 --- a/main.go +++ b/main.go @@ -37,15 +37,13 @@ func init() { ffPathUtilities = &kernel.FFPathUtilities{FFmpeg: "", FFprobe: ""} convertorService := kernel.NewService(ffPathUtilities) - layoutLocalizerListener := kernel.NewLayoutLocalizerListener() - localizerService.AddListener(layoutLocalizerListener) queue := kernel.NewQueueList() application = kernel.NewApp( appMetadata, localizerService, queue, - kernel.NewQueueLayoutObject(queue, localizerService, layoutLocalizerListener), + kernel.NewQueueLayoutObject(queue, localizerService), convertorService, ) } @@ -101,9 +99,7 @@ func main() { localizerRepository := localizer.NewRepository(settingRepository) menuView := menu.NewView(application) - localizerListener := handler.NewLocalizerListener() - application.GetLocalizerService().AddListener(localizerListener) - mainMenu := handler.NewMenuHandler(application, convertorHandler, menuView, localizerView, localizerRepository, localizerListener) + mainMenu := handler.NewMenuHandler(application, convertorHandler, menuView, localizerView, localizerRepository) mainHandler := handler.NewMainHandler(application, convertorHandler, mainMenu, localizerRepository) mainHandler.Start()