using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Windows; using System.Windows.Media; using JD.Asset; using JD.Asset.Database; using JD.Kernel.Classes; using JetBrains.Annotations; using Plume.Interfaces.WPF; using TeaBox; namespace JD.AssetBrowser.ViewModel { public class AssetDatabaseViewModel : Singleton, INotifyPropertyChanged { public AssetDatabaseViewModel() { Model = AssetDatabase.Instance; Assets = new ConcurrentDictionary(); Model.PropertyChanged += ModelOnPropertyChanged; Model.EnumerateAssetsComplete += OnEnumerateAssetsComplete; Model.RefreshDependenciesComplete += OnRefreshDependenciesComplete; UpdateStatus(); } private void OnEnumerateAssetsComplete(object sender, EventArgs e) { Application.Current.Dispatcher.Invoke(() => { foreach (var asset in Model.Assets) { GetOrCreateViewModel(asset.Value); } }); Model.Assets.ItemsAdded += AssetsAdded; Model.Assets.ItemsRemoved += AssetsRemoved; AssetEnumerationComplete?.Invoke(this, EventArgs.Empty); } private void OnRefreshDependenciesComplete(object sender, EventArgs e) { foreach (var asset in Assets) { asset.Value.RefreshDependencies(); } } public AssetDatabase Model { get; set; } public ConcurrentDictionary Assets { get; set; } public int AssetCount => Assets.Count; public AssetViewModel GetOrCreateViewModel(string path) { return GetOrCreateViewModel(AssetDatabase.Instance.Assets.GetOrCreate(path)); } public AssetViewModel GetOrCreateViewModel(IAsset asset) { if (Assets.ContainsKey(asset.Path)) { return Assets[asset.Path]; } var viewModel = new AssetViewModel { Model = asset }; Assets.TryAdd(asset.Path, viewModel); return viewModel; } private void ModelOnPropertyChanged(object sender, PropertyChangedEventArgs e) { UpdateStatus(); OnPropertyChanged(e.PropertyName); } private void UpdateStatus() { var statusbarManager = ((IWpfApplication)Application.Current).StatusBarManager; if (Model.TaskInProgress) { statusbarManager.SetBackgroundColor(Colors.OrangeRed); statusbarManager.SetProgressVisible(true); statusbarManager.SetProgress(Model.Progress); if (Model.Progress == -1) { statusbarManager.SetProgressIndeterminate(true); } statusbarManager.SetMessage(Model.StatusText); } else { statusbarManager.SetProgressVisible(false); statusbarManager.ResetDefaultBackgroundColor(); statusbarManager.SetMessage("Ready"); } } private void AssetsAdded(IAsset[] assets) { Application.Current.Dispatcher.Invoke(() => { foreach (var asset in assets) { GetOrCreateViewModel(asset); } }); OnPropertyChanged(nameof(AssetCount)); OnPropertyChanged(nameof(Assets)); } private void AssetsRemoved(IAsset[] assets) { foreach (var asset in assets) { Assets.TryRemove(asset.Path, out AssetViewModel assetViewModel); } OnPropertyChanged(nameof(AssetCount)); OnPropertyChanged(nameof(Assets)); } public event EventHandler AssetEnumerationComplete; public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged(string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }