/
ProductListsActivity.kt
318 lines (278 loc) 路 13.4 KB
/
ProductListsActivity.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
/*
* 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.productlists
import android.app.ProgressDialog
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.DialogAction
import com.afollestad.materialdialogs.MaterialDialog
import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.analytics.AnalyticsEvent
import openfoodfacts.github.scrachx.openfood.analytics.MatomoAnalytics
import openfoodfacts.github.scrachx.openfood.databinding.ActivityProductListsBinding
import openfoodfacts.github.scrachx.openfood.features.productlist.ProductListActivity
import openfoodfacts.github.scrachx.openfood.features.productlist.ProductListActivity.Companion.KEY_LIST_ID
import openfoodfacts.github.scrachx.openfood.features.productlist.ProductListActivity.Companion.KEY_LIST_NAME
import openfoodfacts.github.scrachx.openfood.features.productlist.ProductListActivity.Companion.KEY_PRODUCT_TO_ADD
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.RecyclerItemClickListener
import openfoodfacts.github.scrachx.openfood.models.DaoSession
import openfoodfacts.github.scrachx.openfood.models.Product
import openfoodfacts.github.scrachx.openfood.models.entities.*
import openfoodfacts.github.scrachx.openfood.utils.SwipeController
import openfoodfacts.github.scrachx.openfood.utils.isEmpty
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
import java.io.InputStream
import java.io.InputStreamReader
import java.util.*
import javax.inject.Inject
@AndroidEntryPoint
class ProductListsActivity : BaseActivity(), SwipeController.Actions {
private var _binding: ActivityProductListsBinding? = null
private val binding get() = _binding!!
@Inject
lateinit var daoSession: DaoSession
@Inject
lateinit var matomoAnalytics: MatomoAnalytics
private lateinit var adapter: ProductListsAdapter
private lateinit var productListsDao: ProductListsDao
private val disp = CompositeDisposable()
override fun onDestroy() {
disp.dispose()
super.onDestroy()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityProductListsBinding.inflate(layoutInflater)
setContentView(binding.root)
setTitle(R.string.your_lists)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
binding.bottomNavigation.bottomNavigation.installBottomNavigation(this)
binding.fabAdd.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_plus_blue_24, 0, 0, 0)
// FIXME: remove runBlocking
productListsDao = runBlocking { daoSession.getProductListsDaoWithDefaultList(this@ProductListsActivity) }
val productLists = productListsDao.loadAll().toMutableList()
adapter = ProductListsAdapter(this@ProductListsActivity, productLists)
binding.productListsRecyclerView.layoutManager = LinearLayoutManager(this)
binding.productListsRecyclerView.adapter = adapter
binding.tipBox.loadToolTip()
val bundle = intent.extras
if (bundle != null) {
val productToAdd = bundle[KEY_PRODUCT] as Product?
showCreateListDialog(productToAdd)
}
binding.productListsRecyclerView.addOnItemTouchListener(
RecyclerItemClickListener(this) { _, position ->
val id = productLists[position].id
val listName = productLists[position].listName
Intent(this, ProductListActivity::class.java).apply {
putExtra(KEY_LIST_ID, id)
putExtra(KEY_LIST_NAME, listName)
startActivityForResult(this, 1)
}
}
)
val swipeController = SwipeController(this, this)
val itemTouchHelper = ItemTouchHelper(swipeController)
itemTouchHelper.attachToRecyclerView(binding.productListsRecyclerView)
binding.fabAdd.setOnClickListener { showCreateListDialog() }
}
private fun showCreateListDialog(productToAdd: Product? = null) {
MaterialDialog.Builder(this)
.title(R.string.txt_create_new_list)
.alwaysCallInputCallback()
.input(R.string.create_new_list_list_name, R.string.empty, false) { dialog, listName ->
// validate if there is another list with the same name
val cannotAdd = checkListNameExist(listName.toString())
dialog.inputEditText?.error = if (cannotAdd) resources.getString(R.string.error_duplicate_listname) else null
dialog.getActionButton(DialogAction.POSITIVE).isEnabled = !cannotAdd
}
.positiveText(R.string.dialog_create)
.negativeText(R.string.dialog_cancel)
.onPositive { dialog, _ -> // this enable to avoid dismissing dialog if list name already exist
matomoAnalytics.trackEvent(AnalyticsEvent.ShoppingListCreated)
val inputEditText = dialog.inputEditText!!
val listName = inputEditText.text.toString()
val productList = ProductLists(listName, if (productToAdd != null) 1 else 0)
adapter.lists.add(productList)
productListsDao.insert(productList)
adapter.notifyDataSetChanged()
if (productToAdd != null) {
val id = productList.id
Intent(this@ProductListsActivity, ProductListActivity::class.java).apply {
putExtra(KEY_LIST_ID, id)
putExtra(KEY_LIST_NAME, listName)
putExtra(KEY_PRODUCT_TO_ADD, productToAdd)
startActivityForResult(this, 1)
}
} else {
dialog.dismiss()
}
}.show()
}
/**
* Check if listname already in products lists.
*/
private fun checkListNameExist(listName: String) =
adapter.lists.firstOrNull { it.listName == listName } != null
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && resultCode == RESULT_OK && data!!.extras!!.getBoolean("update")) {
adapter.notifyDataSetChanged()
}
}
@RequiresApi(Build.VERSION_CODES.KITKAT)
private val chooseFileContract = registerForActivityResult(ActivityResultContracts.OpenDocument())
{ uri ->
if (uri != null) {
try {
parseCSV(contentResolver.openInputStream(uri))
} catch (e: Exception) {
Log.e(ProductListsActivity::class.simpleName, "Error importing CSV.", e)
}
}
}
override fun onRightClicked(position: Int) {
if (adapter.lists.isNullOrEmpty()) return
val list = adapter.lists[position]
// delete the product from YOUR_LISTED_PRODUCT_TABLE
daoSession.listedProductDao.queryBuilder()
.where(ListedProductDao.Properties.ListId.eq(list.id))
.buildDelete()
.executeDeleteWithoutDetachingEntities()
daoSession.clear()
productListsDao.delete(list)
adapter.remove(list)
adapter.notifyItemRangeChanged(position, adapter.itemCount)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_product_lists, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
R.id.action_import_csv -> {
openCSVToImport()
true
}
else -> super.onOptionsItemSelected(item)
}
private fun openCSVToImport() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
chooseFileContract.launch(arrayOf("text/csv"))
} else {
Toast.makeText(this, "Feature disabled for your android version.", Toast.LENGTH_LONG).show()
}
}
public override fun onResume() {
super.onResume()
binding.bottomNavigation.bottomNavigation.selectNavigationItem(R.id.my_lists)
}
private fun parseCSV(inputStream: InputStream?) {
val progressDialog = ProgressDialog(this@ProductListsActivity)
progressDialog.show()
Observable.create { emitter: ObservableEmitter<Int> ->
Single.fromCallable {
val listProductDao = daoSession.listedProductDao
val list = mutableListOf<ListedProduct>()
try {
CSVParser(
InputStreamReader(inputStream),
CSVFormat.DEFAULT.withFirstRecordAsHeader()
).use { parser ->
val size = parser.records.size
parser.records.withIndex().forEach { (index, record) ->
val listName = record[2]
var daoList = productListsDao.queryBuilder()
.where(ProductListsDao.Properties.ListName.eq(listName)).unique()
if (daoList == null) {
//create new list
val productList = ProductLists(listName, 0)
adapter.lists.add(productList)
productListsDao.insert(productList)
daoList = productListsDao.queryBuilder()
.where(ProductListsDao.Properties.ListName.eq(listName)).unique()
}
val yourListedProduct = ListedProduct().apply {
this.barcode = record[0]
this.productName = record[1]
this.listName = listName
this.productDetails = record[3]
this.listId = daoList.id
}
list += yourListedProduct
emitter.onNext(index * 100 / size)
}
listProductDao.insertOrReplaceInTx(list)
return@fromCallable true
}
} catch (e: Exception) {
Log.e("ParseCSV", e.message, e)
return@fromCallable false
}
}.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe { success ->
progressDialog.dismiss()
if (success) {
Toast.makeText(this@ProductListsActivity, getString(R.string.toast_import_csv), Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this@ProductListsActivity, getString(R.string.toast_import_csv_error), Toast.LENGTH_SHORT).show()
}
}.addTo(disp)
}.subscribe { value -> progressDialog.progress = value!! }.addTo(disp)
}
companion object {
private const val KEY_PRODUCT = "product"
@JvmStatic
fun start(context: Context, productToAdd: Product) = context.startActivity(
Intent(context, ProductListsActivity::class.java).apply {
putExtra(KEY_PRODUCT, productToAdd)
})
@JvmStatic
fun start(context: Context) = context.startActivity(Intent(context, ProductListsActivity::class.java))
suspend fun DaoSession.getProductListsDaoWithDefaultList(context: Context): ProductListsDao = withContext(Dispatchers.IO) {
if (productListsDao.isEmpty()) {
productListsDao.insertInTx(
ProductLists(context.getString(R.string.txt_eaten_products), 0),
ProductLists(context.getString(R.string.txt_products_to_buy), 0)
)
}
return@withContext productListsDao
}
}
}