using LSFE.MobApp.Contracts; using LSFE.MobApp.Entities.Doctor; using LSFE.MobApp.Helpers; using LSFE.MobApp.Services; using Microsoft.Extensions.Configuration; using System.ComponentModel; using System.Runtime.CompilerServices; namespace LSFE.MobApp.ViewModels.Common; public abstract class BaseDoctorVM : INotifyPropertyChanged, IDisposable { protected readonly Repository _doctorRepository; protected readonly ISyncService _syncService; protected readonly AddressService _addressService; protected readonly IConfiguration _configuration; private bool _disposed; protected Doctor _doctorEntity; protected BaseDoctorVM( Repository doctorRepository, ISyncService syncService, AddressService addressService, IConfiguration configuration) { _doctorRepository = doctorRepository; _syncService = syncService; _addressService = addressService; _configuration = configuration; // Automatically register for status updates DoctorStatusHelper.RegisterForStatusUpdates(this, OnDoctorStatusUpdated); } /// /// Override this method to handle status updates in derived classes /// protected abstract void OnDoctorStatusUpdated(int doctorId); /// /// Helper method to refresh a specific doctor from repository /// protected async Task RefreshDoctorFromRepositoryAsync(int doctorId) { return await _doctorRepository.FirstOrDefaultAsync(x => x.DoctorId == doctorId); } #region INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion #region IDisposable public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Unregister from status updates DoctorStatusHelper.UnregisterFromStatusUpdates(this); } _disposed = true; } } #endregion }