-
-
Notifications
You must be signed in to change notification settings - Fork 446
/
SearchByCodeFragment.kt
122 lines (103 loc) 路 4.37 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package openfoodfacts.github.scrachx.openfood.features.searchbycode
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import dagger.hilt.android.AndroidEntryPoint
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.features.shared.NavigationWithDrawerBaseFragment
import openfoodfacts.github.scrachx.openfood.utils.*
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.NavigationDrawerType
import java.time.Duration
import java.time.temporal.ChronoUnit
import java.util.concurrent.TimeUnit
import kotlin.time.DurationUnit
/**
* @see R.layout.fragment_find_product
*/
@AndroidEntryPoint
class SearchByCodeFragment : NavigationWithDrawerBaseFragment() {
private var _binding: FragmentFindProductBinding? = null
private val binding get() = _binding!!
private val viewModel: SearchByCodeViewModel by viewModels()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentFindProductBinding.inflate(inflater)
return binding.root
}
override fun onDestroyView() {
removeDrawerListener()
super.onDestroyView()
_binding = null
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.editTextBarcode.let { e ->
e.isSelected = false
// Accept both the Enter key (from the keyboard) or the search virtual key
e.setOnEditorActionListener { _, actionId, keyEvent ->
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
keyEvent.keyCode == KeyEvent.KEYCODE_ENTER
) {
checkBarcodeThenSearch()
true
} else {
false
}
}
}
binding.buttonBarcode.setOnClickListener { checkBarcodeThenSearch() }
// Get barcode from intent or saved instance or from arguments, in this order
val barcode = requireActivity().intent.getStringExtra(INTENT_KEY_BARCODE).takeUnless { it.isNullOrBlank() }
?: savedInstanceState?.getString(INTENT_KEY_BARCODE).takeUnless { it.isNullOrBlank() }
?: arguments?.getString(INTENT_KEY_BARCODE).takeUnless { it.isNullOrBlank() }
// If we have the barcode, set the textbox and start the search
if (!barcode.isNullOrBlank()) {
binding.editTextBarcode.setText(barcode, TextView.BufferType.EDITABLE)
checkBarcodeThenSearch()
}
}
private fun checkBarcodeThenSearch() {
requireActivity().hideKeyboard()
val code = binding.editTextBarcode.text.toString()
when {
code.isEmpty() -> {
binding.editTextBarcode.error = resources.getString(R.string.txtBarcodeRequire)
}
!isBarcodeValid(code) -> {
binding.editTextBarcode.error = resources.getString(R.string.txtBarcodeNotValid)
}
else -> {
viewModel.openProduct(code, 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)
}
override fun onDrawerClosed() {
super.onDrawerClosed()
// Force the keyboard to be visible
if (binding.editTextBarcode.isEmpty()) {
binding.editTextBarcode.requestFocus()
requireActivity().showKeyboard()
}
}
companion object {
const val INTENT_KEY_BARCODE = "barcode"
fun newInstance(barcode: String? = null) = SearchByCodeFragment().apply {
arguments = Bundle().apply {
putString(INTENT_KEY_BARCODE, barcode)
}
}
}
}