Пример #1
0
        /// <summary>
        /// 写真読み込み家の初期化
        /// </summary>
        private void PhotoInitialize()
        {
            #region ファイル読み込み
            SelectPhotoDirCommand = new ReactiveCommand();
            SelectPhotoDirCommand.Subscribe(() => SelectDirectory(path => {
                Config.Value.PhotoDir = path;
                Config.ForceNotify();
            }));
            SelectExportDirCommand = new ReactiveCommand();
            SelectExportDirCommand.Subscribe(() => SelectDirectory(path => {
                Config.Value.ExportDir = path;
                Config.ForceNotify();
            }));
            ReadPhotoCommand = new ReactiveCommand();
            ReadPhotoCommand.Subscribe(async() => {
                IsPhotoUIEnable.Value = false;

                var photos = await LoadPhotos(new Progress <string>(msg => Log(msg)));
                //反映
                Photos.Clear();
                Tags.Clear();
                Photos.AddRangeOnScheduler(photos);
                Tags.AddRangeOnScheduler(Config.Value.Tags);

                IsPhotoUIEnable.Value = true;
                Log($"{photos.Length}枚の画像を読み込み。プレビュー画像が古い場合、{Config.Value.PreviewTempPath}を削除してから再度実行してください。");
            });
            #endregion

            // フィルタ
            IsPhotoUIEnable.PropertyChangedAsObservable()
            .Select(x => Photos.AsEnumerable())
            .CombineLatest(TagFilterAll, TagFilterNone, TagFilterTag, TagFilterSelectedTag,
                           (p, isAll, isNone, isTag, tagText) => {
                // RadioButtonが2回判定が来る, trueがふたつあるときは無視
                if ((isAll ? 1 : 0) + (isNone ? 1 : 0) + (isTag ? 1 : 0) > 1)
                {
                    return(null);
                }
                if (isAll)
                {
                    return(p);
                }
                else if (isNone)
                {
                    return(p.Where(x => x.TagDetails.Length == 0));
                }
                else if (isTag)
                {
                    return(p.Where(x => x.TagDetails.Any(t => t.ToString().Equals(tagText))));
                }
                else
                {
                    throw new NotImplementedException();
                }
            })
            .Where(photos => photos != null)
            .Subscribe(photos => {
                FilteredPhotos.Clear();
                FilteredPhotos.AddRangeOnScheduler(photos);
            });
            #region ファイル名変更
            SelectedPhoto.Where(x => x != null)
            .Subscribe(p => {
                ChangeFileName.Value = p.OriginalName;
            });
            ChangeFileNameCommand =
                ChangeFileName.Select(x => !string.IsNullOrWhiteSpace(x))
                .ToReactiveCommand();
            ChangeFileNameCommand.Subscribe(() => {
                var oldName = SelectedPhoto.Value.OriginalName;
                var newName = ChangeFileName.Value;
                var newPath = Path.GetFullPath($"{Config.Value.PhotoDir}/{newName}");
                if (File.Exists(newPath))
                {
                    Log("すでに存在するファイル名です");
                    return;
                }
                File.Move(SelectedPhoto.Value.OriginalPath, newPath);
                SelectedPhoto.Value.OriginalName = newName;
                // サムネが消えるのでリロードする
                SelectedPhoto.Value.GeneratePreviewImage();

                Log($"ファイル名を変更 {oldName} -> {newName}");
            });
            #endregion

            #region Tag
            AddTagCommand =
                NewTagName.Select(x => !string.IsNullOrWhiteSpace(x))
                .ToReactiveCommand(false);

            AddTagCommand.Subscribe(() => {
                var tag = Tags.FirstOrDefault(x => x.Name.Equals(NewTagName.Value));
                if (tag == null)
                {
                    Tags.AddOnScheduler(new Tag()
                    {
                        CreateAt    = DateTime.Now,
                        Name        = NewTagName.Value,
                        Description = NewTagDescription.Value,
                    });
                }
                else
                {
                    tag.Description = NewTagDescription.Value;
                }
            });
            ToggleTagCommand =
                SelectedPhoto.Select(x => x != null)
                .ToReactiveCommand <Tag>(false);
            //タグがある場合は削除、なければ追加
            ToggleTagCommand.Subscribe(t => {
                var tagList = SelectedPhoto.Value.TagDetails.ToList();
                var target  = tagList.FirstOrDefault(x => x.Name.Equals(t.Name));
                if (target != null)
                {
                    tagList.Remove(target);
                }
                else
                {
                    tagList.Add(t);
                }
                SelectedPhoto.Value.TagDetails = tagList.ToArray();
                SelectedPhoto.ForceNotify();
            });
            #endregion

            #region Export
            ExportCommand = IsPhotoUIEnable.ToReactiveCommand();
            ExportCommand.Subscribe(async() => {
                IsPhotoUIEnable.Value = false;
                // ファイルの削除
                if (Directory.Exists(Config.Value.ExportDir))
                {
                    Directory.Delete(Config.Value.ExportDir, true);
                    Log($"{Config.Value.ExportDir}を削除");
                }
                // データのエクスポート
                await ExportPhotos(new Progress <string>(msg => Log(msg)));
                // Webから参照するデータをエクスポートする
                var data    = new JObject() as dynamic;
                data.photos = JArray.FromObject(Photos.ToArray());
                data.tags   = JArray.FromObject(Tags.ToArray());
                data.update = DateTime.Now;
                var json    = JsonConvert.SerializeObject(data, Formatting.Indented);
                File.WriteAllText($"{Config.Value.ExportDir}/{Config.Value.ExportFileName}", json, Encoding.UTF8);
                Log($"{Config.Value.ExportFileName}に設定ファイルを出力");

                // 情報のエクスポート
                Config.Value.Photos = Photos.ToArray();
                Config.Value.Tags   = Tags.ToArray();
                SaveConfig();

                IsPhotoUIEnable.Value = true;
                Log("エクスポート完了");

                Process.Start(Config.Value.ExportDir);
            });

            #endregion

            ClosingCommand = new ReactiveCommand();
            ClosingCommand.Subscribe(() => {
                SaveConfig();
            });
        }