From 6358d5d8ccb91acaaf396c6f86847fdc89c74f68 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Wed, 31 Jan 2024 21:02:12 +0600 Subject: [PATCH] The language selection is remembered. Added a setting for changing the language. --- src/convertor/view.go | 3 + .../view.go => convertor/view_setting.go} | 29 +------ src/handler/convertor.go | 22 ++---- src/handler/main.go | 37 +++++++++ src/handler/menu.go | 78 +++++++++++++++++++ src/languages/active.en.toml | 12 +++ src/languages/active.kk.toml | 12 +++ src/languages/active.ru.toml | 3 + src/languages/translate.en.toml | 11 +++ src/languages/translate.kk.toml | 11 +++ src/localizer/repository.go | 26 +++++++ src/localizer/service.go | 11 +++ src/main.go | 11 ++- src/setting/repository.go | 20 +++++ 14 files changed, 240 insertions(+), 46 deletions(-) rename src/{setting/view.go => convertor/view_setting.go} (81%) create mode 100644 src/handler/main.go create mode 100644 src/handler/menu.go create mode 100644 src/localizer/repository.go diff --git a/src/convertor/view.go b/src/convertor/view.go index 3fb68ee..669514e 100644 --- a/src/convertor/view.go +++ b/src/convertor/view.go @@ -17,6 +17,9 @@ type ViewContract interface { Main( runConvert func(setting HandleConvertSetting, progressbar *widget.ProgressBar) error, ) + SelectFFPath( + func(ffmpegPath string, ffprobePath string) error, + ) } type View struct { diff --git a/src/setting/view.go b/src/convertor/view_setting.go similarity index 81% rename from src/setting/view.go rename to src/convertor/view_setting.go index 397972f..0d29a09 100644 --- a/src/setting/view.go +++ b/src/convertor/view_setting.go @@ -1,8 +1,7 @@ -package setting +package convertor import ( "ffmpegGui/helper" - "ffmpegGui/localizer" "fyne.io/fyne/v2" "fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/container" @@ -13,22 +12,6 @@ import ( "net/url" ) -type ViewContract interface { - SelectFFPath(func(ffmpegPath string, ffprobePath string) error) -} - -type View struct { - w fyne.Window - localizerService localizer.ServiceContract -} - -func NewView(w fyne.Window, localizerService localizer.ServiceContract) *View { - return &View{ - w: w, - localizerService: localizerService, - } -} - func (v View) SelectFFPath(save func(ffmpegPath string, ffprobePath string) error) { errorMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255}) errorMessage.TextSize = 16 @@ -124,13 +107,3 @@ func (v View) getButtonSelectFile() (filePath *string, button *widget.Button, bu return } - -func setStringErrorStyle(text *canvas.Text) { - text.Color = color.RGBA{255, 0, 0, 255} - text.Refresh() -} - -func setStringSuccessStyle(text *canvas.Text) { - text.Color = color.RGBA{49, 127, 114, 255} - text.Refresh() -} diff --git a/src/handler/convertor.go b/src/handler/convertor.go index 053ece0..73ee521 100644 --- a/src/handler/convertor.go +++ b/src/handler/convertor.go @@ -15,11 +15,13 @@ import ( "strings" ) +type ConvertorHandlerContract interface { + MainConvertor() +} + type ConvertorHandler struct { convertorService convertor.ServiceContract convertorView convertor.ViewContract - settingView setting.ViewContract - localizerView localizer.ViewContract settingRepository setting.RepositoryContract localizerService localizer.ServiceContract } @@ -27,33 +29,23 @@ type ConvertorHandler struct { func NewConvertorHandler( convertorService convertor.ServiceContract, convertorView convertor.ViewContract, - settingView setting.ViewContract, - localizerView localizer.ViewContract, settingRepository setting.RepositoryContract, localizerService localizer.ServiceContract, ) *ConvertorHandler { return &ConvertorHandler{ convertorService: convertorService, convertorView: convertorView, - settingView: settingView, - localizerView: localizerView, settingRepository: settingRepository, localizerService: localizerService, } } -func (h ConvertorHandler) LanguageSelection() { - h.localizerView.LanguageSelection(func(lang localizer.Lang) { - h.GetConvertor() - }) -} - -func (h ConvertorHandler) GetConvertor() { +func (h ConvertorHandler) MainConvertor() { if h.checkingFFPathUtilities() == true { h.convertorView.Main(h.runConvert) return } - h.settingView.SelectFFPath(h.saveSettingFFPath) + h.convertorView.SelectFFPath(h.saveSettingFFPath) } func (h ConvertorHandler) runConvert(setting convertor.HandleConvertSetting, progressbar *widget.ProgressBar) error { @@ -124,7 +116,7 @@ func (h ConvertorHandler) saveSettingFFPath(ffmpegPath string, ffprobePath strin ffprobeEntity := setting.Setting{Code: "ffprobe", Value: ffprobePath} _, _ = h.settingRepository.Create(ffprobeEntity) - h.GetConvertor() + h.MainConvertor() return nil } diff --git a/src/handler/main.go b/src/handler/main.go new file mode 100644 index 0000000..30084e5 --- /dev/null +++ b/src/handler/main.go @@ -0,0 +1,37 @@ +package handler + +import ( + "ffmpegGui/localizer" +) + +type MainHandler struct { + convertorHandler ConvertorHandlerContract + menuHandler MenuHandlerContract + localizerRepository localizer.RepositoryContract + localizerService localizer.ServiceContract +} + +func NewMainHandler( + convertorHandler ConvertorHandlerContract, + menuHandler MenuHandlerContract, + localizerRepository localizer.RepositoryContract, + localizerService localizer.ServiceContract, +) *MainHandler { + return &MainHandler{ + convertorHandler: convertorHandler, + menuHandler: menuHandler, + localizerRepository: localizerRepository, + localizerService: localizerService, + } +} + +func (h MainHandler) Start() { + language, err := h.localizerRepository.GetCode() + if err != nil { + h.menuHandler.LanguageSelection() + return + } + _ = h.localizerService.SetCurrentLanguageByCode(language) + + h.convertorHandler.MainConvertor() +} diff --git a/src/handler/menu.go b/src/handler/menu.go new file mode 100644 index 0000000..53d6cdb --- /dev/null +++ b/src/handler/menu.go @@ -0,0 +1,78 @@ +package handler + +import ( + "ffmpegGui/localizer" + "fyne.io/fyne/v2" + "github.com/nicksnyder/go-i18n/v2/i18n" +) + +type MenuHandlerContract interface { + GetMainMenu() *fyne.MainMenu + LanguageSelection() +} + +type menuItems struct { + menuItem map[string]*fyne.MenuItem + menu map[string]*fyne.Menu +} + +type MenuHandler struct { + convertorHandler ConvertorHandlerContract + localizerService localizer.ServiceContract + localizerView localizer.ViewContract + localizerRepository localizer.RepositoryContract + menuItems *menuItems +} + +func NewMenuHandler( + convertorHandler ConvertorHandlerContract, + localizerService localizer.ServiceContract, + localizerView localizer.ViewContract, + localizerRepository localizer.RepositoryContract, +) *MenuHandler { + return &MenuHandler{ + convertorHandler: convertorHandler, + localizerService: localizerService, + localizerView: localizerView, + localizerRepository: localizerRepository, + menuItems: &menuItems{menuItem: map[string]*fyne.MenuItem{}, menu: map[string]*fyne.Menu{}}, + } +} + +func (h MenuHandler) GetMainMenu() *fyne.MainMenu { + quit := fyne.NewMenuItem(h.localizerService.GetMessage(&i18n.LocalizeConfig{ + MessageID: "exit", + }), nil) + quit.IsQuit = true + h.menuItems.menuItem["exit"] = quit + + languageSelection := fyne.NewMenuItem(h.localizerService.GetMessage(&i18n.LocalizeConfig{ + MessageID: "changeLanguage", + }), h.LanguageSelection) + h.menuItems.menuItem["changeLanguage"] = languageSelection + + settings := fyne.NewMenu(h.localizerService.GetMessage(&i18n.LocalizeConfig{ + MessageID: "settings", + }), languageSelection, quit) + h.menuItems.menu["settings"] = settings + + return fyne.NewMainMenu(settings) +} + +func (h MenuHandler) LanguageSelection() { + h.localizerView.LanguageSelection(func(lang localizer.Lang) { + h.localizerRepository.Save(lang.Code) + h.menuMessageReload() + h.convertorHandler.MainConvertor() + }) +} + +func (h MenuHandler) menuMessageReload() { + for messageID, menu := range h.menuItems.menuItem { + menu.Label = h.localizerService.GetMessage(&i18n.LocalizeConfig{MessageID: messageID}) + } + for messageID, menu := range h.menuItems.menu { + menu.Label = h.localizerService.GetMessage(&i18n.LocalizeConfig{MessageID: messageID}) + menu.Refresh() + } +} diff --git a/src/languages/active.en.toml b/src/languages/active.en.toml index 5fa57a1..cd9dfee 100644 --- a/src/languages/active.en.toml +++ b/src/languages/active.en.toml @@ -2,6 +2,10 @@ hash = "sha1-52b13f1b13e82d22e8c4102332db5d4ec551247b" other = "Folder where it will be saved:" +[changeLanguage] +hash = "sha1-8b276eaf378d485c769fb3d5dcc06dfc25b0c01b" +other = "Change language" + [checkboxOverwriteOutputFilesTitle] hash = "sha1-5860124bb781e7ef680f573fa93977e96328d4e7" other = "Allow file to be overwritten" @@ -42,6 +46,10 @@ other = "this is not FFprobe" hash = "sha1-83da899677cdc90e4344e3b94ee03c46b51bee4c" other = "You haven't selected a folder to save!" +[exit] +hash = "sha1-c42457057d1ab7950cea00719cbe0b078891775f" +other = "Exit" + [fileVideoForConversionTitle] hash = "sha1-5e727d4a2ff3f21080e51e81641595b2e668f3be" other = "File for conversion:" @@ -70,6 +78,10 @@ other = "Save" hash = "sha1-95581446a28d968ff1a027c623159a7eb08654cf" other = "Specify the path to FFmpeg and FFprobe" +[settings] +hash = "sha1-7f17c7c62a7fd8d1a508481f4778688927734c2f" +other = "Settings" + [titleDownloadLink] hash = "sha1-92df86371f6c3a06ca1e4754f113142776a32d49" other = "You can download it from here" diff --git a/src/languages/active.kk.toml b/src/languages/active.kk.toml index f1f713e..6e443e3 100644 --- a/src/languages/active.kk.toml +++ b/src/languages/active.kk.toml @@ -2,6 +2,10 @@ hash = "sha1-52b13f1b13e82d22e8c4102332db5d4ec551247b" other = "Файлды сақтауға арналған каталог:" +[changeLanguage] +hash = "sha1-8b276eaf378d485c769fb3d5dcc06dfc25b0c01b" +other = "Тілді өзгерту" + [checkboxOverwriteOutputFilesTitle] hash = "sha1-5860124bb781e7ef680f573fa93977e96328d4e7" other = "Файлды қайта жазуға рұқсат беріңіз" @@ -42,6 +46,10 @@ other = "бұл FFprobe емес" hash = "sha1-83da899677cdc90e4344e3b94ee03c46b51bee4c" other = "Сіз сақталатын қалтаны таңдамадыңыз!" +[exit] +hash = "sha1-c42457057d1ab7950cea00719cbe0b078891775f" +other = "Шығу" + [fileVideoForConversionTitle] hash = "sha1-5e727d4a2ff3f21080e51e81641595b2e668f3be" other = "Түрлендіруге арналған файл:" @@ -70,6 +78,10 @@ other = "Сақтау" hash = "sha1-95581446a28d968ff1a027c623159a7eb08654cf" other = "FFmpeg және FFprobe жолын көрсетіңіз" +[settings] +hash = "sha1-7f17c7c62a7fd8d1a508481f4778688927734c2f" +other = "Параметрлер" + [titleDownloadLink] hash = "sha1-92df86371f6c3a06ca1e4754f113142776a32d49" other = "Сіз оны осы жерден жүктей аласыз" diff --git a/src/languages/active.ru.toml b/src/languages/active.ru.toml index b4185d9..11e08c5 100644 --- a/src/languages/active.ru.toml +++ b/src/languages/active.ru.toml @@ -1,4 +1,5 @@ buttonForSelectedDirTitle = "Папка куда будет сохраняться:" +changeLanguage = "Поменять язык" checkboxOverwriteOutputFilesTitle = "Разрешить перезаписать файл" choose = "выбрать" converterVideoFilesSubmitTitle = "Конвертировать" @@ -9,6 +10,7 @@ errorDatabase = "не смогли создать файл 'database' в пап errorFFmpeg = "это не FFmpeg" errorFFprobe = "это не FFprobe" errorSelectedFolderSave = "Не выбрали папку для сохранения!" +exit = "Выход" fileVideoForConversionTitle = "Файл для ковертации:" languageSelectionFormHead = "Переключить язык" languageSelectionHead = "Выберите язык" @@ -16,4 +18,5 @@ pathToFfmpeg = "Путь к ffmpeg:" pathToFfprobe = "Путь к ffprobe:" save = "Сохранить" selectFFPathTitle = "Укажите путь к FFmpeg и к FFprobe" +settings = "Настройки" titleDownloadLink = "Скачать можно от сюда" diff --git a/src/languages/translate.en.toml b/src/languages/translate.en.toml index e69de29..5adccc4 100644 --- a/src/languages/translate.en.toml +++ b/src/languages/translate.en.toml @@ -0,0 +1,11 @@ +[changeLanguage] +hash = "sha1-8b276eaf378d485c769fb3d5dcc06dfc25b0c01b" +other = "Change language" + +[exit] +hash = "sha1-c42457057d1ab7950cea00719cbe0b078891775f" +other = "Exit" + +[settings] +hash = "sha1-7f17c7c62a7fd8d1a508481f4778688927734c2f" +other = "Settings" diff --git a/src/languages/translate.kk.toml b/src/languages/translate.kk.toml index e69de29..863f5ce 100644 --- a/src/languages/translate.kk.toml +++ b/src/languages/translate.kk.toml @@ -0,0 +1,11 @@ +[changeLanguage] +hash = "sha1-8b276eaf378d485c769fb3d5dcc06dfc25b0c01b" +other = "Тілді өзгерту" + +[exit] +hash = "sha1-c42457057d1ab7950cea00719cbe0b078891775f" +other = "Шығу" + +[settings] +hash = "sha1-7f17c7c62a7fd8d1a508481f4778688927734c2f" +other = "Параметрлер" diff --git a/src/localizer/repository.go b/src/localizer/repository.go new file mode 100644 index 0000000..c4d5ddd --- /dev/null +++ b/src/localizer/repository.go @@ -0,0 +1,26 @@ +package localizer + +import ( + "ffmpegGui/setting" +) + +type RepositoryContract interface { + GetCode() (string, error) + Save(code string) (setting.Setting, error) +} + +type Repository struct { + settingRepository setting.RepositoryContract +} + +func NewRepository(settingRepository setting.RepositoryContract) *Repository { + return &Repository{settingRepository: settingRepository} +} + +func (r Repository) GetCode() (string, error) { + return r.settingRepository.GetValue("language") +} + +func (r Repository) Save(code string) (setting.Setting, error) { + return r.settingRepository.CreateOrUpdate("language", code) +} diff --git a/src/localizer/service.go b/src/localizer/service.go index 129c31b..6003453 100644 --- a/src/localizer/service.go +++ b/src/localizer/service.go @@ -14,6 +14,7 @@ type ServiceContract interface { GetLanguages() []Lang GetMessage(localizeConfig *i18n.LocalizeConfig) string SetCurrentLanguage(lang Lang) error + SetCurrentLanguageByCode(code string) error GetCurrentLanguage() *CurrentLanguage } @@ -101,6 +102,16 @@ func (s Service) SetCurrentLanguage(lang Lang) error { return nil } +func (s Service) SetCurrentLanguageByCode(code string) error { + language, err := language.Parse(code) + if err != nil { + return err + } + title := cases.Title(language).String(display.Self.Name(language)) + lang := Lang{Code: language.String(), Title: title} + return s.SetCurrentLanguage(lang) +} + func (s Service) GetCurrentLanguage() *CurrentLanguage { return s.currentLanguage } diff --git a/src/main.go b/src/main.go index a8e6eeb..ec369ce 100644 --- a/src/main.go +++ b/src/main.go @@ -79,12 +79,17 @@ func main() { localizerView := localizer.NewView(w, localizerService) convertorView := convertor.NewView(w, localizerService) - settingView := setting.NewView(w, localizerService) convertorService := convertor.NewService(ffPathUtilities) defer appCloseWithConvert(convertorService) - mainHandler := handler.NewConvertorHandler(convertorService, convertorView, settingView, localizerView, settingRepository, localizerService) + convertorHandler := handler.NewConvertorHandler(convertorService, convertorView, settingRepository, localizerService) - mainHandler.LanguageSelection() + localizerRepository := localizer.NewRepository(settingRepository) + mainMenu := handler.NewMenuHandler(convertorHandler, localizerService, localizerView, localizerRepository) + + mainHandler := handler.NewMainHandler(convertorHandler, mainMenu, localizerRepository, localizerService) + mainHandler.Start() + + w.SetMainMenu(mainMenu.GetMainMenu()) w.ShowAndRun() } diff --git a/src/setting/repository.go b/src/setting/repository.go index 065bb60..34c350b 100644 --- a/src/setting/repository.go +++ b/src/setting/repository.go @@ -1,11 +1,13 @@ package setting import ( + "errors" "gorm.io/gorm" ) type RepositoryContract interface { Create(setting Setting) (Setting, error) + CreateOrUpdate(code string, value string) (Setting, error) GetValue(code string) (value string, err error) } @@ -33,3 +35,21 @@ func (r Repository) Create(setting Setting) (Setting, error) { } return setting, err } + +func (r Repository) CreateOrUpdate(code string, value string) (Setting, error) { + var setting Setting + err := r.db.Where("code = ?", code).First(&setting).Error + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) == true { + setting = Setting{Code: code, Value: value} + return r.Create(setting) + } else { + return setting, err + } + } + err = r.db.Model(&setting).UpdateColumn("value", value).Error + if err != nil { + return setting, err + } + return setting, err +}