You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.1 KiB

  1. local MAX_COUNT=5
  2. local TASK_ID=1
  3. local DownloaderHeadManager = {
  4. downCount=0,
  5. tasks={},
  6. dowloadTasks={},
  7. scheduleId=nil,
  8. }--class("DownloaderHeadManager")
  9. function DownloaderHeadManager.addTask(node,imageUrl, fileName, updateHeadImage)
  10. --如果在队列里面 还没开始下载 则更新数据就行
  11. local isExist,oldTask=DownloaderHeadManager.isExitTask(node)
  12. if isExist then
  13. oldTask.imageUrl=imageUrl
  14. oldTask.fileName=fileName
  15. oldTask.updateHeadImage=updateHeadImage
  16. return
  17. end
  18. --如果已经开始下载
  19. local isDownload,oldTask=DownloaderHeadManager.isExitDowloadTask(node)
  20. if isDownload then
  21. if oldTask.imageUrl==imageUrl then
  22. oldTask.updateHeadImage=updateHeadImage
  23. return
  24. end
  25. end
  26. local task={
  27. node=node,
  28. imageUrl=imageUrl,
  29. fileName=fileName,
  30. updateHeadImage=updateHeadImage,
  31. id=TASK_ID,
  32. }
  33. dump(task,"DownloaderHeadManager.addTask")
  34. -- logD(" DownloaderHeadManager.addTask",tabletostring(task))
  35. table.insert(DownloaderHeadManager.tasks,task)
  36. TASK_ID=TASK_ID+1
  37. DownloaderHeadManager.update()
  38. -- if not DownloaderHeadManager.scheduleId then
  39. -- DownloaderHeadManager.scheduleId=cc.Director:getInstance():getScheduler():scheduleScriptFunc(DownloaderHeadManager.update , 0 , false)
  40. -- end
  41. end
  42. function DownloaderHeadManager.isExitTask(node)
  43. for k,v in pairs(DownloaderHeadManager.tasks) do
  44. if v.node==node then
  45. return true,v
  46. end
  47. end
  48. return false,nil
  49. end
  50. function DownloaderHeadManager.isExitDowloadTask(node)
  51. for k,v in pairs(DownloaderHeadManager.dowloadTasks) do
  52. if v.node==node then
  53. return true,v
  54. end
  55. end
  56. return false,nil
  57. end
  58. function DownloaderHeadManager.removeDownloadTask(task)
  59. for k,v in pairs(DownloaderHeadManager.dowloadTasks) do
  60. if v.id==task.id then
  61. table.remove(DownloaderHeadManager.dowloadTasks,k)
  62. return
  63. end
  64. end
  65. end
  66. function DownloaderHeadManager.update()
  67. logD("DownloaderHeadManager.update11111111111")
  68. if #DownloaderHeadManager.tasks>0 and DownloaderHeadManager.downCount<MAX_COUNT then
  69. print("DownloaderHeadManager.update downCount:"..DownloaderHeadManager.downCount.." total:"..(#DownloaderHeadManager.tasks))
  70. DownloaderHeadManager.downCount=DownloaderHeadManager.downCount+1
  71. local task=DownloaderHeadManager.tasks[#DownloaderHeadManager.tasks]
  72. DownloaderHeadManager.tasks[#DownloaderHeadManager.tasks]=nil
  73. if task.node and not tolua.isnull(task.node) then
  74. DownloaderHeadManager.download(task)
  75. else
  76. print("不存在 DownloaderHeadManager.update downCount:"..DownloaderHeadManager.downCount.." total:"..(#DownloaderHeadManager.tasks))
  77. DownloaderHeadManager.downCount=DownloaderHeadManager.downCount-1
  78. end
  79. end
  80. logD("DownloaderHeadManager.update2222222222")
  81. -- if #DownloaderHeadManager.tasks ==0 then
  82. -- logD("结束头像更新")
  83. -- cc.Director:getInstance():getScheduler():unscheduleScriptEntry(DownloaderHeadManager.scheduleId)
  84. -- DownloaderHeadManager.scheduleId=nil
  85. -- end
  86. end
  87. function DownloaderHeadManager.download(task)
  88. -- DownloaderHeadManager.dowloadTasks[]
  89. table.insert(DownloaderHeadManager.dowloadTasks,task)
  90. local function callback()
  91. dump(task,"DownloaderHeadManager.download callback downCount:"..DownloaderHeadManager.downCount)
  92. DownloaderHeadManager.downCount=DownloaderHeadManager.downCount-1
  93. DownloaderHeadManager.update()
  94. if task.updateHeadImage then
  95. task.updateHeadImage()
  96. end
  97. DownloaderHeadManager.removeDownloadTask(task)
  98. end
  99. dowloadImageFile(task.imageUrl,task.fileName,callback)
  100. -- if isWin32Platform() then
  101. -- getImageFromUrlWithTime(task.imageUrl, task.fileName, nil, callback)
  102. -- else
  103. -- local fullPath = cc.FileUtils:getInstance():getWritablePath()..task.fileName
  104. -- local function onFinish(result)
  105. -- local resultJson = json.decode(result)
  106. -- dump(resultJson,"DownloaderHeadManager.download onFinish")
  107. -- local code = tonumber(resultJson.code)
  108. -- local msg = resultJson.message
  109. -- callback()
  110. -- local isExist = cc.FileSystem:fileExists(fullPath)
  111. -- if isExist then
  112. -- saveImageTime(task.fileName, os.time())
  113. -- end
  114. -- end
  115. -- app.plugin:createTask(task.imageUrl,fullPath,onFinish,-1)
  116. -- end
  117. end
  118. return DownloaderHeadManager