|
-
- local MAX_COUNT=5
- local TASK_ID=1
-
- local DownloaderHeadManager = {
- downCount=0,
- tasks={},
- dowloadTasks={},
- scheduleId=nil,
- }--class("DownloaderHeadManager")
-
-
- function DownloaderHeadManager.addTask(node,imageUrl, fileName, updateHeadImage)
- --如果在队列里面 还没开始下载 则更新数据就行
- local isExist,oldTask=DownloaderHeadManager.isExitTask(node)
- if isExist then
- oldTask.imageUrl=imageUrl
- oldTask.fileName=fileName
- oldTask.updateHeadImage=updateHeadImage
- return
- end
-
- --如果已经开始下载
- local isDownload,oldTask=DownloaderHeadManager.isExitDowloadTask(node)
- if isDownload then
- if oldTask.imageUrl==imageUrl then
- oldTask.updateHeadImage=updateHeadImage
- return
- end
- end
-
- local task={
- node=node,
- imageUrl=imageUrl,
- fileName=fileName,
- updateHeadImage=updateHeadImage,
- id=TASK_ID,
- }
- dump(task,"DownloaderHeadManager.addTask")
- -- logD(" DownloaderHeadManager.addTask",tabletostring(task))
- table.insert(DownloaderHeadManager.tasks,task)
- TASK_ID=TASK_ID+1
-
- DownloaderHeadManager.update()
- -- if not DownloaderHeadManager.scheduleId then
- -- DownloaderHeadManager.scheduleId=cc.Director:getInstance():getScheduler():scheduleScriptFunc(DownloaderHeadManager.update , 0 , false)
- -- end
- end
-
- function DownloaderHeadManager.isExitTask(node)
- for k,v in pairs(DownloaderHeadManager.tasks) do
- if v.node==node then
- return true,v
- end
- end
- return false,nil
- end
-
- function DownloaderHeadManager.isExitDowloadTask(node)
- for k,v in pairs(DownloaderHeadManager.dowloadTasks) do
- if v.node==node then
- return true,v
- end
- end
- return false,nil
- end
-
- function DownloaderHeadManager.removeDownloadTask(task)
- for k,v in pairs(DownloaderHeadManager.dowloadTasks) do
- if v.id==task.id then
- table.remove(DownloaderHeadManager.dowloadTasks,k)
- return
- end
- end
- end
-
- function DownloaderHeadManager.update()
- logD("DownloaderHeadManager.update11111111111")
- if #DownloaderHeadManager.tasks>0 and DownloaderHeadManager.downCount<MAX_COUNT then
- print("DownloaderHeadManager.update downCount:"..DownloaderHeadManager.downCount.." total:"..(#DownloaderHeadManager.tasks))
- DownloaderHeadManager.downCount=DownloaderHeadManager.downCount+1
- local task=DownloaderHeadManager.tasks[#DownloaderHeadManager.tasks]
- DownloaderHeadManager.tasks[#DownloaderHeadManager.tasks]=nil
- if task.node and not tolua.isnull(task.node) then
- DownloaderHeadManager.download(task)
- else
- print("不存在 DownloaderHeadManager.update downCount:"..DownloaderHeadManager.downCount.." total:"..(#DownloaderHeadManager.tasks))
- DownloaderHeadManager.downCount=DownloaderHeadManager.downCount-1
- end
- end
- logD("DownloaderHeadManager.update2222222222")
- -- if #DownloaderHeadManager.tasks ==0 then
- -- logD("结束头像更新")
- -- cc.Director:getInstance():getScheduler():unscheduleScriptEntry(DownloaderHeadManager.scheduleId)
- -- DownloaderHeadManager.scheduleId=nil
- -- end
- end
-
-
- function DownloaderHeadManager.download(task)
- -- DownloaderHeadManager.dowloadTasks[]
- table.insert(DownloaderHeadManager.dowloadTasks,task)
-
- local function callback()
- dump(task,"DownloaderHeadManager.download callback downCount:"..DownloaderHeadManager.downCount)
- DownloaderHeadManager.downCount=DownloaderHeadManager.downCount-1
- DownloaderHeadManager.update()
- if task.updateHeadImage then
- task.updateHeadImage()
- end
- DownloaderHeadManager.removeDownloadTask(task)
- end
-
- dowloadImageFile(task.imageUrl,task.fileName,callback)
- -- if isWin32Platform() then
- -- getImageFromUrlWithTime(task.imageUrl, task.fileName, nil, callback)
- -- else
- -- local fullPath = cc.FileUtils:getInstance():getWritablePath()..task.fileName
-
- -- local function onFinish(result)
- -- local resultJson = json.decode(result)
- -- dump(resultJson,"DownloaderHeadManager.download onFinish")
- -- local code = tonumber(resultJson.code)
- -- local msg = resultJson.message
- -- callback()
- -- local isExist = cc.FileSystem:fileExists(fullPath)
- -- if isExist then
- -- saveImageTime(task.fileName, os.time())
- -- end
- -- end
-
- -- app.plugin:createTask(task.imageUrl,fullPath,onFinish,-1)
- -- end
- end
-
- return DownloaderHeadManager
|