private IObservable <Unit> SyncItems(string beginTime) { var catalogApi = new CatalogApi(); var objectTypes = new List <SearchCatalogObjectsRequest.ObjectTypesEnum> { SearchCatalogObjectsRequest.ObjectTypesEnum.ITEM }; var request = new SearchCatalogObjectsRequest(ObjectTypes: objectTypes, BeginTime: beginTime); IObservable <CatalogObject> resultStream = catalogApi .SearchCatalogObjectsAsync(request) .ToObservable() .Where(x => x.Objects != null) .SelectMany(x => x.Objects); IObservable <Unit> deletedItems = resultStream .Where(x => x.IsDeleted.Value && x.ItemData.CategoryId != null) .GroupBy(x => x.ItemData.CategoryId) .SelectMany( group => { return(group .Select(catalogObject => catalogObject.Id) .SelectMany(itemId => _itemRepoFactory.Create(group.Key).Delete(itemId))); }); IObservable <Unit> addedOrModifiedItems = resultStream .Where(x => !x.IsDeleted.Value && x.ItemData.CategoryId != null) .Select(MapDtoToItem) .GroupBy(x => x.CategoryId) .SelectMany( x => { return(x .SelectMany(item => _itemRepoFactory.Create(x.Key).Upsert(item))); }); return(Observable.Merge(deletedItems, addedOrModifiedItems)); }
public CatalogItemListViewModel( string categoryId, ICatalogItemRepoFactory itemRepoFactory = null, IViewStackService viewStackService = null) { itemRepoFactory = itemRepoFactory ?? Locator.Current.GetService <ICatalogItemRepoFactory>(); viewStackService = viewStackService ?? Locator.Current.GetService <IViewStackService>(); var itemRepo = itemRepoFactory.Create(categoryId); LoadItems = ReactiveCommand.CreateFromObservable( () => { return(itemRepo .GetItems() .SelectMany(x => x) .Select(x => new CatalogItemCellViewModel(x, itemRepo)) .ToList() .Select(x => x as IReadOnlyList <ICatalogItemCellViewModel>)); }); _items = LoadItems.ToProperty(this, x => x.Items); var firebaseStorageService = Locator.Current.GetService <GameCtor.FirebaseStorage.DotNet.IFirebaseStorageService>(); DownloadImages = ReactiveCommand.CreateFromObservable( () => { return(Items .ToObservable() .SelectMany( itemCell => { return firebaseStorageService .GetDownloadUrl("catalogPhotos/" + itemCell.Id + ".jpg") .Catch <string, Exception>(ex => Observable.Return <string>(null)) .Where(x => x != null) .Do(imageUrl => itemCell.ImageUrl = imageUrl) .Select(_ => itemCell); }) .SelectMany( itemCell => { return itemRepo .Upsert(itemCell.Model); })); }); }
public CatalogCategoryViewModel( string categoryId, ICatalogItemRepoFactory catalogItemRepoFactory = null, IViewStackService viewStackService = null) : base(viewStackService) { catalogItemRepoFactory = catalogItemRepoFactory ?? Locator.Current.GetService <ICatalogItemRepoFactory>(); ICatalogItemRepo catalogItemRepo = catalogItemRepoFactory.Create(categoryId); CatalogItems = new List <ICatalogItemCellViewModel>(); LoadCatalogItems = ReactiveCommand.CreateFromObservable( () => { return(catalogItemRepo.GetItems() .SelectMany(x => x) .Select(x => new CatalogItemCellViewModel(x) as ICatalogItemCellViewModel) .Do(x => CatalogItems.Add(x))); }); LoadCatalogItems .ThrownExceptions .Subscribe( ex => { this.Log().Debug(ex.Message); }); this .WhenAnyValue(vm => vm.SelectedItem) .Where(x => x != null) .SelectMany(x => LoadSelectedPage(x)) .Subscribe(); //this // .WhenAnyValue(vm => vm.ItemAppearing) // .Where(item => item != null && item.Id == CatalogItems[CatalogItems.Count - 1].Id) // .Select(_ => Unit.Default) // .InvokeCommand(LoadCatalogItems) // .DisposeWith(disposables); //_isRefreshing = LoadCatalogItems // .IsExecuting // .ToProperty(this, vm => vm.IsRefreshing, true); }