public InfiniteScrollViewModel(IInventoryDataService inventoryDataService) { _inventoryDataService = inventoryDataService; _pagingSubject = new BehaviorSubject <IPageRequest>(new PageRequest(0, pageSize)); Func <InventoryItemViewModel, bool> Search(string searchTerm) => viewModel => { if (string.IsNullOrEmpty(searchTerm)) { return(true); } var lower = searchTerm.ToLower(); return(viewModel.Brand.ToLower().Contains(lower) || (viewModel.Coffee?.ToLower().Contains(lower) ?? false)); }; var searchChanged = this.WhenAnyValue(x => x.SearchText) .Throttle(TimeSpan.FromMilliseconds(800), RxApp.TaskpoolScheduler) .DistinctUntilChanged() .Select(Search); _inventoryDataService .ChangeSet .Transform(x => new InventoryItemViewModel { Id = x.Id, Brand = x.BrandName, Coffee = x.CoffeeName, Roast = x.Roast, Packaging = x.Packaging }) .AutoRefresh(x => x.Id) .DeferUntilLoaded() .Filter(searchChanged) .Sort(SortExpressionComparer <InventoryItemViewModel> .Descending(x => x.Roast)) .Page(_pagingSubject.AsObservable()) .ObserveOn(RxApp.MainThreadScheduler) .Bind(out _items) .DisposeMany() .Subscribe() .DisposeWith(Garbage); this.WhenAnyObservable(x => x.Refresh.IsExecuting) .StartWith(false) .DistinctUntilChanged() .ToProperty(this, nameof(IsRefreshing), out _isRefreshing, deferSubscription: true) .DisposeWith(Garbage); this.WhenAnyObservable(x => x.Load.IsExecuting) .ToProperty(this, nameof(IsLoading), out _isLoading, deferSubscription: true) .DisposeWith(Garbage); Refresh = ReactiveCommand.CreateFromObservable <EventArgs, Unit>(ExecuteRefresh); Load = ReactiveCommand.CreateFromObservable <int, Unit>(ExecuteLoad); _pagingSubject.DisposeWith(Garbage); }
public DirectoryViewModel(IScreen hostScreen, IDirectoryTools directoryTools) { this.directoryTools = directoryTools; HostScreen = hostScreen; var sourceDirectory = Preferences.Get("sourceDirectory", string.Empty); subDirectories.DisposeWith(Disposables); SelectedDirectory = new DirectoryContent(Path.GetFileName(sourceDirectory), sourceDirectory); this.WhenAnyValue(vm => vm.SelectedDirectory) .Where(x => x != null) .Subscribe(directoryContent => SetDirectoryContent(directoryContent.FullPath)) .DisposeWith(Disposables); subDirectoriesHelper = subDirectories.ToProperty(this, vm => vm.SubDirectories); subDirectoriesHelper.DisposeWith(Disposables); CmdSetSoureDirectory = ReactiveCommand.CreateFromTask(_ => { Preferences.Set("sourceDirectory", SelectedDirectory.FullPath); HostScreen.Router.Navigate.Execute(new ScannedChatsViewModel(hostScreen, directoryTools)) .Subscribe() .DisposeWith(Disposables); return(Task.FromResult(Unit.Default)); }) .SetupErrorHandling(Disposables); SetDirectoryContent(sourceDirectory); }
/// <summary> /// Initializes a new instance of the <see cref="PopupViewStackServiceBase"/> class. /// </summary> /// <param name="view">The view.</param> /// <param name="popupNavigation">The popup navigation.</param> /// <param name="viewLocator">The view locator.</param> /// <param name="viewModelFactory">The view model factory.</param> protected PopupViewStackServiceBase(IView view, IPopupNavigation popupNavigation, IViewLocator viewLocator, IViewModelFactory viewModelFactory) : base(view, viewModelFactory) { _popupNavigation = popupNavigation; _viewLocator = viewLocator; PopupSubject = new BehaviorSubject<IImmutableList<IViewModel>>(ImmutableList<IViewModel>.Empty); Pushing = Observable.FromEvent<EventHandler<PopupNavigationEventArgs>, PopupNavigationEventArgs>( eventHandler => { void Handler(object? sender, PopupNavigationEventArgs args) => eventHandler(args); return Handler; }, x => _popupNavigation.Pushing += x, x => _popupNavigation.Pushing -= x) .Select(x => new PopupNavigationEvent((IViewFor)x.Page, x.IsAnimated)); Pushed = Observable.FromEvent<EventHandler<PopupNavigationEventArgs>, PopupNavigationEventArgs>( eventHandler => { void Handler(object? sender, PopupNavigationEventArgs args) => eventHandler(args); return Handler; }, x => _popupNavigation.Pushed += x, x => _popupNavigation.Pushed -= x) .Select(x => new PopupNavigationEvent((IViewFor)x.Page, x.IsAnimated)); Popping = Observable.FromEvent<EventHandler<PopupNavigationEventArgs>, PopupNavigationEventArgs>( eventHandler => { void Handler(object? sender, PopupNavigationEventArgs args) => eventHandler(args); return Handler; }, x => _popupNavigation.Popping += x, x => _popupNavigation.Popping -= x) .Select(x => new PopupNavigationEvent((IViewFor)x.Page, x.IsAnimated)); Popped = Observable.FromEvent<EventHandler<PopupNavigationEventArgs>, PopupNavigationEventArgs>( eventHandler => { void Handler(object? sender, PopupNavigationEventArgs args) => eventHandler(args); return Handler; }, x => _popupNavigation.Popped += x, x => _popupNavigation.Popped -= x) .Select(x => new PopupNavigationEvent((IViewFor)x.Page, x.IsAnimated)); Popped .Subscribe(popped => popped.ViewModel.InvokeViewModelAction<IDestructible>(x => x.Destroy())) .DisposeWith(NavigationDisposables); PopupSubject.DisposeWith(NavigationDisposables); }
/// <summary> /// Initializes a new instance of the <see cref="StoreSearchViewModel"/> class. /// </summary> /// <param name="popupViewStackService">The popup view stack service.</param> /// <param name="storeService">The store service.</param> /// <param name="notificationManager">The notification manager.</param> public StoreSearchViewModel( IPopupViewStackService popupViewStackService, IStoreService storeService, INotificationManager notificationManager) : base(popupViewStackService) { _popupViewStackService = popupViewStackService; _storeService = storeService; _notificationManager = notificationManager; _filterFunction.DisposeWith(Subscriptions); _storeService .Stores .Connect() .RefCount() .Filter(_filterFunction.AsObservable()) .Transform(x => new StoreCardViewModel(x)) .ObserveOn(RxApp.MainThreadScheduler) .Bind(out _stores) .DisposeMany() .Subscribe() .DisposeWith(Subscriptions); _storeService .Metadata .ObserveOn(RxApp.MainThreadScheduler) .Bind(out _storeNames) .DisposeMany() .Subscribe() .DisposeWith(Subscriptions); var isLoading = this.WhenAnyObservable( x => x.Search.IsExecuting, x => x.InitializeData.IsExecuting, x => x.Details.IsExecuting, x => x.Category.IsExecuting, (search, initialize, details, category) => search || initialize || details || category); isLoading .ToProperty(this, nameof(IsLoading), out _isLoading, deferSubscription: true) .DisposeWith(Subscriptions); var canExecute = isLoading.Select(x => !x).StartWith(true); Search = ReactiveCommand.CreateFromObservable(ExecuteSearch, canExecute); Details = ReactiveCommand.CreateFromObservable <StoreCardViewModel, Unit>(ExecuteDetails); InitializeData = ReactiveCommand.CreateFromObservable(ExecuteInitializeData); Category = ReactiveCommand.CreateFromObservable <string, Unit>(ExecuteCategory, canExecute); }
/// <summary> /// Initializes a new instance of the <see cref="ViewStackServiceBase"/> class. /// </summary> /// <param name="view">The view.</param> /// <param name="viewModelFactory">The view model factory.</param> protected ViewStackServiceBase(IView view, IViewModelFactory viewModelFactory) { Logger = this.Log(); View = view ?? throw new ArgumentNullException(nameof(view)); Factory = viewModelFactory; ModalSubject = new BehaviorSubject <IImmutableList <IViewModel> >(ImmutableList <IViewModel> .Empty); PageSubject = new BehaviorSubject <IImmutableList <IViewModel> >(ImmutableList <IViewModel> .Empty); View .PagePopped .Subscribe(poppedPage => { var currentPageStack = PageSubject.Value; if (currentPageStack.Count > 0 && poppedPage == currentPageStack[currentPageStack.Count - 1]) { var removedPage = PopStackAndTick(PageSubject); Logger.Debug(CultureInfo.InvariantCulture, "Removed page '{0}' from stack.", removedPage.Id); } }) .DisposeWith(NavigationDisposables); ModalSubject.DisposeWith(NavigationDisposables); PageSubject.DisposeWith(NavigationDisposables); }