package com.base.browserwhite.utils

import android.content.Context
import com.base.browserwhite.bean.DownloadBean
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.io.File
import java.lang.reflect.Type

object DownloadHelper {

    fun Context.getDownloadJson(): File {
        val file = File(this.filesDir, "downloadRecord")
        val recordFile = File(file, "download.json")
        if (!file.exists()) {
            file.mkdirs()
        }
        if (!recordFile.exists()) {
            recordFile.createNewFile()
        }
        return recordFile
    }

    fun getNotFinishList(recordFile: File, gson: Gson=Gson()): List<DownloadBean> {
        val olderText = recordFile.readText()
        val type: Type = object : TypeToken<List<DownloadBean>>() {}.type
        return gson.fromJson(olderText, type) ?: listOf()
    }

}