FoodFragment spinner replacement

This commit is contained in:
Milos Kozak 2022-03-19 15:32:06 +01:00
parent a3ffd23e29
commit 2441ee1592
3 changed files with 60 additions and 84 deletions

View file

@ -114,7 +114,7 @@ class WizardDialog : DaggerDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
this.arguments?.let { bundle -> this.arguments?.let { bundle ->
carbsPassedIntoWizard = bundle.getInt("carbs_input").toDouble() carbsPassedIntoWizard = bundle.getDouble("carbs_input")
notesPassedIntoWizard = bundle.getString("notes_input").toString() notesPassedIntoWizard = bundle.getString("notes_input").toString()
} }

View file

@ -1,7 +1,6 @@
package info.nightscout.androidaps.plugins.general.food package info.nightscout.androidaps.plugins.general.food
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.graphics.Paint
import android.os.Bundle import android.os.Bundle
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
@ -24,18 +23,18 @@ import info.nightscout.androidaps.databinding.FoodFragmentBinding
import info.nightscout.androidaps.databinding.FoodItemBinding import info.nightscout.androidaps.databinding.FoodItemBinding
import info.nightscout.androidaps.dialogs.WizardDialog import info.nightscout.androidaps.dialogs.WizardDialog
import info.nightscout.androidaps.events.EventFoodDatabaseChanged import info.nightscout.androidaps.events.EventFoodDatabaseChanged
import info.nightscout.shared.logging.AAPSLogger import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.shared.logging.LTag
import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.logging.UserEntryLogger
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.androidaps.utils.protection.ProtectionCheck import info.nightscout.androidaps.utils.protection.ProtectionCheck
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.utils.ui.UIRunnable import info.nightscout.androidaps.utils.ui.UIRunnable
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import io.reactivex.rxjava3.core.Completable import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign import io.reactivex.rxjava3.kotlin.plusAssign
@ -43,7 +42,6 @@ import io.reactivex.rxjava3.kotlin.subscribeBy
import java.util.* import java.util.*
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
import kotlin.collections.ArrayList
class FoodFragment : DaggerFragment() { class FoodFragment : DaggerFragment() {
@ -66,10 +64,8 @@ class FoodFragment : DaggerFragment() {
// onDestroyView. // onDestroyView.
private val binding get() = _binding!! private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
_binding = FoodFragmentBinding.inflate(inflater, container, false) FoodFragmentBinding.inflate(inflater, container, false).also { _binding = it }.root
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -80,8 +76,10 @@ class FoodFragment : DaggerFragment() {
binding.refreshFromNightscout.setOnClickListener { binding.refreshFromNightscout.setOnClickListener {
context?.let { context -> context?.let { context ->
OKDialog.showConfirmation(context, rh.gs(R.string.refresheventsfromnightscout) + " ?", { OKDialog.showConfirmation(context, rh.gs(R.string.refresheventsfromnightscout) + " ?", {
uel.log(Action.FOOD, Sources.Food, rh.gs(R.string.refresheventsfromnightscout), uel.log(
ValueWithUnit.SimpleString(rh.gsNotLocalised(R.string.refresheventsfromnightscout))) Action.FOOD, Sources.Food, rh.gs(R.string.refresheventsfromnightscout),
ValueWithUnit.SimpleString(rh.gsNotLocalised(R.string.refresheventsfromnightscout))
)
disposable += Completable.fromAction { repository.deleteAllFoods() } disposable += Completable.fromAction { repository.deleteAllFoods() }
.subscribeOn(aapsSchedulers.io) .subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.main) .observeOn(aapsSchedulers.main)
@ -97,30 +95,12 @@ class FoodFragment : DaggerFragment() {
binding.clearfilter.setOnClickListener { binding.clearfilter.setOnClickListener {
binding.filter.setText("") binding.filter.setText("")
binding.category.setSelection(0) binding.categoryList.setText(rh.gs(R.string.none), false)
binding.subcategory.setSelection(0) binding.subcategoryList.setText(rh.gs(R.string.none), false)
filterData() filterData()
} }
binding.category.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { binding.categoryList.onItemClickListener = AdapterView.OnItemClickListener { _, _, _, _ -> fillSubcategories(); filterData() }
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { binding.subcategoryList.onItemClickListener = AdapterView.OnItemClickListener { _, _, _, _ -> filterData() }
fillSubcategories()
filterData()
}
override fun onNothingSelected(parent: AdapterView<*>?) {
fillSubcategories()
filterData()
}
}
binding.subcategory.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
filterData()
}
override fun onNothingSelected(parent: AdapterView<*>?) {
filterData()
}
}
binding.filter.addTextChangedListener(object : TextWatcher { binding.filter.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
@ -134,12 +114,11 @@ class FoodFragment : DaggerFragment() {
@Synchronized @Synchronized
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
disposable.add(rxBus disposable += rxBus
.toObservable(EventFoodDatabaseChanged::class.java) .toObservable(EventFoodDatabaseChanged::class.java)
.observeOn(aapsSchedulers.main) .observeOn(aapsSchedulers.main)
.debounce(1L, TimeUnit.SECONDS) .debounce(1L, TimeUnit.SECONDS)
.subscribe({ swapAdapter() }, fabricPrivacy::logException) .subscribe({ swapAdapter() }, fabricPrivacy::logException)
)
swapAdapter() swapAdapter()
} }
@ -178,13 +157,13 @@ class FoodFragment : DaggerFragment() {
val categories = ArrayList(catSet) val categories = ArrayList(catSet)
categories.add(0, rh.gs(R.string.none)) categories.add(0, rh.gs(R.string.none))
context?.let { context -> context?.let { context ->
val adapterCategories = ArrayAdapter(context, R.layout.spinner_centered, categories) binding.categoryList.setAdapter(ArrayAdapter(context, R.layout.spinner_centered, categories))
binding.category.adapter = adapterCategories binding.categoryList.setText(rh.gs(R.string.none), false)
} }
} }
private fun fillSubcategories() { private fun fillSubcategories() {
val categoryFilter = binding.category.selectedItem.toString() val categoryFilter = binding.categoryList.text.toString()
val subCatSet: MutableSet<CharSequence> = HashSet() val subCatSet: MutableSet<CharSequence> = HashSet()
if (categoryFilter != rh.gs(R.string.none)) { if (categoryFilter != rh.gs(R.string.none)) {
for (f in unfiltered) { for (f in unfiltered) {
@ -198,17 +177,15 @@ class FoodFragment : DaggerFragment() {
val subcategories = ArrayList(subCatSet) val subcategories = ArrayList(subCatSet)
subcategories.add(0, rh.gs(R.string.none)) subcategories.add(0, rh.gs(R.string.none))
context?.let { context -> context?.let { context ->
val adapterSubcategories = ArrayAdapter(context, R.layout.spinner_centered, subcategories) binding.subcategoryList.setAdapter(ArrayAdapter(context, R.layout.spinner_centered, subcategories))
binding.subcategory.adapter = adapterSubcategories binding.subcategoryList.setText(rh.gs(R.string.none), false)
} }
} }
private fun filterData() { private fun filterData() {
val textFilter = binding.filter.text.toString() val textFilter = binding.filter.text.toString()
val categoryFilter = binding.category.selectedItem?.toString() val categoryFilter = binding.categoryList.text.toString()
?: rh.gs(R.string.none) val subcategoryFilter = binding.subcategoryList.text.toString()
val subcategoryFilter = binding.subcategory.selectedItem?.toString()
?: rh.gs(R.string.none)
val newFiltered = ArrayList<Food>() val newFiltered = ArrayList<Food>()
for (f in unfiltered) { for (f in unfiltered) {
if (f.category == null || f.subCategory == null) continue if (f.category == null || f.subCategory == null) continue
@ -271,14 +248,13 @@ class FoodFragment : DaggerFragment() {
val food = v.tag as Food val food = v.tag as Food
activity?.let { activity -> activity?.let { activity ->
protectionCheck.queryProtection(activity, ProtectionCheck.Protection.BOLUS, UIRunnable { protectionCheck.queryProtection(activity, ProtectionCheck.Protection.BOLUS, UIRunnable {
if (isAdded) { if (isAdded)
val wizardDialog = WizardDialog() WizardDialog().also { dialog ->
val bundle = Bundle() dialog.arguments = Bundle().also { bundle ->
bundle.putInt("carbs_input", food.carbs) bundle.putDouble("carbs_input", food.carbs.toDouble())
bundle.putString("notes_input", " ${food.name} - ${food.carbs}g") bundle.putString("notes_input", " ${food.name} - ${food.carbs}g")
wizardDialog.setArguments(bundle)
wizardDialog.show(childFragmentManager, "Food Item")
} }
}.show(childFragmentManager, "Food Item")
}) })
} }
} }

View file

@ -49,44 +49,44 @@
app:srcCompat="@android:drawable/ic_menu_close_clear_cancel" /> app:srcCompat="@android:drawable/ic_menu_close_clear_cancel" />
</LinearLayout> </LinearLayout>
<LinearLayout <com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="5dp" android:layout_marginStart="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp" android:layout_marginEnd="5dp"
android:text="@string/category" /> android:hint="@string/category"
app:boxStrokeColor="@color/list_delimiter">
<Spinner <androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/category" android:id="@+id/categoryList"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="end" android:clickable="true"
android:minWidth="100dp" /> android:enabled="false"
tools:ignore="KeyboardInaccessibleWidget" />
<TextView </com.google.android.material.textfield.TextInputLayout>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView <com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp" android:layout_marginEnd="5dp"
android:text="@string/subcategory" /> android:hint="@string/subcategory"
app:boxStrokeColor="@color/list_delimiter">
<Spinner <androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/subcategory" android:id="@+id/subcategoryList"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="end" android:clickable="true"
android:minWidth="100dp" /> android:enabled="false"
tools:ignore="KeyboardInaccessibleWidget" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview" android:id="@+id/recyclerview"