diff --git a/Runtime/Binding/Localization/BaseLocalizedTextBinding.cs b/Runtime/Binding/Localization/BaseLocalizedTextBinding.cs index b221411..9675935 100644 --- a/Runtime/Binding/Localization/BaseLocalizedTextBinding.cs +++ b/Runtime/Binding/Localization/BaseLocalizedTextBinding.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.ComponentModel; using MVVMToolkit.Binding.Localization.Source; using UnityEngine.Localization; @@ -16,34 +15,22 @@ public class LocalizedTextBinding : BaseLocalizedTextBinding private readonly T _boundElement; private readonly Action _operation; private readonly BindingGroup _rootBinding; - private readonly LocalizedString _localizedString; public LocalizedTextBinding(T boundElement, INotifyPropertyChanged binding, LocalizedString ls, Action operation) { _operation = operation; _boundElement = boundElement; - - _localizedString = ls; - - _rootBinding = new(binding, _localizedString); - - _localizedString.Arguments ??= new List(); - if (!_localizedString.Arguments.Contains(_rootBinding)) - _localizedString.Arguments.Add(_rootBinding); - - - _localizedString.StringChanged += StringChanged; - - _localizedString.GetLocalizedStringAsync().Completed += handle => { StringChanged(handle.Result); }; + _rootBinding = new(binding, ls); + ls.StringChanged += StringChanged; + ls.GetLocalizedStringAsync().Completed += handle => { StringChanged(handle.Result); }; } private void StringChanged(string value) => _operation(_boundElement, value); public override void Unbind() { - _rootBinding.ClearBindings(); - _localizedString.Arguments?.Remove(_rootBinding); + _rootBinding.Dispose(); } } } \ No newline at end of file diff --git a/Runtime/Binding/Localization/LocalizationBindingUtility.cs b/Runtime/Binding/Localization/LocalizationBindingUtility.cs new file mode 100644 index 0000000..7ab65e8 --- /dev/null +++ b/Runtime/Binding/Localization/LocalizationBindingUtility.cs @@ -0,0 +1,30 @@ +using System.ComponentModel; +using MVVMToolkit.Binding.Localization.Source; +using UnityEngine.Localization; + +namespace MVVMToolkit.Binding.Localization +{ + public static class LocalizationBindingUtility + { + public static void BindContext(this LocalizedString ls, INotifyPropertyChanged bindingContext) + { + var unused = new BindingGroup(bindingContext, ls); + } + + public static void UnbindAllContext(this LocalizedString ls) + { + if (ls.Arguments is not null) + { + for (var ind = ls.Arguments.Count - 1; ind >= 0; ind--) + { + var argument = ls.Arguments[ind]; + if (argument is BindingGroup binding) + { + binding.Dispose(); + ls.Arguments.RemoveAt(ind); + } + } + } + } + } +} \ No newline at end of file diff --git a/Runtime/Binding/Localization/LocalizationBindingUtility.cs.meta b/Runtime/Binding/Localization/LocalizationBindingUtility.cs.meta new file mode 100644 index 0000000..1faaaf1 --- /dev/null +++ b/Runtime/Binding/Localization/LocalizationBindingUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fe0f2596338f4456b9d5764027317e73 +timeCreated: 1681672609 \ No newline at end of file diff --git a/Runtime/Binding/Localization/Source/BindingGroup.cs b/Runtime/Binding/Localization/Source/BindingGroup.cs index df80ae9..d05d028 100644 --- a/Runtime/Binding/Localization/Source/BindingGroup.cs +++ b/Runtime/Binding/Localization/Source/BindingGroup.cs @@ -15,14 +15,19 @@ public class BindingGroup : IVariable, IDisposable public BindingGroup(INotifyPropertyChanged binding, LocalizedString parent) { Parent = parent; - this.binding = binding; + _binding = binding; + + Parent.Arguments ??= new List(); + Parent.Arguments.Add(this); binding.PropertyChanged += UpdateVariable; } - public void ClearBindings() + public void Dispose() { - foreach (var (_, variable) in variableLookup) + Parent.Arguments?.Remove(this); + + foreach (var (_, variable) in _variableLookup) { if (variable is BindingGroupVariable group) { @@ -30,51 +35,46 @@ public void ClearBindings() } } - variableLookup.Clear(); - } - - public void Dispose() - { - ClearBindings(); - binding.PropertyChanged -= UpdateVariable; + _variableLookup.Clear(); + _binding.PropertyChanged -= UpdateVariable; } - public INotifyPropertyChanged binding; + private readonly INotifyPropertyChanged _binding; - private readonly Dictionary variableLookup = new(); + private readonly Dictionary _variableLookup = new(); private void UpdateVariable(object sender, PropertyChangedEventArgs e) { - if (variableLookup.TryGetValue(e.PropertyName, out var variable)) variable.Set(); + if (_variableLookup.TryGetValue(e.PropertyName, out var variable)) variable.Set(); } public bool BindGroup(string key, out BindingGroupVariable group) { group = null; - if (variableLookup.TryGetValue(key, out var variable)) + if (_variableLookup.TryGetValue(key, out var variable)) { group = (BindingGroupVariable)variable; return true; } - var property = PropertyUtility.GetGetProperty(binding, key); + var property = PropertyUtility.GetGetProperty(_binding, key); - group = new(() => new((INotifyPropertyChanged)property.GetValue(binding), Parent)); - variableLookup.Add(key, group); + group = new(() => new((INotifyPropertyChanged)property.GetValue(_binding), Parent)); + _variableLookup.Add(key, group); return true; } public bool BindVariable(string key, out ILocalizationVariable variable) { - if (variableLookup.TryGetValue(key, out variable)) + if (_variableLookup.TryGetValue(key, out variable)) { return true; } - var property = PropertyUtility.GetGetProperty(binding, key); + var property = PropertyUtility.GetGetProperty(_binding, key); - variable = BindingUtility.LocalizationVariable(property, binding); - variableLookup.Add(key, variable); + variable = BindingUtility.LocalizationVariable(property, _binding); + _variableLookup.Add(key, variable); return true; }