Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Update GlideImage class to use modified URLs and DiskCacheStrategy
- Modify all 20 URLs as described below:
- Add 3 new constant properties to GlideImage class above your URLs:
- private val randomSiteIdentifier = "<RANDOM>" // note < and > are illegal URL characters
- private val appScreenWidth = "<appscreenwidth>"
- private val appScreenHeight = "<appscreenheight>"
- Now modify your URLs to use these new properties using <RANDOM> at the end of any URLs that link to sites that provide a random image on every https request, replace all width values with <appscreenwidth> and all height values with <appscreenheight> as shown below:
- "https://loremflickr.com/${appScreenWidth}/${appScreenHeight}$randomSiteIdentifier"
- "https://placebeard.it/${appScreenWidth}x${appScreenHeight}"
- Add these three new properties for diskCacheStrategy, width and height under your URLs:
- // new properties
- private var diskCacheStrategy = DiskCacheStrategy.NONE
- private var width = 300
- private var height = 400
- Update loadGlideImage() method with this code just above the actual load ( Glide.with ):
- var updatedUrl = url
- if (url.contains(randomSiteIdentifier)) {
- diskCacheStrategy = DiskCacheStrategy.NONE
- updatedUrl = url.replace(randomSiteIdentifier, "")
- } else {
- diskCacheStrategy = DiskCacheStrategy.ALL
- }
- updatedUrl = updatedUrl.replace(appScreenWidth, width.toString())
- updatedUrl = updatedUrl.replace(appScreenHeight, height.toString())
- Modify the Glide.with statement to use these two new properties:
- Glide.with(context) // our context = the Activity
- .load(updatedUrl)
- .diskCacheStrategy(diskCacheStrategy)
- Modify the rest of the code in Glide.with to use these two new properties:
- .listener(object: RequestListener<Drawable>{
- override fun onLoadFailed(
- e: GlideException?,
- model: Any?,
- target: Target<Drawable>?,
- isFirstResource: Boolean
- ): Boolean {
- progressBar.visibility = View.GONE
- context.toast("Glide Load Failed: $updatedUrl")
- return false
- }
- override fun onResourceReady(
- resource: Drawable?,
- model: Any?,
- target: Target<Drawable>?,
- dataSource: DataSource?,
- isFirstResource: Boolean
- ): Boolean {
- progressBar.visibility = View.GONE
- imageView.setImageDrawable(resource)
- lastURL = updatedUrl
- sharedPreference.save(TheApp.context.getString(R.string.last_url_key), lastURL)
- return false
- }
- })
- .into(imageView)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement