HomeScreenController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using Foundation;
  4. using UIKit;
  5. using KitchenSyncShared;
  6. namespace KitchenSyncIos
  7. {
  8. partial class HomeScreenController : UIViewController
  9. {
  10. private DataSource _source;
  11. private readonly TaskManager _taskMgr = new TaskManager();
  12. private const string SYNC_URL = "http://localhost:4984/kitchen-sync";
  13. public HomeScreenController (IntPtr handle) : base (handle)
  14. {
  15. }
  16. public override void ViewDidLoad ()
  17. {
  18. base.ViewDidLoad();
  19. Title = "KitchenSync";
  20. tableView.Source = _source = new DataSource(this);
  21. textField.Delegate = new TextFieldDelegate();
  22. tableView.Delegate = new TableViewDelegate(_source);
  23. _taskMgr.StartSync(SYNC_URL);
  24. }
  25. class TableViewDelegate : UITableViewDelegate
  26. {
  27. private readonly DataSource _source;
  28. public TableViewDelegate(DataSource dataSource)
  29. {
  30. _source = dataSource;
  31. }
  32. public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
  33. {
  34. var task = _source.Tasks[indexPath.Row];
  35. task.Checked = !task.Checked;
  36. var manager = new TaskManager();
  37. manager.SaveTask(task);
  38. }
  39. }
  40. class TextFieldDelegate : UITextFieldDelegate
  41. {
  42. public override bool ShouldReturn(UITextField textField)
  43. {
  44. textField.ResignFirstResponder();
  45. return textField.Text.Length > 0;
  46. }
  47. public override void EditingEnded(UITextField textField)
  48. {
  49. if(textField.Text.Length == 0) {
  50. return;
  51. }
  52. var task = new Task() {
  53. Text = textField.Text,
  54. Checked = false,
  55. CreationDate = DateTime.UtcNow
  56. };
  57. var taskManager = new TaskManager();
  58. taskManager.SaveTask(task);
  59. }
  60. }
  61. class DataSource : UITableViewSource
  62. {
  63. static readonly NSString CellIdentifier = new NSString ("TaskCell");
  64. private readonly HomeScreenController controller;
  65. private IList<Task> _rows;
  66. private readonly TaskManager taskMgr = new TaskManager ();
  67. public IList<Task> Tasks
  68. {
  69. get { return _rows; }
  70. }
  71. public DataSource (HomeScreenController controller)
  72. {
  73. this.controller = controller;
  74. taskMgr.TasksUpdated += (sender, e) => {
  75. _rows = e;
  76. controller.tableView.ReloadData();
  77. };
  78. }
  79. public override nint RowsInSection (UITableView tableview, nint section)
  80. {
  81. return _rows == null ? 0 : _rows.Count;
  82. }
  83. public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
  84. {
  85. var cell = tableView.DequeueReusableCell (CellIdentifier);
  86. if(cell == null) {
  87. cell = new UITableViewCell(UITableViewCellStyle.Default, CellIdentifier);
  88. }
  89. var task = _rows[indexPath.Row];
  90. cell.TextLabel.Text = task.Text;
  91. cell.Accessory = task.Checked ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None;
  92. return cell;
  93. }
  94. public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
  95. {
  96. if (editingStyle == UITableViewCellEditingStyle.Delete) {
  97. taskMgr.DeleteTask (_rows[indexPath.Row]);
  98. }
  99. }
  100. }
  101. }
  102. }