forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSourceSorting.cs
143 lines (118 loc) · 4.54 KB
/
DataSourceSorting.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
#region Copyright Syncfusion Inc. 2001-2016.
// Copyright Syncfusion Inc. 2001-2016. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using CoreGraphics;
using Syncfusion.DataSource;
using System;
using System.Collections.Generic;
using System.Text;
using UIKit;
namespace SampleBrowser
{
public class DataSourceSorting : SampleView
{
#region Fields
UITableView tableView;
ContatsViewModel viewModel;
DataSource sfDataSource;
UISearchBar searchbar;
#endregion
#region Static Methods
static bool UserInterfaceIdiomIsPhone {
get { return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone; }
}
#endregion
#region Constructor
public DataSourceSorting()
{
tableView = new UITableView();
tableView.RowHeight = 70;
tableView.SeparatorColor = UIColor.Clear;
tableView.EstimatedRowHeight = 70;
tableView.AllowsSelection = false;
viewModel = new ContatsViewModel();
sfDataSource = new DataSource();
sfDataSource.Source = viewModel.ContactsList;
sfDataSource.LiveDataUpdateMode = LiveDataUpdateMode.AllowDataShaping;
sfDataSource.DisplayItems.CollectionChanged += DisplayItems_CollectionChanged;
sfDataSource.SortDescriptors.Add(new SortDescriptor("ContactName", Syncfusion.DataSource.ListSortDirection.Ascending));
tableView.Source = new TableViewSource(sfDataSource);
searchbar = new UISearchBar();
searchbar.OnEditingStarted += HandleOnEditingStarted;
searchbar.TextChanged += HandleTextChanged;
searchbar.CancelButtonClicked += HandleCancelButtonClicked;
searchbar.EnablesReturnKeyAutomatically = false;
searchbar.Placeholder = "Search Contact";
this.OptionView = CreateOptionView();
this.AddSubview(searchbar);
this.AddSubview (tableView);
}
#endregion
#region Private Methods
private void DisplayItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
tableView.ReloadData();
}
void HandleCancelButtonClicked(object sender, EventArgs e)
{
searchbar.ResignFirstResponder();
searchbar.SetShowsCancelButton(false, true);
}
void HandleTextChanged(object sender, UISearchBarTextChangedEventArgs e)
{
if (sfDataSource != null)
{
this.sfDataSource.Filter = FilterContacts;
this.sfDataSource.RefreshFilter();
}
}
void HandleOnEditingStarted(object sender, EventArgs e)
{
searchbar.SetShowsCancelButton(true, true);
}
private bool FilterContacts(object obj)
{
var contacts = obj as Contacts;
if (searchbar.Text == null || contacts.ContactName.ToLower().Contains(searchbar.Text.ToLower())
|| contacts.ContactNumber.ToLower().Contains(searchbar.Text.ToLower()))
return true;
else
return false;
}
#endregion
#region Override Methods
public UIView CreateOptionView()
{
var stacklayout = new StackLayout ();
var label = new UILabel ();
label.Text = "Sort Direction";
label.TextAlignment = UITextAlignment.Center;
label.Frame = new CGRect (0, 0, 0, 100);
var picker = new UIPickerView ();
picker.Model = new SortingPickerModel(sfDataSource,tableView);
stacklayout.AddSubview (label);
stacklayout.AddSubview (picker);
return stacklayout;
}
public override void LayoutSubviews ()
{
searchbar.Frame = (new CGRect(0, 0, this.Frame.Width, 40));
if (UIDevice.CurrentDevice.CheckSystemVersion(7, 1))
{
searchbar.SizeToFit();
this.tableView.Frame = new CGRect(0, searchbar.Bounds.Height, this.Frame.Width, this.Frame.Height - 44 );
}
else
{
searchbar.SizeToFit();
this.tableView.Frame = new CGRect(0, searchbar.Bounds.Height, this.Frame.Width, this.Frame.Height - 44);
}
base.LayoutSubviews();
}
#endregion
}
}