예제 #1
0
        public HohoemaVideoListingPageViewModelBase(HohoemaApp app, PageManager pageManager, MylistRegistrationDialogService mylistDialogService, bool isRequireSignIn = true, bool useDefaultPageTitle = true)
            : base(app, pageManager, useDefaultPageTitle: useDefaultPageTitle)
        {
            MylistDialogService = mylistDialogService;


            var SelectionItemsChanged = SelectedItems.ToCollectionChanged().ToUnit();

#if DEBUG
            SelectedItems.CollectionChangedAsObservable()
            .Subscribe(x =>
            {
                Debug.WriteLine("Selected Count: " + SelectedItems.Count);
            });
#endif


            PlayAllCommand = SelectionItemsChanged
                             .Select(_ => SelectedItems.Count > 0)
                             .ToReactiveCommand(false)
                             .AddTo(_CompositeDisposable);

            PlayAllCommand
            .SubscribeOnUIDispatcher()
            .Subscribe(_ =>
            {
                // TODO: プレイリストに登録
                // プレイリストを空にしてから選択動画を登録

                //				SelectedVideoInfoItems.First()?.PlayCommand.Execute();
            })
            .AddTo(_CompositeDisposable);

            CancelCacheDownloadRequest = SelectionItemsChanged
                                         .Select(_ => SelectedItems.Count > 0)
                                         .ToReactiveCommand(false)
                                         .AddTo(_CompositeDisposable);

            CancelCacheDownloadRequest
            .SubscribeOnUIDispatcher()
            .Subscribe(async _ =>
            {
                var items  = EnumerateCacheRequestedVideoItems().ToList();
                var action = AsyncInfo.Run <uint>(async(cancelToken, progress) =>
                {
                    uint count = 0;
                    foreach (var item in items)
                    {
                        await item.NicoVideo.CancelCacheRequest();

                        ++count;
                        progress.Report(count);
                    }

                    ClearSelection();
                });

                await PageManager.StartNoUIWork("キャッシュリクエストをキャンセル中", items.Count, () => action);
            }
                       )
            .AddTo(_CompositeDisposable);


            // クオリティ指定無しのキャッシュDLリクエスト
            RequestCacheDownload = SelectionItemsChanged
                                   .Select(_ => SelectedItems.Count > 0 && CanDownload)
                                   .ToReactiveCommand(false)
                                   .AddTo(_CompositeDisposable);

            RequestCacheDownload
            .SubscribeOnUIDispatcher()
            .Subscribe(async _ =>
            {
                foreach (var item in EnumerateCanDownloadVideoItem())
                {
                    await item.NicoVideo.RequestCache(HohoemaApp.UserSettings.PlayerSettings.DefaultQuality);
                }

                ClearSelection();
                await UpdateList();
            })
            .AddTo(_CompositeDisposable);



            RegistratioMylistCommand = SelectionItemsChanged
                                       .Select(x => SelectedItems.Count > 0)
                                       .ToReactiveCommand(false)
                                       .AddTo(_CompositeDisposable);
            RegistratioMylistCommand
            .SubscribeOnUIDispatcher()
            .Subscribe(async _ =>
            {
                var mylists             = HohoemaApp.UserMylistManager.UserMylists;
                var localMylists        = HohoemaApp.Playlist.Playlists;
                var selectDialogService = App.Current.Container.Resolve <ContentSelectDialogService>();
                var result = await selectDialogService.ShowDialog(
                    "追加先マイリストを選択",
                    new List <ISelectableContainer>()
                {
                    new ChoiceFromListSelectableContainer("マイリスト",
                                                          mylists.Select(x => new SelectDialogPayload()
                    {
                        Label = x.Name, Id = x.Id, Context = x
                    })
                                                          ),
                    new ChoiceFromListSelectableContainer("ローカルマイリスト",
                                                          localMylists.Select(x => new SelectDialogPayload()
                    {
                        Label = x.Name, Id = x.Id, Context = x
                    })
                                                          )
                }
                    );

                if (result == null)
                {
                    return;
                }

                var targetMylist = result.Context as IPlayableList;

                var items  = SelectedItems.ToList();
                var action = AsyncInfo.Run <uint>(async(cancelToken, progress) =>
                {
                    uint progressCount = 0;


                    Debug.WriteLine($"一括マイリストに追加を開始...");
                    int successCount = 0;
                    int existCount   = 0;
                    int failedCount  = 0;
                    foreach (var video in SelectedItems)
                    {
                        if (targetMylist.Origin == PlaylistOrigin.LoginUser)
                        {
                            var mylistGroup        = targetMylist as MylistGroupInfo;
                            var registrationResult = await mylistGroup.Registration(
                                video.RawVideoId
                                , ""
                                , withRefresh: false /* あとで一括でリフレッシュ */
                                );

                            switch (registrationResult)
                            {
                            case Mntone.Nico2.ContentManageResult.Success: successCount++; break;

                            case Mntone.Nico2.ContentManageResult.Exist: existCount++; break;

                            case Mntone.Nico2.ContentManageResult.Failed: failedCount++; break;

                            default:
                                break;
                            }

                            Debug.WriteLine($"{video.Title}[{video.RawVideoId}]:{registrationResult.ToString()}");
                        }
                        else if (targetMylist.Origin == PlaylistOrigin.Local)
                        {
                            var localMylist = targetMylist as LocalMylist;
                            if (localMylist.PlaylistItems.FirstOrDefault(x => x.ContentId == video.RawVideoId) != null)
                            {
                                existCount++;
                            }
                            else
                            {
                                var resultItem = localMylist.AddVideo(video.RawVideoId, video.Title);
                                if (resultItem != null)
                                {
                                    successCount++;
                                }
                                else
                                {
                                    failedCount++;
                                }
                            }
                        }

                        progressCount++;
                        progress.Report(progressCount);
                    }


                    if (targetMylist.Origin == PlaylistOrigin.LoginUser)
                    {
                        var mylistGroup = targetMylist as MylistGroupInfo;
                        await mylistGroup.Refresh();

                        // マイリストに追加に失敗したものを残すように
                        // 登録済みのアイテムを選択アイテムリストから削除
                        foreach (var item in SelectedItems.ToArray())
                        {
                            if (mylistGroup.CheckRegistratedVideoId(item.RawVideoId))
                            {
                                SelectedItems.Remove(item);
                            }
                        }
                    }

                    // リフレッシュ



                    // ユーザーに結果を通知

                    var titleText    = $"「{targetMylist.Name}」に {successCount}件 の動画を登録しました";
                    var toastService = App.Current.Container.Resolve <ToastNotificationService>();
                    var resultText   = $"";
                    if (existCount > 0)
                    {
                        resultText += $"重複:{existCount} 件";
                    }
                    if (failedCount > 0)
                    {
                        resultText += $"\n登録に失敗した {failedCount}件 は選択されたままです";
                    }

                    toastService.ShowText(titleText, resultText);

                    //					ResetList();

                    Debug.WriteLine($"一括マイリストに追加を完了---------------");
                    ClearSelection();
                });

                await PageManager.StartNoUIWork("マイリストに追加", items.Count, () => action);
            }
                       );


            Playlists = HohoemaApp.Playlist.Playlists.ToReadOnlyReactiveCollection();
        }
        public HohoemaVideoListingPageViewModelBase(HohoemaApp app, PageManager pageManager, bool isRequireSignIn = true, bool useDefaultPageTitle = true)
            : base(app, pageManager, useDefaultPageTitle: useDefaultPageTitle)
        {
            var SelectionItemsChanged = SelectedItems.ToCollectionChanged().ToUnit();

#if DEBUG
            SelectedItems.CollectionChangedAsObservable()
            .Subscribe(x =>
            {
                Debug.WriteLine("Selected Count: " + SelectedItems.Count);
            })

            .AddTo(_CompositeDisposable);
#endif


            PlayAllCommand = SelectionItemsChanged
                             .Select(_ => SelectedItems.Count > 0)
                             .ToReactiveCommand(false)
                             .AddTo(_CompositeDisposable);

            PlayAllCommand
            .SubscribeOnUIDispatcher()
            .Subscribe(_ =>
            {
                // TODO: プレイリストに登録
                // プレイリストを空にしてから選択動画を登録

                //				SelectedVideoInfoItems.First()?.PlayCommand.Execute();
            })
            .AddTo(_CompositeDisposable);

            CancelCacheDownloadRequest = SelectionItemsChanged
                                         .Select(_ => SelectedItems.Count > 0)
                                         .ToReactiveCommand(false)
                                         .AddTo(_CompositeDisposable);

            CancelCacheDownloadRequest
            .SubscribeOnUIDispatcher()
            .Subscribe(async _ =>
            {
                var items  = EnumerateCacheRequestedVideoItems().ToList();
                var action = AsyncInfo.Run <uint>(async(cancelToken, progress) =>
                {
                    uint count = 0;
                    foreach (var item in items)
                    {
                        foreach (var quality in item.CachedQualityVideos.ToArray())
                        {
                            await HohoemaApp.CacheManager.CancelCacheRequest(item.RawVideoId, quality.Quality);
                        }

                        ++count;
                        progress.Report(count);
                    }

                    ClearSelection();
                });

                await PageManager.StartNoUIWork("キャッシュリクエストをキャンセル中", items.Count, () => action);
            }
                       )
            .AddTo(_CompositeDisposable);


            // クオリティ指定無しのキャッシュDLリクエスト
            RequestCacheDownload = SelectionItemsChanged
                                   .Select(_ => SelectedItems.Count > 0 && CanDownload)
                                   .ToReactiveCommand(false)
                                   .AddTo(_CompositeDisposable);

            RequestCacheDownload
            .SubscribeOnUIDispatcher()
            .Subscribe(async _ =>
            {
                foreach (var item in SelectedItems)
                {
                    await HohoemaApp.CacheManager.RequestCache(item.RawVideoId, NicoVideoQuality.Smile_Original);
                }

                ClearSelection();
                await UpdateList();
            })
            .AddTo(_CompositeDisposable);



            RegistratioMylistCommand = SelectionItemsChanged
                                       .Select(x => SelectedItems.Count > 0)
                                       .ToReactiveCommand(false)
                                       .AddTo(_CompositeDisposable);
            RegistratioMylistCommand
            .SubscribeOnUIDispatcher()
            .Subscribe(async _ =>
            {
                var targetMylist = await HohoemaApp.ChoiceMylist();

                if (targetMylist == null)
                {
                    return;
                }

                var items  = SelectedItems.ToList();
                var action = AsyncInfo.Run <uint>(async(cancelToken, progress) =>
                {
                    uint progressCount = 0;

                    Debug.WriteLine($"一括マイリストに追加を開始...");
                    int successCount = 0;
                    int existCount   = 0;
                    int failedCount  = 0;
                    foreach (var video in SelectedItems)
                    {
                        var registrationResult = await HohoemaApp.AddMylistItem(targetMylist, video.Label, video.RawVideoId);

                        switch (registrationResult)
                        {
                        case Mntone.Nico2.ContentManageResult.Success: successCount++; break;

                        case Mntone.Nico2.ContentManageResult.Exist: existCount++; break;

                        case Mntone.Nico2.ContentManageResult.Failed: failedCount++; break;

                        default:
                            break;
                        }

                        Debug.WriteLine($"{video.Label}[{video.RawVideoId}]:{registrationResult.ToString()}");

                        progressCount++;
                        progress.Report(progressCount);
                    }


                    if (targetMylist.Origin == PlaylistOrigin.LoginUser)
                    {
                        var mylistGroup = targetMylist as MylistGroupInfo;
                        await mylistGroup.Refresh();

                        // マイリストに追加に失敗したものを残すように
                        // 登録済みのアイテムを選択アイテムリストから削除
                        foreach (var item in SelectedItems.ToArray())
                        {
                            if (mylistGroup.CheckRegistratedVideoId(item.RawVideoId))
                            {
                                SelectedItems.Remove(item);
                            }
                        }
                    }

                    // リフレッシュ



                    // ユーザーに結果を通知

                    var titleText  = $"「{targetMylist.Label}」に {successCount}件 の動画を登録しました";
                    var resultText = $"";
                    if (existCount > 0)
                    {
                        resultText += $"重複:{existCount} 件";
                    }
                    if (failedCount > 0)
                    {
                        resultText += $"\n登録に失敗した {failedCount}件 は選択されたままです";
                    }

                    (App.Current as App).PublishInAppNotification(InAppNotificationPayload.CreateReadOnlyNotification(
                                                                      titleText,
                                                                      TimeSpan.FromSeconds(7)
                                                                      ));
                    //					ResetList();

                    Debug.WriteLine($"一括マイリストに追加を完了---------------");
                    ClearSelection();
                });

                await PageManager.StartNoUIWork("マイリストに追加", items.Count, () => action);
            }
                       )
            .AddTo(_CompositeDisposable);


            Playlists = HohoemaApp.Playlist.Playlists.ToReadOnlyReactiveCollection();
        }