realtime AddToDownloadList

This commit is contained in:
josc146 2023-05-21 12:07:53 +08:00
parent d72144d8c8
commit 8d41215b12

View File

@ -43,19 +43,16 @@ func existsInDownloadList(url string) bool {
func (a *App) PauseDownload(url string) { func (a *App) PauseDownload(url string) {
for i, ds := range downloadList { for i, ds := range downloadList {
if ds.Url == url { if ds.Url == url {
ds.resp.Cancel() if ds.resp != nil {
ds.resp.Cancel()
}
downloadList[i] = DownloadStatus{ downloadList[i] = DownloadStatus{
resp: ds.resp, resp: nil,
Name: ds.Name, Name: ds.Name,
Path: ds.Path, Path: ds.Path,
Url: ds.Url, Url: ds.Url,
Transferred: ds.Transferred,
Size: ds.Size,
Speed: 0,
Progress: ds.Progress,
Downloading: false, Downloading: false,
Done: ds.Done,
} }
} }
} }
@ -73,12 +70,7 @@ func (a *App) ContinueDownload(url string) {
Name: ds.Name, Name: ds.Name,
Path: ds.Path, Path: ds.Path,
Url: ds.Url, Url: ds.Url,
Transferred: ds.Transferred,
Size: ds.Size,
Speed: ds.Speed,
Progress: ds.Progress,
Downloading: true, Downloading: true,
Done: ds.Done,
} }
} }
} }
@ -91,12 +83,7 @@ func (a *App) AddToDownloadList(path string, url string) {
Name: filepath.Base(path), Name: filepath.Base(path),
Path: path, Path: path,
Url: url, Url: url,
Transferred: 0,
Size: 0,
Speed: 0,
Progress: 0,
Downloading: true, Downloading: true,
Done: false,
}) })
a.ContinueDownload(url) a.ContinueDownload(url)
} else { } else {
@ -110,17 +97,31 @@ func (a *App) downloadLoop() {
for { for {
<-ticker.C <-ticker.C
for i, ds := range downloadList { for i, ds := range downloadList {
transferred := int64(0)
size := int64(0)
speed := float64(0)
progress := float64(0)
downloading := ds.Downloading
done := false
if ds.resp != nil {
transferred = ds.resp.BytesComplete()
size = ds.resp.Size()
speed = ds.resp.BytesPerSecond()
progress = 100 * ds.resp.Progress()
downloading = !ds.resp.IsComplete()
done = ds.resp.Progress() == 1
}
downloadList[i] = DownloadStatus{ downloadList[i] = DownloadStatus{
resp: ds.resp, resp: ds.resp,
Name: ds.Name, Name: ds.Name,
Path: ds.Path, Path: ds.Path,
Url: ds.Url, Url: ds.Url,
Transferred: ds.resp.BytesComplete(), Transferred: transferred,
Size: ds.resp.Size(), Size: size,
Speed: ds.resp.BytesPerSecond(), Speed: speed,
Progress: 100 * ds.resp.Progress(), Progress: progress,
Downloading: !ds.resp.IsComplete(), Downloading: downloading,
Done: ds.resp.Progress() == 1, Done: done,
} }
} }
runtime.EventsEmit(a.ctx, "downloadList", downloadList) runtime.EventsEmit(a.ctx, "downloadList", downloadList)