Skip to content

Instantly share code, notes, and snippets.

@crypticminds
Last active January 4, 2020 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crypticminds/ad575383d2b5d63e3626f74235a864d7 to your computer and use it in GitHub Desktop.
Save crypticminds/ad575383d2b5d63e3626f74235a864d7 to your computer and use it in GitHub Desktop.
package com.arcane.coldstorage
import android.graphics.Bitmap
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.arcane.coldstorage.cache.ImageCache
import com.arcane.coldstoragecache.callback.OnValueFetchedCallback
import com.arcane.coldstoragecache.converter.impl.StringToBitmapConverter
class MainActivity : AppCompatActivity(), OnValueFetchedCallback<Any?> {
companion object {
val URLS = arrayListOf(
"https://images.unsplash.com/photo-1452857297128-d9c29adba80b?ixlib=rb-1.2.1&w=1000&q=80",
"https://i.ytimg.com/vi/Pc20_oJQusc/maxresdefault.jpg",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS1HHodR1IgMESyE95LqwLRTRFnfCpmKKw5RQHqnP_kWV9ugKaiIQ&s"
)
}
/**
* An instance of image cache.
*/
private val imageCache: ImageCache = ImageCache()
/**
* The image view where the images will be displayed.
*/
private lateinit var imageView: ImageView
/**
* The button used to change the image.
*/
private lateinit var changeButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
imageView = findViewById(R.id.image_view)
changeButton = findViewById(R.id.change)
checkImageCaching()
changeButton.setOnClickListener {
checkImageCaching()
}
}
/**
* Method to test image caching.
*/
private fun checkImageCaching() {
val converter = StringToBitmapConverter()
imageCache.get(
URLS.shuffled().take(1)[0],
this,
converter
)
}
/**
* When the image is downloaded , adding the image to
* the image view.
*/
override fun valueFetched(output: Any?) {
imageCache.commitToSharedPref(applicationContext)
runOnUiThread {
val outputAsBitmap = output as Bitmap
imageView.setImageBitmap(outputAsBitmap)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment