/
StationsSearchViewModel.cs
82 lines (66 loc) · 2.35 KB
/
StationsSearchViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.Windows.Threading;
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MundlTransit.WP8.Data.Reference;
using MundlTransit.WP8.Services;
namespace MundlTransit.WP8.ViewModels.Stations
{
public class StationsSearchViewModel : StationsViewModelBase
{
private const int MinimumSearchStringLength = 3;
private const int TimerDelayForAutoSearchInMilliseconds = 250;
private DispatcherTimer _delayTimer;
private IDataService _dataService;
public StationsSearchViewModel(INavigationService navigationService, IDataService ds)
: base(StationsViewModelEnum.Search, navigationService)
{
_dataService = ds;
DisplayName = "search";
}
protected override void OnInitialize()
{
base.OnInitialize();
_delayTimer = new DispatcherTimer();
_delayTimer.Tick += OnInputDelayElapsed;
_delayTimer.Interval = TimeSpan.FromMilliseconds(TimerDelayForAutoSearchInMilliseconds);
}
private string _searchText;
public string SearchText
{
get { return _searchText; }
set
{
_searchText = value;
NotifyOfPropertyChange(() => SearchText);
if (CanStartSearch(_searchText))
{
_delayTimer.Start();
}
}
}
private bool CanStartSearch(string searchString)
{
return (searchString.Length >= MinimumSearchStringLength);
}
void OnInputDelayElapsed(object sender, EventArgs e)
{
_delayTimer.Stop();
SearchForMatchesAsync(_searchText);
}
public IObservableCollection<Haltestelle> Haltestellen { get; set; }
public async void SearchForMatchesAsync(string searchString)
{
if (!CanStartSearch(searchString))
return;
Haltestellen = null;
NotifyOfPropertyChange(() => Haltestellen);
var result = await _dataService.GetHaltestellenContainingAsync(searchString);
Haltestellen = new BindableCollection<Haltestelle>(result);
NotifyOfPropertyChange(() => Haltestellen);
}
}
}