123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using AvaloniaApplication1.Models;
- using ReactiveUI;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace AvaloniaApplication1.ViewModels
- {
- public class UserMainViewModel : ViewModelBase
- {
- private readonly ApiService _apiService;
- User user;
- List<Gender> genders;
- string info;
- bool isLoading;
- public bool IsLoading { get => isLoading; set => this.RaiseAndSetIfChanged(ref isLoading, value); }
- bool isDataLoaded;
- public bool IsDataLoaded{ get => isDataLoaded; set => this.RaiseAndSetIfChanged(ref isDataLoaded, value); }
- public User CurrentUser
- {
- get => user; set
- {
- Info = "";
- user = value;
- }
- }
- public string Info { get => info; set => this.RaiseAndSetIfChanged(ref info, value); }
- public List<Gender> Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders, value); }
- public Gender selectedGender;
- public Gender SelectedGender
- {
- get => selectedGender; set
- {
- selectedGender = value;
- Info = "";
- }
- }
- public UserMainViewModel(int id)
- {
- _apiService = new ApiService();
- LoadDataAsync(id);
- }
- private async void LoadDataAsync(int id)
- {
- IsLoading = true;
- IsDataLoaded = false;
- try
- {
- CurrentUser = await _apiService.GetAsync<User>($"getUserById?id={id}");
- genders = await _apiService.GetAsync<List<Gender>>("getGenders");
- selectedGender = CurrentUser.GenderNavigation;
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Îøèáêà ïðè âûçîâå API: {ex.Message}");
- }
- finally { IsLoading = false; IsDataLoaded = true; }
- }
- public bool IsNotLoading { get => !IsLoading; }
- public DateTimeOffset DateTimeOffset
- {
- get
- {
- if (CurrentUser != null)
- { new DateTimeOffset(CurrentUser.DateOfBirth, TimeSpan.Zero); }
- return DateTimeOffset.Now;
- }
- set
- {
- if (CurrentUser != null)
- { }
- else
- {
- CurrentUser.DateOfBirth = new DateTime(value.Year, value.Month, value.Day);
- Info = "";
- }
-
- }
- }
- public void Save()
- {
- try
- {
- myConnection.SaveChanges();
- Info = "Äàííûå óñïåøíî ñîõðàíåíû";
- }
- catch (Exception ex)
- {
- Info = $"Îøèáêà: {ex.Message}";
- }
- }
- }
- }
|