Refactoring LocalizerContract.

Instead of AddListener I made AddChangeCallback. And removed unnecessary dependencies.
This commit is contained in:
Leonid Nikitin 2024-03-05 20:13:58 +05:00
parent e766c6d465
commit 1eb7ea4a93
Signed by: kor-elf
GPG Key ID: 3C0F720C170F6E1D
4 changed files with 57 additions and 121 deletions

View File

@ -19,7 +19,6 @@ type MenuHandler struct {
menuView menu.ViewContract menuView menu.ViewContract
localizerView localizer.ViewContract localizerView localizer.ViewContract
localizerRepository localizer.RepositoryContract localizerRepository localizer.RepositoryContract
localizerListener localizerListenerContract
} }
func NewMenuHandler( func NewMenuHandler(
@ -28,7 +27,6 @@ func NewMenuHandler(
menuView menu.ViewContract, menuView menu.ViewContract,
localizerView localizer.ViewContract, localizerView localizer.ViewContract,
localizerRepository localizer.RepositoryContract, localizerRepository localizer.RepositoryContract,
localizerListener localizerListenerContract,
) *MenuHandler { ) *MenuHandler {
return &MenuHandler{ return &MenuHandler{
app: app, app: app,
@ -36,7 +34,6 @@ func NewMenuHandler(
menuView: menuView, menuView: menuView,
localizerView: localizerView, localizerView: localizerView,
localizerRepository: localizerRepository, localizerRepository: localizerRepository,
localizerListener: localizerListener,
} }
} }
@ -52,22 +49,31 @@ func (h MenuHandler) getMenuSettings() *fyne.Menu {
MessageID: "exit", MessageID: "exit",
}), nil) }), nil)
quit.IsQuit = true 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{ languageSelection := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "changeLanguage", MessageID: "changeLanguage",
}), h.LanguageSelection) }), 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{ ffPathSelection := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "changeFFPath", MessageID: "changeFFPath",
}), h.convertorHandler.FfPathSelection) }), 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{ settings := fyne.NewMenu(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "settings", MessageID: "settings",
}), languageSelection, ffPathSelection, quit) }), languageSelection, ffPathSelection, quit)
h.localizerListener.AddMenu("settings", settings) h.app.GetLocalizerService().AddChangeCallback("settings", func(text string) {
settings.Label = text
settings.Refresh()
})
return settings return settings
} }
@ -76,12 +82,17 @@ func (h MenuHandler) getMenuHelp() *fyne.Menu {
about := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ about := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "about", MessageID: "about",
}), h.openAbout) }), 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{ help := fyne.NewMenu(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "help", MessageID: "help",
}), about) }), about)
h.localizerListener.AddMenu("help", help) h.app.GetLocalizerService().AddChangeCallback("help", func(text string) {
help.Label = text
help.Refresh()
})
return help return help
} }
@ -109,41 +120,3 @@ func (h MenuHandler) LanguageSelection() {
h.convertorHandler.MainConvertor() 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()
}
}

View File

@ -65,7 +65,6 @@ type QueueLayoutObject struct {
container *fyne.Container container *fyne.Container
items map[int]QueueLayoutItem items map[int]QueueLayoutItem
localizerService LocalizerContract localizerService LocalizerContract
layoutLocalizerListener LayoutLocalizerListenerContract
queueStatisticsFormat *queueStatisticsFormat queueStatisticsFormat *queueStatisticsFormat
} }
@ -78,17 +77,17 @@ type QueueLayoutItem struct {
status *StatusContract 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 := widget.NewLabel(localizerService.GetMessage(&i18n.LocalizeConfig{MessageID: "queue"}))
title.TextStyle.Bold = true title.TextStyle.Bold = true
layoutLocalizerListener.AddItem("queue", func(text string) { localizerService.AddChangeCallback("queue", func(text string) {
title.Text = text title.Text = text
title.Refresh() title.Refresh()
}) })
items := map[int]QueueLayoutItem{} items := map[int]QueueLayoutItem{}
queueStatisticsFormat := newQueueStatisticsFormat(localizerService, layoutLocalizerListener, &items) queueStatisticsFormat := newQueueStatisticsFormat(localizerService, &items)
queueLayoutObject := &QueueLayoutObject{ queueLayoutObject := &QueueLayoutObject{
queue: queue, queue: queue,
@ -98,7 +97,6 @@ func NewQueueLayoutObject(queue QueueListContract, localizerService LocalizerCon
), ),
items: items, items: items,
localizerService: localizerService, localizerService: localizerService,
layoutLocalizerListener: layoutLocalizerListener,
queueStatisticsFormat: queueStatisticsFormat, queueStatisticsFormat: queueStatisticsFormat,
} }
@ -281,39 +279,6 @@ func (p Progress) Run(stdOut io.ReadCloser, stdErr io.ReadCloser) error {
return nil 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 { type queueStatistics struct {
widget *widget.Check widget *widget.Check
title string title string
@ -327,12 +292,12 @@ type queueStatisticsFormat struct {
total *queueStatistics total *queueStatistics
} }
func newQueueStatisticsFormat(localizerService LocalizerContract, layoutLocalizerListener LayoutLocalizerListenerContract, queueItems *map[int]QueueLayoutItem) *queueStatisticsFormat { func newQueueStatisticsFormat(localizerService LocalizerContract, queueItems *map[int]QueueLayoutItem) *queueStatisticsFormat {
checkWaiting := newQueueStatistics("waitingQueue", localizerService, layoutLocalizerListener) checkWaiting := newQueueStatistics("waitingQueue", localizerService)
checkInProgress := newQueueStatistics("inProgressQueue", localizerService, layoutLocalizerListener) checkInProgress := newQueueStatistics("inProgressQueue", localizerService)
checkCompleted := newQueueStatistics("completedQueue", localizerService, layoutLocalizerListener) checkCompleted := newQueueStatistics("completedQueue", localizerService)
checkError := newQueueStatistics("errorQueue", localizerService, layoutLocalizerListener) checkError := newQueueStatistics("errorQueue", localizerService)
checkTotal := newQueueStatistics("total", localizerService, layoutLocalizerListener) checkTotal := newQueueStatistics("total", localizerService)
queueStatisticsFormat := &queueStatisticsFormat{ queueStatisticsFormat := &queueStatisticsFormat{
waiting: checkWaiting, waiting: checkWaiting,
@ -524,7 +489,7 @@ func (f queueStatisticsFormat) allUnCheckboxChecked() {
f.error.widget.Refresh() 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 := widget.NewCheck("", nil)
checkbox.Checked = true checkbox.Checked = true
@ -539,7 +504,7 @@ func newQueueStatistics(messaigeID string, localizerService LocalizerContract, l
queueStatistics.formatText(false) queueStatistics.formatText(false)
layoutLocalizerListener.AddItem(messaigeID, func(text string) { localizerService.AddChangeCallback(messaigeID, func(text string) {
queueStatistics.title = strings.ToLower(text) queueStatistics.title = strings.ToLower(text)
queueStatistics.formatText(true) queueStatistics.formatText(true)
queueStatistics.widget.Refresh() queueStatistics.widget.Refresh()

View File

@ -16,11 +16,7 @@ type LocalizerContract interface {
SetCurrentLanguage(lang Lang) error SetCurrentLanguage(lang Lang) error
SetCurrentLanguageByCode(code string) error SetCurrentLanguageByCode(code string) error
GetCurrentLanguage() *CurrentLanguage GetCurrentLanguage() *CurrentLanguage
AddListener(listener LocalizerListenerContract) AddChangeCallback(messageID string, callback func(text string))
}
type LocalizerListenerContract interface {
Change(localizerService LocalizerContract)
} }
type Lang struct { type Lang struct {
@ -34,11 +30,16 @@ type CurrentLanguage struct {
localizerDefault *i18n.Localizer localizerDefault *i18n.Localizer
} }
type changeCallback struct {
messageID string
callback func(text string)
}
type Localizer struct { type Localizer struct {
bundle *i18n.Bundle bundle *i18n.Bundle
languages []Lang languages []Lang
currentLanguage *CurrentLanguage currentLanguage *CurrentLanguage
localizerListener map[int]LocalizerListenerContract changeCallbacks map[int]*changeCallback
} }
func NewLocalizer(directory string, languageDefault language.Tag) (*Localizer, error) { func NewLocalizer(directory string, languageDefault language.Tag) (*Localizer, error) {
@ -63,7 +64,7 @@ func NewLocalizer(directory string, languageDefault language.Tag) (*Localizer, e
localizer: localizerDefault, localizer: localizerDefault,
localizerDefault: localizerDefault, localizerDefault: localizerDefault,
}, },
localizerListener: map[int]LocalizerListenerContract{}, changeCallbacks: map[int]*changeCallback{},
}, nil }, nil
} }
@ -123,13 +124,14 @@ func (l Localizer) GetCurrentLanguage() *CurrentLanguage {
return l.currentLanguage return l.currentLanguage
} }
func (l Localizer) AddListener(listener LocalizerListenerContract) { func (l Localizer) AddChangeCallback(messageID string, callback func(text string)) {
l.localizerListener[len(l.localizerListener)] = listener l.changeCallbacks[len(l.changeCallbacks)] = &changeCallback{messageID: messageID, callback: callback}
} }
func (l Localizer) eventSetCurrentLanguage() { func (l Localizer) eventSetCurrentLanguage() {
for _, listener := range l.localizerListener { for _, changeCallback := range l.changeCallbacks {
listener.Change(l) text := l.GetMessage(&i18n.LocalizeConfig{MessageID: changeCallback.messageID})
changeCallback.callback(text)
} }
} }

View File

@ -37,15 +37,13 @@ func init() {
ffPathUtilities = &kernel.FFPathUtilities{FFmpeg: "", FFprobe: ""} ffPathUtilities = &kernel.FFPathUtilities{FFmpeg: "", FFprobe: ""}
convertorService := kernel.NewService(ffPathUtilities) convertorService := kernel.NewService(ffPathUtilities)
layoutLocalizerListener := kernel.NewLayoutLocalizerListener()
localizerService.AddListener(layoutLocalizerListener)
queue := kernel.NewQueueList() queue := kernel.NewQueueList()
application = kernel.NewApp( application = kernel.NewApp(
appMetadata, appMetadata,
localizerService, localizerService,
queue, queue,
kernel.NewQueueLayoutObject(queue, localizerService, layoutLocalizerListener), kernel.NewQueueLayoutObject(queue, localizerService),
convertorService, convertorService,
) )
} }
@ -101,9 +99,7 @@ func main() {
localizerRepository := localizer.NewRepository(settingRepository) localizerRepository := localizer.NewRepository(settingRepository)
menuView := menu.NewView(application) menuView := menu.NewView(application)
localizerListener := handler.NewLocalizerListener() mainMenu := handler.NewMenuHandler(application, convertorHandler, menuView, localizerView, localizerRepository)
application.GetLocalizerService().AddListener(localizerListener)
mainMenu := handler.NewMenuHandler(application, convertorHandler, menuView, localizerView, localizerRepository, localizerListener)
mainHandler := handler.NewMainHandler(application, convertorHandler, mainMenu, localizerRepository) mainHandler := handler.NewMainHandler(application, convertorHandler, mainMenu, localizerRepository)
mainHandler.Start() mainHandler.Start()