Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
3.7 kB
4
Indexable
Never
namespace Labb3ProgTemplate.Managerrs;

public static class UserManager
{
    private static readonly IEnumerable<User>? _users = new List<User>();
    private static User _currentUser;
    //public static IEnumerable<User>? Users => _users;

    //New
    public static IEnumerable<User>? Users {
        get => _users;
        set {
            _users = value;
            UserListChanged?.Invoke();
        }
    };

    public static User CurrentUser  
    {
        get => _currentUser;
        set
        { 
            _currentUser = value;
            CurrentUserChanged?.Invoke();
        }
    }

    public static event Action CurrentUserChanged;

    // Skicka detta efter att användarlistan ändrats eller lästs in
    public static event Action UserListChanged;

    public static bool IsAdminLoggedIn => CurrentUser.Type is UserTypes.Admin;

    public static void ChangeCurrentUser(string name, string password, UserTypes type)
    {
        throw new NotImplementedException();
    }

    public static void LogOut()
    {
        throw new NotImplementedException();
    }

    public static async Task SaveUsersToFile(List<User> users)
    {
        var directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Phu");
        Directory.CreateDirectory(directory);
        var fileName = "userData.json";
        var filePath = Path.Combine(directory, fileName);

        var newUser = users.Select(user => new
        {
            user.Name,
            user.Password,
            Type = user.Type.ToString()
        });

        var jsonOptions = new JsonSerializerOptions()
        {
            WriteIndented = true
        };

        string json = JsonSerializer.Serialize(newUser, jsonOptions);

        using StreamWriter sw = new StreamWriter(filePath, append: true);
        sw.WriteLine(json);

        //New
        Users = users;
    }

    public static async Task LoadUsersFromFile()
    {
        //New
        //Read from file

        Users = //from file
        throw new NotImplementedException();
    }
}

---------


namespace Labb3ProgTemplate.Views
{
    /// <summary>
    /// Interaction logic for LoginView.xaml
    /// </summary>
    public partial class LoginView : UserControl
    {
        public LoginView()
        {
            InitializeComponent();
            UserManager.CurrentUserChanged += UserManager_CurrentUserChanged;

            //New
            UserManager.UserListChanged += UserManager_UserListChanged;
        }

        private void UserManager_CurrentUserChanged()
        {

            throw new NotImplementedException();
        }

        //New
        private async Task UserManager_UserListChanged()
        {
            
        }

        private void LoginBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void RegisterAdminBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            string name = RegisterName.Text;
            string password = RegisterPwd.Password;

            Admin newAdmin = new Admin(name, password);

            //New
            UserManager.Users = UserManager.Users.ToList().Add(newAdmin);
            
            // var updatedUserList = UserManager.Users.ToList();
            // updatedUserList.Add(newAdmin);

            UserManager.SaveUsersToFile(updatedUserList);

            RegisterName.Clear();
            RegisterPwd.Clear();
        }

        private void RegisterCustomerBtn_OnClickmerBtn_Click(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}