Editable Table using MvxTableViewController

AnilPundhirAnilPundhir USMember, University

Hello,

I am working on an editable list where the Model has a bool property to mark the row Checked with cell accessory.
I was able to toggle the checkmark based on its original value. I am struggling with saving the changed value back to the db. Ideally I would want to make as many changes as I want to and mark the model for the cell dirty and collect the dirty rows while Save Action.

  1. Is this the right approach?
  2. How can I bind the changes?

Here is the code for the view. ViewModel is simple. it just fetches the data into a list so far and has save and cancel actions which are empty so far.

BaseTableViewController inherits from MvxTableViewController

[Register("ActionView")]
public class ActionView : BaseTableViewController
{
private BindableProgress _bindableProgress;
private UIBarButtonItem _rightButton;

    public new ActionViewModel ViewModel {
        get { return (ActionViewModel)base.ViewModel; }
        set { base.ViewModel = value; }
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        _bindableProgress = new BindableProgress(View);

        Title = "Actions";
        var tableSource = new ActionsListTableViewSource(TableView);
        TableView.Source = tableSource;

        var set = this.CreateBindingSet<ActionView, ActionViewModel>();
        set.Bind(tableSource).To(vm => vm.Actions);
        set.Bind(tableSource).For(s => s.SelectionChangedCommand).To(vm => vm.ActionSelectedCommand);
        set.Bind(_bindableProgress).For(b => b.Visible).To(vm => vm.IsBusy);
        set.Apply();
        TableView.ReloadData();

        _rightButton = new UIBarButtonItem(UIBarButtonSystemItem.Action);
        _rightButton.Clicked += HandleRightButtonClicked;
        NavigationItem.RightBarButtonItem = _rightButton;
    }

    private void HandleRightButtonClicked(object sender, EventArgs e)
    {
        var sheet = new UIActionSheet("Actions");
        sheet.AddButton("Save");
        sheet.AddButton("Cancel");
        sheet.Clicked += HandleActionSheetButtonClicked;
        sheet.ShowFrom(_rightButton, true);
    }

    private void HandleActionSheetButtonClicked(object sender, UIButtonEventArgs e)
    {
        switch (e.ButtonIndex)
        {
            case 0:
                SavePressed();
                break;
            case 1:
                CancelPressed();
                break;
        }
    }

    protected void SavePressed()
    {
        ViewModel.SaveAction();
    }

    protected void CancelPressed()
    {
        ViewModel.CancelAction();
    }
}


public class ActionsListTableViewSource : MvxTableViewSource
{
    static readonly NSString CellIdentifier = new NSString("ActionCell");

    public ActionsListTableViewSource(UITableView tableView) 
        : base(tableView)
    {
    }

    protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
    {
        UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
        if (cell == null)
        {
            cell = new ActionsListTableViewCell(UITableViewCellStyle.Default, CellIdentifier);

            if (item is UserActionModel)
            {
                UserActionModel actionModel = (UserActionModel)item;
                if (actionModel.IsActive.HasValue && actionModel.IsActive.Value)
                {
                    cell.Accessory = UITableViewCellAccessory.Checkmark;
                }
            }

            if (cell.Accessory != UITableViewCellAccessory.Checkmark)
            {
                cell.Accessory = UITableViewCellAccessory.None;
            }
        }
        return cell;
    }

    public override string TitleForHeader(UITableView tableView, int section)
    {
        return string.Empty;
    }

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.CellAt(indexPath);
        if (cell.Accessory == UITableViewCellAccessory.Checkmark)
            cell.Accessory = UITableViewCellAccessory.None;
        else
            cell.Accessory = UITableViewCellAccessory.Checkmark;

        tableView.DeselectRow (indexPath, true);
    }
}

public class ActionsListTableViewCell : MvxStandardTableViewCell
{
    public const string BindingText = @"TitleText ActionName;DetailText ActionDesctiption";

    public static readonly MvxBindingDescription[] BindingDescriptions = new []
    {
        new MvxBindingDescription()
        {
            TargetName = "TitleText",
            Source = new MvxPathSourceStepDescription()
            {
                SourcePropertyPath = "ActionName"
            }
        },
        new MvxBindingDescription()
        {
            TargetName = "DetailText",
            Source = new MvxPathSourceStepDescription()
            {
                SourcePropertyPath = "ActionDesctiption"
            }
        },
    };

    public ActionsListTableViewCell(UITableViewCellStyle cellStyle, NSString cellIdentifier)
        : base(BindingDescriptions, cellStyle, cellIdentifier)
    {
    }
}

Thanks,
Anil

Sign In or Register to comment.