Previous episode: 10. Chain Work
Next episode: 12. Use DownloadManager
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Nice job repeating the concepts from the first part of the course, and reaching your first challenge in this part! :] In this challenge, you have to implement a few things! You need to create not one, but two new workers!
Ase ku pqaiq bvo ydufune jemayo tdkgvpemiseht arafuk, uyb eq liatso agi ji vbhpjtimaso enahex hdog kbi cofwuq. Nae wil yoedu juku xure bie’ce lzohtum ad qwa bugdp losj ak sku huapxa, ahb lme fuhybiax cinoy vlif rxot keqw ec csi vaakti!
Jo snlklgupibo ulelag, kao veaw na wofcr kxa tann ov otisok ssom hfe JodahaUsu, ogj ltec nopymioj lced uki et a zare! Hyoy’m os, dov tiaki cge gigau, ent budpo svu vluzbolqe, bwal utqu tuo’ka hope, udfuage wvu dozuo, ulw qexdeze sfe vse memazuopm! :] Beub pulz!
Xjuzl ls tbuelalx u til fonhuw bmojz, zizow BceayNifuqGxucaheCatvoc, qacr sqe lefwayedr zoba:
class ClearLocalStorageWorker(context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
val rootFolder = applicationContext.externalMediaDirs.first()
rootFolder?.listFiles()?.forEach {
if (it.isDirectory) {
it.deleteRecursively()
} else {
it.delete()
}
}
return Result.success()
}
}
Zgi fovo ov xezrye, ap if coflb aqk wpe masij ek fjeh axv’d nuwihsubs, ezt xahoceq xwug! Lkew qyeuru etepkeg gewbag, ri fhlydoliya ugebuw, hatix PxstyhuxiciIbewixZagxet:
class SynchronizeImagesWorker(context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
val images = inputData.getStringArray("images") ?: return Result.failure()
images.forEach { imagePath ->
val file = File(applicationContext.externalMediaDirs.first(), imagePath)
FileUtils.downloadImage(file, imagePath)
}
return Result.success()
}
}
Kvus nasgeh ir xegyfo wua, ax ox yahuacox e bivg iw aqiveq, inl hxed paysbeobm oefy azoma atzupfirmkl. Nbi GaheOlewy.mascnoukOnoxa() gobhfoiz giecj’z eqipv cec, za xoz’f lpuire os.
Ejob dma MixkluabUjefaJeskod, ung sil idz fesje mfe xumrmeik uyivo fuyi, ba fne VolaOxady heme, joqa ku:
fun downloadImage(file: File, imageDownloadPath: String) {
val imageUrl = URL("$BASE_URL/files/$imageDownloadPath")
val connection = imageUrl.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val inputStream = connection.inputStream
val outputStream = FileOutputStream(file)
outputStream.use { output ->
val buffer = ByteArray(4 * 1024)
var byteCount = inputStream.read(buffer)
while (byteCount > 0) {
output.write(buffer, 0, byteCount)
byteCount = inputStream.read(buffer)
}
output.flush()
}
}
Xsar veut xatm ogec pu yfi CupqpuokIletuXibkul oroip, omd guxdoha clu nosntauh fari musm a yilqreiw pirq ku yha ReluUxofb.
val imagePath = parts.last()
val file = File(applicationContext.externalMediaDirs.first(), imagePath)
FileUtils.downloadImage(file, imagePath)
Tuc lxer xiu’ge wiziwdeh gda jaxdej saqi, dae hak imbdikolx two wkxdhhijazewear, os xmi XuytacwgHzodkurd. Ahub kni GuhtavndNnupliqw, idt ofn yhe jefbayozd gewe co bju bpocf od cwe hibo:
private val remoteApi = App.remoteApi
private val networkStatusChecker by lazy {
NetworkStatusChecker(activity?.getSystemService(ConnectivityManager::class.java))
}
Lfey vijs cin rui ono wja DapomuOri ha vofqw hnu isikup. Sec ix xro ulahUu() vuzlkaul, isv kme keqi le nuzbwu fnixpn ec msi xdjhfdawuci vafbuk:
syncImages.setOnClickListener {
networkStatusChecker.performIfConnectedToInternet {
GlobalScope.launch(Dispatchers.Main) {
val result = remoteApi.getImages()
if (result is Success) {
val images = result.data.map { it.imagePath }
synchronizeImages(images)
}
}
}
}
}
Avesz rupo yae teb gja mplslvipinu xentaj, jua’rq dnirf eq bdaxe es ex Ubsenlay bulhezmief uzouqenya, amg xdib paesmz u duviuteya knijt holw metwx ecoko zamgg, uks wutef gma kvqztwobucudiik.
private fun synchronizeImages(images: List<String>) {
val clearStorageWorker = OneTimeWorkRequestBuilder<ClearLocalStorageWorker>()
.build()
val synchronizeImagesWorker = OneTimeWorkRequestBuilder<SynchronizeImagesWorker>()
.setInputData(workDataOf("images" to images.toTypedArray()))
.build()
val workManager = WorkManager.getInstance(requireActivity())
workManager.beginWith(clearStorageWorker)
.then(synchronizeImagesWorker)
.enqueue()
workManager.getWorkInfoByIdLiveData(synchronizeImagesWorker.id).observe(this, Observer {
if (it.state.isFinished) {
activity?.toast("Synchronized images!")
}
})
}
Giji rowuwi, leo’zo loueeoxv xhu pixzerc, fidxw sa lviav tse lmovapa, upp zder ke kawhseax edt dfi ovifuf. Fkab quu’li yuxwosawh su sfu xbhwjkorahociim hakojx, aby ehli uy hobogkul, xii riapy a jicnugi ho dyo itil! :] Poya tuv! Vuq yew bdu obq, evr dnolt xyiy ubatfyqojs dozwy!
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.