Skip to content

Commit 191bb36

Browse files
Fix bugs
1 parent a8b2fb2 commit 191bb36

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

download.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func startDownload(url string, path string, contentLength int64, outputFile *os.
3232
return
3333
}
3434
for activeWorkers >= workers {
35-
time.Sleep(500 * time.Millisecond)
35+
time.Sleep(200 * time.Millisecond)
3636
}
3737
label := fmt.Sprintf("Worker %v/%v", workerId, int64(contentLength/chunkSize))
3838
progressBar := widget.NewProgressBar()
@@ -43,17 +43,19 @@ func startDownload(url string, path string, contentLength int64, outputFile *os.
4343
}
4444
threadContainer.Add(progressBarContainer.container)
4545
go downloadChunk(url, path, workerId, outputFile, offset, progressBarContainer)
46-
activeWorkers++
46+
time.Sleep(50 * time.Millisecond)
4747
workerId++
4848
}
4949
for activeWorkers > 0 {
5050
time.Sleep(1 * time.Second)
5151
}
52-
dialog.ShowInformation("Download Complete", "Your file has been successfully downloaded!", mainWindow)
53-
enableDownloads()
52+
if downloading {
53+
dialog.ShowInformation("Download Complete", "Your file has been successfully downloaded!", mainWindow)
54+
}
5455
}
5556

5657
func downloadChunk(url string, path string, workerId int, outputFile *os.File, offset int64, progressBarContainer *ChunkContainer) {
58+
activeWorkers++
5759
success := false
5860

5961
for !success {
@@ -100,14 +102,15 @@ func downloadChunk(url string, path string, workerId int, outputFile *os.File, o
100102
)
101103
if err != nil {
102104
if err.Error() == "cancelled" {
103-
break
105+
activeWorkers--
106+
return
104107
}
105108
dialog.ShowInformation("Error (retrying)", fmt.Sprintf("Worker %v has ran into an error:\n%v", workerId, wrapText(err.Error())), mainWindow)
106109
continue
107110
}
108111
success = true
109112
}
110113

111-
usedContainers = append(usedContainers, progressBarContainer.container)
112114
activeWorkers--
115+
usedContainers = append(usedContainers, progressBarContainer.container)
113116
}

main.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ func main() {
110110

111111
func cleanContainers() {
112112
for downloading || activeWorkers >= 1 {
113-
for _, container := range usedContainers {
113+
removeIndex := -1
114+
for index, container := range usedContainers {
114115
threadContainer.Remove(container)
116+
removeIndex = index
117+
}
118+
fmt.Println(len(usedContainers), activeWorkers)
119+
if removeIndex != -1 {
120+
usedContainers = append(usedContainers[:removeIndex], usedContainers[removeIndex+1:]...)
115121
}
116122
threadContainer.Refresh()
117123
time.Sleep(50 * time.Millisecond)
@@ -122,6 +128,8 @@ func enableDownloads() {
122128
downloadButton.SetText("Download")
123129
downloadButton.Enable()
124130
downloading = false
131+
activeWorkers = 0
132+
usedContainers = []*fyne.Container{}
125133
threadContainer.RemoveAll()
126134
threadContainer.Add(layout.NewSpacer())
127135
threadContainer.Add(fyne.NewContainerWithLayout(layout.NewCenterLayout(), widget.NewLabel("There are no active workers...")))
@@ -211,9 +219,7 @@ func startDownloadManager(urlEntry *widget.Entry, pathEntry *widget.Entry) {
211219
}
212220

213221
startDownload(url, path, contentLength, outputFile)
214-
if downloading {
215-
enableDownloads()
216-
}
222+
enableDownloads()
217223
}
218224

219225
func showAdvancedOptions() {

0 commit comments

Comments
 (0)