public SelectLocationViewModel(AppViewModel screen, IConfigurationService configurationService, IGeocodeService geocoder)
        {
            this.HostScreen = screen;

            this.configurationService = configurationService;
            this.geocoder             = geocoder;

            ExecuteSearch = ReactiveCommand.CreateFromTask <string, IEnumerable <Location> >(
                async searchTerm => await geocoder.SearchForLocationsAsync(searchTerm)
                );

            this.SaveSelectedLocation = ReactiveCommand.CreateFromTask(async() =>
            {
                await configurationService.ChangeSavedLocationAsync(this.SelectedLocation);

                //Attempt to re-load app
                screen.LoadCommand.Execute().Subscribe();
            }, this.WhenAny(vm => vm.SelectedLocation, location => location.GetValue() != null));

            this.WhenAnyValue(x => x.SearchTerm)
            .Throttle(TimeSpan.FromMilliseconds(500), RxApp.MainThreadScheduler)
            .Select(x => x?.Trim())
            .DistinctUntilChanged()
            .Where(x => !string.IsNullOrWhiteSpace(x))
            .InvokeCommand(ExecuteSearch);

            this.ExecuteSearch.IsExecuting.ToProperty(this, x => x.IsSearching, out this.isSearching, false);

            searchResults = ExecuteSearch.ToProperty(this, x => x.SearchResults, new List <Location>());
        }
示例#2
0
        public ViewModel()
        {
            client = new WebSearchClient(new ApiKeyServiceClientCredentials("f891515b49d94b64aa91955aa92c1691"));

            ExecuteSearch = ReactiveCommand.CreateFromTask((string s) => Search(s));
            ExecuteSearch.Subscribe(r => SearchResults = r);

            this.WhenAnyValue(x => x.SearchQuery)
            .Throttle(TimeSpan.FromSeconds(0.8))
            .Select(query => query?.Trim())
            .DistinctUntilChanged()
            .Where(query => !string.IsNullOrWhiteSpace(query))
            .InvokeCommand(ExecuteSearch);
        }
示例#3
0
        public AppViewModel(ReactiveAsyncCommand reactiveAsyncCommand       = null,
                            IObservable <List <FlickrPhoto> > searchResults = null)
        {
            ExecuteSearch = reactiveAsyncCommand ?? new ReactiveAsyncCommand();
            ReactiveCommandMixins.InvokeCommand(this.ObservableForProperty(model => model.SearchTerm)
                                                .Throttle(TimeSpan.FromMilliseconds(800), RxApp.TaskpoolScheduler)
                                                .Select(x => x.Value)
                                                .DistinctUntilChanged()
                                                .Where(x => !string.IsNullOrWhiteSpace(x)), ExecuteSearch);
            _spinnerVisibility =
                ExecuteSearch.ItemsInflight.Select(x => x > 0 ? Visibility.Visible : Visibility.Collapsed)
                .ToProperty(this, model => model.SpinnerVisibility, Visibility.Hidden);
            var results = searchResults ??
                          ExecuteSearch.RegisterAsyncFunction(term => GetSearchResultFromFlickr((string)term));

            _searchResults = results.ToProperty(this, model => model.SearchResults, new List <FlickrPhoto>());
        }
示例#4
0
 protected void Button1_Click(object sender, EventArgs e)
 {
     if (IsPostBack)
     {
         Label2.Visible = true;
         if (TextBox1.Text == "")
         {
             Message = "Please fill out the field";
             Label2.Style["color"] = "red";
             TextBox1.BorderColor  = System.Drawing.Color.Crimson;
         }
         else
         {
             TextBox1.BorderColor = System.Drawing.Color.DeepSkyBlue;
             //String filename = @"C:\rohit.bansal\WebApplication1\WebApplication1\mergerResults.csv";
             String filename = "";
             try
             {
                 filename = AppDomain.CurrentDomain.BaseDirectory + "mergerResults.csv";
             }
             catch
             {
             }
             ExecuteSearch ES = new ExecuteSearch(filename);
             try
             {
                 TextBox1.Text = Session["suggestion"].ToString();
                 Session.Remove("suggestion");
             }
             catch
             {
             }
             ES.getSearchResults(TextBox1.Text, out Results, out did_you_mean);
             if (Results == null)
             {
                 Label2.Style["color"] = "red";
                 repLinks.Visible      = false;
                 LinkButton1.Visible   = false;
                 LinkButton2.Visible   = false;
                 Message = "Unable to load indexfile";
             }
             else
             {
                 if (Did_you_mean == true)
                 {
                     Label2.Visible   = false;
                     Label3.Visible   = true;
                     LinkButton4.Text = Results1[0].Key;
                     Session.Add("suggestion", LinkButton4.Text);
                     LinkButton4.Visible = true;
                     LinkButton1.Visible = false;
                     LinkButton2.Visible = false;
                     repLinks.Visible    = false;
                 }
                 else
                 {
                     NumResults = Results1.Length;
                     if (NumResults > 0)
                     {
                         WindowSize = WindowSize < NumResults ? WindowSize : NumResults;
                         for (int index = Lower; index < WindowSize; index++)
                         {
                             try
                             {
                                 CurrentWindow1[index] = new KeyValuePair <string, string>(Results1[index].Key, Results1[index].Value);
                             }
                             catch
                             {
                                 break;
                             }
                         }
                         repLinks.DataSource = CurrentWindow1;
                         repLinks.DataBind();
                         repLinks.Visible    = true;
                         LinkButton1.Visible = false;
                         LinkButton2.Visible = NumResults > WindowSize ? true : false;
                         Upper = NumResults > windowSize ? Upper : NumResults;
                         Session.Add("WikiSearch_numResult", NumResults);
                         Session.Add("WikiSearch_lower", Lower);
                         Session.Add("WikiSearch_upper", Upper);
                         Session.Add("WikiSearchResults", Results1);
                         Label2.Style["color"] = "#9c9c9c";
                         Message = "About " + NearestNum(NumResults) + " results found";
                     }
                     else
                     {
                         Label2.Style["color"] = "red";
                         Message             = "No results found. Check your spellings and try again";
                         repLinks.Visible    = false;
                         LinkButton1.Visible = false;
                         LinkButton2.Visible = false;
                     }
                 }
             }
         }
         Label2.Text = Message;
     }
 }
        public AppViewModel(ReactiveAsyncCommand testExecuteSearchCommand = null, IObservable <List <FlickrPhoto> > testSearchResults = null)
        {
            ExecuteSearch = testExecuteSearchCommand ?? new ReactiveAsyncCommand();

            /* Creating our UI declaratively
             *
             * The Properties in this ViewModel are related to each other in different
             * ways - with other frameworks, it is difficult to describe each relation
             * succinctly; the code to implement "The UI spinner spins while the search
             * is live" usually ends up spread out over several event handlers.
             *
             * However, with RxUI, we can describe how properties are related in a very
             * organized clear way. Let's describe the workflow of what the user does in
             * this application, in the order they do it.
             */

            // We're going to take a Property and turn it into an Observable here - this
            // Observable will yield a value every time the Search term changes (which in
            // the XAML, is connected to the TextBox).
            //
            // We're going to use the Throttle operator to ignore changes that
            // happen too quickly, since we don't want to issue a search for each
            // key pressed! We then pull the Value of the change, then filter
            // out changes that are identical, as well as strings that are empty.
            //
            // Finally, we use RxUI's InvokeCommand operator, which takes the String
            // and calls the Execute method on the ExecuteSearch Command, after
            // making sure the Command can be executed via calling CanExecute.
            this.ObservableForProperty(x => x.SearchTerm)
            .Throttle(TimeSpan.FromMilliseconds(800), RxApp.DeferredScheduler)
            .Select(x => x.Value)
            .DistinctUntilChanged()
            .Where(x => !String.IsNullOrWhiteSpace(x))
            .InvokeCommand(ExecuteSearch);


            // How would we describe when to show the spinner in English? We
            // might say something like, "The spinner's visibility is whether
            // the search is running". RxUI lets us write these kinds of
            // statements in code.
            //
            // ExecuteSearch has an IObservable<int> called ItemsInFlight that
            // fires every time a new item starts or stops. We Select() that into
            // a Visibility (0 = Collapsed, > 0 = Visible), then we will use RxUI's
            // ToProperty operator, which is a helper to create an
            // ObservableAsPropertyHelper object.
            //
            // Essentially, we're saying here, "The value of SpinnerVisibility is
            // the in-flight items Selected into a Visibility"
            _SpinnerVisibility = ExecuteSearch.ItemsInflight
                                 .Select(x => x > 0 ? Visibility.Visible : Visibility.Collapsed)
                                 .ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);

            // Here, we're going to actually describe what happens when the Command
            // gets invoked - we're going to run the GetSearchResultsFromFlickr every
            // time the Command is executed.
            //
            // The important bit here is the return value - an Observable. We're going
            // to end up here with a Stream of FlickrPhoto Lists: every time someone
            // calls Execute, we eventually end up with a new list.

            IObservable <List <FlickrPhoto> > results;

            if (testSearchResults != null)
            {
                results = testSearchResults;
            }
            else
            {
                results = ExecuteSearch.RegisterAsyncFunction(term => GetSearchResultsFromFlickr((string)term));
            }

            // ...which we then immediately put into the SearchResults Property.
            _SearchResults = results.ToProperty(this, x => x.SearchResults, new List <FlickrPhoto>());
        }
示例#6
0
        public MainViewModel()
        {
            ExecuteSearch = ReactiveCommand.CreateAsyncTask(_ => GetSearchResultsFromFlickr(SearchTerm));

            /* Creating our UI declaratively
             *
             * The Properties in this ViewModel are related to each other in different
             * ways - with other frameworks, it is difficult to describe each relation
             * succinctly; the code to implement "The UI spinner spins while the search
             * is live" usually ends up spread out over several event handlers.
             *
             * However, with RxUI, we can describe how properties are related in a very
             * organized clear way. Let's describe the workflow of what the user does in
             * this application, in the order they do it.
             */

            // We're going to take a Property and turn it into an Observable here - this
            // Observable will yield a value every time the Search term changes (which in
            // the XAML, is connected to the TextBox).
            //
            // We're going to use the Throttle operator to ignore changes that
            // happen too quickly, since we don't want to issue a search for each
            // key pressed! We then pull the Value of the change, then filter
            // out changes that are identical, as well as strings that are empty.
            //
            // Finally, we use RxUI's InvokeCommand operator, which takes the String
            // and calls the Execute method on the ExecuteSearch Command, after
            // making sure the Command can be executed via calling CanExecute.
            this.WhenAnyValue(x => x.SearchTerm)
            .Throttle(TimeSpan.FromMilliseconds(800))
            .Select(x => x.Trim())
            .DistinctUntilChanged()
            .Where(x => !string.IsNullOrWhiteSpace(x))
            .InvokeCommand(ExecuteSearch);

            // How would we describe when to show the spinner in English? We
            // might say something like, "The spinner's visibility is whether
            // the search is running". RxUI lets us write these kinds of
            // statements in code.
            //
            // ExecuteSearch has an IObservable<bool> called IsExecuting that
            // fires every time the command changes execution state. We Select() that into
            // a Visibility then we will use RxUI's
            // ToProperty operator, which is a helper to create an
            // ObservableAsPropertyHelper object.

            _spinnerVisibility = ExecuteSearch.IsExecuting
                                 .ToProperty(this, x => x.SpinnerVisibility, false);

            // We subscribe to the "ThrownExceptions" property of our ReactiveCommand,
            // where ReactiveUI pipes any exceptions that are thrown in
            // "GetSearchResultsFromFlickr" into. See the "Error Handling" section
            // for more information about this.
            ExecuteSearch.ThrownExceptions.Subscribe(ex =>
            {
/* Handle errors here */
            });

            // Here, we're going to actually describe what happens when the Command
            // gets invoked - we're going to run the GetSearchResultsFromFlickr every
            // time the Command is executed.
            //
            // The important bit here is the return value - an Observable. We're going
            // to end up here with a Stream of FlickrPhoto Lists: every time someone
            // calls Execute, we eventually end up with a new list which we then
            // immediately put into the SearchResults property, that will then
            // automatically fire INotifyPropertyChanged.
            _searchResults = ExecuteSearch.ToProperty(this, x => x.SearchResults, new List <FlickrPhoto>());
        }