2020-05-14 20:14:41 +02:00
|
|
|
package com.h.pixeldroid.fragments
|
2020-04-23 18:49:32 +02:00
|
|
|
|
|
|
|
import android.graphics.Color
|
|
|
|
import android.graphics.drawable.ColorDrawable
|
|
|
|
import android.os.Bundle
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.ImageView
|
|
|
|
import com.bumptech.glide.Glide
|
2020-10-04 23:27:06 +02:00
|
|
|
import com.google.android.material.snackbar.Snackbar
|
2020-05-14 20:14:41 +02:00
|
|
|
import com.h.pixeldroid.R
|
2020-10-04 23:27:06 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_image.*
|
2020-04-23 18:49:32 +02:00
|
|
|
|
|
|
|
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
|
|
|
private const val IMG_URL = "imgurl"
|
2020-10-04 23:27:06 +02:00
|
|
|
private const val IMG_DESCRIPTION = "imgdescription"
|
2020-04-23 18:49:32 +02:00
|
|
|
private const val RQST_BLDR = "rqstbldr"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple [Fragment] subclass.
|
|
|
|
* Use the [ImageFragment.newInstance] factory method to
|
|
|
|
* create an instance of this fragment.
|
|
|
|
*/
|
|
|
|
class ImageFragment : Fragment() {
|
|
|
|
private lateinit var imgUrl: String
|
2020-10-04 23:27:06 +02:00
|
|
|
private lateinit var imgDescription: String
|
2020-04-23 18:49:32 +02:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
arguments?.let {
|
2020-10-04 23:27:06 +02:00
|
|
|
imgUrl = it.getString(IMG_URL)!!
|
|
|
|
imgDescription = it.getString(IMG_DESCRIPTION)!!.ifEmpty { getString(R.string.no_description) }
|
2020-04-23 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
2020-04-30 20:01:35 +02:00
|
|
|
val view = inflater.inflate(R.layout.fragment_image, container, false)
|
2020-04-23 18:49:32 +02:00
|
|
|
|
2020-04-30 20:01:35 +02:00
|
|
|
view.findViewById<ImageView>(R.id.imageImageView).setOnLongClickListener {
|
2020-10-04 23:27:06 +02:00
|
|
|
Snackbar.make(it, imgDescription, Snackbar.LENGTH_SHORT).show()
|
2020-04-30 20:01:35 +02:00
|
|
|
true
|
|
|
|
}
|
2020-10-04 23:27:06 +02:00
|
|
|
|
2020-04-23 18:49:32 +02:00
|
|
|
// Inflate the layout for this fragment
|
2020-04-30 20:01:35 +02:00
|
|
|
return view
|
2020-04-23 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
//Load the image into to view
|
2020-05-14 20:14:41 +02:00
|
|
|
Glide.with(this)
|
2020-04-23 18:49:32 +02:00
|
|
|
.asDrawable().fitCenter()
|
|
|
|
.placeholder(ColorDrawable(Color.GRAY))
|
2020-05-14 20:14:41 +02:00
|
|
|
.load(imgUrl)
|
|
|
|
.into(view.findViewById(R.id.imageImageView)!!)
|
2020-10-04 23:27:06 +02:00
|
|
|
imageImageView.contentDescription = imgDescription
|
2020-04-23 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
/**
|
|
|
|
* Use this factory method to create a new instance of
|
|
|
|
* this fragment using the provided parameters.
|
|
|
|
*
|
|
|
|
* @param imageUrl the url of the image we want to create a fragment for
|
|
|
|
* @return A new instance of fragment ImageFragment.
|
|
|
|
*/
|
|
|
|
@JvmStatic
|
2020-10-04 23:27:06 +02:00
|
|
|
fun newInstance(imageUrl: String, imageDescription: String) =
|
2020-04-23 18:49:32 +02:00
|
|
|
ImageFragment().apply {
|
|
|
|
arguments = Bundle().apply {
|
|
|
|
putString(IMG_URL, imageUrl)
|
2020-10-04 23:27:06 +02:00
|
|
|
putString(IMG_DESCRIPTION, imageDescription)
|
2020-04-23 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|