Forum Xamarin Xamarin.Forms

MVVM pattern and calling an api

edyboyedyboy Member ✭✭✭
edited July 14 in Xamarin.Forms

Hello community

I have three problems

1) I have a button that's get enable, if the Username and Password are filled

LoginVM

public ICommand LoginCommand { get; set; }
        public ICommand GoToRegisterCommand { get; set; }
        public User User { get; set; }
        public HttpResponseMessage Msg { get; set; }
        public JObject Obj { get; set; }
        public LoginViewModel() {

            User = new User();

            IsBusy = false;
            IsEnable = false;
            LoginCommand = new Command(async () => {
                IsBusy = true;
                IsEnable = false;

                await Task.Run(async () => {
                    Obj = new JObject {
                { "username", User.Username.ToLower() },
                { "password", User.Password.ToLower() }
                    };

                    Msg = await NetworkHelpper.SendData(Constants.AUTH, Obj);

                    if (Msg == null)
                        return;

                    if (Msg.IsSuccessStatusCode) {
                        result = Msg.Content.ReadAsStringAsync().Result;
                        var details = JObject.Parse(result);
                        string token = details["token"].ToString();
                        await SecureStorage.SetAsync("oauth_token", token);
                        IsBusy = false;
                        IsEnable = true;
                        await Application.Current.MainPage.Navigation.PushAsync(new HomeMaterPage());
                    } else {
                        IsBusy = false;
                        IsEnable = true;
                        result = Msg.Content.ReadAsStringAsync().Result;
                        await Application.Current.MainPage.DisplayAlert("Error", result, "ok");
                    }
                });
            });
            GoToRegisterCommand = new Command(async () => {
                await Application.Current.MainPage.Navigation.PushAsync(new RegisterScreen());
            });
        }


        private bool _IsBusy;
        public bool IsBusy {
            get { return _IsBusy; }
            set {
                if (_IsBusy != value) {
                    _IsBusy = value;
                    RaisePropertyChanged();
                }
            }
        }


        private bool _IsEnable;
        public bool IsEnable {
            get { return _IsEnable; }
            set {
                if (_IsEnable != value) {
                    _IsEnable = value;
                    RaisePropertyChanged();
                    if (User.Validator()) {
                        IsEnable = false;
                    } else {
                        IsEnable = true;
                    }
                }
            }
        }
    }

User class

 private string _Username;
    public string Username {
        get { return _Username; }
        set {
            if (_Username != value) {
                _Username = value;
                RaisePropertyChanged();
            }
        }
    }

    public bool Validator() {
        return string.IsNullOrEmpty(_Username) || string.IsNullOrEmpty(Password);
    }

    private string _Password;
    public string Password {
        get { return _Password; }
        set {
            if (_Password != value) {
                _Password = value;
                RaisePropertyChanged();
            }
        }
    }
}

}

2)the Button dosent get enabled

3) for some reason the loginVM is telling me that the app is doing too much work in its Main thread
4) from the other tread, I cannot touch the views on the main thread

The strange part in tha the registration is also calling the server and I don't get the message "Too much work there"

I know that when I get data from an api, I usually make an async/away method. What is diferent in this case, why do I have to call a ne thread

This is my entry point if somebody need

 public App() {
            InitializeComponent();

            MainPage = new NavigationPage(new LoginScreen());
        }

        protected override void OnStart() {
        }

        protected override void OnSleep() {
        }

        protected override void OnResume() {
        }
    }
Tagged:

Answers

Sign In or Register to comment.