From aeec33eb9e7d34b9cf92ee0a867f183e0b303e1f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 25 Jan 2021 11:35:36 +0100 Subject: [PATCH] FoodFragment -> jetpack --- .../plugins/general/food/FoodFragment.kt | 109 ++++++++------- app/src/main/res/layout/food_fragment.xml | 131 +++++++++--------- app/src/main/res/layout/food_item.xml | 19 ++- app/src/main/res/values/strings.xml | 1 + 4 files changed, 135 insertions(+), 125 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.kt index e74067a095..58b7fdb779 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/food/FoodFragment.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.food +import android.annotation.SuppressLint import android.content.DialogInterface import android.graphics.Paint import android.os.Bundle @@ -10,11 +11,12 @@ import android.view.View import android.view.ViewGroup import android.widget.AdapterView import android.widget.ArrayAdapter -import android.widget.TextView import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import dagger.android.support.DaggerFragment import info.nightscout.androidaps.R +import info.nightscout.androidaps.databinding.FoodFragmentBinding +import info.nightscout.androidaps.databinding.FoodItemBinding import info.nightscout.androidaps.events.EventFoodDatabaseChanged import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.general.food.FoodFragment.RecyclerViewAdapter.FoodsViewHolder @@ -24,7 +26,6 @@ import info.nightscout.androidaps.utils.alertDialogs.OKDialog.showConfirmation import info.nightscout.androidaps.utils.resources.ResourceHelper import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable -import kotlinx.android.synthetic.main.food_fragment.* import java.util.* import javax.inject.Inject import kotlin.collections.ArrayList @@ -41,24 +42,32 @@ class FoodFragment : DaggerFragment() { private lateinit var unfiltered: List private lateinit var filtered: MutableList - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.food_fragment, container, false) + private var _binding: FoodFragmentBinding? = null + + // This property is only valid between onCreateView and + // onDestroyView. + private val binding get() = _binding!! + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + _binding = FoodFragmentBinding.inflate(inflater, container, false) + return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - food_recyclerview.setHasFixedSize(true) - food_recyclerview.layoutManager = LinearLayoutManager(view.context) - food_recyclerview.adapter = RecyclerViewAdapter(foodPlugin.service?.foodData ?: ArrayList()) + binding.recyclerview.setHasFixedSize(true) + binding.recyclerview.layoutManager = LinearLayoutManager(view.context) + binding.recyclerview.adapter = RecyclerViewAdapter(foodPlugin.service?.foodData + ?: ArrayList()) - food_clearfilter.setOnClickListener { - food_filter.setText("") - food_category.setSelection(0) - food_subcategory.setSelection(0) + binding.clearfilter.setOnClickListener { + binding.filter.setText("") + binding.category.setSelection(0) + binding.subcategory.setSelection(0) filterData() } - food_category.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + binding.category.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { fillSubcategories() filterData() @@ -69,7 +78,7 @@ class FoodFragment : DaggerFragment() { filterData() } } - food_subcategory.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + binding.subcategory.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { filterData() } @@ -78,7 +87,7 @@ class FoodFragment : DaggerFragment() { filterData() } } - food_filter.addTextChangedListener(object : TextWatcher { + binding.filter.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { filterData() @@ -92,7 +101,8 @@ class FoodFragment : DaggerFragment() { filterData() } - @Synchronized override fun onResume() { + @Synchronized + override fun onResume() { super.onResume() disposable.add(rxBus .toObservable(EventFoodDatabaseChanged::class.java) @@ -102,11 +112,18 @@ class FoodFragment : DaggerFragment() { updateGui() } - @Synchronized override fun onPause() { + @Synchronized + override fun onPause() { super.onPause() disposable.clear() } + @Synchronized + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + private fun loadData() { unfiltered = foodPlugin.service?.foodData ?: ArrayList() } @@ -121,12 +138,12 @@ class FoodFragment : DaggerFragment() { categories.add(0, resourceHelper.gs(R.string.none)) context?.let { context -> val adapterCategories = ArrayAdapter(context, R.layout.spinner_centered, categories) - food_category.adapter = adapterCategories + binding.category.adapter = adapterCategories } } private fun fillSubcategories() { - val categoryFilter = food_category.selectedItem.toString() + val categoryFilter = binding.category.selectedItem.toString() val subCatSet: MutableSet = HashSet() if (categoryFilter != resourceHelper.gs(R.string.none)) { for (f in unfiltered) { @@ -138,65 +155,61 @@ class FoodFragment : DaggerFragment() { subcategories.add(0, resourceHelper.gs(R.string.none)) context?.let { context -> val adapterSubcategories = ArrayAdapter(context, R.layout.spinner_centered, subcategories) - food_subcategory.adapter = adapterSubcategories + binding.subcategory.adapter = adapterSubcategories } } private fun filterData() { - val textFilter = food_filter.text.toString() - val categoryFilter = food_category.selectedItem.toString() - val subcategoryFilter = food_subcategory.selectedItem.toString() - val newfiltered = ArrayList() + val textFilter = binding.filter.text.toString() + val categoryFilter = binding.category.selectedItem.toString() + val subcategoryFilter = binding.subcategory.selectedItem.toString() + val newFiltered = ArrayList() for (f in unfiltered) { if (f.name == null || f.category == null || f.subcategory == null) continue if (subcategoryFilter != resourceHelper.gs(R.string.none) && f.subcategory != subcategoryFilter) continue if (categoryFilter != resourceHelper.gs(R.string.none) && f.category != categoryFilter) continue if (textFilter != "" && !f.name.toLowerCase(Locale.getDefault()).contains(textFilter.toLowerCase(Locale.getDefault()))) continue - newfiltered.add(f) + newFiltered.add(f) } - filtered = newfiltered + filtered = newFiltered updateGui() } - protected fun updateGui() { - food_recyclerview?.swapAdapter(RecyclerViewAdapter(filtered), true) + private fun updateGui() { + binding.recyclerview.swapAdapter(RecyclerViewAdapter(filtered), true) } inner class RecyclerViewAdapter internal constructor(var foodList: List) : RecyclerView.Adapter() { + override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): FoodsViewHolder { val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.food_item, viewGroup, false) return FoodsViewHolder(v) } + @SuppressLint("SetTextI18n") override fun onBindViewHolder(holder: FoodsViewHolder, position: Int) { val food = foodList[position] - holder.ns.visibility = if (food._id != null) View.VISIBLE else View.GONE - holder.name.text = food.name - holder.portion.text = food.portion.toString() + food.units - holder.carbs.text = food.carbs.toString() + resourceHelper.gs(R.string.shortgramm) - holder.fat.text = resourceHelper.gs(R.string.shortfat) + ": " + food.fat + resourceHelper.gs(R.string.shortgramm) - if (food.fat == 0) holder.fat.visibility = View.INVISIBLE - holder.protein.text = resourceHelper.gs(R.string.shortprotein) + ": " + food.protein + resourceHelper.gs(R.string.shortgramm) - if (food.protein == 0) holder.protein.visibility = View.INVISIBLE - holder.energy.text = resourceHelper.gs(R.string.shortenergy) + ": " + food.energy + resourceHelper.gs(R.string.shortkilojoul) - if (food.energy == 0) holder.energy.visibility = View.INVISIBLE - holder.remove.tag = food + holder.binding.nsSign.visibility = if (food._id != null) View.VISIBLE else View.GONE + holder.binding.name.text = food.name + holder.binding.portion.text = food.portion.toString() + food.units + holder.binding.carbs.text = food.carbs.toString() + resourceHelper.gs(R.string.shortgramm) + holder.binding.fat.text = resourceHelper.gs(R.string.shortfat) + ": " + food.fat + resourceHelper.gs(R.string.shortgramm) + if (food.fat == 0) holder.binding.fat.visibility = View.INVISIBLE + holder.binding.protein.text = resourceHelper.gs(R.string.shortprotein) + ": " + food.protein + resourceHelper.gs(R.string.shortgramm) + if (food.protein == 0) holder.binding.protein.visibility = View.INVISIBLE + holder.binding.energy.text = resourceHelper.gs(R.string.shortenergy) + ": " + food.energy + resourceHelper.gs(R.string.shortkilojoul) + if (food.energy == 0) holder.binding.energy.visibility = View.INVISIBLE + holder.binding.remove.tag = food } override fun getItemCount(): Int = foodList.size inner class FoodsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { - var name: TextView = itemView.findViewById(R.id.food_name) - var portion: TextView = itemView.findViewById(R.id.food_portion) - var carbs: TextView = itemView.findViewById(R.id.food_carbs) - var fat: TextView = itemView.findViewById(R.id.food_fat) - var protein: TextView = itemView.findViewById(R.id.food_protein) - var energy: TextView = itemView.findViewById(R.id.food_energy) - var ns: TextView = itemView.findViewById(R.id.ns_sign) - var remove: TextView = itemView.findViewById(R.id.food_remove) + + val binding = FoodItemBinding.bind(itemView) init { - remove.setOnClickListener { v: View -> + binding.remove.setOnClickListener { v: View -> val food = v.tag as Food activity?.let { activity -> showConfirmation(activity, resourceHelper.gs(R.string.confirmation), resourceHelper.gs(R.string.removerecord) + "\n" + food.name, DialogInterface.OnClickListener { _: DialogInterface?, _: Int -> @@ -207,7 +220,7 @@ class FoodFragment : DaggerFragment() { }, null) } } - remove.paintFlags = remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG + binding.remove.paintFlags = binding.remove.paintFlags or Paint.UNDERLINE_TEXT_FLAG } } } diff --git a/app/src/main/res/layout/food_fragment.xml b/app/src/main/res/layout/food_fragment.xml index 38e319ad7f..f192bdf62e 100644 --- a/app/src/main/res/layout/food_fragment.xml +++ b/app/src/main/res/layout/food_fragment.xml @@ -1,89 +1,86 @@ - + android:layout_height="wrap_content" + android:orientation="horizontal"> - + android:contentDescription="@string/filter" + android:paddingLeft="5dp" + android:paddingRight="5dp" + app:srcCompat="@android:drawable/ic_menu_search" /> - - - - - - - - + android:layout_weight="1" + android:autofillHints="@string/filter" + android:ems="10" + android:inputType="text" + android:text="" /> - + + - + - + - + - + - + - - - + - + + + + + diff --git a/app/src/main/res/layout/food_item.xml b/app/src/main/res/layout/food_item.xml index b10acea784..593c03ff13 100644 --- a/app/src/main/res/layout/food_item.xml +++ b/app/src/main/res/layout/food_item.xml @@ -21,23 +21,22 @@ android:orientation="horizontal"> Crash logs upload disabled! Graph Chart menu + Clear filter