Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. Update GlideImage class to use modified URLs and DiskCacheStrategy
  2. Modify all 20 URLs as described below:
  3.  
  4. Add 3 new constant properties to GlideImage class above your URLs:
  5.  
  6.  
  7.  
  8. private val randomSiteIdentifier = "<RANDOM>" // note < and > are illegal URL characters
  9. private val appScreenWidth = "<appscreenwidth>"
  10. private val appScreenHeight = "<appscreenheight>"
  11.  
  12.  
  13. 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:
  14.  
  15. "https://loremflickr.com/${appScreenWidth}/${appScreenHeight}$randomSiteIdentifier"
  16. "https://placebeard.it/${appScreenWidth}x${appScreenHeight}"
  17.  
  18.  
  19. Add these three new properties for diskCacheStrategy, width and height under your URLs:
  20.  
  21.  
  22.  
  23. // new properties
  24. private var diskCacheStrategy = DiskCacheStrategy.NONE
  25. private var width = 300
  26. private var height = 400
  27.  
  28.  
  29.  
  30. Update loadGlideImage() method with this code just above the actual load ( Glide.with ):
  31.  
  32.  
  33.  
  34. var updatedUrl = url
  35.  
  36. if (url.contains(randomSiteIdentifier)) {
  37. diskCacheStrategy = DiskCacheStrategy.NONE
  38. updatedUrl = url.replace(randomSiteIdentifier, "")
  39. } else {
  40. diskCacheStrategy = DiskCacheStrategy.ALL
  41. }
  42.  
  43. updatedUrl = updatedUrl.replace(appScreenWidth, width.toString())
  44. updatedUrl = updatedUrl.replace(appScreenHeight, height.toString())
  45.  
  46.  
  47.  
  48. Modify the Glide.with statement to use these two new properties:
  49.  
  50. Glide.with(context) // our context = the Activity
  51. .load(updatedUrl)
  52. .diskCacheStrategy(diskCacheStrategy)
  53.  
  54.  
  55.  
  56. Modify the rest of the code in Glide.with to use these two new properties:
  57.  
  58. .listener(object: RequestListener<Drawable>{
  59. override fun onLoadFailed(
  60. e: GlideException?,
  61. model: Any?,
  62. target: Target<Drawable>?,
  63. isFirstResource: Boolean
  64. ): Boolean {
  65. progressBar.visibility = View.GONE
  66. context.toast("Glide Load Failed: $updatedUrl")
  67. return false
  68. }
  69.  
  70. override fun onResourceReady(
  71. resource: Drawable?,
  72. model: Any?,
  73. target: Target<Drawable>?,
  74. dataSource: DataSource?,
  75. isFirstResource: Boolean
  76. ): Boolean {
  77. progressBar.visibility = View.GONE
  78. imageView.setImageDrawable(resource)
  79.  
  80. lastURL = updatedUrl
  81.  
  82. sharedPreference.save(TheApp.context.getString(R.string.last_url_key), lastURL)
  83.  
  84. return false
  85. }
  86. })
  87. .into(imageView)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement