Instantly share code, notes, and snippets.

@illuzor /WallpaperFragment.kt Secret
Last active Oct 2, 2018

Embed
What would you like to do?
package com.illuzor.lesson.wallpapers.screens
import android.Manifest
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.illuzor.lesson.wallpapers.GlideApp
import com.illuzor.lesson.wallpapers.R
import com.illuzor.lesson.wallpapers.extensions.runInBackground
import com.illuzor.lesson.wallpapers.extensions.runInMainThread
import com.illuzor.lesson.wallpapers.extensions.toast
import com.illuzor.lesson.wallpapers.model.ViewModelBase.State.*
import com.illuzor.lesson.wallpapers.model.ViewModelWallpaper
import com.illuzor.lesson.wallpapers.ui.ProgressDialog
import kotlinx.android.synthetic.main.fragment_wallpaper.*
import java.io.File
@Suppress("PrivatePropertyName")
class WallpaperFragment : AbstractFragment() {
private val REQUEST_STORAGE_PERMISSION_CODE = 0
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()
}
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
contentView = pv_wallpaper
if (model.state != PROGRESS && imageFile.exists()) {
loadFileToImageView()
} else {
checkData()
}
}
private fun checkData() {
if (view == null) return
when (model.state) {
CREATED -> {
showProgress()
model.setListener { checkData() }
model.load(relativeUrl, imageFile)
}
PROGRESS -> {
showProgress()
model.setListener { checkData() }
}
LOADED -> loadFileToImageView()
ERROR -> {
showError(R.string.unable_to_load) {
showProgress()
model.setListener { checkData() }
model.load(relativeUrl, imageFile)
}
}
}
}
private fun loadFileToImageView() {
showContent()
GlideApp.with(context!!)
.load(Uri.fromFile(imageFile))
.transition(DrawableTransitionOptions.withCrossFade())
.into(pv_wallpaper)
loaded = true
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu_wallpaper, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (!loaded && item.itemId != android.R.id.home) {
toast(R.string.not_loaded_yet)
} else {
when (item.itemId) {
R.id.menu_item_set_wallpaper -> setWallpaper()
R.id.menu_item_share_image -> shareWallpaper()
R.id.menu_item_save_image -> checkPermissionForImageSave()
}
}
return super.onOptionsItemSelected(item)
}
private fun setWallpaper() {
}
private fun shareWallpaper() {
}
private fun checkPermissionForImageSave() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
activity!!.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveImageToGallery()
} else {
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_STORAGE_PERMISSION_CODE)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (requestCode == REQUEST_STORAGE_PERMISSION_CODE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
saveImageToGallery()
} else {
toast(R.string.unable_to_save_permission)
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
private fun saveImageToGallery() {
val dialog = ProgressDialog()
dialog.isCancelable = false
dialog.title = getString(R.string.save_image)
dialog.show(fragmentManager, "")
runInBackground {
MediaStore.Images.Media.insertImage(context!!.contentResolver, imageFile.path, "wallpaper", "")
runInMainThread {
dialog.dismiss()
toast(R.string.saved)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment