bitwarden-android/src/Core/Utilities/ExtendedViewModel.cs

39 lines
1.3 KiB
C#
Raw Normal View History

2019-03-29 12:52:57 -04:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Bit.Core.Utilities
{
public abstract class ExtendedViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
2019-03-29 16:52:57 -04:00
protected bool SetProperty<T>(ref T backingStore, T value, Action onChanged = null,
2019-04-26 00:26:09 -04:00
[CallerMemberName]string propertyName = "", string[] additionalPropertyNames = null)
2019-03-29 12:52:57 -04:00
{
if(EqualityComparer<T>.Default.Equals(backingStore, value))
{
return false;
}
backingStore = value;
2019-05-08 14:37:12 -04:00
TriggerPropertyChanged(propertyName, additionalPropertyNames);
onChanged?.Invoke();
return true;
}
protected void TriggerPropertyChanged(string propertyName, string[] additionalPropertyNames = null)
{
2019-03-29 16:52:57 -04:00
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
2019-04-26 00:26:09 -04:00
if(PropertyChanged != null && additionalPropertyNames != null)
{
foreach(var prop in additionalPropertyNames)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(prop));
}
}
2019-03-29 12:52:57 -04:00
}
}
}