Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Jeffrey Stedfast
file 365 lines (322 sloc) 11.327 kb
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
//
// SQLiteTableViewController.cs
//
// Author: Jeffrey Stedfast <jeff@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System;

using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace MonoTouch.SQLite {
public abstract class SQLiteTableViewController<T> : AllInOneTableViewController where T : new ()
{
/// <summary>
/// Initializes a new instance of the <see cref="MonoTouch.SQLite.SQLiteTableViewController`1"/> class.
/// </summary>
/// <param name='sqlitedb'>
/// The SQLite database connection.
/// </param>
/// <param name='pageSize'>
/// The number of items to page-in and page-out as the user scrolls.
/// </param>
/// <param name='orderBy'>
/// The field to sort by and the order in which to display the data or null to display the data in the default order.
/// </param>
/// <param name='sectionExpression'>
/// The sub-expression used to get distinct sections and their titles or null to display the data as a flat list.
/// </param>
public SQLiteTableViewController (SQLiteConnection sqlitedb, int pageSize, SQLiteOrderBy orderBy, string sectionExpression)
: base (UITableViewStyle.Plain, true)
{
SearchModel = new SQLiteTableModel<T> (sqlitedb, pageSize, orderBy, sectionExpression);
Model = new SQLiteTableModel<T> (sqlitedb, pageSize, orderBy, sectionExpression);
AutoHideSearch = true;
}

/// <summary>
/// Initializes a new instance of the <see cref="MonoTouch.SQLite.SQLiteTableViewController`1"/> class.
/// </summary>
/// <param name='sqlitedb'>
/// The SQLite database connection.
/// </param>
/// <param name='pageSize'>
/// The number of items to page-in and page-out as the user scrolls.
/// </param>
/// <param name='sectionExpression'>
/// The sub-expression used to get distinct sections and their titles or null to display the data as a flat list.
/// </param>
public SQLiteTableViewController (SQLiteConnection sqlitedb, int pageSize, string sectionExpression)
: this (sqlitedb, pageSize, null, sectionExpression)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MonoTouch.SQLite.SQLiteTableViewController`1"/> class.
/// </summary>
/// <param name='sqlitedb'>
/// The SQLite database connection.
/// </param>
/// <param name='pageSize'>
/// The number of items to page-in and page-out as the user scrolls.
/// </param>
/// <param name='orderBy'>
/// The field to sort by and the order in which to display the data or null to display the data in the default order.
/// </param>
public SQLiteTableViewController (SQLiteConnection sqlitedb, int pageSize, SQLiteOrderBy orderBy)
: this (sqlitedb, pageSize, orderBy, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MonoTouch.SQLite.SQLiteTableViewController`1"/> class.
/// </summary>
/// <param name='sqlitedb'>
/// The SQLite database connection.
/// </param>
/// <param name='pageSize'>
/// The number of items to page-in and page-out as the user scrolls.
/// </param>
public SQLiteTableViewController (SQLiteConnection sqlitedb, int pageSize)
: this (sqlitedb, pageSize, null, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MonoTouch.SQLite.SQLiteTableViewController`1"/> class.
///
/// Note: If you use this .ctor, you'll need to implement <see cref="CreateModel(bool)"/>.
/// </summary>
/// <param name='style'>
/// The UITableViewStyle.
/// </param>
public SQLiteTableViewController (UITableViewStyle style) : base (style, true)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MonoTouch.SQLite.SQLiteTableViewController`1"/>
/// class using the UITableViewStyle.Plain style.
///
/// Note: If you use this .ctor, you'll need to implement <see cref="CreateModel(bool)"/>.
/// </summary>
public SQLiteTableViewController () : base (UITableViewStyle.Plain, true)
{
}

/// <summary>
/// Gets the model used when displaying data filtered by the search criteria.
/// </summary>
/// <value>
/// The search data model.
/// </value>
public SQLiteTableModel<T> SearchModel {
get; private set;
}

/// <summary>
/// Gets the model used when displaying unfiltered data.
/// </summary>
/// <value>
/// The data model.
/// </value>
public SQLiteTableModel<T> Model {
get; private set;
}

/// <summary>
/// Reloads the data.
/// </summary>
public void ReloadData ()
{
if (CanSearch)
SearchModel.ReloadData ();

Model.ReloadData ();

if (SearchDisplayController != null)
SearchDisplayController.SearchResultsTableView.ReloadData ();

TableView.ReloadData ();
}

/// <summary>
/// Creates the model.
/// </summary>
/// <returns>
/// The model.
/// </returns>
/// <param name='forSearching'>
/// Whether or not the model will be used for searching.
/// </param>
protected virtual SQLiteTableModel<T> CreateModel (bool forSearching)
{
throw new NotImplementedException ("You need to implement CreateModel(bool).");
}

public override void ViewDidLoad ()
{
if (Model == null)
Model = CreateModel (false);

if (CanSearch) {
if (SearchModel == null)
SearchModel = CreateModel (true);
}

base.ViewDidLoad ();

if (CanSearch) {
SearchDisplayController.SearchResultsTableView.SectionFooterHeight = 0;
SearchDisplayController.SearchResultsTableView.AllowsSelection = true;
}

TableView.SectionFooterHeight = 0;
TableView.AllowsSelection = true;
}

public override void DidReceiveMemoryWarning ()
{
if (CanSearch && SearchModel != null)
SearchModel.ClearCache ();

if (Model != null)
Model.ClearCache ();

base.DidReceiveMemoryWarning ();
}

/// <summary>
/// Gets the models associated with the given UITableView. This will
/// either be the <see cref="Model"/> or the <see cref="SearchModel"/>.
/// </summary>
/// <returns>
/// The model for for the given table view.
/// </returns>
/// <param name='tableView'>
/// The table view.
/// </param>
protected SQLiteTableModel<T> ModelForTableView (UITableView tableView)
{
if (CanSearch && tableView == SearchDisplayController.SearchResultsTableView)
return SearchModel;
else
return Model;
}

/// <summary>
/// Gets the path for the given item in the given table view if it is
/// currently visible to the user.
/// </summary>
/// <returns>
/// The path for the given item if it is visible, or null if it is not.
/// </returns>
/// <param name='tableView'>
/// The table view that is responsible for displaying the item.
/// </param>
/// <param name='item'>
/// The item to get the path for.
/// </param>
protected NSIndexPath PathForVisibleItem (UITableView tableView, T item)
{
SQLiteTableModel<T> model = ModelForTableView (tableView);

foreach (var path in tableView.IndexPathsForVisibleRows) {
var visibleItem = model.GetItem (path.Section, path.Row);

if (visibleItem.Equals (item))
return path;
}

return null;
}

/// <summary>
/// Reloads the row for the given item if it is currently visible to the user.
/// This method is useful if the item values have changed.
/// </summary>
/// <param name='tableView'>
/// The table view that is responsible for displaying the item.
/// </param>
/// <param name='item'>
/// The item that should be re-displayed.
/// </param>
public void ReloadRowForItem (UITableView tableView, T item)
{
var path = PathForVisibleItem (tableView, item);

if (path != null)
tableView.ReloadRows (new NSIndexPath[] { path }, UITableViewRowAnimation.None);
}

public override int NumberOfSections (UITableView tableView)
{
return ModelForTableView (tableView).SectionCount;
}

public override string TitleForHeader (UITableView tableView, int section)
{
string[] titles = ModelForTableView (tableView).SectionTitles;

return titles != null ? titles[section] : null;
}

//public override float GetHeightForHeader (UITableView tableView, int section)
//{
// string[] titles = ModelForTableView (tableView).SectionTitles;
//
// if (titles == null)
// return 0;
//
// return base.GetHeightForHeader (tableView, section);
//}

//public override float GetHeightForFooter (UITableView tableView, int section)
//{
// return 0;
//}

public override int RowsInSection (UITableView tableView, int section)
{
return ModelForTableView (tableView).GetRowCount (section);
}

/// <summary>
/// Gets the cell for the given item.
/// </summary>
/// <returns>
/// The cell used for displaying information about the given item.
/// </returns>
/// <param name='tableView'>
/// The table view responsible for displaying the item.
/// </param>
/// <param name='indexPath'>
/// The path of the item in the table view.
/// </param>
/// <param name='item'>
/// The item to display.
/// </param>
protected abstract UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath, T item);

/// <summary>
/// Gets the item specified by the given path.
/// </summary>
/// <returns>
/// The item.
/// </returns>
/// <param name='tableView'>
/// The table view responsible for displaying the item.
/// </param>
/// <param name='indexPath'>
/// The path of the item in the table view.
/// </param>
protected T GetItem (UITableView tableView, NSIndexPath indexPath)
{
return ModelForTableView (tableView).GetItem (indexPath.Section, indexPath.Row);
}

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
return GetCell (tableView, indexPath, GetItem (tableView, indexPath));
}

protected override bool ShouldReloadForSearchString (UISearchDisplayController controller, string search)
{
SearchModel.SearchText = search;
return true;
}

protected override void Dispose (bool disposing)
{
base.Dispose (disposing);

if (SearchModel != null) {
SearchModel.Dispose ();
SearchModel = null;
}

if (Model != null) {
Model.Dispose ();
Model = null;
}
}
}
}
Something went wrong with that request. Please try again.