1、需求
一般来说,对于下载队列的需求一般会有一下几点:
A、基本的增删改查
B、插入优先级
C、打断当前下载,优先下载要用
D、可能的其他特殊需求,比如某一类下载需要按顺序下,因此新增的优先下载项就不是插入下载池的最前面,而是需要加在该类下载的最后,其他下载之前。
2、设计
A、选用list作为下载池队列,便于自由管理
B、基本的增删改查的实现
/// <summary>
/// 加入下载池最后
/// </summary>
public void AddDownloadPool(DownLoadPoolItem item)
{
if (HasDownloadItem(item))
return;
Instance.downloadQueue.Add(item);
}
/// <summary>
/// 加入下载池最后
/// </summary>
public void AddDownloadPool(List<DownLoadPoolItem> items)
{
for (int i = 0; i < items.Count; i++)
{
DownLoadPoolItem _item = Instance.downloadQueue.Find((x) => x.itemName == items[i].itemName);
if (_item != null)
{
Instance.downloadQueue.Remove(_item);
items[i] = _item;
}
}
Instance.downloadQueue.AddRange(items);
}
/// <summary>
/// 插入下载池,默认插入池的最前面
/// </summary>
public void InsetDownloadPool(DownLoadPoolItem item, int index = 0)
{
//如果第一个已经是item,不需要插入直接return
if (Instance.downloadQueue.Count > 0 && Instance.downloadQueue[0].itemName == item.itemName)
return;
DownLoadPoolItem _item = Instance.downloadQueue.Find((x) => x.itemName == item.itemName);
DebugBuild.Log(item.itemName + "-------------------插入静默下载队列----1个--------------" + (_item == null));
if (_item != null)
{
Instance.downloadQueue.Remove(_item);
}
Instance.downloadQueue.Insert(index, item);
}
/// <summary>
/// 插入下载池,默认插入池的最前面
/// </summary>
public void InsetDownloadPool(List<DownLoadPoolItem> items, int index = 0)
{
for (int i = 0; i < items.Count; i++)
{
DownLoadPoolItem _item = Instance.downloadQueue.Find((x) => x.itemName == items[i].itemName);
DebugBuild.Log(items[i].itemName + "-------------------插入静默下载队列-----多个-------------" + (_item == null));
if (_item != null)
{
Instance.downloadQueue.Remove(_item);
items[i] = _item;
}
}
Instance.downloadQueue.InsertRange(index, items);
}
public static bool HasDownloadItem(DownLoadPoolItem item)
{
return Instance.downloadQueue.Find((x) => x.itemName == item.itemName) != null;
}
public static bool HasDownloadItem(string fileName)
{
return Instance.downloadQueue.Find((x) => x.filename == fileName) != null;
}
/// <summary>
/// 根据文件名移除池中对应下载
/// </summary>
/// <param name="filename"></param>
public static void DeleteDownloadPool(string filename)
{
var item = Instance.downloadQueue.Find((x) => x.filename == filename);
if (item == null)
return;
Instance.downloadQueue.Remove(item);
}
C、制定基本下载流程
D、插入优先下载是否需要打断当前下载
当前下载存起来,便于管理
/// <summary>
/// 设置当前下载,并从下载池中移除
/// </summary>
public static DownLoadPoolItem SetNowDownLoadItem()
{
if (Instance.downloadQueue.Count == 0)
{
DebugBuild.LogError("--------------------SetNowDownLoadItem----------------------下载池无下载项---------------------------");
return null;
}
if (!GetNowDownLoadIsNull)
{
DebugBuild.LogError("--------------------设置当前下载----------------------当前有下载请先打断---------------------------");
return null;
}
nowDownLoadItem = Instance.downloadQueue[0];
DebugBuild.Log("---------------------------设置当前下载-------------------------"+ nowDownLoadItem.itemName);
Instance.downloadQueue.RemoveAt(0);
return nowDownLoadItem;
}
/// <summary>
/// 设置当前下载被打断,回到下载池,并设置当前为null
/// </summary>
public static void SetNowItemBackPool()
{
//如果当前有正在下载,则加到下载池第一个
if (!GetNowDownLoadIsNull)
{
DebugBuild.Log("-------------------------------------当前下载被打断,回到下载池-------------------------------------------"+ nowDownLoadItem.itemName);
Instance.InsetDownloadPool(nowDownLoadItem);
nowDownLoadItem = null;
}
}
E、基本下载流程测试
F、特殊需求添加