-
Notifications
You must be signed in to change notification settings - Fork 7
/
SearchPage.xaml.cs
129 lines (110 loc) · 3.53 KB
/
SearchPage.xaml.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.Windows.Threading;
namespace GoogleVoice
{
public partial class SearchPage : PhoneApplicationPage
{
Conversations mFiltered;
GoogleVoiceClient mVoice;
public SearchPage()
{
InitializeComponent();
mVoice = App.mVoice;
mFiltered = new Conversations();
mFiltered.Dispatcher = Dispatcher;
mSearchList.ItemsSource = mFiltered;
mTimer.Tick += new EventHandler(mTimer_Tick);
mTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
ListBox lb = (sender as ListBox);
if (lb == null)
return;
Conversation convo = lb.SelectedItem as Conversation;
lb.SelectedItem = null;
if (convo == null)
return;
if (!convo.IsRead)
mVoice.MarkAsRead(convo, true);
NavigationService.Navigate(new Uri(string.Format("/ConversationPage.xaml?id={0}", convo.Id), UriKind.Relative));
}
catch (Exception)
{
}
}
void mTimer_Tick(object sender, EventArgs e)
{
mTimer.Stop();
if (mIsSearching)
return;
string search = mSearchBox.Text;
if (mLastSearch == search)
return;
mLastSearch = search;
try
{
search = "%" + search + "%";
lock (SqliteDatabase.Instance)
{
//var messages = SqliteDatabase.Connection.Query<Conversation>("SELECT Conversation.LastMessageTimeText, Conversation.DisplayNumber, Conversation.Id, Conversation.StartTime, Conversation.OtherParticipant, Conversation.IsRead, Conversation.PhoneNumber, Conversation.DisplayStartTime, Conversation.Type, Message.Text as LastMessageText FROM Conversation, Message WHERE Message.Text LIKE ? AND Conversation.Id=Message.ConversationId ORDER BY StartTime DESC LIMIT 5", search);
var messages = SqliteDatabase.Connection.DeferredQuery<Conversation>("SELECT *, Conversation.Id as Id, Message.Text as LastMessageText FROM Conversation, Message WHERE Message.Text LIKE ? AND Conversation.Id=Message.ConversationId AND (Type=2 OR Type=10 OR Type=11) ORDER BY StartTime DESC LIMIT 10", search);
var people = SqliteDatabase.Connection.DeferredQuery<Conversation>("SELECT * FROM Conversation WHERE OtherParticipant LIKE ? ORDER BY StartTime DESC LIMIT 2", search);
var union = messages.Union(people);
var missing = mFiltered.Except(union).ToList();
foreach (var m in missing)
{
mFiltered.Remove(m.Id);
}
int added = 0;
foreach (var m in messages)
{
if (mFiltered.Contains(m.Id))
continue;
if (added++ == 5)
break;
mFiltered.BinaryInsert(m);
}
foreach (var m in people)
{
if (mFiltered.Contains(m.Id))
continue;
if (added++ == 5)
break;
m.Dispatcher = Dispatcher;
mFiltered.BinaryInsert(m);
}
}
}
catch (Exception)
{
}
finally
{
mIsSearching = false;
}
}
DispatcherTimer mTimer = new DispatcherTimer();
bool mIsSearching = false;
string mLastSearch;
private void mSearchBox_TextChanged(object sender, TextChangedEventArgs e)
{
mTimer.Stop();
mTimer.Start();
}
}
}