/
SearchResultsViewController.cs
315 lines (217 loc) · 9.07 KB
/
SearchResultsViewController.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using Foundation;
using System;
using UIKit;
using CoreGraphics;
using System.Collections.Generic;
using AircraftForSale.PCL;
using AircraftForSale.PCL.Helpers;
using SDWebImage;
using System.Linq;
using Google.Analytics;
namespace AircraftForSale
{
public partial class SearchResultsViewController : UIViewController, IUISearchResultsUpdating
{
public List<Ad> SearchResultsAdList
{
get;
set;
}
public string Classification
{
get;
set;
}
public UITableView SearchResultsTableView
{
get;
set;
}
public UISearchController searchController;
public SearchResultsViewController()
{
//this.AdSelectedAction = addSelectedAction;
}
public Ad SelectedAd
{
get;
set;
}
public Action AdSelectedAction
{
get;
set;
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
// This screen name value will remain set on the tracker and sent with
// hits until it is set to a new value or to null.
Gai.SharedInstance.DefaultTracker.Set(GaiConstants.ScreenName, "SearchResults View");
Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateScreenView().Build());
}
//~SearchResultsViewController()
//{
// Console.WriteLine("SearchResultsViewController is about to be garbage collected");
//}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var titleString = "";
if (Classification != null)
{
titleString = Classification + " " + "Search";
}
else
{
titleString = "Aircraft Search";
}
View.BackgroundColor = UIColor.Black;
var statusBarHeight = UIApplication.SharedApplication.StatusBarFrame.Height;
var closeButton = new UIButton(new CGRect(View.Bounds.Width - 110, statusBarHeight, 100, 25));
closeButton.SetTitle("Close", UIControlState.Normal);
closeButton.SetTitleColor(UIColor.White, UIControlState.Normal);
View.Add(closeButton);
//var titleView = new UITextView(titleRect);
var tableViewYValue = statusBarHeight + (closeButton.Bounds.Height + 5);
var tableViewHeight = this.View.Bounds.Height - tableViewYValue;
SearchResultsTableView = new UITableView(new CGRect(0, tableViewYValue, this.View.Bounds.Width, tableViewHeight));
var backgroundImage = UIImage.FromBundle("new_home_bg1.png").ResizeImage((float)this.View.Bounds.Width, (float)this.View.Bounds.Height);
//SearchResultsTableView.BackgroundColor = UIColor.FromPatternImage(backgroundImage);
//var imageView = new UIImageView()
this.View.BackgroundColor = UIColor.FromPatternImage(backgroundImage);
SearchResultsTableView.BackgroundColor = UIColor.Clear;
View.Add(SearchResultsTableView);
searchController = new UISearchController((UIViewController)null);
searchController.DimsBackgroundDuringPresentation = false;
SearchResultsTableView.TableHeaderView = searchController.SearchBar;
searchController.SearchBar.SizeToFit();
this.DefinesPresentationContext = true;
searchController.SearchResultsUpdater = this;
var titleViewHeight = this.View.Bounds.Height - (this.SearchResultsTableView.Bounds.Height);
var titleView = new UITextView(new CGRect(0, 0, this.View.Bounds.Width * .4f, titleViewHeight));
titleView.Center = new CGPoint(View.Frame.Size.Width / 2, titleViewHeight / 1.8f);
titleView.Text = titleString;
titleView.TextAlignment = UITextAlignment.Center;
titleView.TextColor = UIColor.White;
titleView.BackgroundColor = UIColor.Clear;
titleView.Font = UIFont.BoldSystemFontOfSize(UIKit.UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone ? 15f : 20f);
titleView.AdjustsFontForContentSizeCategory = true;
this.View.Add(titleView);
closeButton.TouchUpInside += (sender, e) =>
{
this.DismissViewController(true, null);
};
SearchResultsTableView.Source = new SearchResultsTableSource(this);
}
public List<Ad> filteredAds { get; set; }
public void UpdateSearchResultsForSearchController(UISearchController searchController)
{
if (searchController.Active)
filteredAds = new List<Ad>();
else
filteredAds = null;
var textToSearchFor = searchController.SearchBar.Text;
var index = searchController.SearchBar.SelectedScopeButtonIndex;
FilterContentForSearchText(searchController.SearchBar.Text);
}
void FilterContentForSearchText(string text)
{
//InvokeOnMainThread(() =>
//{
if (filteredAds != null)
{
filteredAds.Clear();
filteredAds.AddRange(
this.SearchResultsAdList.Where(e =>
(string.IsNullOrWhiteSpace(text) || e.Name.ToUpper().Contains(text.ToUpper()))
));
}
SearchResultsTableView.ReloadData();
//}
}
}
public class SearchResultsTableSource : UITableViewSource
{
WeakReference parent;
public SearchResultsViewController Owner
{
get
{
return parent.Target as SearchResultsViewController;
}
set
{
parent = new WeakReference(value);
}
}
NSString CellIdentifier = (NSString)"searchResultsPrototypeCell";
public SearchResultsTableSource(SearchResultsViewController owner)
{
Owner = owner;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
if (Owner.filteredAds != null)
return Owner.filteredAds.Count;
return Owner.SearchResultsAdList.Count;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Subtitle, CellIdentifier);
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
var backgroundColor = UIColor.FromWhiteAlpha(1f, .3f);
cell.BackgroundColor = backgroundColor;
}
//Ad ad = Owner.SearchResultsAdList[indexPath.Row];
Ad ad = (Owner.filteredAds != null)
? Owner.filteredAds[indexPath.Row]
: Owner.SearchResultsAdList[indexPath.Row];
//cell.UpdateCell(item, this);
cell.TextLabel.Text = ad.Name;
cell.DetailTextLabel.Text = ad.Teaser;
var image = SDImageCache.SharedImageCache.ImageFromDiskCache(ad.ImageURL);
// var image = HelperMethods.GetImageFromCacheOrDefault(ad.ImageURL);
if (image == null)
{
try
{
image = HelperMethods.FromUrl(ad.ImageURL);
}catch(Exception e)
{
image = UIImage.FromBundle("ad_placeholder.jpg");
}
}
var maxWidth = UIKit.UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone ? 100f : 150f;
image = image.MaxResizeImage(maxWidth, maxWidth);
cell.ImageView.Image = image;
return cell;
}
public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
var maxWidth = UIKit.UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone ? 100f : 150f;
return maxWidth;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.CellAt(indexPath);
Ad ad = (Owner.filteredAds != null)
? Owner.filteredAds[indexPath.Row]
: Owner.SearchResultsAdList[indexPath.Row];
if (Owner.searchController.Active)
{
Owner.searchController.DismissViewController(true, null);
}
Owner.SelectedAd = ad;
if (Owner.AdSelectedAction != null)
{
Owner.AdSelectedAction.Invoke();
}
Owner.DismissViewController(true, null);
tableView.DeselectRow(indexPath, true);
}
}
}