Refresh in a View from a ViewModel

0

Team, I have a question. I'm developing a details screen for an order, where there is a button used to take up this request.

To get clearer, this behavior details screen of different shapes for different statuses:

using CoreGraphics;
using MvvmCross.Binding.BindingContext;
using MvvmCross.Core.ViewModels;
using MvvmCross.iOS.Views;
using UIKit;
using Valdemar.Core.ViewModels;

namespace Valdemar.IOs
{
    public sealed partial class DetailView : MvxViewController<DetailViewModel>
    {
        private bool _constructed;

        public DetailView() : base("DetailView", null)
        {
            ViewDidLoad();
        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            if (ViewModel == null)
            {
                return;
            }

            var close = UIButton.FromType(UIButtonType.System);
            close.Frame = new CGRect(8, 35, 50, 20);
            close.SetTitle("Voltar", UIControlState.Normal);
            Add(close);

            /*
             * Binding
             */
            var set = this.CreateBindingSet<DetailView, Core.ViewModels.DetailViewModel>();

            set.Bind(lblWhat).To(vm => vm.Item.itemsDes);
            set.Bind(lblHowMuch).To(vm => vm.Item.valueDes);
            set.Bind(lblWhen).To(vm => vm.Item.limitTime);
            set.Bind(lblStatus).To(vm => vm.Item.status);
            set.Bind(lblUser).To(vm => vm.Item.usersTOByIdClientUser.personsTO.namePerson);
            //set.Bind(lblAdress).To(vm => vm.Item.usersTOByIdClientUser);
            set.Bind(lblResponsible).To(vm => vm.Item.valdecoOwner);
            set.Bind(lblConfirmed).To(vm => vm.Item.confirmed);
            set.Bind(lblCreated).To(vm => vm.Item.created);
            set.Bind(close).To(vm => vm.CloseCommand);
            set.Bind(close).For("Clicked").To(vm => vm.CloseCommand);

            if (lblStatus.Text == "A")
            {
                btnAction.SetTitle("Assumir Pedido!", UIControlState.Normal);
                set.Bind(btnAction).To(vm => vm.BtnAssumeCommand);
                lblStatus.Hidden = true;
                lblConfirmation.Hidden = true;
                lblResponsible.Hidden = true;
            }
            else if (lblStatus.Text == "V")
            {
                set.Bind(lblStatus).To(vm => vm.V);
                btnAction.SetTitle("Entreguei!", UIControlState.Normal);
                set.Bind(btnAction).To(vm => vm.BtnDeliverCommand);
                lblConfirmation.Hidden = true;
            }
            else if (lblStatus.Text == "E")
            {
                set.Bind(lblStatus).To(vm => vm.E);
                lblConfirmation.Hidden = false;
                btnAction.Hidden = true;
            }
            else if (lblStatus.Text == "C")
            {
                lblConfirmation.Hidden = true;
                btnAction.Hidden = true;
                lblConfirmed.Hidden = false;
                set.Bind(lblStatus).To(vm => vm.C);
            }

            set.Apply();
        }
    }
}

When I launch the button command, I will have to change the status of my request, and therefore hide the "Assume" button.

private async Task<bool> assumeOrder()
{
    var response = await orderService.assumeResp(getOrder());

    if (response == null)
    {
        messageService.showMessage("Pedido " + Item.id + " assumido com sucesso!");

        orderService.deleteAvailableOrder(Item);

        RaisePropertyChanged(() => Item);

        return false;
    }

    return true;
}

What is the best way to do it, especially if the button "disappears"? I'm trying through the RaisePropertyChanged (() => Item), it updates the properties but does not re-access the View to change it according to its new Status.

    
asked by anonymous 27.04.2017 / 21:47

1 answer

0

Man, I did not have to do something like that for Xamarin.iOs specifically and if I understand you want the button to be active or not according to the validation of the same, right? if so, would not it be the case that you assign a CanExecute in the BtnAssumeCommand?

It would look something like this:

public ICommand DeleteItemCommand{
get { 
    return m_DeleteItemCommand ?? (m_DeleteItemCommand = new MvxCommand<int>(ExecDeleteItem,CanExecDeleteItem)); 
}}

public void ExecDeleteItem(int index){
Mvx.Trace($"Deleting item at {index}");}


public bool CanExecDeleteItem(int index){Mvx.Trace($"Checking removal at {index}");return index > 0;}

See full example here .

I'm giving you this information based on what I already did in Xamarin.Forms, as I said I have not done something like that in iOs, but I hope I have helped.

OBS. The code in the example did not come in as "Code Sample" when it was indented, so it's that way.

    
28.04.2017 / 13:29