コード例 #1
0
        public CategoryService() : base("allCategories")
        {
            InitCommands();

            CacheData.Do(_ => Debug.WriteLine("Caching category items"))
            .InvokeCommand(this, x => x.CacheCollection)
            .DisposeWith(Disposables);

            CategoryItems = CacheCollection.Select(items => items.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name))
                            .Do(_ => Debug.WriteLine("Updated collection of categories"))
                            .Publish()
                            .RefCount();

            Refresh = ReactiveCommand.Create <Unit, long>(_ => DateTime.Now.Ticks);

            Refresh.ThrownExceptions.Subscribe(ex =>
            {
                Debug.WriteLine($"Failed to Refresh: {ex.Message}");
            }).DisposeWith(Disposables);

            Refresh.Select(_ => Unit.Default)
            .Do(_ => Debug.WriteLine("Updating UI prior to syncing"))
            .SelectMany(async _ => await LoadDataFromTable())
            .Do(data => CachedData = new ObservableCollection <Category>(data))
            .Select(_ => Unit.Default)
            .Do(_ => Debug.WriteLine("Syncing category items"))
            .InvokeCommand(this, x => x.SyncItems).DisposeWith(Disposables);

            AddCategoryItem = ReactiveCommand.Create <string, Category>(name =>
            {
                return(new Category
                {
                    Name = name
                });
            });

            AddCategoryItem.ThrownExceptions.Subscribe(ex =>
            {
                Debug.WriteLine($"Failed to AddCategoryItem: {ex.Message}");
            }).DisposeWith(Disposables);

            AddCategoryItem.Do(_ => Debug.WriteLine("Adding category item to database"))
            .InvokeCommand(this, x => x.AddItem)
            .DisposeWith(Disposables);
        }