|
@@ -0,0 +1,175 @@
|
|
|
|
|
+package com.example.snapshots
|
|
|
|
|
+
|
|
|
|
|
+import android.annotation.SuppressLint
|
|
|
|
|
+import android.content.Context
|
|
|
|
|
+import android.os.Bundle
|
|
|
|
|
+import android.util.Log
|
|
|
|
|
+import androidx.fragment.app.Fragment
|
|
|
|
|
+import android.view.LayoutInflater
|
|
|
|
|
+import android.view.View
|
|
|
|
|
+import android.view.ViewGroup
|
|
|
|
|
+import android.widget.Toast
|
|
|
|
|
+import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
+import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
+import com.bumptech.glide.Glide
|
|
|
|
|
+import com.bumptech.glide.load.engine.DiskCacheStrategy
|
|
|
|
|
+import com.example.snapshots.databinding.FragmentHomeBinding
|
|
|
|
|
+import com.example.snapshots.databinding.ItemSnapshotBinding
|
|
|
|
|
+import com.firebase.ui.database.FirebaseRecyclerAdapter
|
|
|
|
|
+import com.firebase.ui.database.FirebaseRecyclerOptions
|
|
|
|
|
+import com.firebase.ui.database.SnapshotParser
|
|
|
|
|
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
|
|
|
+import com.google.android.material.snackbar.Snackbar
|
|
|
|
|
+import com.google.firebase.auth.FirebaseAuth
|
|
|
|
|
+import com.google.firebase.database.DatabaseError
|
|
|
|
|
+import com.google.firebase.database.DatabaseReference
|
|
|
|
|
+import com.google.firebase.database.FirebaseDatabase
|
|
|
|
|
+import com.google.firebase.storage.FirebaseStorage
|
|
|
|
|
+
|
|
|
|
|
+class HomeFragment : Fragment(), HomeAux {
|
|
|
|
|
+ private lateinit var mBinding: FragmentHomeBinding
|
|
|
|
|
+ private lateinit var mFirebaseAdapter: FirebaseRecyclerAdapter<Snapshot, SnapshotHolder>
|
|
|
|
|
+ private lateinit var mLayoutManager: RecyclerView.LayoutManager
|
|
|
|
|
+ private lateinit var mSnapshotsRef: DatabaseReference
|
|
|
|
|
+
|
|
|
|
|
+ override fun onCreateView(
|
|
|
|
|
+ inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
|
+ savedInstanceState: Bundle?
|
|
|
|
|
+ ): View {
|
|
|
|
|
+ mBinding = FragmentHomeBinding.inflate(inflater, container, false)
|
|
|
|
|
+ return mBinding.root
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
+ super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
+
|
|
|
|
|
+ val query = FirebaseDatabase.getInstance().reference.child("snapshots")
|
|
|
|
|
+ val options = FirebaseRecyclerOptions.Builder<Snapshot>().setQuery(query,SnapshotParser {
|
|
|
|
|
+ val snapshot = it.getValue(Snapshot::class.java)
|
|
|
|
|
+ snapshot!!.id = it.key!!
|
|
|
|
|
+ snapshot
|
|
|
|
|
+ }).build()
|
|
|
|
|
+
|
|
|
|
|
+ mFirebaseAdapter = object : FirebaseRecyclerAdapter<Snapshot, SnapshotHolder>(options) {
|
|
|
|
|
+ private lateinit var mContext: Context
|
|
|
|
|
+
|
|
|
|
|
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SnapshotHolder {
|
|
|
|
|
+ mContext = parent.context
|
|
|
|
|
+
|
|
|
|
|
+ val view = LayoutInflater.from(mContext).inflate(R.layout.item_snapshot, parent, false)
|
|
|
|
|
+
|
|
|
|
|
+ return SnapshotHolder(view)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onBindViewHolder(holder: SnapshotHolder, position: Int, model: Snapshot) {
|
|
|
|
|
+ val snapshot = getItem(position)
|
|
|
|
|
+ with(holder) {
|
|
|
|
|
+ setListener(snapshot)
|
|
|
|
|
+ binding.tvTitle.text = snapshot.title
|
|
|
|
|
+ binding.cbLike.text = snapshot.likeList.keys.size.toString() // Cuantos likes tiene
|
|
|
|
|
+ // Si nosotros le dimos like
|
|
|
|
|
+ FirebaseAuth.getInstance().currentUser?.let {
|
|
|
|
|
+ binding.cbLike.isChecked = snapshot.likeList.containsKey(it.uid)
|
|
|
|
|
+ }
|
|
|
|
|
+ Glide.with(mContext)
|
|
|
|
|
+ .load(snapshot.photoUrl)
|
|
|
|
|
+ .diskCacheStrategy(DiskCacheStrategy.ALL)
|
|
|
|
|
+ .centerCrop()
|
|
|
|
|
+ .into(binding.ivPhoto)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressLint("NotifyDataSetChanged") // Error RecyclerView
|
|
|
|
|
+ override fun onDataChanged() {
|
|
|
|
|
+ super.onDataChanged()
|
|
|
|
|
+ mBinding.progressBar.visibility = View.GONE
|
|
|
|
|
+ notifyDataSetChanged()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onError(error: DatabaseError) {
|
|
|
|
|
+ super.onError(error)
|
|
|
|
|
+ Toast.makeText(mContext, error.message, Toast.LENGTH_SHORT).show()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ mLayoutManager = LinearLayoutManager(context)
|
|
|
|
|
+ // mBinding.recyclerView.itemAnimator = null
|
|
|
|
|
+ mBinding.recyclerView.apply {
|
|
|
|
|
+ setHasFixedSize(true)
|
|
|
|
|
+ layoutManager = mLayoutManager
|
|
|
|
|
+ adapter = mFirebaseAdapter
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ setupFirebase()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private fun setupFirebase() {
|
|
|
|
|
+ mSnapshotsRef = FirebaseDatabase.getInstance().reference.child("snapshots")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onStart() {
|
|
|
|
|
+ super.onStart()
|
|
|
|
|
+ mFirebaseAdapter.startListening()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onStop() {
|
|
|
|
|
+ super.onStop()
|
|
|
|
|
+ mFirebaseAdapter.stopListening()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private fun deleteSnapshot (snapshot: Snapshot) {
|
|
|
|
|
+ context?.let {
|
|
|
|
|
+ MaterialAlertDialogBuilder(it)
|
|
|
|
|
+ .setTitle(R.string.dialog_delete_title)
|
|
|
|
|
+ .setPositiveButton(R.string.dialog_delete_confirm) { _, _ ->
|
|
|
|
|
+ Log.i("User actual", FirebaseAuth.getInstance().currentUser!!.uid)
|
|
|
|
|
+ val storageReference = FirebaseStorage.getInstance().reference
|
|
|
|
|
+ val refSnapshot = storageReference.child("snapshots").child(FirebaseAuth.getInstance().currentUser!!.uid).child(snapshot.id)
|
|
|
|
|
+ // storageReference.child(FirebaseAuth.getInstance().currentUser!!.uid).child(snapshot.id)
|
|
|
|
|
+ refSnapshot.delete().addOnCompleteListener { result ->
|
|
|
|
|
+ if (result.isSuccessful){
|
|
|
|
|
+ mSnapshotsRef.child(snapshot.id).removeValue()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Snackbar.make(mBinding.root, getString(R.string.home_delete_photo_error),
|
|
|
|
|
+ Snackbar.LENGTH_LONG).show()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ .setNegativeButton(R.string.dialog_delete_cancel, null)
|
|
|
|
|
+ .show()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private fun setLike (snapshot: Snapshot, checked: Boolean) {
|
|
|
|
|
+ val databaseReference = FirebaseDatabase.getInstance().reference.child("snapshots")
|
|
|
|
|
+ if (checked) {
|
|
|
|
|
+ databaseReference.child(snapshot.id).child("likeList")
|
|
|
|
|
+ .child(FirebaseAuth.getInstance().currentUser!!.uid).setValue(checked)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ databaseReference.child(snapshot.id).child("likeList")
|
|
|
|
|
+ .child(FirebaseAuth.getInstance().currentUser!!.uid).setValue(null)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ inner class SnapshotHolder (view: View) : RecyclerView.ViewHolder (view) {
|
|
|
|
|
+ val binding = ItemSnapshotBinding .bind(view)
|
|
|
|
|
+
|
|
|
|
|
+ fun setListener (snapshot: Snapshot) {
|
|
|
|
|
+ with(binding) {
|
|
|
|
|
+ btnDelete.setOnClickListener {
|
|
|
|
|
+ // Log.i("Seleccionado: ", snapshot.toString()
|
|
|
|
|
+ deleteSnapshot(snapshot)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ cbLike.setOnCheckedChangeListener { _, checked ->
|
|
|
|
|
+ setLike(snapshot, checked)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun refresh() {
|
|
|
|
|
+ mBinding.recyclerView.smoothScrollToPosition(0)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|