/
ProductViewActivity.kt
285 lines (241 loc) 路 11.5 KB
/
ProductViewActivity.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
/*
* 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
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import openfoodfacts.github.scrachx.openfood.AppFlavors.OBF
import openfoodfacts.github.scrachx.openfood.AppFlavors.OFF
import openfoodfacts.github.scrachx.openfood.AppFlavors.OPF
import openfoodfacts.github.scrachx.openfood.AppFlavors.OPFF
import openfoodfacts.github.scrachx.openfood.AppFlavors.isFlavors
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.databinding.ActivityProductBinding
import openfoodfacts.github.scrachx.openfood.features.product.ProductFragmentPagerAdapter
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.view.ProductViewActivity.ShowIngredientsAction.PERFORM_OCR
import openfoodfacts.github.scrachx.openfood.features.product.view.ProductViewActivity.ShowIngredientsAction.SEND_UPDATED
import openfoodfacts.github.scrachx.openfood.features.product.view.contributors.ContributorsFragment
import openfoodfacts.github.scrachx.openfood.features.product.view.environment.EnvironmentProductFragment
import openfoodfacts.github.scrachx.openfood.features.product.view.ingredients.IngredientsProductFragment
import openfoodfacts.github.scrachx.openfood.features.product.view.ingredients_analysis.IngredientsAnalysisProductFragment
import openfoodfacts.github.scrachx.openfood.features.product.view.nutrition.NutritionProductFragment
import openfoodfacts.github.scrachx.openfood.features.product.view.photos.ProductPhotosFragment
import openfoodfacts.github.scrachx.openfood.features.product.view.serverattributes.ServerAttributesFragment
import openfoodfacts.github.scrachx.openfood.features.product.view.summary.SummaryProductFragment
import openfoodfacts.github.scrachx.openfood.features.shared.BaseActivity
import openfoodfacts.github.scrachx.openfood.listeners.CommonBottomListenerInstaller.installBottomNavigation
import openfoodfacts.github.scrachx.openfood.listeners.CommonBottomListenerInstaller.selectNavigationItem
import openfoodfacts.github.scrachx.openfood.listeners.OnRefreshListener
import openfoodfacts.github.scrachx.openfood.models.ProductState
import openfoodfacts.github.scrachx.openfood.models.eventbus.ProductNeedsRefreshEvent
import openfoodfacts.github.scrachx.openfood.utils.Utils
import openfoodfacts.github.scrachx.openfood.utils.requireProductState
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import javax.inject.Inject
@AndroidEntryPoint
class ProductViewActivity : BaseActivity(), IProductView, OnRefreshListener {
private var _binding: ActivityProductBinding? = null
private val binding get() = _binding!!
@Inject
lateinit var sharedPreferences: SharedPreferences
private var productState: ProductState? = null
private var adapterResult: ProductFragmentPagerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (resources.getBoolean(R.bool.portrait_only)) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
_binding = ActivityProductBinding.inflate(layoutInflater)
setContentView(binding.root)
title = getString(R.string.app_name_long)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
if (!checkActionFromIntent()) {
productState = requireProductState()
initViews()
}
}
override fun onStart() {
super.onStart()
EventBus.getDefault().register(this)
}
override fun onResume() {
super.onResume()
// To check if the activity is resumed and new product is opened through a deep link. If not, then only we can call requireProductState()
if (!checkActionFromIntent())
productState = requireProductState().also { adapterResult!!.refresh(it) }
}
override fun onStop() {
EventBus.getDefault().unregister(this)
super.onStop()
}
/**
* Get the product data from the barcode. This takes the barcode and retrieves the information.
*
* @param barcode from the URL.
*/
private suspend fun fetchProduct(barcode: String) = try {
client.getProductStateFull(barcode, Utils.HEADER_USER_AGENT_SCAN)
} catch (err: Exception) {
Log.w(this::class.simpleName, "Failed to load product $barcode.", err)
finish()
null
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Open product editing after successful login
if (requestCode == LOGIN_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
ProductEditActivity.start(this, productState!!.product!!)
}
}
/**
* Initialise the content that shows the content on the device.
*/
private fun initViews() {
adapterResult = setupViewPager(binding.pager)
TabLayoutMediator(binding.tabs, binding.pager) { tab, position ->
tab.text = adapterResult!!.getPageTitle(position)
}.attach()
binding.navigationBottomInclude.bottomNavigation.selectNavigationItem(0)
binding.navigationBottomInclude.bottomNavigation.installBottomNavigation(this)
}
override fun onOptionsItemSelected(item: MenuItem) = onOptionsItemSelected(this, item)
@Subscribe
fun onEventBusProductNeedsRefreshEvent(event: ProductNeedsRefreshEvent) {
if (event.barcode == productState!!.product!!.code) {
onRefresh()
}
}
override fun onRefresh() {
openProduct(productState!!.product!!.code)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
}
override fun showIngredientsTab(action: ShowIngredientsAction) {
if (adapterResult == null || adapterResult!!.itemCount == 0) return
for (i in 0 until adapterResult!!.itemCount) {
val fragment = adapterResult!!.createFragment(i)
if (fragment is IngredientsProductFragment) {
binding.pager.currentItem = i
when (action) {
PERFORM_OCR -> fragment.extractIngredients()
SEND_UPDATED -> fragment.changeIngImage()
}
return
}
}
}
private fun setupViewPager(viewPager: ViewPager2) = setupViewPager(
viewPager,
productState!!,
this
)
enum class ShowIngredientsAction {
PERFORM_OCR, SEND_UPDATED
}
/** To check if an product is opened through a deep link */
private fun checkActionFromIntent() = when (intent.action) {
Intent.ACTION_VIEW -> {
val data = intent.data
val barcode = data.toString().split("/")[4]
// Fetch product from server, then initialize views
lifecycleScope.launch {
val pState = fetchProduct(barcode) ?: return@launch
productState = pState
intent.putExtra(KEY_STATE, pState)
// Adding check on productState.getProduct() to avoid NPE
// (happens in setViewPager()) when product not found
if (productState != null && productState!!.product != null) initViews()
else finish()
}
true
}
else -> false
}
companion object {
private const val LOGIN_ACTIVITY_REQUEST_CODE = 1
fun start(context: Context, productState: ProductState) {
context.startActivity(Intent(context, ProductViewActivity::class.java).apply {
putExtra(KEY_STATE, productState)
})
}
/**
* CAREFUL ! YOU MUST INSTANTIATE YOUR OWN ADAPTERRESULT BEFORE CALLING THIS METHOD
*/
fun setupViewPager(
viewPager: ViewPager2,
productState: ProductState,
activity: FragmentActivity
): ProductFragmentPagerAdapter {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity)
val adapter = ProductFragmentPagerAdapter(activity)
val titles = activity.resources.getStringArray(R.array.nav_drawer_items_product)
val newTitles = activity.resources.getStringArray(R.array.nav_drawer_new_items_product)
adapter += SummaryProductFragment.newInstance(productState) to titles[0]
// Add Ingredients fragment for off, obf and opff
if (isFlavors(OFF, OBF, OPFF)) {
adapter += IngredientsProductFragment.newInstance(productState) to titles[1]
}
if (isFlavors(OFF, OPFF)) {
adapter += NutritionProductFragment.newInstance(productState) to titles[2]
}
if (isFlavors(OFF)) {
adapter += EnvironmentProductFragment.newInstance(productState) to titles[4]
}
if (isFlavors(OPF) || isFlavors(OFF, OPFF, OBF) && isPhotoMode(sharedPreferences, activity)) {
adapter += ProductPhotosFragment.newInstance(productState) to newTitles[0]
}
if (isFlavors(OFF, OBF)) {
adapter += ServerAttributesFragment.newInstance(productState) to activity.getString(R.string.synthesis_tab)
}
if (isFlavors(OBF)) {
adapter += IngredientsAnalysisProductFragment.newInstance(productState) to newTitles[1]
}
if (sharedPreferences.getBoolean(activity.getString(R.string.pref_contribution_tab_key), false)) {
adapter += ContributorsFragment.newInstance(productState) to activity.getString(R.string.contribution_tab)
}
viewPager.adapter = adapter
return adapter
}
private fun isPhotoMode(sharedPreferences: SharedPreferences, context: Context) =
sharedPreferences.getBoolean(context.getString(R.string.pref_show_product_photos_key), false)
fun onOptionsItemSelected(activity: Activity, item: MenuItem) = when (item.itemId) {
android.R.id.home -> {
// Respond to the action bar's Up/Home button
activity.finish()
true
}
else -> false
}
}
}