111 lines
3.7 KiB
C#
111 lines
3.7 KiB
C#
using System.Buffers.Text;
|
|
using Phonebook.Models;
|
|
using Phonebook.Services;
|
|
using Phonebook.Utils;
|
|
using Contact = Phonebook.Models.Contact;
|
|
|
|
namespace Phonebook {
|
|
|
|
public partial class ContactDetailPage : ContentPage
|
|
{
|
|
private Contact _contact;
|
|
private DatabaseService _databaseService;
|
|
|
|
public ContactDetailPage(Contact contact)
|
|
{
|
|
InitializeComponent();
|
|
_contact = contact;
|
|
_databaseService = new DatabaseService();
|
|
BindingContext = _contact;
|
|
|
|
if (!string.IsNullOrEmpty(_contact.PhotoPath))
|
|
{
|
|
contactImage.Source = _contact.PhotoPath;
|
|
}
|
|
|
|
LoadContactData();
|
|
}
|
|
|
|
private async void LoadContactData()
|
|
{
|
|
// Если контакт уже существует в БД (имеет Id)
|
|
if (_contact.Id > 0)
|
|
{
|
|
// Получаем актуальные данные из базы
|
|
var fullContact = await _databaseService.GetContactAsync(_contact.Id);
|
|
if (fullContact != null)
|
|
{
|
|
_contact = fullContact;
|
|
}
|
|
}
|
|
|
|
// Привязываем данные к элементам управления
|
|
nameEntry.Text = _contact.Name;
|
|
phoneEntry.Text = _contact.PhoneNumber;
|
|
descriptionEntry.Text = _contact.Description;
|
|
addressEntry.Text = _contact.Address;
|
|
emailEntry.Text = _contact.Email;
|
|
|
|
|
|
if (!string.IsNullOrEmpty(_contact.PhotoBase64))
|
|
{
|
|
contactImage.Source = ImageHelper.Base64ToStreamImageSource(_contact.PhotoBase64);
|
|
return;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(_contact.PhotoPath))
|
|
{
|
|
contactImage.Source = ImageSource.FromFile(_contact.PhotoPath);
|
|
}
|
|
else
|
|
{
|
|
contactImage.Source = "default_contact.png";
|
|
}
|
|
}
|
|
|
|
private async void OnSaveClicked(object sender, EventArgs e)
|
|
{
|
|
_contact.Name = nameEntry.Text;
|
|
_contact.PhoneNumber = phoneEntry.Text;
|
|
_contact.Description = descriptionEntry.Text;
|
|
_contact.Email = emailEntry.Text;
|
|
_contact.Address = addressEntry.Text;
|
|
|
|
await _databaseService.SaveContactAsync(_contact);
|
|
await Navigation.PopAsync();
|
|
}
|
|
|
|
private async void OnDeleteClicked(object sender, EventArgs e)
|
|
{
|
|
var answer = await DisplayAlert("Удаление", "Вы уверены, что хотите удалить этот контакт?", "Да", "Нет");
|
|
if (!answer) return;
|
|
|
|
await _databaseService.DeleteContactAsync(_contact);
|
|
await Navigation.PopAsync();
|
|
}
|
|
|
|
private async void OnImageTapped(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
var result = await MediaPicker.PickPhotoAsync();
|
|
if (result == null) return;
|
|
|
|
var stream = await result.OpenReadAsync();
|
|
contactImage.Source = ImageSource.FromStream(() => stream);
|
|
|
|
var saveStream = new MemoryStream();
|
|
await (await result.OpenReadAsync()).CopyToAsync(saveStream);
|
|
|
|
var bytes = saveStream.ToArray();
|
|
|
|
_contact.PhotoBase64 = Convert.ToBase64String(bytes);
|
|
await _databaseService.SaveContactAsync(_contact);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await DisplayAlert("Ошибка", ex.Message, "OK");
|
|
}
|
|
}
|
|
}
|
|
} |