viewModel.error.observe(this, Observer {
showError(it)
})
viewModel.progress.observe(this, Observer {
setProgress(it)
})
supportFragmentManager.beginTransaction().add(R.id.container, homeFragment, homeFragment.tag)
.hide(homeFragment).commit()
supportFragmentManager.beginTransaction().add(R.id.container, favouriteFragment, favouriteFragment.tag)
.hide(favouriteFragment).commit()
supportFragmentManager.beginTransaction().add(R.id.container, cartFragment, cartFragment.tag)
.hide(cartFragment).commit()
supportFragmentManager.beginTransaction().add(R.id.container, ordersFragment, ordersFragment.tag)
.hide(ordersFragment).commit()
supportFragmentManager.beginTransaction().add(R.id.container, userFragment, userFragment.tag)
.hide(userFragment).commit()
supportFragmentManager.beginTransaction().show(activeFragment).commit()
binding.navBottom.setOnItemSelectedListener {
when (it.itemId) {
R.id.homeFragment -> {
supportFragmentManager.beginTransaction().hide(activeFragment)
.show(homeFragment).commit()
activeFragment = homeFragment
}
R.id.favouriteFragment -> {
supportFragmentManager.beginTransaction().hide(activeFragment)
.show(favouriteFragment).commit()
activeFragment = favouriteFragment
}
R.id.cartFragment -> {
supportFragmentManager.beginTransaction().hide(activeFragment)
.show(cartFragment).commit()
activeFragment = cartFragment
}
R.id.orderFragment -> {
supportFragmentManager.beginTransaction().hide(activeFragment)
.show(ordersFragment).commit()
activeFragment = ordersFragment
}
R.id.userFragment -> {
supportFragmentManager.beginTransaction().hide(activeFragment)
.show(userFragment).commit()
activeFragment = userFragment
}
}
return@setOnItemSelectedListener true
}
if(intent.hasExtra(Constants.EXTRA_DATA)){
// binding.navBottom.postDelayed({
binding.navBottom.selectedItemId = intent.getIntExtra(Constants.EXTRA_DATA, R.id.homeFragment)
// }, 1000)
}
}
override fun loadData() {
}
override fun initData() {
}
override fun updateData() {
}
}
package uz.bdm.base.screen.main.home
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import uz.bdm.base.R
import uz.bdm.base.base.BaseAdapterListener
import uz.bdm.base.base.BaseFragment
import uz.bdm.base.base.showError
import uz.bdm.base.databinding.FragmentHomeBinding
import uz.bdm.base.model.*
import uz.bdm.base.screen.main.category.CategoryDetailActivity
import uz.bdm.base.screen.main.MainViewModel
import uz.bdm.base.screen.main.productlist.ProductDetailActivity
import uz.bdm.base.screen.main.productlist.ProductListActivity
import uz.bdm.base.utils.Constants
import uz.bdm.base.utils.Prefs
import uz.bdm.base.view.adapters.BranchListAdapter
import uz.bdm.base.view.adapters.CategoryAdapter
import uz.bdm.base.view.adapters.PagerAdapter
import uz.bdm.base.view.adapters.TopProductAdapter
class HomeFragment : BaseFragment() {
companion object {
@JvmStatic
fun newInstance() = HomeFragment()
}
private var countDownTimer: CountDownTimer? = null
private lateinit var pagerAdapter: PagerAdapter
private var count: Int = 0
lateinit var viewModel: MainViewModel
private var branchId: String = ""
private var categoryItemById = ArrayList()
var requestProductModel = RequestProductModel(
"", "", "",
"", true, emptyList()
)
override fun getViewBinding(): FragmentHomeBinding = FragmentHomeBinding.inflate(layoutInflater)
override fun setupViews() {
viewModel = ViewModelProvider(this)[MainViewModel::class.java]
binding.swipe.setOnRefreshListener {
loadData()
}
viewModel.progress.observe(this, Observer {
binding.swipe.isRefreshing = false
})
viewModel.error.observe(this, Observer {
requireActivity().showError(it)
})
viewModel.branchListData.observe(this, Observer {
if (Prefs.getSelectedBranch() == null) {
it[0].checked = true
requestProductModel.branchId = it[0].id
viewModel.getCategory(it[0].id)
Prefs.setSelectedBranch(it[0])
} else {
it.forEach { item ->
if (item.id == Prefs.getSelectedBranch()!!.id) {
item.checked = true
}
}
requestProductModel.branchId = Prefs.getSelectedBranch()!!.id
viewModel.getCategory(Prefs.getSelectedBranch()!!.id)
}
viewModel.getProducts(Prefs.getSelectedBranch()!!.id,requestProductModel)
binding.rvBranchList.adapter = BranchListAdapter(it, object : BaseAdapterListener {
override fun onClickItem(item: Any?) {
item as BranchModel
Prefs.setSelectedBranch(item)
branchId = item.id
viewModel.getCategory(branchId)
requestProductModel.branchId = item.id
viewModel.getProducts(Prefs.getSelectedBranch()!!.id,requestProductModel)
}
})
})
viewModel.offerData.observe(this, Observer {
count = it.size
pagerAdapter = PagerAdapter(it)
binding.viewPager.adapter = pagerAdapter
timer()
setPagerAdapter()
setIndicator()
setUpActiveIndicator(0)
})
viewModel.categoryData.observe(this, Observer {
Prefs.setCategories(it)
categoryItemById.clear()
it.forEach { item ->
if (item.category_id == "00000000-0000-0000-0000-000000000000") {
categoryItemById.add(item)
}
}
binding.rvCategory.adapter =
CategoryAdapter(categoryItemById, object : BaseAdapterListener {
override fun onClickItem(item: Any?) {
item as CategoryModel
Prefs.setCategoryItem(item)
if(Prefs.getCategories()?.filter { it.category == item.name }.isNullOrEmpty()){
val intent = Intent(requireContext(), ProductListActivity::class.java)
intent.putExtra(Constants.EXTRA_DATA, item.id)
startActivity(intent)
} else {
val intent = Intent(requireContext(),CategoryDetailActivity::class.java)
intent.putExtra(Constants.EXTRA_DATA, item)
startActivity(intent)
}
}
})
})
viewModel.productsData.observe(this, Observer {
binding.rvTopProduct.adapter = TopProductAdapter(it, object:BaseAdapterListener{
override fun onClickItem(item: Any?) {
item as ProductModel
val intent = Intent(requireContext(),ProductDetailActivity::class.java)
intent.putExtra(Constants.EXTRA_DATA,item)
startActivity(intent)
}
})
})
binding.rvBranchList.layoutManager =
LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
binding.rvCategory.layoutManager = GridLayoutManager(requireContext(), 2)
binding.rvTopProduct.layoutManager = GridLayoutManager(requireContext(), 2)
}
override fun loadData() {
viewModel.getOffer()
viewModel.getBranchList()
}
override fun setData() {
}
private fun setPagerAdapter() {
binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
setUpActiveIndicator(position)
}
})
}
private fun setIndicator() {
binding.indicatorContainer.removeAllViews()
val indicators = arrayOfNulls(pagerAdapter.itemCount)
val layoutParams: LinearLayout.LayoutParams =
LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
layoutParams.setMargins(6, 0, 6, 0)
for (i in indicators.indices) {
indicators[i] = ImageView(requireContext())
indicators[i]?.setImageDrawable(
ContextCompat.getDrawable(requireContext(), R.drawable.indicator_deactive)
)
indicators[i]?.layoutParams = layoutParams
binding.indicatorContainer.addView(indicators[i])
}
}
private fun setUpActiveIndicator(position: Int) {
val childCount = binding.indicatorContainer.childCount
for (i in 0 until childCount) {
val indicator = binding.indicatorContainer.getChildAt(i) as ImageView
if (i == position) {
indicator.setImageDrawable(
ContextCompat.getDrawable(
requireActivity(),
R.drawable.indicator_active
)
)
} else {
indicator.setImageDrawable(
ContextCompat.getDrawable(
requireActivity(),
R.drawable.indicator_deactive
)
)
}
}
}
Do'stlaringiz bilan baham: |