Exemple #1
0
 public App()
 {
     Locator.CurrentMutable.RegisterConstant(new BookmarksSource(), typeof(IBookmarksSource));
     bookmarks = Locator.Current.GetService <IBookmarksSource>();
     Locator.CurrentMutable.RegisterConstant(new UserConfig(), typeof(IUserConfig));
     config = Locator.Current.GetService <IUserConfig>();
     Locator.CurrentMutable.RegisterConstant(new DataSource(config), typeof(IDataSource));
     //
     Locator.CurrentMutable.RegisterLazySingleton(() => new AppViewModel(Locator.Current.GetService <IDataSource>(), Locator.Current.GetService <IBookmarksSource>(), Locator.Current.GetService <IUserConfig>()));
     Locator.CurrentMutable.RegisterLazySingleton(() => new MainWindow(), typeof(IViewFor <AppViewModel>));
     Locator.CurrentMutable.RegisterLazySingleton(() => new BookmarkView(), typeof(IViewFor <BookmarkViewModel>));
     Locator.CurrentMutable.RegisterLazySingleton(() => new SettingView(), typeof(IViewFor <SettingViewModel>));
     //
 }
        public BookmarkViewModel(Action <BookmarkViewModel, Bookmark> closeCallback, IBookmarksSource bookmarks)
        {
            BookmarksSource = bookmarks ?? Locator.Current.GetService <IBookmarksSource>();

            BookmarksSource.Bookmarks.Connect().ObserveOn(RxApp.MainThreadScheduler)
            .Sort(new BookmarkComparer(StringComparison.OrdinalIgnoreCase))
            .Bind(BookmarkList).DisposeMany().Subscribe();

            _closeCallback = closeCallback;
            Open           = ReactiveCommand.Create(() =>
            {
                _closeCallback(this, SelectedBookmark);
            });

            Cancel = ReactiveCommand.Create(() =>
            {
                _closeCallback(this, null);
            });

            DeleteBookmark = ReactiveCommand.Create <Bookmark>(bookmark => BookmarksSource.Delete(bookmark));
        }
        public AppViewModel(IDataSource dataSource, IBookmarksSource bookmarksSource, IUserConfig config)
        {
            DataSource      = dataSource ?? Locator.Current.GetService <IDataSource>();
            BookmarksSource = bookmarksSource ?? Locator.Current.GetService <IBookmarksSource>();
            Config          = config ?? Locator.Current.GetService <IUserConfig>();

            Initialize();

            #region Toggle Fullscreen

            ToggleFullscreen = ReactiveCommand.Create(() => IsFullscreen = !IsFullscreen);

            #endregion

            #region Scroll Change

            this.WhenAnyValue(x => x._activeImage).Subscribe(x => ActiveImage = x + 1);

            #endregion Scroll Change

            #region ChapterList Change

            DataSource.ChapterList.Connect().Bind(_chapterList).Subscribe();
            this.WhenAnyValue(x => x._chapterList.Count)
            .Subscribe(_ =>
            {
                ChapterList = _chapterList?.Select(x => x.File).ToList();
            });

            #endregion ChapterList Change

            #region Chapter Change

            NextClick     = ReactiveCommand.Create(() => _activeIndex = _activeIndex >= ChapterList.Count - 1 ? ChapterList.Count - 1 : _activeIndex + 1);
            PreviousClick = ReactiveCommand.Create(() => _activeIndex = _activeIndex <= 0 ? 0 : _activeIndex - 1);
            this.WhenAnyValue(x => x._activeIndex)
            .Subscribe(activeIndex =>
            {
                ActiveIndex = activeIndex;
                UpdateAsync().ConfigureAwait(false);
                EnablePrevClick = activeIndex > 0;
                EnableNextClick = activeIndex < ChapterList.Count - 1;
            });
            DataSource.ImageList.Connect().ObserveOn(RxApp.MainThreadScheduler)
            .OnItemAdded(x =>
            {
                var sum    = ImageDimension.Count == 0 ? 0 : ImageHeight.Last();
                var width  = Math.Min(x.Width, ViewportWidth);
                var height = x.Width < ViewportWidth ? x.Height : x.Height * (ViewportWidth / x.Width);
                sum       += (height * ZoomScale) + _imageMarginSetter;
                ImageDimension.Add((width, height));
                ImageHeight.Add(sum);
            }).Bind(ImageList).DisposeMany().Subscribe();

            this.WhenAnyValue(x => x.ImageList.Count)
            .Subscribe(x =>
            {
                ImageCount = x;
            });

            #endregion Chapter Change

            #region Viewport Change

            this.WhenAnyValue(x => x.ViewportWidth).Subscribe(newViewport =>
            {
                try
                {
                    for (int i = 0; i < ImageDimension.Count; i++)
                    {
                        if (Ts.IsCancellationRequested)
                        {
                            return;
                        }
                        var width         = Math.Min(ImageList[i].Width, newViewport);
                        var height        = ImageList[i].Width < newViewport ? ImageList[i].Height : ImageList[i].Height * (newViewport / ImageList[i].Width);
                        ImageDimension[i] = (width, height);
                    }
                    UpdateImageHeight();
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.Print(e.ToString()); // to do : fix here
                }
            });
            #endregion

            #region Settings

            #region Zoom

            IncreaseZoom = ReactiveCommand.Create(() => ZoomScaleSetter = (_zoomScaleSetter + 10).ToString());
            DecreaseZoom = ReactiveCommand.Create(() => ZoomScaleSetter = _zoomScaleSetter >= 11 ? (_zoomScaleSetter - 10).ToString() : "10");
            this.WhenAnyValue(x => x.ZoomScaleSetter)
            .Subscribe(_ =>
            {
                var succes = int.TryParse(ZoomScaleSetter, out int number);
                if (succes && number != _zoomScaleSetter)
                {
                    _zoomScaleSetter = number;
                    if (_zoomScaleSetter < 10)
                    {
                        _zoomScaleSetter = 10;
                    }
                    ZoomScale = _zoomScaleSetter == 100 ? 1 : Math.Round(_zoomScaleSetter / 99.999999999999, 3);
                    UpdateImageHeight();
                }
                ZoomScaleSetter = _zoomScaleSetter.ToString();
            });

            #endregion Zoom

            #region Margin

            this.WhenAnyValue(x => x.ImageMarginSetter)
            .Subscribe(_ =>
            {
                var succes = int.TryParse(ImageMarginSetter, out int number);
                if (succes && number != _imageMarginSetter)
                {
                    _imageMarginSetter = number;
                    if (_imageMarginSetter < 0)
                    {
                        _imageMarginSetter = 0;
                    }
                    Config.ImageMargin = _imageMarginSetter;
                    UpdateImageHeight();
                }
                ImageMarginSetter = _imageMarginSetter.ToString();
                ImageMargin       = $"0,0,0,{_imageMarginSetter}";
            });

            #endregion Margin

            #region Scroll Increment

            this.WhenAnyValue(x => x.ScrollIncrement)
            .Subscribe(_ =>
            {
                var succes = int.TryParse(ScrollIncrement, out int number);
                if (succes && number != _scrollIncrement)
                {
                    _scrollIncrement       = number;
                    Config.ScrollIncrement = _scrollIncrement;
                }
                ScrollIncrement = _scrollIncrement.ToString();
            });

            #endregion Scroll Increment

            #endregion Settings

            #region dialog

            OpenSetting  = ReactiveCommand.CreateFromTask(ShowSettingDialog);
            OpenFolder   = ReactiveCommand.CreateFromTask(OpenFolderDialog);
            OpenBookmark = ReactiveCommand.CreateFromTask(OpenBookmarkDialog);

            #endregion dialog
        }