Guest User

Untitled

a guest
Jan 1st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. public ICommand LoginCommand
  2. {
  3. get
  4. {
  5. return new Command(async () =>
  6. {
  7. Message = await _apiServices.LoginAsync(Username, Password);
  8. if (_apiServices.LastResponse.IsSuccessStatusCode) //login successful?
  9. {
  10. // Check if the user details are complete and if not throw up the Edit page.
  11. if (Settings.DetailsComplete)
  12. {
  13. Application.Current.MainPage = new NavigationPage(new HomePage());
  14. }
  15. else
  16. {
  17. await _apiServices.GetUserDetails(); //Check the details from the server
  18. Application.Current.MainPage = new NavigationPage(new UserDetails());
  19. }
  20. }
  21. });
  22. }
  23. }
  24.  
  25. public async Task<bool> GetUserDetails()
  26. {
  27. var client = new HttpClient();
  28. client.DefaultRequestHeaders.Authorization
  29. = new AuthenticationHeaderValue("Bearer", Settings.AccessToken);
  30. var model = new RGUserDTOModel();
  31.  
  32. //Send and respond
  33. var json = await client.GetStringAsync(ServerURL + UserDetailsEndPoint);
  34. var userdetails = JsonConvert.DeserializeObject<RGUserDTOModel>(json);
  35. UpdateUserSettings(userdetails);
  36. return true; // allows for error handling
  37. }
  38.  
  39. public partial class UserDetails : ContentPage
  40. {
  41. public UserDetails()
  42. {
  43. InitializeComponent();
  44. BindingContext = new RGUserViewModel();
  45. Title = "Update your details";
  46. }
  47.  
  48. }
  49.  
  50. <?xml version="1.0" encoding="utf-8" ?>
  51. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  52. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  53. x:Class="xxxxxxGameApp.Views.UserDetails">
  54.  
  55. <ContentPage.Content>
  56. <StackLayout>
  57. <Label Text="Edit your account details:" />
  58. <Entry Text="{Binding Email}"
  59. Placeholder="email"
  60. FontSize="Large"/>
  61. <Entry Text="{Binding Id}"
  62. Placeholder="Id"
  63. FontSize="Large"/>
  64. <Entry Text="{Binding Title}"
  65. Placeholder="Title"
  66. FontSize="Large"/>
  67. <Entry Text="{Binding FirstName}"
  68. Placeholder="FirstName"
  69. FontSize="Large"/>
  70. <Entry Text="{Binding MiddleName}"
  71. Placeholder="MiddleName"
  72. FontSize="Large"/>
  73. <Entry Text="{Binding LastName}"
  74. Placeholder="LastName"
  75. FontSize="Large"/>
  76. <Entry Text="{Binding DisplayName}"
  77. Placeholder="DisplayName"
  78. FontSize="Large"/>
  79.  
  80. <Entry Text="{Binding Password}"
  81. Placeholder="password"
  82. IsPassword="False"
  83. FontSize="Large"/>
  84. <Button Command="{Binding UpdateCommand}"
  85. Text="Update Details"
  86. FontSize="Large"/>
  87.  
  88. </StackLayout>
  89. </ContentPage.Content>
  90. </ContentPage>
  91.  
  92. class RGUserViewModel : INotifyPropertyChanged
  93. {
  94.  
  95. ApiServices _apiServices = new ApiServices();
  96. RGUserDTOModel DTOmodel = new RGUserDTOModel();
  97.  
  98. public event PropertyChangedEventHandler PropertyChanged;
  99. public void OnPropertyChanged([CallerMemberName] string name = "")
  100. {
  101. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  102. }
  103.  
  104. private string Id { get; set; } = Settings.Id;
  105.  
  106. public string Email { get; set; } = "5678"; //Settings.Email;
  107.  
  108. public string UserName { get; set; } = "1234"; //= Settings.Username;
  109.  
  110. public DateTime? DateOfBirth { get; set; } = Settings.DateOfBirth;
  111.  
  112. public string Title { get; set; } = Settings.Title;
  113.  
  114. public string FirstName { get; set; } = Settings.FirstName;
  115.  
  116. public string MiddleName { get; set; } = Settings.MiddleName;
  117.  
  118. public string LastName { get; set; } = Settings.LastName;
  119.  
  120. public string DisplayName { get; set; } = Settings.DisplayName;
  121.  
  122. public string FullName
  123. {
  124. get
  125. {
  126. return FirstName + (MiddleName != null ? " " + MiddleName : "") + (LastName != null ? " " + LastName : "");
  127. }
  128. }
  129.  
  130. public bool DetailsComplete { get; set; } = Settings.DetailsComplete;
  131.  
  132. private string _message { get; set; } = "No Message";
  133. public string Message
  134. {
  135. get { return _message; }
  136. set
  137. {
  138. if (_message != value)
  139. {
  140. _message = value;
  141. OnPropertyChanged();
  142. }
  143. }
  144. }
  145.  
  146. public ICommand UpdateCommand
  147. {
  148. get
  149. {
  150. return new Command(async () =>
  151. {
  152. UpdateDTOmodel(); //Move input to the DTOmodel
  153. var isSuccess = await _apiServices.UpdateUserDetailAsync(DTOmodel);
  154. if (isSuccess)
  155. {
  156. Message = " Registered Successfully";
  157. }
  158. else
  159. {
  160. Message = "Update Error : " + Environment.NewLine + _apiServices.ErrorListasString;
  161. }
  162.  
  163. });
  164. }
  165. }
  166.  
  167. private void UpdateDTOmodel()
  168. {
  169. DTOmodel.Id = Id;
  170. DTOmodel.Title = Title;
  171. DTOmodel.FirstName = FirstName;
  172. DTOmodel.MiddleName = MiddleName;
  173. DTOmodel.LastName = LastName;
  174. DTOmodel.DisplayName = DisplayName;
  175. DTOmodel.DateOfBirth = DateOfBirth;
  176. DTOmodel.UserName = UserName;
  177. DTOmodel.Email = Email;
  178. DTOmodel.DetailsComplete = DetailsComplete;
  179. }
  180. }
Add Comment
Please, Sign In to add comment