Created
April 14, 2021 04:52
-
-
Save rohitjakhar/f784394721b68dfd7717050b8979aef6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rohitjakhar.sagarkhurana.ui.product | |
import android.content.Intent | |
import android.os.Bundle | |
import android.view.* | |
import androidx.fragment.app.Fragment | |
import androidx.fragment.app.viewModels | |
import androidx.lifecycle.lifecycleScope | |
import androidx.navigation.fragment.findNavController | |
import androidx.recyclerview.widget.DividerItemDecoration | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import com.google.firebase.auth.ktx.auth | |
import com.google.firebase.ktx.Firebase | |
import com.rohitjakhar.sagarkhurana.R | |
import com.rohitjakhar.sagarkhurana.adapter.ProductAdapter | |
import com.rohitjakhar.sagarkhurana.databinding.FragmentProductBinding | |
import com.rohitjakhar.sagarkhurana.ui.auth.AuthActivity | |
import com.rohitjakhar.sagarkhurana.utils.Helper.dLog | |
import com.rohitjakhar.sagarkhurana.viewmodel.ProductViewModel | |
import kotlinx.coroutines.flow.collect | |
import kotlinx.coroutines.flow.filter | |
import kotlinx.coroutines.launch | |
class ProductFragment : Fragment() { | |
private var _binding: FragmentProductBinding? = null | |
private val binding get() = _binding!! | |
private val mAdapter = ProductAdapter() | |
private val productViewModel by viewModels<ProductViewModel>() | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View { | |
_binding = FragmentProductBinding.inflate(inflater, container, false) | |
setHasOptionsMenu(true) | |
binding.rvProduc.apply { | |
setHasFixedSize(true) | |
layoutManager = LinearLayoutManager(requireContext()) | |
adapter = mAdapter | |
addItemDecoration( | |
DividerItemDecoration( | |
requireContext(), | |
DividerItemDecoration.HORIZONTAL | |
) | |
) | |
} | |
lifecycleScope.launch { | |
productViewModel.fetechAllData.collect { | |
mAdapter.submitData(it) | |
} | |
} | |
binding.filterLayout.setOnClickListener { | |
dLog("clicked") | |
mAdapter.refresh() | |
lifecycleScope.launch { | |
productViewModel.filterData.collect { | |
mAdapter.submitData(it) | |
dLog(it.toString()) | |
} | |
mAdapter.notifyDataSetChanged() | |
} | |
} | |
binding.imgBtnAddProduct.setOnClickListener { | |
val ac = ProductFragmentDirections.actionNavProductFragmentToAddProductFragment() | |
findNavController().navigate(ac) | |
} | |
return binding.root | |
} | |
override fun onDestroy() { | |
_binding = null | |
super.onDestroy() | |
} | |
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { | |
inflater.inflate(R.menu.top_menu, menu) | |
super.onCreateOptionsMenu(menu, inflater) | |
} | |
override fun onOptionsItemSelected(item: MenuItem): Boolean { | |
if (item.itemId == R.id.nav_log_out) { | |
try { | |
dLog("user Logout") | |
Firebase.auth.signOut() | |
startActivity( | |
Intent(requireActivity(), AuthActivity::class.java).apply { | |
this.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | |
} | |
) | |
requireActivity().finish() | |
} catch (e: Exception) { | |
dLog("error logout: ${e.message}") | |
} | |
} | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment