78 lines
2.7 KiB
C#
78 lines
2.7 KiB
C#
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Runtime.CompilerServices;
|
|
using UnityEngine;
|
|
|
|
namespace DDD
|
|
{
|
|
public abstract class SimpleViewModel : MonoBehaviour, INotifyPropertyChanged
|
|
{
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
protected virtual void Awake() { }
|
|
protected virtual void OnEnable() { }
|
|
protected virtual void Start() { }
|
|
protected virtual void OnDisable() { }
|
|
protected virtual void OnDestroy() { }
|
|
|
|
public virtual void Initialize() { }
|
|
public virtual void Cleanup() { }
|
|
|
|
/// <summary>
|
|
/// PropertyChanged 이벤트 발생
|
|
/// </summary>
|
|
/// <param name="propertyName">변경된 속성 이름 (자동으로 설정됨)</param>
|
|
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 필드 값 변경 및 PropertyChanged 이벤트 발생
|
|
/// </summary>
|
|
/// <typeparam name="T">필드 타입</typeparam>
|
|
/// <param name="field">변경할 필드 참조</param>
|
|
/// <param name="value">새로운 값</param>
|
|
/// <param name="propertyName">속성 이름 (자동으로 설정됨)</param>
|
|
/// <returns>값이 실제로 변경되었는지 여부</returns>
|
|
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
|
|
{
|
|
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
|
|
|
|
field = value;
|
|
OnPropertyChanged(propertyName);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 배치 업데이트를 위한 플래그
|
|
/// </summary>
|
|
private bool _isUpdating;
|
|
|
|
/// <summary>
|
|
/// 배치 업데이트 중 보류된 알림들
|
|
/// </summary>
|
|
private readonly HashSet<string> _pendingNotifications = new();
|
|
|
|
/// <summary>
|
|
/// 배치 업데이트 시작 - 여러 속성 변경을 한 번에 처리
|
|
/// </summary>
|
|
protected void BeginUpdate() => _isUpdating = true;
|
|
|
|
/// <summary>
|
|
/// 배치 업데이트 종료 - 보류된 모든 알림을 처리
|
|
/// </summary>
|
|
protected void EndUpdate()
|
|
{
|
|
_isUpdating = false;
|
|
if (_pendingNotifications.Count > 0)
|
|
{
|
|
foreach (var prop in _pendingNotifications)
|
|
{
|
|
OnPropertyChanged(prop);
|
|
}
|
|
_pendingNotifications.Clear();
|
|
}
|
|
}
|
|
}
|
|
} |