Commit fa26ef72 authored by Aayush Gupta's avatar Aayush Gupta
Browse files

categories: Migrate to viewBinding

- Kotlin Android Extensions is deprecated
- It has important advantages over using findViewById

Ref:
- [1]: https://developer.android.com/topic/libraries/view-binding/migration
- [2]: https://developer.android.com/topic/libraries/view-binding#findviewbyid

Signed-off-by: Aayush Gupta's avatarAayush Gupta <theimpulson@e.email>
parent bb062b03
......@@ -22,38 +22,46 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import foundation.e.apps.R
import foundation.e.apps.categories.model.Category
import foundation.e.apps.categories.viewmodel.CategoriesViewModel
import kotlinx.android.synthetic.main.error_layout.view.*
import kotlinx.android.synthetic.main.fragment_application_categories.view.*
import foundation.e.apps.databinding.FragmentApplicationCategoriesBinding
class ApplicationsFragment() : Fragment() {
private var _binding: FragmentApplicationCategoriesBinding? = null
private val binding get() = _binding!!
private lateinit var categoriesViewModel: CategoriesViewModel
var color:Int = 0;
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentApplicationCategoriesBinding.inflate(inflater, container, false)
categoriesViewModel = ViewModelProvider(this).get(CategoriesViewModel::class.java)
val view = inflater.inflate(R.layout.fragment_application_categories, container, false)
view.categories_list.layoutManager = LinearLayoutManager(context)
// Fragment variables
val categoriesList = binding.categoriesList
val progressBar = binding.progressBar
val errorContainer = binding.errorLayout.errorContainer
val errorResolve = binding.errorLayout.errorResolve
val errorDescription = binding.errorLayout.errorDescription
categoriesList.layoutManager = LinearLayoutManager(context)
color = requireArguments().getInt("color",0)
view.categories_list.visibility = View.GONE
view.progress_bar.visibility = View.VISIBLE
view.error_container.visibility = View.GONE
view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
view.progress_bar.visibility = View.VISIBLE
categoriesList.visibility = View.GONE
progressBar.visibility = View.VISIBLE
errorContainer.visibility = View.GONE
errorResolve.setOnClickListener {
progressBar.visibility = View.VISIBLE
categoriesViewModel.loadCategories(requireContext())
}
view.error_resolve.setTextColor(Color.parseColor("#ffffff"))
view.error_resolve.setBackgroundColor(color)
errorResolve.setTextColor(Color.parseColor("#ffffff"))
errorResolve.setBackgroundColor(color)
// Bind to the list of applications categories
categoriesViewModel.getApplicationsCategories().observe(viewLifecycleOwner, Observer {
......@@ -61,28 +69,34 @@ class ApplicationsFragment() : Fragment() {
//Add New Category
if (!it.any { Category -> Category.id == "system_apps" })
it.add(Category("system_apps"))
view.categories_list.adapter = context?.let { context -> CategoriesListAdapter(context, it, color) }
view.categories_list.visibility = View.VISIBLE
view.progress_bar.visibility = View.GONE
categoriesList.adapter = context?.let { context -> CategoriesListAdapter(context, it, color) }
categoriesList.visibility = View.VISIBLE
progressBar.visibility = View.GONE
}
})
// Bind to the screen error
categoriesViewModel.getScreenError().observe(viewLifecycleOwner, Observer {
if (it != null) {
view.error_description.text = requireActivity().getString(it.description)
view.error_container.visibility = View.VISIBLE
view.progress_bar.visibility = View.GONE
errorDescription.text = requireActivity().getString(it.description)
errorContainer.visibility = View.VISIBLE
progressBar.visibility = View.GONE
} else {
view.error_container.visibility = View.GONE
errorContainer.visibility = View.GONE
}
})
if (categoriesViewModel.getApplicationsCategories().value!!.isEmpty()) {
categoriesViewModel.loadCategories(requireContext())
}
return view
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
companion object{
fun newInstance(color:Int?) : ApplicationsFragment{
val applicationsFragment = ApplicationsFragment()
......
......@@ -24,18 +24,21 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import foundation.e.apps.R
import foundation.e.apps.databinding.FragmentCategoriesBinding
class CategoriesFragment : Fragment() {
private var _binding: FragmentCategoriesBinding? = null
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentCategoriesBinding.inflate(inflater, container, false)
// Fragment variables
val tabLayout = binding.tabLayout
val viewPager = binding.viewPager
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_categories, container, false)
val tabLayout = view.findViewById<TabLayout>(R.id.tab_layout)
val viewPager = view.findViewById<ViewPager>(R.id.view_pager)
var color = getAccentColor(requireActivity());
viewPager.adapter = CategoriesViewPagerAdapter(requireActivity().supportFragmentManager, tabLayout.tabCount, color)
viewPager.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout))
......@@ -57,7 +60,12 @@ class CategoriesFragment : Fragment() {
}
})
return view
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
/*
......
......@@ -21,16 +21,12 @@ import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.RecyclerView
import foundation.e.apps.R
import foundation.e.apps.categories.category.CategoryActivity
import foundation.e.apps.categories.model.Category
import foundation.e.apps.databinding.CategoryListItemBinding
import foundation.e.apps.utils.Constants
class CategoriesListAdapter(private val context: Context, private var categories: ArrayList<Category>, color: Int?)
......@@ -43,17 +39,20 @@ class CategoriesListAdapter(private val context: Context, private var categories
}
class CategoryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val categoryContainer: RelativeLayout = view.findViewById(R.id.category_container)
val categoryIcon: ImageView = view.findViewById(R.id.category_icon)
val categoryTitle: TextView = view.findViewById(R.id.category_title)
class CategoryViewHolder(binding: CategoryListItemBinding) : RecyclerView.ViewHolder(binding.root) {
val categoryContainer = binding.categoryContainer
val categoryIcon = binding.categoryIcon
val categoryTitle = binding.categoryTitle
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
val categoryContainer = LayoutInflater.from(parent.context).inflate(
R.layout.category_list_item,
parent, false)
return CategoryViewHolder(categoryContainer)
return CategoryViewHolder(
CategoryListItemBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
override fun getItemCount(): Int {
......
......@@ -22,62 +22,75 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import foundation.e.apps.R
import foundation.e.apps.categories.viewmodel.CategoriesViewModel
import kotlinx.android.synthetic.main.error_layout.view.*
import kotlinx.android.synthetic.main.fragment_games_categories.view.*
import foundation.e.apps.databinding.FragmentGamesCategoriesBinding
class GamesFragment() : Fragment() {
private var _binding: FragmentGamesCategoriesBinding? = null
private val binding get() = _binding!!
private lateinit var categoriesViewModel: CategoriesViewModel
var color:Int = 0;
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentGamesCategoriesBinding.inflate(inflater, container, false)
categoriesViewModel = ViewModelProvider(this).get(CategoriesViewModel::class.java)
val view = inflater.inflate(R.layout.fragment_games_categories, container, false)
view.categories_list.layoutManager = LinearLayoutManager(context)
// Fragment variables
val categoriesList = binding.categoriesList
val progressBar = binding.progressBar
val errorContainer = binding.errorLayout.errorContainer
val errorResolve = binding.errorLayout.errorResolve
val errorDescription = binding.errorLayout.errorDescription
categoriesList.layoutManager = LinearLayoutManager(context)
color = requireArguments().getInt("color",0)
view.categories_list.visibility = View.GONE
view.progress_bar.visibility = View.VISIBLE
view.error_container.visibility = View.GONE
view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
view.progress_bar.visibility = View.VISIBLE
categoriesList.visibility = View.GONE
progressBar.visibility = View.VISIBLE
errorContainer.visibility = View.GONE
errorResolve.setOnClickListener {
progressBar.visibility = View.VISIBLE
categoriesViewModel.loadCategories(requireContext())
}
view.error_resolve.setTextColor(Color.parseColor("#ffffff"))
view.error_resolve.setBackgroundColor(color)
errorResolve.setTextColor(Color.parseColor("#ffffff"))
errorResolve.setBackgroundColor(color)
// Bind to the list of games categories
categoriesViewModel.getGamesCategories().observe(viewLifecycleOwner, Observer {
if (it!!.isNotEmpty()) {
view.categories_list.adapter = context?.let { context -> CategoriesListAdapter(context, it, color) }
view.categories_list.visibility = View.VISIBLE
view.progress_bar.visibility = View.GONE
categoriesList.adapter = context?.let { context -> CategoriesListAdapter(context, it, color) }
categoriesList.visibility = View.VISIBLE
progressBar.visibility = View.GONE
}
})
// Bind to the screen error
categoriesViewModel.getScreenError().observe(viewLifecycleOwner, Observer {
if (it != null) {
view.error_description.text = requireActivity().getString(it.description)
view.error_container.visibility = View.VISIBLE
view.progress_bar.visibility = View.GONE
errorDescription.text = requireActivity().getString(it.description)
errorContainer.visibility = View.VISIBLE
progressBar.visibility = View.GONE
} else {
view.error_container.visibility = View.GONE
errorContainer.visibility = View.GONE
}
})
if (categoriesViewModel.getGamesCategories().value!!.isEmpty()) {
categoriesViewModel.loadCategories(requireContext())
}
return view
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
companion object{
......
......@@ -21,57 +21,69 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import foundation.e.apps.R
import foundation.e.apps.categories.viewmodel.CategoriesViewModel
import kotlinx.android.synthetic.main.error_layout.view.*
import kotlinx.android.synthetic.main.fragment_application_categories.view.*
import foundation.e.apps.databinding.FragmentApplicationCategoriesBinding
class PwasFragment : Fragment() {
private var _binding: FragmentApplicationCategoriesBinding? = null
private val binding get() = _binding!!
private lateinit var categoriesViewModel: CategoriesViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentApplicationCategoriesBinding.inflate(inflater, container, false)
categoriesViewModel = ViewModelProvider(this).get(CategoriesViewModel::class.java)
val view = inflater.inflate(R.layout.fragment_application_categories, container, false)
view.categories_list.layoutManager = LinearLayoutManager(context)
// Fragment variables
val categoriesList = binding.categoriesList
val progressBar = binding.progressBar
val errorContainer = binding.errorLayout.errorContainer
val errorResolve = binding.errorLayout.errorResolve
val errorDescription = binding.errorLayout.errorDescription
view.categories_list.visibility = View.GONE
view.progress_bar.visibility = View.VISIBLE
view.error_container.visibility = View.GONE
view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
view.progress_bar.visibility = View.VISIBLE
categoriesList.layoutManager = LinearLayoutManager(context)
categoriesList.visibility = View.GONE
progressBar.visibility = View.VISIBLE
errorContainer.visibility = View.GONE
errorResolve.setOnClickListener {
progressBar.visibility = View.VISIBLE
categoriesViewModel.loadCategories(requireContext())
}
// Bind to the list of pwas categories
categoriesViewModel.getPwasCategories().observe(viewLifecycleOwner, Observer {
if (it!!.isNotEmpty()) {
view.categories_list.adapter = context?.let { context -> CategoriesListAdapter(context, it, null) }
view.categories_list.visibility = View.VISIBLE
view.progress_bar.visibility = View.GONE
categoriesList.adapter = context?.let { context -> CategoriesListAdapter(context, it, null) }
categoriesList.visibility = View.VISIBLE
progressBar.visibility = View.GONE
}
})
// Bind to the screen error
categoriesViewModel.getScreenError().observe(viewLifecycleOwner, Observer {
if (it != null) {
view.error_description.text = requireActivity().getString(it.description)
view.error_container.visibility = View.VISIBLE
view.progress_bar.visibility = View.GONE
errorDescription.text = requireActivity().getString(it.description)
errorContainer.visibility = View.VISIBLE
progressBar.visibility = View.GONE
} else {
view.error_container.visibility = View.GONE
errorContainer.visibility = View.GONE
}
})
if (categoriesViewModel.getPwasCategories().value!!.isEmpty()) {
categoriesViewModel.loadCategories(requireContext())
}
return view
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
......@@ -30,6 +30,7 @@
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="@+id/error_layout"
layout="@layout/error_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......
......@@ -30,6 +30,7 @@
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="@+id/error_layout"
layout="@layout/error_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment