using System.Collections.Generic; using System.Linq; namespace System.Application.UI.Adapters { /// /// 适配器 /// /// public interface IAdapter : ICreateViewModels { /// /// 视图模型 /// IList ViewModels { get; set; } /// /// 列表项点击事件 /// event EventHandler>? ItemClick; /// /// 列表项【长按】点击事件 /// event EventHandler>? ItemLongClick; void NotifyDataSetChanged(); /// /// 替换视图模型,默认替换后刷新 /// /// /// public void Replace(IEnumerable? newViewModels, bool notify = true) { if (ViewModels.IsReadOnly) { if (newViewModels != null && newViewModels.Any()) { ViewModels = CreateViewModels(newViewModels); } else { ViewModels = CreateViewModels(); } } else { ViewModels.Clear(); if (newViewModels != null && newViewModels.Any()) { ViewModels.AddRange(newViewModels); } } if (notify) { NotifyDataSetChanged(); } } /// /// 添加新的视图模型,默认添加后刷新 /// /// /// public void AddRange(IEnumerable? newViewModels, bool notify = true) { if (newViewModels != null && newViewModels.Any()) { ViewModels.AddRange(newViewModels); if (notify) { NotifyDataSetChanged(); } } } /// /// 根据下标移除视图模型,默认移除后刷新 /// /// /// public void RemoveAt(int index, bool notify = true) { if (index >= 0 && index < ViewModels.Count) { ViewModels.RemoveAt(index); if (notify) { NotifyDataSetChanged(); } } } /// /// 根据值移除视图模型,默认移除后刷新 /// /// /// public void Remove(TViewModel vm, bool notify = true) { if (ViewModels.Remove(vm)) { if (notify) { NotifyDataSetChanged(); } } } /// /// 根据点击参数移除视图模型,默认移除后刷新 /// /// /// public void Remove(ItemClickEventArgs args, bool notify = true) => RemoveAt(args.Position, notify); } }