Refactor package "convertor" "Main" method.

Moved the button for selecting a video file into a separate function.
This commit is contained in:
Leonid Nikitin 2024-01-15 20:28:02 +06:00
parent 97dd0f4b32
commit dddbfa65bc
Signed by: kor-elf
GPG Key ID: 7DE8F80C5CEC2C0D
3 changed files with 85 additions and 59 deletions

View File

@ -1,6 +1,7 @@
package convertor package convertor
import ( import (
"errors"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
@ -8,7 +9,7 @@ import (
type ServiceContract interface { type ServiceContract interface {
RunConvert(setting ConvertSetting) error RunConvert(setting ConvertSetting) error
GetTotalDuration(file File) (float64, error) GetTotalDuration(file *File) (float64, error)
} }
type Service struct { type Service struct {
@ -23,7 +24,7 @@ type File struct {
} }
type ConvertSetting struct { type ConvertSetting struct {
VideoFileInput File VideoFileInput *File
SocketPath string SocketPath string
} }
@ -67,12 +68,12 @@ func (s Service) RunConvert(setting ConvertSetting) error {
return nil return nil
} }
func (s Service) GetTotalDuration(file File) (duration float64, err error) { func (s Service) GetTotalDuration(file *File) (duration float64, err error) {
args := "-v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 " + file.Path args := []string{"-v", "error", "-select_streams", "v:0", "-count_packets", "-show_entries", "stream=nb_read_packets", "-of", "csv=p=0", file.Path}
cmd := exec.Command(s.pathFFprobe, strings.Split(args, " ")...) cmd := exec.Command(s.pathFFprobe, args...)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return 0, err return 0, errors.New(strings.TrimSpace(string(out)))
} }
return strconv.ParseFloat(strings.TrimSpace(string(out)), 64) return strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
} }

View File

@ -12,7 +12,7 @@ import (
type ViewContract interface { type ViewContract interface {
Main( Main(
runConvert func(setting HandleConvertSetting) error, runConvert func(setting HandleConvertSetting) error,
getSocketPath func(File, *widget.ProgressBar) (string, error), getSocketPath func(*File, *widget.ProgressBar) (string, error),
) )
} }
@ -21,24 +21,24 @@ type View struct {
} }
type HandleConvertSetting struct { type HandleConvertSetting struct {
VideoFileInput File VideoFileInput *File
SocketPath string SocketPath string
} }
type enableFormConversionStruct struct {
fileVideoForConversion *widget.Button
form *widget.Form
}
func NewView(w fyne.Window) *View { func NewView(w fyne.Window) *View {
return &View{w} return &View{w}
} }
func (v View) Main( func (v View) Main(
runConvert func(setting HandleConvertSetting) error, runConvert func(setting HandleConvertSetting) error,
getSocketPath func(File, *widget.ProgressBar) (string, error), getSocketPath func(*File, *widget.ProgressBar) (string, error),
) { ) {
var fileInput File form := &widget.Form{}
var form *widget.Form
fileVideoForConversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
fileVideoForConversionMessage.TextSize = 16
fileVideoForConversionMessage.TextStyle = fyne.TextStyle{Bold: true}
conversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255}) conversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
conversionMessage.TextSize = 16 conversionMessage.TextSize = 16
@ -47,49 +47,29 @@ func (v View) Main(
progress := widget.NewProgressBar() progress := widget.NewProgressBar()
progress.Hide() progress.Hide()
fileVideoForConversion := widget.NewButton("выбрать", func() { fileVideoForConversion, fileVideoForConversionMessage, fileInput := v.getButtonFileVideoForConversion(form, progress, conversionMessage)
fileDialog := dialog.NewFileOpen(
func(r fyne.URIReadCloser, err error) {
if err != nil {
fileVideoForConversionMessage.Text = err.Error()
setStringErrorStyle(fileVideoForConversionMessage)
return
}
if r == nil { form.Items = []*widget.FormItem{
return
}
fileInput = File{
Path: r.URI().Path(),
Name: r.URI().Name(),
Ext: r.URI().Extension(),
}
fileVideoForConversionMessage.Text = r.URI().Path()
setStringSuccessStyle(fileVideoForConversionMessage)
form.Enable()
}, v.w)
fileDialog.Show()
})
form = &widget.Form{
Items: []*widget.FormItem{
{Text: "Файл для ковертации:", Widget: fileVideoForConversion}, {Text: "Файл для ковертации:", Widget: fileVideoForConversion},
{Widget: fileVideoForConversionMessage}, {Widget: fileVideoForConversionMessage},
}, }
SubmitText: "Конвертировать", form.SubmitText = "Конвертировать"
OnSubmit: func() {
enableFormConversionStruct := enableFormConversionStruct{
fileVideoForConversion: fileVideoForConversion,
form: form,
}
form.OnSubmit = func() {
fileVideoForConversion.Disable() fileVideoForConversion.Disable()
form.Disable() form.Disable()
socketPath, err := getSocketPath(fileInput, progress) socketPath, err := getSocketPath(fileInput, progress)
if err != nil { if err != nil {
conversionMessage.Text = err.Error() showConversionMessage(conversionMessage, err)
setStringErrorStyle(conversionMessage) enableFormConversion(enableFormConversionStruct)
fileVideoForConversion.Enable() return
form.Enable()
} }
setting := HandleConvertSetting{ setting := HandleConvertSetting{
@ -98,18 +78,53 @@ func (v View) Main(
} }
err = runConvert(setting) err = runConvert(setting)
if err != nil { if err != nil {
conversionMessage.Text = err.Error() showConversionMessage(conversionMessage, err)
setStringErrorStyle(conversionMessage) enableFormConversion(enableFormConversionStruct)
return
} }
fileVideoForConversion.Enable() enableFormConversion(enableFormConversionStruct)
form.Enable()
},
} }
v.w.SetContent(widget.NewCard("Конвертор видео файлов", "", container.NewVBox(form, conversionMessage, progress))) v.w.SetContent(widget.NewCard("Конвертор видео файлов", "", container.NewVBox(form, conversionMessage, progress)))
form.Disable() form.Disable()
} }
func (v View) getButtonFileVideoForConversion(form *widget.Form, progress *widget.ProgressBar, conversionMessage *canvas.Text) (*widget.Button, *canvas.Text, *File) {
fileInput := &File{}
fileVideoForConversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
fileVideoForConversionMessage.TextSize = 16
fileVideoForConversionMessage.TextStyle = fyne.TextStyle{Bold: true}
button := widget.NewButton("выбрать", func() {
fileDialog := dialog.NewFileOpen(
func(r fyne.URIReadCloser, err error) {
if err != nil {
fileVideoForConversionMessage.Text = err.Error()
setStringErrorStyle(fileVideoForConversionMessage)
return
}
if r == nil {
return
}
fileInput.Path = r.URI().Path()
fileInput.Name = r.URI().Name()
fileInput.Ext = r.URI().Extension()
fileVideoForConversionMessage.Text = r.URI().Path()
setStringSuccessStyle(fileVideoForConversionMessage)
form.Enable()
progress.Hide()
conversionMessage.Text = ""
}, v.w)
fileDialog.Show()
})
return button, fileVideoForConversionMessage, fileInput
}
func setStringErrorStyle(text *canvas.Text) { func setStringErrorStyle(text *canvas.Text) {
text.Color = color.RGBA{255, 0, 0, 255} text.Color = color.RGBA{255, 0, 0, 255}
text.Refresh() text.Refresh()
@ -119,3 +134,13 @@ func setStringSuccessStyle(text *canvas.Text) {
text.Color = color.RGBA{49, 127, 114, 255} text.Color = color.RGBA{49, 127, 114, 255}
text.Refresh() text.Refresh()
} }
func showConversionMessage(conversionMessage *canvas.Text, err error) {
conversionMessage.Text = err.Error()
setStringErrorStyle(conversionMessage)
}
func enableFormConversion(enableFormConversionStruct enableFormConversionStruct) {
enableFormConversionStruct.fileVideoForConversion.Enable()
enableFormConversionStruct.form.Enable()
}

View File

@ -38,7 +38,7 @@ func (h ConvertorHandler) GetConvertor() {
h.convertorView.Main(h.runConvert, h.getSockPath) h.convertorView.Main(h.runConvert, h.getSockPath)
} }
func (h ConvertorHandler) getSockPath(file convertor.File, progressbar *widget.ProgressBar) (string, error) { func (h ConvertorHandler) getSockPath(file *convertor.File, progressbar *widget.ProgressBar) (string, error) {
totalDuration, err := h.getTotalDuration(file) totalDuration, err := h.getTotalDuration(file)
if err != nil { if err != nil {
@ -101,6 +101,6 @@ func (h ConvertorHandler) runConvert(setting convertor.HandleConvertSetting) err
) )
} }
func (h ConvertorHandler) getTotalDuration(file convertor.File) (float64, error) { func (h ConvertorHandler) getTotalDuration(file *convertor.File) (float64, error) {
return h.convertorService.GetTotalDuration(file) return h.convertorService.GetTotalDuration(file)
} }