コード例 #1
0
        public BaseApiResult SetupPlayer(WallpaperType wpType, string customDownloadUrl)
        {
            string url = customDownloadUrl;

            if (string.IsNullOrEmpty(url))
            {
                url = WallpaperApi.PlayerUrls.FirstOrDefault(m => m.Type == wpType).DownloadUrl;
            }

            void WallpaperManager_SetupPlayerProgressChangedEvent(object sender, SetupPlayerProgressChangedArgs e)
            {
                _lastSetupPlayerRaiseLimiter.Execute(async() =>
                {
                    try
                    {
                        System.Diagnostics.Debug.WriteLine($"{e.ProgressPercentage} {e.ActionType}");
                        //向所有客户端推送,刷新后也能显示
                        var client = _hubEventEmitter.AllClient();
                        await client.SendAsync("SetupPlayerProgressChanged", e);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex);
                    }
                }, 1000);
            }

            _lastSetupPlayerRaiseLimiter = new RaiseLimiter();
            WallpaperApi.SetupPlayerProgressChangedEvent -= WallpaperManager_SetupPlayerProgressChangedEvent;
            WallpaperApi.SetupPlayerProgressChangedEvent += WallpaperManager_SetupPlayerProgressChangedEvent;
            var result = WallpaperApi.SetupPlayer(wpType, url, (async _ =>
            {
                //设置完成
                await _lastSetupPlayerRaiseLimiter.WaitExit();
                WallpaperApi.SetupPlayerProgressChangedEvent -= WallpaperManager_SetupPlayerProgressChangedEvent;
            }));

            if (result.Ok)
            {
                //开始成功
                //_lastSetupPlayerRaiseLimiter = new RaiseLimiter();
                //WallpaperApi.SetupPlayerProgressChangedEvent += WallpaperManager_SetupPlayerProgressChangedEvent;
                //_lastConnectionId = Context.ConnectionId;
            }

            return(result);
        }
コード例 #2
0
        //下载壁纸
        public Task <BaseApiResult> DownloadWallpaper(string wallpaper, string cover, WallpaperProjectInfo info)
        {
            if (wallpaper == null)
            {
                return(Task.FromResult(BaseApiResult.ErrorState(ErrorType.Failed)));
            }

            string destFolder = null;

            if (info != null && info.ID != null)
            {
                destFolder = Path.Combine(AppManager.UserSetting.Wallpaper.WallpaperSaveDir, info.ID);
            }
            else
            {
                destFolder = WallpaperApi.GetDraftDir(AppManager.UserSetting.Wallpaper.WallpaperSaveDir);
            }

            if (info == null)
            {
                info = new WallpaperProjectInfo();
            }

            CancellationTokenSource cts = new CancellationTokenSource();

            _ = Task.Run(async() =>
            {
                RaiseLimiter _raiseLimiter = new RaiseLimiter();

                var wpProgressInfo = new Progress <(float competed, float total)>((e) =>
                {
                    _raiseLimiter.Execute(async() =>
                    {
                        var client = _hubEventEmitter.AllClient();
                        await client.SendAsync("DownloadWallpaperProgressChanged", new { path = wallpaper, e.competed, e.total, percent = e.competed / e.total * 50, completed = false });
                    }, 1000);
                });

                var coverProgressInfo = new Progress <(float competed, float total)>((e) =>
                {
                    _raiseLimiter.Execute(async() =>
                    {
                        var client  = _hubEventEmitter.AllClient();
                        var percent = e.competed / e.total * 50 + 50;
                        await client.SendAsync("DownloadWallpaperProgressChanged", new { path = cover, e.competed, e.total, percent, completed = percent == 100 });
                    }, 1000);
                });

                info.File     = Path.GetFileName(wallpaper);
                string destWp = Path.Combine(destFolder, info.File);
                await NetworkHelper.DownloadFileAsync(wallpaper, destWp, cts.Token, wpProgressInfo);
                if (cover != null)
                {
                    info.Preview     = Path.GetFileName(cover);
                    string destCover = Path.Combine(destFolder, info.Preview);
                    await NetworkHelper.DownloadFileAsync(cover, destCover, cts.Token, coverProgressInfo);
                }

                //生成json
                await UpdateProjectInfo(destFolder, info);
            });
            return(Task.FromResult(BaseApiResult.SuccessState()));
        }