Beispiel #1
0
 //Zkratka pro vyvolání údalosti při chybě
 private void CallError(string message)
 {
     State = States.Error;
     operation.Post(new SendOrPostCallback(delegate(object state)
     {
         OnDownloadError?.Invoke(this, message);
     }), null);
 }
        private void NetworkStateChanged(Object sender, ConnectivityChangedEventArgs e)
        {
            OnDownloadError.Invoke(this, e);

            if (!_webClient.IsBusy && e.NetworkAccess == Xamarin.Essentials.NetworkAccess.Internet)
            {
                _webClient.DownloadFileAsync(new  Uri(url), pathToNewFile);
            }
        }
Beispiel #3
0
        private void ProcessEvent()
        {
            if (m_Event.Count > 0)
            {
                DownloadEvent de = m_Event.Pop();
                m_Event.Clear();
                switch (de)
                {
                case DownloadEvent.Downloaded:
                {
                    SavaFileFromMemory();
                }
                break;

                case DownloadEvent.Begin:
                    break;

                case DownloadEvent.DownloadOrExit:
                    break;

                case DownloadEvent.Error:
                    OnDownloadError eL = m_OnError;
                    m_IsDownloading = false;
                    Clear();
                    if (eL != null)
                    {
                        eL(m_EventMsg);
                        eL = null;
                    }
                    break;

                case DownloadEvent.Finish:
                    OnDownloadFinished fL = m_OnFinished;
                    Clear();
                    if (fL != null)
                    {
                        fL(m_SaveFile, 0, 0);
                    }
                    break;

                case DownloadEvent.Progress:
                    break;

                default:
                    break;
                }
            }
        }
Beispiel #4
0
        public bool AddDownloadTask(string uri, string localPath, int fileSize, OnDownloadProgress onProgress, OnDownloadError onError, OnDownloadFinished onFinshed, OnDownloadBegin onBegin, OnDownloadValidCheck validChecker, bool logError)
        {
            if (m_IsDownloading)
            {
                Log.e("HttpDownloaderMgr is busy!");
                return(false);
            }

            if (string.IsNullOrEmpty(uri) == true)
            {
                Log.e("uri is empty");
                return(false);
            }

            if (string.IsNullOrEmpty(localPath) == true)
            {
                Log.e("LocalPath is empty");
                return(false);
            }

            if (onError == null || onFinshed == null)
            {
                Log.e("onError & onFinshed should not be null!");
                return(false);
            }

            Clear();

            m_LogError        = logError;
            m_FileSize        = fileSize;
            m_OnProgress      = onProgress;
            m_OnError         = onError;
            m_OnFinished      = onFinshed;
            m_OnDownloadBegin = onBegin;
            m_OnValidCheck    = validChecker;

            m_Uri      = uri;
            m_SaveFile = localPath;

            m_IsDownloading = true;

            m_PreSize        = 0;
            m_LastChangeTime = currentTimeTick;

            StartCoroutine(StartIEnumeratorTask());
            return(true);
        }
Beispiel #5
0
        // 添加下载任务,目前只支持一个任务同时进行
        public bool AddDownloadTask(string uri, string localPath, OnDownloadProgress onProgress, OnDownloadError onError, OnDownloadFinished onFinshed, OnDownloadBegin onBegin = null)
        {
            if (m_IsDownloading)
            {
                Log.e("HttpDownloaderMgr is busy!");
                return(false);
            }

            if (string.IsNullOrEmpty(uri) == true)
            {
                Log.e("uri is empty");
                return(false);
            }

            if (string.IsNullOrEmpty(localPath) == true)
            {
                Log.e("LocalPath is empty");
                return(false);
            }

            if (onError == null || onFinshed == null)
            {
                Log.e("onError & onFinshed should not be null!");
                return(false);
            }

            m_OnProgress      = onProgress;
            m_OnError         = onError;
            m_OnFinished      = onFinshed;
            m_OnDownloadBegin = onBegin;

            m_Uri      = uri;
            m_SaveFile = localPath;

            m_TaskCount++;

            //Log.i("[HttpDownload]about to download new data:" + m_Uri);

            return(true);
        }
Beispiel #6
0
        public void Clear()
        {
            if (m_IsDownloading)
            {
                return;
            }

            if (m_WWW != null)
            {
                m_WWW.Dispose();
                m_WWW = null;
            }

            m_PreSize        = 0;
            m_LastChangeTime = currentTimeTick;
            m_Event.Clear();

            m_OnProgress      = null;
            m_OnError         = null;
            m_OnFinished      = null;
            m_OnDownloadBegin = null;
            m_OnValidCheck    = null;
            //StopAllCoroutines();
        }
Beispiel #7
0
        /// <summary>
        /// 根据解析出来的模型进行下载
        /// </summary>
        /// <param name="basicModel"></param>
        /// <returns></returns>
        public async Task <bool> DownloadAsync(BasicMusicInfoModel basicModel)
        {
            return(await Task.Run(async() =>
            {
                var obj = (QqMusicInfoModel)basicModel;
                try
                {
                    OnBeforeDownload?.Invoke($"{basicModel.SongName}_{basicModel.SingerName}");
                    var vkeyInfo = await GetVkeyInfo(obj.SongId, obj.MediaMid);
                    var url = new Uri(string.Format(_downloadUrl, obj.MediaMid, vkeyInfo.data.items[0].vkey));

                    var saveName = $"{obj.SongName}_{obj.SingerName}";
                    await _requests.SaveFileAsync(url, $"{saveName}.m4a");

                    OnAfterDownload?.Invoke($"{basicModel.SongName}_{basicModel.SingerName}");
                    return true;
                }
                catch (Exception e)
                {
                    OnDownloadError?.Invoke(e, $"{basicModel.SongName}_{basicModel.SingerName}");
                    return false;
                }
            }));
        }
Beispiel #8
0
 private void OnError(ResultCode code)
 {
     OnDownloadError?.Invoke(code);
 }