Skip to content

Commit 9870076

Browse files
committed
refactor: Decompose subtitles setter into utils package
Signed-off-by: Felicitas Pojtinger <[email protected]>
1 parent 6014d2a commit 9870076

File tree

8 files changed

+99
-109
lines changed

8 files changed

+99
-109
lines changed

internal/components/controls.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,11 @@ func OpenControlsWindow(
11961196
Str("streamURL", streamURL).
11971197
Msg("Finished downloading subtitles")
11981198

1199-
SetSubtitles(ctx, &window, m, res.Body, tmpDir, ipcFile, &subtitleActivators[0], &subtitlesOverlay)
1199+
if err := utils.SetSubtitles(m, res.Body, tmpDir, ipcFile, &subtitleActivators[0], &subtitlesOverlay); err != nil {
1200+
OpenErrorDialog(ctx, &window, err)
1201+
1202+
return
1203+
}
12001204
}()
12011205
}
12021206
activator.ConnectActivate(&subtitleActivateCallback)
@@ -1600,7 +1604,11 @@ func OpenControlsWindow(
16001604
}
16011605
defer subtitlesFile.Close()
16021606

1603-
SetSubtitles(ctx, &window, m, subtitlesFile, tmpDir, ipcFile, &subtitleActivators[0], &subtitlesOverlay)
1607+
if err := utils.SetSubtitles(m, subtitlesFile, tmpDir, ipcFile, &subtitleActivators[0], &subtitlesOverlay); err != nil {
1608+
OpenErrorDialog(ctx, &window, err)
1609+
1610+
return
1611+
}
16041612

16051613
row := adw.NewActionRow()
16061614

@@ -1620,7 +1628,11 @@ func OpenControlsWindow(
16201628
}
16211629
defer subtitlesFile.Close()
16221630

1623-
SetSubtitles(ctx, &window, m, subtitlesFile, tmpDir, ipcFile, &subtitleActivators[0], &subtitlesOverlay)
1631+
if err := utils.SetSubtitles(m, subtitlesFile, tmpDir, ipcFile, &subtitleActivators[0], &subtitlesOverlay); err != nil {
1632+
OpenErrorDialog(ctx, &window, err)
1633+
1634+
return
1635+
}
16241636
}
16251637
activator.ConnectActivate(&fileSubtitleActivateCallback)
16261638

internal/components/subtitles.go renamed to internal/utils/subtitles.go

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
package components
1+
package utils
22

33
import (
44
. "github.com/pojntfx/go-gettext/pkg/i18n"
55

6-
"context"
76
"encoding/json"
87
"io"
98
"os"
@@ -19,36 +18,27 @@ import (
1918
)
2019

2120
func SetSubtitles(
22-
ctx context.Context,
23-
window *adw.ApplicationWindow,
24-
2521
filePath string,
2622
file io.Reader,
2723
tmpDir string,
2824
ipcFile string,
2925

3026
noneActivator *gtk.CheckButton,
3127
subtitlesOverlay *adw.ToastOverlay,
32-
) {
28+
) error {
3329
subtitlesDir, err := os.MkdirTemp(tmpDir, "subtitles")
3430
if err != nil {
35-
OpenErrorDialog(ctx, window, err)
36-
37-
return
31+
return err
3832
}
3933

4034
subtitlesFile := filepath.Join(subtitlesDir, path.Base(filePath))
4135
f, err := os.Create(subtitlesFile)
4236
if err != nil {
43-
OpenErrorDialog(ctx, window, err)
44-
45-
return
37+
return err
4638
}
4739

4840
if _, err := io.Copy(f, file); err != nil {
49-
OpenErrorDialog(ctx, window, err)
50-
51-
return
41+
return err
5242
}
5343

5444
if err := mpvClient.ExecuteMPVRequest(ipcFile, func(encoder *json.Encoder, decoder *json.Decoder) error {
@@ -63,9 +53,7 @@ func SetSubtitles(
6353
var successResponse mpv.ResponseSuccess
6454
return decoder.Decode(&successResponse)
6555
}); err != nil {
66-
OpenErrorDialog(ctx, window, err)
67-
68-
return
56+
return err
6957
}
7058

7159
var trackListResponse mpv.ResponseTrackList
@@ -78,9 +66,7 @@ func SetSubtitles(
7866

7967
return decoder.Decode(&trackListResponse)
8068
}); err != nil {
81-
OpenErrorDialog(ctx, window, err)
82-
83-
return
69+
return err
8470
}
8571

8672
sid := -1
@@ -108,16 +94,14 @@ func SetSubtitles(
10894
var successResponse mpv.ResponseSuccess
10995
return decoder.Decode(&successResponse)
11096
}); err != nil {
111-
OpenErrorDialog(ctx, window, err)
112-
113-
return
97+
return err
11498
}
11599

116100
toast := adw.NewToast(L("This file does not contain subtitles."))
117101

118102
subtitlesOverlay.AddToast(toast)
119103

120-
return
104+
return nil
121105
}
122106

123107
if err := mpvClient.ExecuteMPVRequest(ipcFile, func(encoder *json.Encoder, decoder *json.Decoder) error {
@@ -133,21 +117,15 @@ func SetSubtitles(
133117
var successResponse mpv.ResponseSuccess
134118
return decoder.Decode(&successResponse)
135119
}); err != nil {
136-
OpenErrorDialog(ctx, window, err)
137-
138-
return
120+
return err
139121
}
140122

141-
if err := mpvClient.ExecuteMPVRequest(ipcFile, func(encoder *json.Encoder, decoder *json.Decoder) error {
123+
return mpvClient.ExecuteMPVRequest(ipcFile, func(encoder *json.Encoder, decoder *json.Decoder) error {
142124
if err := encoder.Encode(mpv.Request{[]interface{}{"set_property", "sub-visibility", "yes"}}); err != nil {
143125
return err
144126
}
145127

146128
var successResponse mpv.ResponseSuccess
147129
return decoder.Decode(&successResponse)
148-
}); err != nil {
149-
OpenErrorDialog(ctx, window, err)
150-
151-
return
152-
}
130+
})
153131
}

po/de/LC_MESSAGES/default.po

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ msgstr "App neustarten"
1616
msgid "Select storage location"
1717
msgstr "Speicherort auswählen"
1818

19-
#: ../internal/components/subtitles.go:116
20-
msgid "This file does not contain subtitles."
21-
msgstr "Diese Datei enthält keine Untertitel."
22-
2319
#: ../internal/components/controls.go:366
2420
msgid "Someone joined the session."
2521
msgstr "Jemand ist der Sitzung beigetreten."
@@ -49,35 +45,35 @@ msgstr "%v MB/%v MB (%v Knoten)"
4945
msgid "Searching for peers"
5046
msgstr "Suche nach Knoten"
5147

52-
#: ../internal/components/controls.go:1206
48+
#: ../internal/components/controls.go:1210
5349
msgid "Disable subtitles"
5450
msgstr "Untertitel deaktivieren"
5551

56-
#: ../internal/components/controls.go:1211
52+
#: ../internal/components/controls.go:1215
5753
msgid "Integrated subtitle"
5854
msgstr "Integrierter Untertitel"
5955

60-
#: ../internal/components/controls.go:1214
56+
#: ../internal/components/controls.go:1218
6157
msgid "Subtitle from torrent"
6258
msgstr "Untertitel aus Torrent"
6359

64-
#: ../internal/components/controls.go:1217
60+
#: ../internal/components/controls.go:1221
6561
msgid "Extra file from torrent"
6662
msgstr "Zusatzdatei aus Torrent"
6763

68-
#: ../internal/components/controls.go:1306
64+
#: ../internal/components/controls.go:1310
6965
msgid "Disable audio"
7066
msgstr "Audio deaktivieren"
7167

72-
#: ../internal/components/controls.go:1308
68+
#: ../internal/components/controls.go:1312
7369
msgid "Track %v"
7470
msgstr "Spur %v"
7571

76-
#: ../internal/components/controls.go:1316
72+
#: ../internal/components/controls.go:1320
7773
msgid "Untitled Track"
7874
msgstr "Unbenannte Spur"
7975

80-
#: ../internal/components/controls.go:1628
76+
#: ../internal/components/controls.go:1640
8177
msgid "Manually added"
8278
msgstr "Manuell hinzugefügt"
8379

@@ -106,6 +102,10 @@ msgstr "Dieser Stream-Code ist ungültig."
106102
msgid "No README found."
107103
msgstr "Keine README-Datei gefunden."
108104

105+
#: ../internal/utils/subtitles.go:100
106+
msgid "This file does not contain subtitles."
107+
msgstr "Diese Datei enthält keine Untertitel."
108+
109109
#: ../mockups/playback/window.blp:7 ../assets/resources/controls.blp:8
110110
#: ../assets/resources/assistant.blp:7 ../assets/resources/assistant.blp:124
111111
#: ../assets/meta/com.pojtinger.felicitas.Multiplex.desktop.in:4

po/default.pot

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ msgstr ""
1010
msgid "Select storage location"
1111
msgstr ""
1212

13-
#: ../internal/components/subtitles.go:116
14-
msgid "This file does not contain subtitles."
15-
msgstr ""
16-
1713
#: ../internal/components/controls.go:366
1814
msgid "Someone joined the session."
1915
msgstr ""
@@ -43,35 +39,35 @@ msgstr ""
4339
msgid "Searching for peers"
4440
msgstr ""
4541

46-
#: ../internal/components/controls.go:1206
42+
#: ../internal/components/controls.go:1210
4743
msgid "Disable subtitles"
4844
msgstr ""
4945

50-
#: ../internal/components/controls.go:1211
46+
#: ../internal/components/controls.go:1215
5147
msgid "Integrated subtitle"
5248
msgstr ""
5349

54-
#: ../internal/components/controls.go:1214
50+
#: ../internal/components/controls.go:1218
5551
msgid "Subtitle from torrent"
5652
msgstr ""
5753

58-
#: ../internal/components/controls.go:1217
54+
#: ../internal/components/controls.go:1221
5955
msgid "Extra file from torrent"
6056
msgstr ""
6157

62-
#: ../internal/components/controls.go:1306
58+
#: ../internal/components/controls.go:1310
6359
msgid "Disable audio"
6460
msgstr ""
6561

66-
#: ../internal/components/controls.go:1308
62+
#: ../internal/components/controls.go:1312
6763
msgid "Track %v"
6864
msgstr ""
6965

70-
#: ../internal/components/controls.go:1316
66+
#: ../internal/components/controls.go:1320
7167
msgid "Untitled Track"
7268
msgstr ""
7369

74-
#: ../internal/components/controls.go:1628
70+
#: ../internal/components/controls.go:1640
7571
msgid "Manually added"
7672
msgstr ""
7773

@@ -99,6 +95,10 @@ msgstr ""
9995
msgid "No README found."
10096
msgstr ""
10197

98+
#: ../internal/utils/subtitles.go:100
99+
msgid "This file does not contain subtitles."
100+
msgstr ""
101+
102102
#: ../mockups/playback/window.blp:7 ../assets/resources/controls.blp:8
103103
#: ../assets/resources/assistant.blp:7 ../assets/resources/assistant.blp:124
104104
#: ../assets/meta/com.pojtinger.felicitas.Multiplex.desktop.in:4

po/en_GB/LC_MESSAGES/default.po

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ msgstr ""
1616
msgid "Select storage location"
1717
msgstr ""
1818

19-
#: ../internal/components/subtitles.go:116
20-
msgid "This file does not contain subtitles."
21-
msgstr ""
22-
2319
#: ../internal/components/controls.go:366
2420
msgid "Someone joined the session."
2521
msgstr ""
@@ -49,35 +45,35 @@ msgstr ""
4945
msgid "Searching for peers"
5046
msgstr ""
5147

52-
#: ../internal/components/controls.go:1206
48+
#: ../internal/components/controls.go:1210
5349
msgid "Disable subtitles"
5450
msgstr ""
5551

56-
#: ../internal/components/controls.go:1211
52+
#: ../internal/components/controls.go:1215
5753
msgid "Integrated subtitle"
5854
msgstr ""
5955

60-
#: ../internal/components/controls.go:1214
56+
#: ../internal/components/controls.go:1218
6157
msgid "Subtitle from torrent"
6258
msgstr ""
6359

64-
#: ../internal/components/controls.go:1217
60+
#: ../internal/components/controls.go:1221
6561
msgid "Extra file from torrent"
6662
msgstr ""
6763

68-
#: ../internal/components/controls.go:1306
64+
#: ../internal/components/controls.go:1310
6965
msgid "Disable audio"
7066
msgstr ""
7167

72-
#: ../internal/components/controls.go:1308
68+
#: ../internal/components/controls.go:1312
7369
msgid "Track %v"
7470
msgstr ""
7571

76-
#: ../internal/components/controls.go:1316
72+
#: ../internal/components/controls.go:1320
7773
msgid "Untitled Track"
7874
msgstr ""
7975

80-
#: ../internal/components/controls.go:1628
76+
#: ../internal/components/controls.go:1640
8177
msgid "Manually added"
8278
msgstr ""
8379

@@ -105,6 +101,10 @@ msgstr ""
105101
msgid "No README found."
106102
msgstr ""
107103

104+
#: ../internal/utils/subtitles.go:100
105+
msgid "This file does not contain subtitles."
106+
msgstr ""
107+
108108
#: ../mockups/playback/window.blp:7 ../assets/resources/controls.blp:8
109109
#: ../assets/resources/assistant.blp:7 ../assets/resources/assistant.blp:124
110110
#: ../assets/meta/com.pojtinger.felicitas.Multiplex.desktop.in:4

0 commit comments

Comments
 (0)