umashankar
umashankar

Reputation: 11

How to Hide the Android ActionBar on ViewModel updation in mvvmcross?

I have a LobbyView and a LobbyViewModel Implemented using MvvmCross for Android

LobbyViewModel has LoginCommand, which is used to authenticate the user. And if the user is authenticated , then I need to hide the actionbar defined in LobbyView

I am not sure how to access the actionbar.Hide() in viewmodel, or is there is any other way through which it can be accessed.

Code for ViewModel:

public class LobbyViewModel : MvxViewModel
{
    ISmartfoxService _smarfoxservice;
    private string _username;
    private string _password;
    private string _title = "Connect To SfsServer";
    private bool _LoginSuccces = false;
    private string _LoginVisibility = "VISIBLE";
    private int _ListVisibility = 8;
    private string _LoginError = "Connecting...";
    public LobbyViewModel(ISmartfoxService smartfoxservice)
    {
        _smarfoxservice = smartfoxservice;
    }


    public string UserName
    {
        get { return _username; }
        set { SetProperty(ref _username, value); }
    }
    public string Pass
    {
        get
        {
            return _password;
        }
        set
        {
            SetProperty(ref _password, value);
        }
    }
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }
    public bool LoginSuccess
    {
        get { return _LoginSuccces; }
        set
        {

            _LoginSuccces = value;
            RaisePropertyChanged(() => LoginSuccess);
            if (value == true)
            {
                LoginVisibility = "Gone";

            }
        }
    }
    public string LoginVisibility
    {
        get { return _LoginVisibility; }
        set { _LoginVisibility = value; RaisePropertyChanged(() => LoginVisibility); }
    }


    public int ListVisibility
    {
        get { return _ListVisibility; }
        set { _ListVisibility = value; RaisePropertyChanged(() => ListVisibility); }
    }
    public string LoginError
    {
        get { return _LoginError; }
        set { _LoginError = value; RaisePropertyChanged(() => LoginError); }
    }

    //  A Sample Login Command which notifies the Lobbyviewmodel about login status
    public ICommand LoginCommand
    {
        get
        {
            return new MvxCommand(() =>
            {
                IPhpService service = new PHPService();
                LoginResponse result = await service.TryLogin(UserName,Pass);
                if(result.isLogged==true)
                 {
                    LoginSuccess=true;
                    // Here, I would to like call the android action bar's Hide method of LobbyView.
                 }
                else
                    LoginSuccess=false;
            });
        }

    }
}

Code for Lobby View :

public class LobbyView : MvxActivity
{
    public CustomAdapter customAdapter;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.LobbyView);
        var set = this.CreateBindingSet<LobbyView, LobbyViewModel>();
        Button btnConnect = (Button)FindViewById(Resource.Id.btnlogin);
        EditText edtUserName = (EditText)FindViewById(Resource.Id.edtlogin);
        EditText edtPassword= (EditText)FindViewById(Resource.Id.edtpassword);
        TextView txtErrorMessage = (TextView)FindViewById(Resource.Id.txtloginerror);
        set.Bind(txtErrorMessage).For(tr => tr.Text).To(vm => vm.LoginError);
        set.Bind(edtUserName).For("Text").To(vm => vm.UserName);
        set.Bind(edtPassword).For("Text").To(vm => vm.Pass);
        set.Bind(btnConnect).For("Text").To(vm => vm.Title);
        set.Bind(btnConnect).For("Click").To(vm => vm.LoginCommand);
        set.Apply();

        ActionBar actionBar = this.ActionBar;
        actionBar.SetDisplayShowHomeEnabled(false);
        actionBar.SetDisplayShowTitleEnabled(false);
        LayoutInflater inflater = LayoutInflater.From(this);
        View mCustomeView=inflater.Inflate(Resource.Layout.custom_action_bar,null);
        actionBar.SetDisplayOptions(ActionBarDisplayOptions.ShowCustom, ActionBarDisplayOptions.ShowHome);
        actionBar.SetCustomView(Resource.Layout.custom_action_bar);
        actionBar.SetDisplayShowCustomEnabled(true);

    }
}

Upvotes: 1

Views: 522

Answers (2)

Cheesebaron
Cheesebaron

Reputation: 24460

Alternatively to @Martijn00 answer, is to weakly subscribe to an event for when the particular property has changed.

_loginSuccessSubscription = ViewModel.WeakSubscribe(() => ViewModel.LoginSucess, LoginSuccessChanged);

private void LoginSuccessChanged(object sender, PropertyChangedEventArgs args)
{
    if (ViewModel.LoginSuccess)
    {
        // do stuff on login...

        // Hide ToolBar (this in this context is the Activity or Fragment)
        this.SupportActionbar.Hide();
    }
}

This way you do not have a strong reference to the event and you can rest a bit easier.

Upvotes: 1

Martijn00
Martijn00

Reputation: 3559

What you can do is:

ViewModel.PropertyChanged += (sender, e) =>
{
    if (e.PropertyName == "LoginSuccess" && ViewModel.LoginSuccess)
    {
        //Hide Actionbar here
    }
}

To enable ViewModel typed names you should add a generic to the viewmodel:

public class LobbyView : MvxActivity<LobbyViewModel>

Upvotes: 1

Related Questions