Instantly share code, notes, and snippets.

@illuzor /WallpaperFragment.kt Secret
Last active Sep 23, 2018

Embed
What would you like to do?
package com.illuzor.lesson.wallpapers.screens
import android.os.Bundle
import androidx.lifecycle.ViewModelProviders
import com.illuzor.lesson.wallpapers.R
import com.illuzor.lesson.wallpapers.model.ViewModelWallpaper
import com.illuzor.lesson.wallpapers.model.ViewModelBase.State.*
import java.io.File
@Suppress("PrivatePropertyName")
class WallpaperFragment : AbstractFragment() {
override val layoutId = R.layout.fragment_wallpaper
private var loaded = false
private lateinit var relativeUrl: String
private lateinit var model: ViewModelWallpaper
private lateinit var imageFile: File
private val CACHE_SIZE = 20
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
model = ViewModelProviders.of(this).get(ViewModelWallpaper::class.java)
val filename = arguments!!.getString("filename", "")
val category = arguments!!.getString("category", "")
relativeUrl = "images/$category/$filename"
createFile("$category-$filename")
}
private fun createFile(filename: String) {
val subfolder = File(context!!.cacheDir.path + "/wallpapers/")
imageFile = subfolder.resolve(filename)
if (imageFile.exists()) return
if (subfolder.exists() && subfolder.listFiles().size >= CACHE_SIZE) {
subfolder.listFiles().forEach { it.delete() }
} else {
subfolder.mkdir()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment