我对书有回收利用的看法。当用户单击一本书时,将启动带有2个按钮的片段,用户可以在其中进行以下操作:
如果用户要下载图书,则进度条将显示在片段的顶部,并显示标题:
Downloading page <n> of <total pagecount>
标题和进度条将更新,直到下载完成。用户可以按下“后退”按钮并下载另一本书。如果用户选择返回第一本书,则该片段应以下载的最新进度启动。
如何最好地实现此功能?
我尝试过的一些事情:
在片段中:
fragmentCallback!!.downloadBook(activity, book, object : DownloadBookProgressCallback {
override fun downloadProgress(page: Int) {
uiThread {
progressBar!!.progress = page
progressText!!.text = "Downloading page " + page + " of " + book.content!!.pageCount!!
}
}
})
接口:
interface FragmentCallback {
fun downloadBook(context: Context, book: ResourcesList, downloadBookProgressCallback: DownloadBookProgressCallback)
fun getDownloadProgress(bookId: Int):Int
fun getDownloadStatus (bookId: Int) : Boolean
fun getMaxPages (bookId: Int) : Int
}
活动中
override fun downloadBook(context: Context, book: ResourcesList, downloadBookProgressCallback: DownloadBookProgressCallback) {
val downloadInstance = DownloadBook()
activeDownloadBooks[book.id] = downloadInstance
downloadInstance.gettingPages(context, book.content!!.pageCount!!, book, downloadBookProgressCallback)
}
override fun getDownloadStatus(bookId: Int): Boolean {
val retrieveInstance = activeDownloadBooks[bookId]
if (retrieveInstance != null) {
return retrieveInstance.getDownloadStatus()
}
return false
}
override fun getDownloadProgress(bookId: Int): Int {
val retrieveInstance = activeDownloadBooks[bookId]
if (retrieveInstance!!.getDownloadStatus()) {
return retrieveInstance.getProgress()
} else {
}
return 0
}
override fun getMaxPages(bookId: Int): Int {
val retrieveInstance = activeDownloadBooks[bookId]
return retrieveInstance!!.getMaxPages()
}
下载课程
class DownloadBook {
private var isDownloading : Boolean = false
private var currentPage = 0
private var maxPages = 0
var sp : SharedPreferences? = null
fun gettingPages(ctx : Context, pageCount : Int, book : ResourcesList, callback: DownloadBookProgressCallback) {
maxPages = pageCount
for (page in 1..pageCount) {
isDownloading = true
println("getting page " + page + " " + book.name)
currentPage = page
callback.downloadProgress(page)
var a = book.content!!.pageURI + page
println(a)
val bmap: Bitmap = Picasso.get().load(a).get()
val cw = ContextWrapper(ctx)
val directory = cw.getDir("BookId" + book.id.toString(), Context.MODE_PRIVATE)
val mypath = File(directory, "page" + page + ".jpg")
var fos: FileOutputStream
try {
fos = FileOutputStream(mypath)
bmap.compress(Bitmap.CompressFormat.PNG, 100, fos)
fos.close()
} catch (e: Exception) {
Log.e("DOWNLOAD_BOOK", e.message, e)
e.printStackTrace()
}
}
sp = PreferenceManager.getDefaultSharedPreferences(ctx)
val editor = sp!!.edit()
println("Saving to shared prefs: " + book.name)
editor.putString(book.id.toString(), book.content!!.pageCount.toString())
editor.apply()
isDownloading = false
}
fun getDownloadStatus() : Boolean {
return isDownloading
}
fun getProgress() : Int {
return currentPage
}
fun getMaxPages() : Int {
return maxPages
}
}
在片段中:检查是否存在当前的下载实例
fragmentCallback = activity as FragmentCallback
if (fragmentCallback != null) {
doAsync {
var stillDownloading = fragmentCallback!!.getDownloadStatus(book_id)
var maxPages = 0
println("Stilldownloading: " + stillDownloading + " Book Id :" + book_id )
if (stillDownloading) {
uiThread {
progressBar!!.visibility = View.VISIBLE
progressText!!.visibility = View.VISIBLE
maxPages = fragmentCallback!!.getMaxPages(book_id)
progressBar!!.max = maxPages
ViewCompat.setBackgroundTintList(
dloadButton!!,
ColorStateList.valueOf(getColorWrapper(activity, R.color.color_grey)))
dloadButton!!.isEnabled = false
}
while (stillDownloading) {
var dloadPage = fragmentCallback!!.getDownloadProgress(book_id)
if (dloadPage > 0) {
uiThread {
progressBar!!.progress = dloadPage
progressText!!.text = "Downloading page " + dloadPage + " of " + maxPages
}
stillDownloading = fragmentCallback!!.getDownloadStatus(book_id)
}
}
}
}
}