You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Seekers/Views/RegistrationPage.xaml.cs

141 lines
4.7 KiB

using Justice.Helpers;
using Justice.Models;
using Microsoft.Maui.Storage;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Justice.Views
{
public partial class RegistrationPage : ContentPage, INotifyPropertyChanged
{
private readonly DatabaseHelper _dbHelper;
private string _identityFilePath;
private bool _isBusy;
public bool IsBusy
{
get => _isBusy;
set
{
_isBusy = value;
OnPropertyChanged();
}
}
public RegistrationPage()
{
InitializeComponent();
_dbHelper = new DatabaseHelper();
BindingContext = this;
}
private async void OnChooseFileClicked(object sender, EventArgs e)
{
try
{
var customFileType = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.Android, new[] { "*/*" } }, // Allow all files on Android
{ DevicePlatform.iOS, new[] { "public.data" } },
{ DevicePlatform.WinUI, new[] { "*" } }
});
var options = new PickOptions
{
PickerTitle = "Select Identity Document",
FileTypes = customFileType
};
var result = await FilePicker.PickAsync(options);
if (result != null)
{
_identityFilePath = result.FullPath;
IdentityFileLabel.Text = $"Selected File: {result.FileName}";
}
else
{
IdentityFileLabel.Text = "No file selected";
}
}
catch (Exception ex)
{
await DisplayAlert("Error", $"Failed to select file: {ex.Message}", "OK");
}
}
private async void OnRegisterClicked(object sender, EventArgs e)
{
if (IsBusy) return;
IsBusy = true;
try
{
var username = UsernameEntry?.Text?.Trim();
var password = PasswordEntry?.Text?.Trim();
var confirmPassword = ConfirmPasswordEntry?.Text?.Trim();
// Validate inputs
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(confirmPassword))
{
await DisplayAlert("Error", "All fields are required.", "OK");
return;
}
if (string.IsNullOrWhiteSpace(_identityFilePath))
{
await DisplayAlert("Error", "Please upload an identity document.", "OK");
return;
}
if (password != confirmPassword)
{
await DisplayAlert("Error", "Passwords do not match.", "OK");
return;
}
// Hash the password asynchronously
var hashedPassword = await Task.Run(() => BCrypt.Net.BCrypt.HashPassword(password));
var user = new User
{
Username = username,
Password = hashedPassword,
Role = "End User", // Fixed role for End User
IdentityFilePath = _identityFilePath // Save file path
};
// Save to database
await _dbHelper.InitializeAsync<User>();
var existingUser = await _dbHelper.FindAsync<User>("SELECT * FROM User WHERE Username = ?", username);
if (existingUser != null)
{
await DisplayAlert("Error", "Username already exists.", "OK");
return;
}
await _dbHelper.InsertAsync(user);
await DisplayAlert("Success", "Registration successful!", "OK");
// Redirect to Login Page
await Shell.Current.GoToAsync("//LoginPage");
}
catch (Exception ex)
{
await DisplayAlert("Error", $"Failed to register: {ex.Message}", "OK");
}
finally
{
IsBusy = false;
}
}
// INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}