/
SearchByCodeFragment.kt
85 lines (73 loc) 路 3.31 KB
/
SearchByCodeFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package openfoodfacts.github.scrachx.openfood.features.searchbycode
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.databinding.FragmentFindProductBinding
import openfoodfacts.github.scrachx.openfood.features.shared.NavigationBaseFragment
import openfoodfacts.github.scrachx.openfood.network.OpenFoodAPIClient
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.NavigationDrawerType
import openfoodfacts.github.scrachx.openfood.utils.Utils
import openfoodfacts.github.scrachx.openfood.utils.isBarcodeValid
/**
* @see R.layout.fragment_find_product
*/
class SearchByCodeFragment : NavigationBaseFragment() {
private var _binding: FragmentFindProductBinding? = null
private val binding get() = _binding!!
private val api by lazy { OpenFoodAPIClient(requireActivity()) }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentFindProductBinding.inflate(inflater)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.editTextBarcode.isSelected = false
binding.buttonBarcode.setOnClickListener { checkBarcodeThenSearch() }
// Get barcode from intent or saved instance or from arguments, in this order
var barCode = requireActivity().intent.getStringExtra(INTENT_KEY_BARCODE)
if (barCode.isNullOrBlank()) {
barCode = savedInstanceState?.getString(INTENT_KEY_BARCODE) ?: arguments?.getString(INTENT_KEY_BARCODE)
}
if (!barCode.isNullOrBlank()) {
setBarcodeThenSearch(barCode)
}
}
private fun setBarcodeThenSearch(code: String) {
binding.editTextBarcode.setText(code, TextView.BufferType.EDITABLE)
checkBarcodeThenSearch()
}
private fun checkBarcodeThenSearch() {
Utils.hideKeyboard(requireActivity())
val barCodeTxt = binding.editTextBarcode.text.toString()
if (barCodeTxt.isEmpty()) {
binding.editTextBarcode.error = resources.getString(R.string.txtBarcodeRequire)
} else if (!isBarcodeValid(barCodeTxt)) {
binding.editTextBarcode.error = resources.getString(R.string.txtBarcodeNotValid)
} else {
api.openProduct(barCodeTxt, requireActivity())
}
}
@NavigationDrawerType
override fun getNavigationDrawerType() = NavigationDrawerListener.ITEM_SEARCH_BY_CODE
override fun onResume() {
super.onResume()
(requireActivity() as AppCompatActivity).supportActionBar?.title = getString(R.string.search_by_barcode_drawer)
}
companion object {
const val INTENT_KEY_BARCODE = "barcode"
fun newInstance(barcode: String? = null) = SearchByCodeFragment().apply {
arguments = Bundle().apply {
putString(INTENT_KEY_BARCODE, barcode)
}
}
}
}