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
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()
}
}

View File

@ -65,7 +65,6 @@ type QueueLayoutObject struct {
container *fyne.Container
items map[int]QueueLayoutItem
localizerService LocalizerContract
layoutLocalizerListener LayoutLocalizerListenerContract
queueStatisticsFormat *queueStatisticsFormat
}
@ -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,
@ -98,7 +97,6 @@ func NewQueueLayoutObject(queue QueueListContract, localizerService LocalizerCon
),
items: items,
localizerService: localizerService,
layoutLocalizerListener: layoutLocalizerListener,
queueStatisticsFormat: queueStatisticsFormat,
}
@ -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()

View File

@ -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
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)
}
}

View File

@ -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()