public CreateProfileViewModel() { Date = DateTime.Now; UpdateUser = new Command(async() => { IsBusy = true; var response = await HttpService.PutRequest <ResponseModel <UserModel>, UserModel>("user/update", User); if (response.Code != 200) { Device.BeginInvokeOnMainThread(async() => { await Application.Current.MainPage.DisplayAlert(response.Code.ToString(), ListStringifyer.StringifyList(response.Errors), "OK"); }); IsBusy = false; return; } if (Avatar != null) { UploadPhoto.Execute(null); } else { IsBusy = false; Navigate(); } }); UploadPhoto = new Command(async() => { var file = System.IO.File.ReadAllBytes(Avatar.AlbumPath); var response = await HttpService.UploadFile <ResponseModel <string> >("user/avatar", file); if (response.Code != 200) { Device.BeginInvokeOnMainThread(async() => { await Application.Current.MainPage.DisplayAlert(response.Code.ToString(), ListStringifyer.StringifyList(response.Errors), "OK"); IsBusy = false; return; }); } Navigate(); }); }
public LoginViewModel(INavigation nav) { this.Navigation = nav; MessagingCenter.Subscribe <Object, UserAuthModel>(this, "GoogleLogin", (sender, user) => { SocialLogin.Execute(user); }); SocialLogin = new Command <UserAuthModel>(async(usr) => { IsBusy = true; string phone = await Application.Current.MainPage.DisplayPromptAsync(null, "Для продолжения введите номер телефона", "OK", null, "Номер телефона", 12, Keyboard.Telephone); usr.Phone = phone; if (string.IsNullOrEmpty(phone) || phone.Length < 11) { await Application.Current.MainPage.DisplayAlert(null, "Введите номер для продолжения", "ОК"); IsBusy = false; return; } var response = await HttpService.PostRequest <ResponseModel <SocialToken>, UserAuthModel>("user/login/social", usr); if (response.Code == 200) { if (!Application.Current.Properties.ContainsKey("Token")) { Application.Current.Properties.Add("Token", response.Response.Token); } else { Application.Current.Properties["Token"] = response.Response.Token; } try { Application.Current.Properties.Add("GoogleLogin", true); } catch { Application.Current.Properties["GoogleLogin"] = true; } await Application.Current.SavePropertiesAsync(); HttpService.Token = response.Response.Token; if (response.Response.Logged) { Application.Current.MainPage = new MainPage(); } else { UserModel User = new UserModel() { Name = usr.Name, Surname = usr.Surname, Email = usr.Email, Phone = usr.Phone, }; await Navigation.PushModalAsync(new CreateProfile(new CreateProfileViewModel(User))); } } else { await Application.Current.MainPage.DisplayAlert("Произошла ошибка", ListStringifyer.StringifyList(response.Errors), "OK"); } IsBusy = false; }); Login = new Command <UserAuthModel>(async(user) => { IsBusy = true; var response = await HttpService.PostRequest <ResponseModel <string>, UserAuthModel>("user/login", user); if (response.Code == 200) { if (!Application.Current.Properties.ContainsKey("Token")) { Application.Current.Properties.Add("Token", response.Response); } else { Application.Current.Properties["Token"] = response.Response; } await Application.Current.SavePropertiesAsync(); HttpService.Token = response.Response; Application.Current.MainPage = new Workflow.Views.MainPage(); } else { Device.BeginInvokeOnMainThread(async() => await Application.Current.MainPage.DisplayAlert("Ошибка", ListStringifyer.StringifyList(response.Errors), "OK")); } IsBusy = false; }); }
public GroupsViewModel() { AllGroups = new ObservableCollection <GroupModel>(); MyGroups = new ObservableCollection <GroupModel>(); LoadAllGroups = new Command(() => { Task.Run(async() => { AllGroups.Clear(); var groups = await HttpService.GetRequest <ResponseModel <List <GroupModel> > >("groups"); if (groups.Code == 200) { foreach (var group in groups.Response) { Device.BeginInvokeOnMainThread(() => AllGroups.Add(group)); } } else { Device.BeginInvokeOnMainThread(() => { Application.Current.MainPage.DisplayAlert("Ошибка", ListStringifyer.StringifyList(groups.Errors), "OK"); }); } }); }); LoadUserGroups = new Command(() => { Task.Run(async() => { IsBusy = true; MyGroups.Clear(); var groups = await HttpService.GetRequest <ResponseModel <List <GroupModel> > >("groups/get"); if (groups.Code == 200) { if (groups.Response != null) { foreach (var group in groups.Response) { Device.BeginInvokeOnMainThread(() => MyGroups.Add(group)); } } } else { Device.BeginInvokeOnMainThread(() => { Application.Current.MainPage.DisplayAlert("Ошибка", ListStringifyer.StringifyList(groups.Errors), "OK"); }); } IsBusy = false; }); }); SearchGroups = new Command <string>((query) => { if (string.IsNullOrEmpty(query)) { LoadUserGroups.Execute(null); return; } IsBusy = true; Task.Run(async() => { var search = await HttpService.GetRequest <ResponseModel <List <GroupModel> > >($"groups?search={query}"); MyGroups.Clear(); if (search.Code == 200) { if (search.Response != null) { foreach (var group in search.Response) { Device.BeginInvokeOnMainThread(() => MyGroups.Add(group)); } } } IsBusy = false; }); }); MessagingCenter.Subscribe <Object, string>(this, "RemoveGroup", (sender, id) => { MyGroups.Remove(MyGroups.First(x => x.ID == id)); }); MessagingCenter.Subscribe <Object, GroupModel>(this, "AddNewGroup", (sender, group) => MyGroups.Add(group)); }