示例#1
0
        public FeedGroupPageViewModel(HohoemaApp hohoemaApp, PageManager pageManager, Views.Service.ContentSelectDialogService contentSelectDialogService)
            : base(hohoemaApp, pageManager)
        {
            ContentSelectDialogService = contentSelectDialogService;

            IsDeleted = new ReactiveProperty <bool>();

            FeedGroupName     = new ReactiveProperty <string>();
            MylistFeedSources = new ObservableCollection <FeedItemSourceListItem>();
            TagFeedSources    = new ObservableCollection <FeedItemSourceListItem>();
            UserFeedSources   = new ObservableCollection <FeedItemSourceListItem>();

            HasMylistFeedSource = MylistFeedSources.ObserveProperty(x => x.Count)
                                  .Select(x => x > 0)
                                  .ToReadOnlyReactiveProperty();
            HasTagFeedSource = TagFeedSources.ObserveProperty(x => x.Count)
                               .Select(x => x > 0)
                               .ToReadOnlyReactiveProperty();
            HasUserFeedSource = UserFeedSources.ObserveProperty(x => x.Count)
                                .Select(x => x > 0)
                                .ToReadOnlyReactiveProperty();


            MylistFavItems = new ObservableCollection <FollowItemInfo>();
            TagFavItems    = new ObservableCollection <FollowItemInfo>();
            UserFavItems   = new ObservableCollection <FollowItemInfo>();

            SelectFromFavItems = new ReactiveProperty <bool>(true);
            SelectedFavInfo    = new ReactiveProperty <FollowItemInfo>();

            FavItemType        = new ReactiveProperty <FollowItemType>();
            FeedSourceId       = new ReactiveProperty <string>();
            FeedSourceItemName = new ReactiveProperty <string>();
            ExistFeedSource    = new ReactiveProperty <bool>();
            IsPublicFeedSource = new ReactiveProperty <bool>();

            CanUseFeedSource = Observable.CombineLatest(
                ExistFeedSource,
                IsPublicFeedSource
                )
                               .Select(x => x.All(y => y))
                               .ToReactiveProperty();

            FavItemType.Subscribe(x =>
            {
                FeedSourceId.Value    = "";
                ExistFeedSource.Value = false;

                FeedSourceItemName.Value = "";


                // お気に入りアイテムがある場合は、「お気に入りから選択」をデフォルトに
                switch (x)
                {
                case FollowItemType.Tag:
                    SelectFromFavItems.Value = TagFavItems.Count > 0;
                    break;

                case FollowItemType.Mylist:
                    SelectFromFavItems.Value = MylistFavItems.Count > 0;
                    break;

                case FollowItemType.User:
                    SelectFromFavItems.Value = UserFavItems.Count > 0;
                    break;

                default:
                    break;
                }
            });

            FeedSourceId.ToUnit()
            .Subscribe(_ =>
            {
                ExistFeedSource.Value    = false;
                FeedSourceItemName.Value = "";
            });

            Observable.Merge(
                SelectFromFavItems.ToUnit(),

                SelectedFavInfo.ToUnit(),

                FavItemType.ToUnit(),
                FeedSourceId.ToUnit().Throttle(TimeSpan.FromSeconds(1))
                )
            .Subscribe(async x =>
            {
                if (SelectFromFavItems.Value)
                {
                    ExistFeedSource.Value    = SelectedFavInfo.Value != null;
                    IsPublicFeedSource.Value = true;
                    FeedSourceItemName.Value = "";
                    return;
                }

                ExistFeedSource.Value = false;

                if (FavItemType.Value == FollowItemType.Tag)
                {
                    ExistFeedSource.Value    = !string.IsNullOrWhiteSpace(FeedSourceId.Value);
                    IsPublicFeedSource.Value = true;
                    FeedSourceItemName.Value = FeedSourceId.Value;
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(FeedSourceId.Value))
                    {
                        ExistFeedSource.Value = false;
                    }
                    else
                    {
                        if (FavItemType.Value == FollowItemType.Mylist)
                        {
                            try
                            {
                                var mylistRes = await HohoemaApp.ContentFinder.GetMylistGroupDetail(FeedSourceId.Value);
                                var mylist    = mylistRes?.MylistGroup;

                                if (mylist != null)
                                {
                                    ExistFeedSource.Value    = true;
                                    IsPublicFeedSource.Value = mylist.IsPublic;
                                    FeedSourceItemName.Value = Mntone.Nico2.StringExtention.DecodeUTF8(mylist.Name);
                                }
                            }
                            catch
                            {
                                ExistFeedSource.Value = false;
                            }
                        }
                        else if (FavItemType.Value == FollowItemType.User)
                        {
                            try
                            {
                                var user = await HohoemaApp.ContentFinder.GetUserDetail(FeedSourceId.Value);
                                if (user != null)
                                {
                                    ExistFeedSource.Value    = true;
                                    IsPublicFeedSource.Value = !user.IsOwnerVideoPrivate;
                                    FeedSourceItemName.Value = user.Nickname;
                                }
                            }
                            catch
                            {
                                ExistFeedSource.Value = false;
                            }
                        }

                        if (!ExistFeedSource.Value)
                        {
                            IsPublicFeedSource.Value = false;
                            FeedSourceItemName.Value = "";
                        }
                    }
                }
            });

            AddFeedCommand =
                Observable.CombineLatest(
                    ExistFeedSource,
                    IsPublicFeedSource
                    )
                .Select(x => x.All(y => y == true))
                .ToReactiveCommand();

            AddFeedCommand.Subscribe(_ =>
            {
                string name = "";
                string id   = "";

                if (SelectFromFavItems.Value)
                {
                    var favInfo = SelectedFavInfo.Value;
                    name        = favInfo.Name;
                    id          = favInfo.Id;

                    if (favInfo.FollowItemType != FavItemType.Value)
                    {
                        throw new Exception();
                    }
                }
                else
                {
                    // idからMylistGroupを引く
                    // 公開されていない場合にはエラー
                    id   = FeedSourceId.Value;
                    name = FeedSourceItemName.Value;

                    FeedSourceItemName.Value = "";
                    FeedSourceId.Value       = "";
                }

                var favManager  = HohoemaApp.FollowManager;
                var feedManager = HohoemaApp.FeedManager;
                IFeedSource feedSource;
                switch (FavItemType.Value)
                {
                case FollowItemType.Tag:

                    feedSource = FeedGroup.AddTagFeedSource(id);
                    if (feedSource != null)
                    {
                        var favInfo = favManager.Tag.FollowInfoItems.SingleOrDefault(x => x.Id == id);
                        if (favInfo != null)
                        {
                            TagFavItems.Remove(favInfo);
                        }

                        TagFeedSources.Add(new FeedItemSourceListItem(feedSource, this));
                    }

                    break;

                case FollowItemType.Mylist:

                    feedSource = FeedGroup.AddMylistFeedSource(name, id);
                    if (feedSource != null)
                    {
                        var favInfo = favManager.Mylist.FollowInfoItems.SingleOrDefault(x => x.Id == id);
                        if (favInfo != null)
                        {
                            MylistFavItems.Remove(favInfo);
                        }

                        MylistFeedSources.Add(new FeedItemSourceListItem(feedSource, this));
                    }

                    break;

                case FollowItemType.User:

                    feedSource = FeedGroup.AddUserFeedSource(name, id);
                    if (feedSource != null)
                    {
                        var favInfo = favManager.User.FollowInfoItems.SingleOrDefault(x => x.Id == id);
                        if (favInfo != null)
                        {
                            UserFavItems.Remove(favInfo);
                        }

                        UserFeedSources.Add(new FeedItemSourceListItem(feedSource, this));
                    }

                    break;

                default:
                    break;
                }

                HohoemaApp.FeedManager.SaveOne(FeedGroup);
            });

            RenameApplyCommand = FeedGroupName
                                 .Where(x => HohoemaApp.FeedManager != null && x != null)
                                 .Select(x => HohoemaApp.FeedManager.CanAddLabel(x))
                                 .ToReactiveCommand();

            RenameApplyCommand.Subscribe(async _ =>
            {
                if (await FeedGroup.Rename(FeedGroupName.Value))
                {
                    UpdateTitle(FeedGroup.Label);
                }

                FeedGroupName.ForceNotify();
            });
        }