/
IngredientsProductFragment.kt
539 lines (467 loc) 路 22.1 KB
/
IngredientsProductFragment.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* Copyright 2016-2020 Open Food Facts
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package openfoodfacts.github.scrachx.openfood.features.product.view.ingredients
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.graphics.Typeface
import android.net.Uri
import android.os.Bundle
import android.text.Spanned
import android.text.SpannedString
import android.text.method.LinkMovementMethod
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.net.toUri
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.squareup.picasso.Picasso
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import openfoodfacts.github.scrachx.openfood.AppFlavor
import openfoodfacts.github.scrachx.openfood.AppFlavor.Companion.isFlavors
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.customtabs.CustomTabActivityHelper
import openfoodfacts.github.scrachx.openfood.customtabs.CustomTabsHelper
import openfoodfacts.github.scrachx.openfood.customtabs.WebViewFallback
import openfoodfacts.github.scrachx.openfood.databinding.FragmentIngredientsProductBinding
import openfoodfacts.github.scrachx.openfood.features.additives.AdditiveFragmentHelper.showAdditives
import openfoodfacts.github.scrachx.openfood.features.images.manage.ImagesManageActivity
import openfoodfacts.github.scrachx.openfood.features.login.LoginActivity.Companion.LoginContract
import openfoodfacts.github.scrachx.openfood.features.product.edit.ProductEditActivity
import openfoodfacts.github.scrachx.openfood.features.product.edit.ProductEditActivity.Companion.KEY_STATE
import openfoodfacts.github.scrachx.openfood.features.product.edit.ProductEditActivity.PerformOCRContract
import openfoodfacts.github.scrachx.openfood.features.product.edit.ProductEditActivity.SendUpdatedImgContract
import openfoodfacts.github.scrachx.openfood.features.shared.BaseFragment
import openfoodfacts.github.scrachx.openfood.images.ProductImage
import openfoodfacts.github.scrachx.openfood.models.DaoSession
import openfoodfacts.github.scrachx.openfood.models.ProductImageField
import openfoodfacts.github.scrachx.openfood.models.ProductState
import openfoodfacts.github.scrachx.openfood.models.entities.SendProduct
import openfoodfacts.github.scrachx.openfood.models.entities.additive.AdditiveName
import openfoodfacts.github.scrachx.openfood.models.entities.allergen.AllergenName
import openfoodfacts.github.scrachx.openfood.models.entities.allergen.AllergenNameDao
import openfoodfacts.github.scrachx.openfood.repositories.ProductRepository
import openfoodfacts.github.scrachx.openfood.repositories.TaxonomiesRepository
import openfoodfacts.github.scrachx.openfood.repositories.WikidataRepository
import openfoodfacts.github.scrachx.openfood.utils.ClickableSpan
import openfoodfacts.github.scrachx.openfood.utils.LOCALE_FILE_SCHEME
import openfoodfacts.github.scrachx.openfood.utils.LocaleManager
import openfoodfacts.github.scrachx.openfood.utils.PhotoReceiverHandler
import openfoodfacts.github.scrachx.openfood.utils.ProductFragmentUtils
import openfoodfacts.github.scrachx.openfood.utils.ProductInfoState
import openfoodfacts.github.scrachx.openfood.utils.ProductInfoState.Data
import openfoodfacts.github.scrachx.openfood.utils.ProductInfoState.Empty
import openfoodfacts.github.scrachx.openfood.utils.ProductInfoState.Loading
import openfoodfacts.github.scrachx.openfood.utils.SearchType
import openfoodfacts.github.scrachx.openfood.utils.getNovaGroupExplanation
import openfoodfacts.github.scrachx.openfood.utils.getNovaGroupResource
import openfoodfacts.github.scrachx.openfood.utils.getSendProduct
import openfoodfacts.github.scrachx.openfood.utils.isPowerSaveMode
import openfoodfacts.github.scrachx.openfood.utils.isUserSet
import openfoodfacts.github.scrachx.openfood.utils.requireProductState
import openfoodfacts.github.scrachx.openfood.utils.showBottomSheet
import openfoodfacts.github.scrachx.openfood.utils.unique
import java.io.File
import javax.inject.Inject
import openfoodfacts.github.scrachx.openfood.features.search.ProductSearchActivity.Companion.start as startSearch
@AndroidEntryPoint
class IngredientsProductFragment : BaseFragment() {
private var _binding: FragmentIngredientsProductBinding? = null
private val binding get() = _binding!!
@Inject
lateinit var client: ProductRepository
@Inject
lateinit var taxonomiesRepository: TaxonomiesRepository
@Inject
lateinit var daoSession: DaoSession
@Inject
lateinit var wikidataClient: WikidataRepository
@Inject
lateinit var picasso: Picasso
@Inject
lateinit var sharedPreferences: SharedPreferences
@Inject
lateinit var localeManager: LocaleManager
private val viewModel: ProductIngredientsViewModel by viewModels()
private val performOCRLauncher = registerForActivityResult(PerformOCRContract())
{ result ->
if (result) {
ingredientExtracted = true
onRefresh()
}
}
private val updateImagesLauncher = registerForActivityResult(SendUpdatedImgContract())
{ result -> if (result) onRefresh() }
private val loginLauncher = registerForActivityResult(LoginContract())
{
ProductEditActivity.start(
requireContext(),
productState.product!!,
sendUpdatedIngredientsImage,
ingredientExtracted
)
}
private lateinit var productState: ProductState
private lateinit var customTabActivityHelper: CustomTabActivityHelper
private lateinit var customTabsIntent: CustomTabsIntent
@Inject
lateinit var photoReceiverHandler: PhotoReceiverHandler
private var ingredientExtracted = false
private var mSendProduct: SendProduct? = null
private var ingredientsImgUrl: String? = null
private var sendUpdatedIngredientsImage = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
productState = requireProductState()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentIngredientsProductBinding.inflate(inflater)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.extractIngredientsPrompt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add_box_blue_18dp,
0,
0,
0)
binding.changeIngImg.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add_a_photo_blue_18dp, 0, 0, 0)
binding.changeIngImg.setOnClickListener { changeIngImage() }
binding.novaMethodLink.setOnClickListener { novaMethodLinkDisplay() }
binding.extractIngredientsPrompt.setOnClickListener { extractIngredients() }
binding.imageViewIngredients.setOnClickListener {
if (ingredientsImgUrl != null && productState.product != null) {
viewModel.openFullScreen(
ingredientsImgUrl,
productState,
binding.imageViewIngredients,
this@IngredientsProductFragment
)
} else
newIngredientImage()
}
viewModel.vitaminsTags.observe(viewLifecycleOwner) {
if (it.isEmpty()) binding.cvVitaminsTagsText.visibility = View.GONE
else {
binding.cvVitaminsTagsText.visibility = View.VISIBLE
binding.vitaminsTagsText.text = buildSpannedString {
bold { append(getString(R.string.vitamin_tags_text)) }
append(tagListToString(it))
}
}
}
viewModel.aminoAcidTagsList.observe(viewLifecycleOwner) {
if (it.isEmpty()) binding.cvAminoAcidTagsText.visibility = View.GONE
else {
binding.cvAminoAcidTagsText.visibility = View.VISIBLE
binding.aminoAcidTagsText.text = buildSpannedString {
bold { append(getString(R.string.amino_acid_tags_text)) }
append(tagListToString(it))
}
}
}
viewModel.mineralTags.observe(viewLifecycleOwner) {
if (it.isEmpty()) binding.cvMineralTagsText.visibility = View.GONE
else {
binding.cvMineralTagsText.visibility = View.VISIBLE
binding.mineralTagsText.text = buildSpannedString {
bold { append(getString(R.string.mineral_tags_text)) }
append(tagListToString(it))
}
}
}
viewModel.mineralTags.observe(viewLifecycleOwner) {
if (it.isNotEmpty()) {
binding.otherNutritionTags.visibility = View.VISIBLE
binding.otherNutritionTags.text = buildSpannedString {
bold { append(getString(R.string.other_tags_text)) }
append(tagListToString(it))
}
}
}
refreshView(productState)
}
override fun onAttach(context: Context) {
super.onAttach(context)
customTabActivityHelper = CustomTabActivityHelper()
customTabsIntent = CustomTabsHelper.getCustomTabsIntent(requireContext(), customTabActivityHelper.session)
productState = requireProductState()
}
override fun refreshView(productState: ProductState) {
super.refreshView(productState)
this.productState = productState
val langCode = localeManager.getLanguage()
if (arguments != null) mSendProduct = getSendProduct()
val product = this.productState.product!!
viewModel.product.value = product
binding.textAdditiveProduct.text = buildSpannedString { bold { append(getString(R.string.txtAdditives)) } }
setAdditivesState(Loading)
viewModel.additives.observe(viewLifecycleOwner) { additives ->
if (additives.isEmpty()) setAdditivesState(Empty)
else setAdditivesState(Data(additives))
}
if (!product.getImageIngredientsUrl(langCode).isNullOrBlank()) {
binding.ingredientImagetipBox.setTipMessage(getString(R.string.onboarding_hint_msg,
getString(R.string.image_edit_tip)))
binding.ingredientImagetipBox.loadToolTip()
binding.addPhotoLabel.visibility = View.GONE
binding.changeIngImg.visibility = View.VISIBLE
// Load Image if isLowBatteryMode is false
if (!requireContext().isPowerSaveMode()) {
picasso.load(product.getImageIngredientsUrl(langCode)).into(binding.imageViewIngredients)
} else {
binding.imageViewIngredients.visibility = View.GONE
}
ingredientsImgUrl = product.getImageIngredientsUrl(langCode)
}
//useful when this fragment is used in offline saving
if (mSendProduct != null && !mSendProduct!!.imgUploadIngredients.isNullOrBlank()) {
binding.addPhotoLabel.visibility = View.GONE
ingredientsImgUrl = mSendProduct!!.imgUploadIngredients
picasso.load(LOCALE_FILE_SCHEME + ingredientsImgUrl).config(Bitmap.Config.RGB_565)
.into(binding.imageViewIngredients)
}
val allergens = getAllergens()
if (!product.getIngredientsText(langCode).isNullOrEmpty()) {
binding.cvTextIngredientProduct.visibility = View.VISIBLE
var txtIngredients = buildSpannedString { append(product.getIngredientsText(langCode)!!.replace("_", "")) }
txtIngredients = boldAllergens(txtIngredients, allergens)
if (product.getIngredientsText(langCode).isNullOrEmpty()) {
binding.extractIngredientsPrompt.visibility = View.VISIBLE
}
val ingredientsListAt = txtIngredients.toString().indexOf(":").coerceAtLeast(0)
if (txtIngredients.toString().substring(ingredientsListAt).trim { it <= ' ' }.isNotEmpty()) {
binding.textIngredientProduct.text = txtIngredients
}
} else {
binding.cvTextIngredientProduct.visibility = View.GONE
if (!product.getImageIngredientsUrl(langCode).isNullOrBlank()) {
binding.extractIngredientsPrompt.visibility = View.VISIBLE
}
}
setAllergensState(Loading)
viewModel.allergens.observe(viewLifecycleOwner) {
if (it.isEmpty()) setAllergensState(Empty)
else setAllergensState(Data(it))
}
if (!product.traces.isNullOrBlank()) {
val language = localeManager.getLanguage()
binding.cvTextTraceProduct.visibility = View.VISIBLE
binding.textTraceProduct.movementMethod = LinkMovementMethod.getInstance()
binding.textTraceProduct.text = buildSpannedString {
bold { append(getString(R.string.txtTraces)) }
append(" ")
val traces = product.traces.split(",")
traces.map {
ProductFragmentUtils.getSearchLinkText(
getTracesName(language, it),
SearchType.TRACE,
requireActivity()
)
}.forEachIndexed { i, el ->
append(el)
if (i != traces.size) append(", ")
}
}
} else {
binding.cvTextTraceProduct.visibility = View.GONE
}
val novaGroups = product.novaGroups
if (novaGroups != null && !isFlavors(AppFlavor.OBF)) {
binding.novaLayout.visibility = View.VISIBLE
binding.novaExplanation.text = getNovaGroupExplanation(novaGroups, requireContext()) ?: ""
binding.novaGroup.setImageResource(product.getNovaGroupResource())
binding.novaGroup.setOnClickListener {
val uri = getString(R.string.url_nova_groups).toUri()
val tabsIntent = CustomTabsHelper.getCustomTabsIntent(requireContext(), customTabActivityHelper.session)
CustomTabActivityHelper.openCustomTab(requireActivity(), tabsIntent, uri, WebViewFallback())
}
} else {
binding.novaLayout.visibility = View.GONE
}
}
private fun getTracesName(languageCode: String, tag: String): String {
val allergenName = daoSession.allergenNameDao.unique {
where(AllergenNameDao.Properties.AllergenTag.eq(tag))
where(AllergenNameDao.Properties.LanguageCode.eq(languageCode))
}
return if (allergenName != null) allergenName.name else tag
}
private fun tagListToString(tagList: List<String>) =
tagList.joinToString(", ", " ") { it.substring(3) }
private fun getAllergensTag(allergen: AllergenName): CharSequence {
val clickableSpan = ClickableSpan {
if (allergen.isWikiDataIdPresent) {
lifecycleScope.launch {
val result = wikidataClient.getEntityData(
allergen.wikiDataId
)
val activity = activity
if (activity?.isFinishing == false) {
showBottomSheet(result, allergen, activity.supportFragmentManager)
}
}
} else {
startSearch(requireContext(), SearchType.ALLERGEN, allergen.allergenTag, allergen.name)
}
}
return buildSpannedString {
append(allergen.name)
setSpan(clickableSpan, 0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
// If allergen is not in the taxonomy list then italicize it
if (!allergen.isNotNull) {
setSpan(StyleSpan(Typeface.ITALIC), 0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
}
private fun boldAllergens(ingredientsText: CharSequence, allergenTags: List<String>): SpannedString {
return buildSpannedString {
append(ingredientsText)
INGREDIENT_REGEX.findAll(ingredientsText).forEach { match ->
val allergenTxt = match.value
.replace("[()]+".toRegex(), "")
.replace("[,.-]".toRegex(), " ")
allergenTags.find { tag -> tag.contains(allergenTxt, true) }?.let {
var start = match.range.first
var end = match.range.last + 1
if ("(" in match.value) {
start += 1
}
if (")" in match.value) {
end -= 1
}
setSpan(StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
}
}
insert(0, buildSpannedString {
bold { append(getString(R.string.txtIngredients) + ' ') }
})
}
}
private fun setAdditivesState(state: ProductInfoState<List<AdditiveName>>) {
when (state) {
is Loading -> {
binding.cvTextAdditiveProduct.visibility = View.VISIBLE
binding.textAdditiveProduct.append(getString(R.string.txtLoading))
}
is Empty -> binding.cvTextAdditiveProduct.visibility = View.GONE
is Data -> {
showAdditives(binding.textAdditiveProduct, state.data, wikidataClient, this)
}
}
}
fun changeIngImage() {
sendUpdatedIngredientsImage = true
if (activity == null) return
val viewPager = requireActivity().findViewById<ViewPager2>(R.id.pager)
if (isFlavors(AppFlavor.OFF)) {
if (!requireContext().isUserSet()) {
showLoginDialog(loginLauncher)
} else {
productState = requireProductState()
updateImagesLauncher.launch(productState.product!!)
}
}
when {
isFlavors(AppFlavor.OPFF) -> viewPager.currentItem = 4
isFlavors(AppFlavor.OBF) -> viewPager.currentItem = 1
isFlavors(AppFlavor.OPF) -> viewPager.currentItem = 0
}
}
private fun setAllergensState(state: ProductInfoState<List<AllergenName>>) {
when (state) {
is Loading -> {
binding.textSubstanceProduct.visibility = View.VISIBLE
binding.textSubstanceProduct.append(getString(R.string.txtLoading))
}
is Empty -> binding.textSubstanceProduct.visibility = View.GONE
is Data -> {
binding.textSubstanceProduct.movementMethod = LinkMovementMethod.getInstance()
binding.textSubstanceProduct.text = buildSpannedString {
bold { append(getString(R.string.txtSubstances)) }
append(" ")
state.data.map { getAllergensTag(it) }.forEachIndexed { i, el ->
append(el)
if (i != state.data.size) append(", ")
}
}
}
}
}
private fun getAllergens(): List<String> {
val allergens = productState.product!!.allergensTags
return if (productState.product != null && allergens.isNotEmpty()) allergens else emptyList()
}
private fun novaMethodLinkDisplay() {
if (productState.product != null && productState.product!!.novaGroups != null) {
val uri = Uri.parse(getString(R.string.url_nova_groups))
val tabsIntent = CustomTabsHelper.getCustomTabsIntent(requireContext(), customTabActivityHelper.session)
CustomTabActivityHelper.openCustomTab(requireActivity(), tabsIntent, uri, WebViewFallback())
}
}
fun extractIngredients() {
if (!isAdded) return
if (requireContext().isUserSet()) {
showLoginDialog(loginLauncher)
} else {
productState = requireProductState()
performOCRLauncher.launch(productState.product)
}
}
private fun newIngredientImage() = doChooseOrTakePhotos()
override fun doOnPhotosPermissionGranted() = newIngredientImage()
private fun onPhotoReturned(newPhotoFile: File) {
val image = ProductImage(
productState.code!!,
ProductImageField.INGREDIENTS,
newPhotoFile,
localeManager.getLanguage()
).apply { filePath = newPhotoFile.absolutePath }
lifecycleScope.launch { client.postImg(image) }
binding.addPhotoLabel.visibility = View.GONE
ingredientsImgUrl = newPhotoFile.absolutePath
picasso.load(newPhotoFile)
.fit()
.into(binding.imageViewIngredients)
}
@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (ImagesManageActivity.isImageModified(requestCode, resultCode)) {
onRefresh()
}
photoReceiverHandler.onActivityResult(this, requestCode, resultCode, data) { onPhotoReturned(it) }
}
companion object {
val INGREDIENT_REGEX = Regex("[\\p{L}\\p{Nd}(),.-]+")
fun newInstance(productState: ProductState) = IngredientsProductFragment().apply {
arguments = Bundle().apply {
putSerializable(KEY_STATE, productState)
}
}
}
}