using Phonebook.Services; using Contact = Phonebook.Models.Contact; using Communication = Microsoft.Maui.ApplicationModel.Communication; using Android.Content; namespace Phonebook; public partial class MainPage : ContentPage { private ContactService _contactService; private DatabaseService _databaseService; public MainPage() { InitializeComponent(); _contactService = IPlatformApplication.Current!.Services.GetService()!; _databaseService = IPlatformApplication.Current!.Services.GetService()!; } private async void OnContactsUpdated() { await UpdateList(); } protected override async void OnAppearing() { base.OnAppearing(); await UpdateList(); _contactService.OnContactsUpdated += OnContactsUpdated; } protected override void OnDisappearing() { base.OnDisappearing(); _contactService.OnContactsUpdated -= OnContactsUpdated; } private async void OnContactSelected(object sender, SelectionChangedEventArgs e) { if (e.CurrentSelection.FirstOrDefault() is not Contact selectedContact) return; await Navigation.PushAsync(new ContactDetailPage(selectedContact)); contactsList.SelectedItem = null; } private async void OnSearchTextChanged(object sender, TextChangedEventArgs e) { await UpdateList(); } private async Task UpdateList() { contactsList.ItemsSource = await _databaseService.SearchContactsAsync(searchBar.Text); } private async void OnAddContactClicked(object sender, EventArgs e) { await Navigation.PushAsync(new ContactDetailPage(new Contact())); } }