From 169884232f6714a0d6a52b2d538c196badabc5c5 Mon Sep 17 00:00:00 2001 From: Alex Ballas Date: Fri, 10 May 2024 01:26:13 +0300 Subject: [PATCH] Fix mute sound issue. Clean up code --- cmd/go2tv-lite/go2tv.go | 6 ++--- cmd/go2tv-lite/version.txt | 2 +- cmd/go2tv/version.txt | 2 +- internal/gui/about.go | 10 ++++---- internal/gui/actions.go | 8 +++--- internal/gui/actions_mobile.go | 2 +- internal/gui/gui.go | 19 ++++++-------- internal/gui/gui_mobile.go | 2 +- internal/gui/main.go | 16 ++++++------ internal/gui/settings.go | 20 +++++++++------ soapcalls/soapcallers.go | 45 +++++++++++++++++----------------- soapcalls/soapcalls.go | 6 ++--- soapcalls/xmlparsers.go | 2 ++ 13 files changed, 71 insertions(+), 69 deletions(-) diff --git a/cmd/go2tv-lite/go2tv.go b/cmd/go2tv-lite/go2tv.go index 11ba57d..147150f 100644 --- a/cmd/go2tv-lite/go2tv.go +++ b/cmd/go2tv-lite/go2tv.go @@ -55,10 +55,8 @@ func main() { os.Exit(0) } - if err != nil { - fmt.Fprintf(os.Stderr, "Encountered error(s): %s\n", err) - os.Exit(1) - } + fmt.Fprintf(os.Stderr, "Encountered error(s): %s\n", err) + os.Exit(1) } } diff --git a/cmd/go2tv-lite/version.txt b/cmd/go2tv-lite/version.txt index 15b989e..41c11ff 100644 --- a/cmd/go2tv-lite/version.txt +++ b/cmd/go2tv-lite/version.txt @@ -1 +1 @@ -1.16.0 +1.16.1 diff --git a/cmd/go2tv/version.txt b/cmd/go2tv/version.txt index 15b989e..41c11ff 100644 --- a/cmd/go2tv/version.txt +++ b/cmd/go2tv/version.txt @@ -1 +1 @@ -1.16.0 +1.16.1 diff --git a/internal/gui/about.go b/internal/gui/about.go index 36215cb..f5c9897 100644 --- a/internal/gui/about.go +++ b/internal/gui/about.go @@ -12,7 +12,7 @@ import ( "strings" "time" - fyne "fyne.io/fyne/v2" + "fyne.io/fyne/v2" "fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/dialog" @@ -90,7 +90,7 @@ func checkVersion(s *NewScreen) { } client := &http.Client{ - Timeout: time.Duration(3 * time.Second), + Timeout: 3 * time.Second, CheckRedirect: func(req *http.Request, via []*http.Request) error { return errRedirectChecker }, @@ -105,12 +105,12 @@ func checkVersion(s *NewScreen) { defer response.Body.Close() if errors.Is(err, errRedirectChecker) { - url, err := response.Location() + responceUrl, err := response.Location() if err != nil { dialog.ShowError(errVersionGet, s.Current) return } - str := strings.Trim(filepath.Base(url.Path), "v") + str := strings.Trim(filepath.Base(responceUrl.Path), "v") str = strings.ReplaceAll(str, ".", "") chversion, err := strconv.Atoi(str) if err != nil { @@ -120,7 +120,7 @@ func checkVersion(s *NewScreen) { switch { case chversion > currversion: - dialog.ShowInformation("Version checker", "New version: "+strings.Trim(filepath.Base(url.Path), "v"), s.Current) + dialog.ShowInformation("Version checker", "New version: "+strings.Trim(filepath.Base(responceUrl.Path), "v"), s.Current) return default: dialog.ShowInformation("Version checker", "No new version", s.Current) diff --git a/internal/gui/actions.go b/internal/gui/actions.go index 91bf4ff..8a04ba7 100644 --- a/internal/gui/actions.go +++ b/internal/gui/actions.go @@ -35,7 +35,7 @@ func muteAction(screen *NewScreen) { return } - if screen.MuteUnmute.Icon == theme.VolumeUpIcon() { + if screen.MuteUnmute.Icon == theme.VolumeMuteIcon() { unmuteAction(screen) return } @@ -476,9 +476,9 @@ func skipNextAction(screen *NewScreen) { return } - name, path := getNextMedia(screen) + name, nextMediaPath := getNextMedia(screen) screen.MediaText.Text = name - screen.mediafile = path + screen.mediafile = nextMediaPath screen.MediaText.Refresh() if !screen.CustomSubsCheck.Checked { @@ -614,7 +614,7 @@ func queueNext(screen *NewScreen, clear bool) (*soapcalls.TVPayload, error) { } fname, fpath := getNextMedia(screen) - _, spath := getNextPossibleSubs(fname, screen) + _, spath := getNextPossibleSubs(fname) var mediaType string var isSeek bool diff --git a/internal/gui/actions_mobile.go b/internal/gui/actions_mobile.go index 5d59544..a037a3f 100644 --- a/internal/gui/actions_mobile.go +++ b/internal/gui/actions_mobile.go @@ -30,7 +30,7 @@ func muteAction(screen *NewScreen) { return } - if screen.MuteUnmute.Icon == theme.VolumeUpIcon() { + if screen.MuteUnmute.Icon == theme.VolumeDownIcon() { unmuteAction(screen) return } diff --git a/internal/gui/gui.go b/internal/gui/gui.go index 652d36b..0d40351 100644 --- a/internal/gui/gui.go +++ b/internal/gui/gui.go @@ -12,7 +12,7 @@ import ( "strings" "sync" - fyne "fyne.io/fyne/v2" + "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/data/binding" @@ -101,8 +101,8 @@ func Start(ctx context.Context, s *NewScreen) { s.Hotkeys = true tabs.OnSelected = func(t *container.TabItem) { - theme := fyne.CurrentApp().Preferences().StringWithFallback("Theme", "Default") - fyne.CurrentApp().Settings().SetTheme(go2tvTheme{theme}) + currentTheme := fyne.CurrentApp().Preferences().StringWithFallback("Theme", "Default") + fyne.CurrentApp().Settings().SetTheme(go2tvTheme{currentTheme}) if t.Text == "Go2TV" { s.Hotkeys = true @@ -201,21 +201,18 @@ func (p *NewScreen) Fini() { func InitFyneNewScreen(v string) *NewScreen { go2tv := app.NewWithID("com.alexballas.go2tv") w := go2tv.NewWindow("Go2TV") - currentdir, err := os.Getwd() + currentDir, err := os.Getwd() if err != nil { - currentdir = "" + currentDir = "" } - theme := fyne.CurrentApp().Preferences().StringWithFallback("Theme", "Default") - fyne.CurrentApp().Settings().SetTheme(go2tvTheme{theme}) - dw := &debugWriter{ ring: ring.New(1000), } return &NewScreen{ Current: w, - currentmfolder: currentdir, + currentmfolder: currentDir, mediaFormats: []string{".mp4", ".avi", ".mkv", ".mpeg", ".mov", ".webm", ".m4v", ".mpv", ".dv", ".mp3", ".flac", ".wav", ".m4a", ".jpg", ".jpeg", ".png"}, version: v, Debug: dw, @@ -304,13 +301,13 @@ func getNextMedia(screen *NewScreen) (string, string) { } func autoSelectNextSubs(v string, screen *NewScreen) { - name, path := getNextPossibleSubs(v, screen) + name, path := getNextPossibleSubs(v) screen.SubsText.Text = name screen.subsfile = path screen.SubsText.Refresh() } -func getNextPossibleSubs(v string, screen *NewScreen) (string, string) { +func getNextPossibleSubs(v string) (string, string) { var name, path string possibleSub := v[0:len(v)- diff --git a/internal/gui/gui_mobile.go b/internal/gui/gui_mobile.go index aff667c..bc797cd 100644 --- a/internal/gui/gui_mobile.go +++ b/internal/gui/gui_mobile.go @@ -10,7 +10,7 @@ import ( "strings" "sync" - fyne "fyne.io/fyne/v2" + "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/dialog" diff --git a/internal/gui/main.go b/internal/gui/main.go index d7f932f..f70cb99 100644 --- a/internal/gui/main.go +++ b/internal/gui/main.go @@ -527,24 +527,24 @@ func refreshDevList(s *NewScreen, data *[]devType) { } for range refreshDevices.C { - datanew, _ := getDevices(2) + newDevices, _ := getDevices(2) outer: for _, old := range *data { oldAddress, _ := url.Parse(old.addr) - for _, new := range datanew { - newAddress, _ := url.Parse(new.addr) + for _, device := range newDevices { + newAddress, _ := url.Parse(device.addr) if newAddress.Host == oldAddress.Host { continue outer } } if utils.HostPortIsAlive(oldAddress.Host) { - datanew = append(datanew, old) + newDevices = append(newDevices, old) } - sort.Slice(datanew, func(i, j int) bool { - return (datanew)[i].name < (datanew)[j].name + sort.Slice(newDevices, func(i, j int) bool { + return (newDevices)[i].name < (newDevices)[j].name }) } @@ -552,14 +552,14 @@ func refreshDevList(s *NewScreen, data *[]devType) { // one of the already selected devices var includes bool u, _ := url.Parse(s.controlURL) - for _, d := range datanew { + for _, d := range newDevices { n, _ := url.Parse(d.addr) if n.Host == u.Host { includes = true } } - *data = datanew + *data = newDevices if !includes && !utils.HostPortIsAlive(u.Host) { s.controlURL = "" diff --git a/internal/gui/settings.go b/internal/gui/settings.go index 21e827b..b15c73f 100644 --- a/internal/gui/settings.go +++ b/internal/gui/settings.go @@ -20,15 +20,21 @@ type go2tvTheme struct { Theme string } -var _ fyne.Theme = go2tvTheme{} -var SystemVariant fyne.ThemeVariant = 999 -var once sync.Once +var ( + _ fyne.Theme = go2tvTheme{} + SystemVariant fyne.ThemeVariant = 999 + signalSystemVariantChange = make(chan struct{}) + once sync.Once +) func (m go2tvTheme) Color(name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color { switch m.Theme { case "GrabVariant": once.Do(func() { SystemVariant = variant + go func() { + signalSystemVariantChange <- struct{}{} + }() }) case "Dark": @@ -84,15 +90,15 @@ func settingsWindow(s *NewScreen) fyne.CanvasObject { switch themeName { case "Light": dropdown.PlaceHolder = "Light" + parseTheme("Light") case "Dark": dropdown.PlaceHolder = "Dark" + parseTheme("Dark") case "GrabVariant", "Default": fyne.CurrentApp().Settings().SetTheme(go2tvTheme{"GrabVariant"}) - // Wait for SystemVariant to get the correct variant from the command above - for SystemVariant == 999 { - time.Sleep(time.Millisecond) - } + // Wait for the SystemVariant variable to change + <-signalSystemVariantChange switch SystemVariant { case theme.VariantDark: diff --git a/soapcalls/soapcallers.go b/soapcalls/soapcallers.go index b3094bb..a36854b 100644 --- a/soapcalls/soapcallers.go +++ b/soapcalls/soapcallers.go @@ -174,7 +174,7 @@ func (p *TVPayload) setAVTransportSoapCall() error { return fmt.Errorf("setAVTransportSoapCall parse error: %w", err) } - xml, err := setAVTransportSoapBuild(p) + xmlData, err := setAVTransportSoapBuild(p) if err != nil { p.Log().Error().Str("Method", "setAVTransportSoapCall").Str("Action", "setAVTransportSoapBuild").Err(err).Msg("") return fmt.Errorf("setAVTransportSoapCall soap build error: %w", err) @@ -185,7 +185,7 @@ func (p *TVPayload) setAVTransportSoapCall() error { retryClient.Logger = nil client := retryClient.StandardClient() - req, err := http.NewRequestWithContext(p.ctx, "POST", parsedURLtransport.String(), bytes.NewReader(xml)) + req, err := http.NewRequestWithContext(p.ctx, "POST", parsedURLtransport.String(), bytes.NewReader(xmlData)) if err != nil { p.Log().Error().Str("Method", "setAVTransportSoapCall").Str("Action", "Prepare POST").Err(err).Msg("") return fmt.Errorf("setAVTransportSoapCall POST error: %w", err) @@ -207,7 +207,7 @@ func (p *TVPayload) setAVTransportSoapCall() error { p.Log().Debug(). Str("Method", "setAVTransportSoapCall").Str("Action", "Request"). RawJSON("Headers", headerBytesReq). - Msg(string(xml)) + Msg(string(xmlData)) res, err := client.Do(req) if err != nil { @@ -247,7 +247,7 @@ func (p *TVPayload) setNextAVTransportSoapCall(clear bool) error { return fmt.Errorf("setNextAVTransportSoapCall parse error: %w", err) } - xml, err := setNextAVTransportSoapBuild(p, clear) + xmlData, err := setNextAVTransportSoapBuild(p, clear) if err != nil { p.Log().Error().Str("Method", "setNextAVTransportSoapCall").Str("Action", "setNextAVTransportSoapBuild").Err(err).Msg("") return fmt.Errorf("setNextAVTransportSoapCall soap build error: %w", err) @@ -255,7 +255,7 @@ func (p *TVPayload) setNextAVTransportSoapCall(clear bool) error { client := &http.Client{} - req, err := http.NewRequestWithContext(p.ctx, "POST", parsedURLtransport.String(), bytes.NewReader(xml)) + req, err := http.NewRequestWithContext(p.ctx, "POST", parsedURLtransport.String(), bytes.NewReader(xmlData)) if err != nil { p.Log().Error().Str("Method", "setNextAVTransportSoapCall").Str("Action", "Prepare POST").Err(err).Msg("") return fmt.Errorf("setNextAVTransportSoapCall POST error: %w", err) @@ -277,7 +277,7 @@ func (p *TVPayload) setNextAVTransportSoapCall(clear bool) error { p.Log().Debug(). Str("Method", "setNextAVTransportSoapCall").Str("Action", "Request"). RawJSON("Headers", headerBytesReq). - Msg(string(xml)) + Msg(string(xmlData)) res, err := client.Do(req) if err != nil { @@ -318,17 +318,17 @@ func (p *TVPayload) PlayPauseStopSoapCall(action string) error { return fmt.Errorf("AVTransportActionSoapCall parse error: %w", err) } - var xml []byte + var xmlData []byte retry := false switch action { case "Play": - xml, err = playSoapBuild() + xmlData, err = playSoapBuild() case "Stop": - xml, err = stopSoapBuild() + xmlData, err = stopSoapBuild() retry = true case "Pause": - xml, err = pauseSoapBuild() + xmlData, err = pauseSoapBuild() } if err != nil { p.Log().Error().Str("Method", "AVTransportActionSoapCall").Str("Action", "Action Error").Err(err).Msg("") @@ -344,7 +344,7 @@ func (p *TVPayload) PlayPauseStopSoapCall(action string) error { client = retryClient.StandardClient() } - req, err := http.NewRequestWithContext(p.ctx, "POST", parsedURLtransport.String(), bytes.NewReader(xml)) + req, err := http.NewRequestWithContext(p.ctx, "POST", parsedURLtransport.String(), bytes.NewReader(xmlData)) if err != nil { p.Log().Error().Str("Method", "AVTransportActionSoapCall").Str("Action", "Prepare POST").Err(err).Msg("") return fmt.Errorf("AVTransportActionSoapCall POST error: %w", err) @@ -366,7 +366,7 @@ func (p *TVPayload) PlayPauseStopSoapCall(action string) error { p.Log().Debug(). Str("Method", "AVTransportActionSoapCall").Str("Action", action+" Request"). RawJSON("Headers", headerBytesReq). - Msg(string(xml)) + Msg(string(xmlData)) res, err := client.Do(req) if err != nil { @@ -407,10 +407,9 @@ func (p *TVPayload) SeekSoapCall(reltime string) error { return fmt.Errorf("SeekSoapCall parse error: %w", err) } - var xml []byte - retry := false + var xmlData []byte - xml, err = seekSoapBuild(reltime) + xmlData, err = seekSoapBuild(reltime) if err != nil { p.Log().Error().Str("Method", "SeekSoapCall").Str("Action", "Action Error").Err(err).Msg("") return fmt.Errorf("SeekSoapCall action error: %w", err) @@ -418,14 +417,14 @@ func (p *TVPayload) SeekSoapCall(reltime string) error { client := &http.Client{} - if retry { - retryClient := retryablehttp.NewClient() - retryClient.RetryMax = 3 - retryClient.Logger = nil - client = retryClient.StandardClient() - } + //if retry { + // retryClient := retryablehttp.NewClient() + // retryClient.RetryMax = 3 + // retryClient.Logger = nil + // client = retryClient.StandardClient() + //} - req, err := http.NewRequestWithContext(p.ctx, "POST", parsedURLtransport.String(), bytes.NewReader(xml)) + req, err := http.NewRequestWithContext(p.ctx, "POST", parsedURLtransport.String(), bytes.NewReader(xmlData)) if err != nil { p.Log().Error().Str("Method", "SeekSoapCall").Str("Action", "Prepare POST").Err(err).Msg("") return fmt.Errorf("SeekSoapCall POST error: %w", err) @@ -447,7 +446,7 @@ func (p *TVPayload) SeekSoapCall(reltime string) error { p.Log().Debug(). Str("Method", "SeekSoapCall").Str("Action", "Seek Request"). RawJSON("Headers", headerBytesReq). - Msg(string(xml)) + Msg(string(xmlData)) res, err := client.Do(req) if err != nil { diff --git a/soapcalls/soapcalls.go b/soapcalls/soapcalls.go index 99bc340..98479a2 100644 --- a/soapcalls/soapcalls.go +++ b/soapcalls/soapcalls.go @@ -60,7 +60,7 @@ func NewTVPayload(o *Options) (*TVPayload, error) { }, nil } -func (tv *TVPayload) ListenAddress() string { - url, _ := url.Parse(tv.MediaURL) - return url.Host +func (p *TVPayload) ListenAddress() string { + mediaUrl, _ := url.Parse(p.MediaURL) + return mediaUrl.Host } diff --git a/soapcalls/xmlparsers.go b/soapcalls/xmlparsers.go index f3445f9..7f1fd5b 100644 --- a/soapcalls/xmlparsers.go +++ b/soapcalls/xmlparsers.go @@ -124,6 +124,8 @@ func DMRextractor(ctx context.Context, dmrurl string) (*DMRextracted, error) { if service.ID == "urn:upnp-org:serviceId:ConnectionManager" { ex.ConnectionManagerURL = parsedURL.Scheme + "://" + parsedURL.Host + service.ControlURL + + _, err = url.ParseRequestURI(ex.ConnectionManagerURL) if err != nil { return nil, fmt.Errorf("DMRextractor invalid ConnectionManagerURL: %w", err) }