Skip to content

Instantly share code, notes, and snippets.

@Martin-Andersen
Last active December 14, 2015 11:39
Show Gist options
  • Save Martin-Andersen/5080933 to your computer and use it in GitHub Desktop.
Save Martin-Andersen/5080933 to your computer and use it in GitHub Desktop.
I am trying to "translate" code for article "ReactiveXaml series: Implementing search with ObservableAsPropertyHelper" to newest RxUI version I am having trouble with this line : _photos = new ObservableAsPropertyHelper<List<FlickrPhoto>>(results, _ => this.RaisePropertyChanged(x=>x.Photos),false); Result is not the correct type;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reactive.Linq;
using System.Web;
using System.Windows;
using System.Xml.Linq;
using ReactiveUI;
using ReactiveUI.Xaml;
namespace Propertydesigner.ViewModels
{
public class OpenDocumentViewModel : ReactiveValidatedObject
{
public OpenDocumentViewModel()
{
ExecuteSearch = new ReactiveAsyncCommand(null, 0);
CancelSearch = new ReactiveAsyncCommand(null, 0);
// Take the inflight items and toggle the visibility
var shouldSpin = ExecuteSearch.ItemsInflight.Select(x => x > 0 ? Visibility.Visible : Visibility.Collapsed);
// This was described last time too, we actually do the async function
// here and RegisterAsyncFunction will return an IObservable which
// gives us the output, one item per invocation of ExecuteSearch.Execute
var results = ExecuteSearch.RegisterAsyncFunction(term => GetSearchResultsFromFlickr((string)term));
// Here’s the awesome bit - every time the SearchTerm changes
// throttled to every 800ms (i.e. drop changes that are happening
// too quickly). Grab the actual text, then only notify on unique
// changes (i.e. ignore ”A” => ”A”). Finally, only tell us when
// the string isn’t empty. When *all* of those things are true,
// fire ExecuteSearch and pass it the term.
this.ObservableForProperty<OpenDocumentViewModel, string>(x => x.SearchTerm)
.Throttle(TimeSpan.FromMilliseconds(800))
.Select(x => x.Value).DistinctUntilChanged()
.Where(x => !String.IsNullOrWhiteSpace(x))
.Subscribe(ExecuteSearch.Execute);
// This code is also boilerplate, it’s the standard way to take our
// observable and wire it up to the property, giving it an initial
// value.
_spinnerVisibility = new ObservableAsPropertyHelper<Visibility>(shouldSpin,
_ =>
this.RaisePropertyChanged(
x => x.SpinnerVisibility),
Visibility.Collapsed);
_photos = new ObservableAsPropertyHelper<List<FlickrPhoto>>((IObservable<List<FlickrPhoto>>)results,
_ => this.RaisePropertyChanged(x => x.Photos));
}
public static List<FlickrPhoto> GetSearchResultsFromFlickr(string searchTerm)
{
XDocument doc = XDocument.Load(String.Format(CultureInfo.InvariantCulture,
"http://api.flickr.com/services/feeds/photos_public.gne?tags={0}&format=rss_200",
HttpUtility.UrlEncode(searchTerm)));
if (doc.Root == null)
return null;
var titles = doc.Root.Descendants("{http://search.yahoo.com/mrss/}title").Select(x => x.Value);
var descriptions = doc.Root.Descendants("{http://search.yahoo.com/mrss/}description").Select(x => HttpUtility.HtmlDecode(x.Value));
var items = titles.Zip(descriptions, (t, d) => new FlickrPhoto() { Title = t, Description = d }).ToArray();
var urls = doc.Root.Descendants("{http://search.yahoo.com/mrss/}thumbnail").Select(x => x.Attributes("url").First().Value);
var ret = items.Zip(urls, (item, url) => { item.Url = url; return item; }).ToList();
return ret;
}
public ReactiveAsyncCommand ExecuteSearch { get; protected set; }
public ReactiveAsyncCommand CancelSearch { get; protected set; }
private string _searchTerm;
public string SearchTerm
{
get { return _searchTerm; }
set { this.RaiseAndSetIfChanged(value); }
}
private ObservableAsPropertyHelper<Visibility> _spinnerVisibility;
public Visibility SpinnerVisibility
{
get { return _spinnerVisibility.Value; }
}
private ObservableAsPropertyHelper<List<FlickrPhoto>> _photos;
public List<FlickrPhoto> Photos
{
get { return _photos.Value; }
}
public class FlickrPhoto
{
public string Title { get; set; }
public string Description { get; set; }
public string Url { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment