Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.07 KB | None | 0 0
  1. package com.udacoding.wisatapedia.control.ui.activity.transaction
  2.  
  3. import android.annotation.SuppressLint
  4. import android.content.res.Configuration
  5. import android.os.Bundle
  6. import android.support.v7.app.ActionBar
  7. import android.support.v7.widget.LinearLayoutManager
  8. import android.util.Log
  9. import android.view.Gravity
  10. import android.view.MenuItem
  11. import android.view.View
  12. import android.widget.TextView
  13. import com.blogbasbas.yukkotlin.util.currencyFormatter
  14. import com.blogbasbas.yukkotlin.util.hasNetwork
  15. import com.google.gson.Gson
  16. import com.udacoding.wisatapedia.control.base.BaseActivity
  17. import com.udacoding.wisatapedia.db.database
  18. import com.udacoding.wisatapedia.db.model_db.TransactionModel
  19. import com.udacoding.wisatapedia.model.promo.DataItemPromo
  20. import com.udacoding.wisatapedia.util.pref.IdUSerSession
  21. import com.valdesekamdem.library.mdtoast.MDToast
  22. import kotlinx.android.synthetic.main.activity_transaction.*
  23. import org.jetbrains.anko.ctx
  24. import org.jetbrains.anko.db.*
  25. import spencerstudios.com.bungeelib.Bungee
  26. import android.os.Handler
  27.  
  28.  
  29. class TransactionActivity : BaseActivity(), InterfaceTransactionActivity.View {
  30. override fun loadShummer() {
  31. progressbasr_transaction.visibility = View.VISIBLE
  32.  
  33. }
  34.  
  35.  
  36. private var presenter: InterfaceTransactionActivity.Presenter? = null
  37. private var dataItemPromo: MutableList<DataItemPromo> = mutableListOf()
  38. lateinit var adapter: RvAdapterTransaction
  39. lateinit var adapterRecomendation: RvAdapterRecomendation
  40. val transactionModel: MutableList<TransactionModel> = mutableListOf()
  41. var queryData: TransactionModel? = null
  42. var data: TransactionModel? = null
  43.  
  44.  
  45. var sendData : String? = null
  46. override fun onCreate(savedInstanceState: Bundle?) {
  47. super.onCreate(savedInstanceState)
  48. setContentView(com.udacoding.wisatapedia.R.layout.activity_transaction)
  49. getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);
  50. getSupportActionBar()?.setHomeButtonEnabled(true);
  51.  
  52. val abar = supportActionBar
  53. val viewActionBar = layoutInflater.inflate(com.udacoding.wisatapedia.R.layout.app_bar, null)
  54. val params = ActionBar.LayoutParams(//Center the textview in the ActionBar !
  55. ActionBar.LayoutParams.WRAP_CONTENT,
  56. ActionBar.LayoutParams.MATCH_PARENT,
  57. Gravity.CENTER)
  58. val textviewTitle = viewActionBar.findViewById(com.udacoding.wisatapedia.R.id.titleTextView) as TextView
  59. textviewTitle.text = getString(com.udacoding.wisatapedia.R.string.shpoing_cart)
  60. abar?.setCustomView(viewActionBar, params)
  61. abar?.setDisplayShowCustomEnabled(true)
  62. abar?.setDisplayShowTitleEnabled(false)
  63. abar?.setDisplayHomeAsUpEnabled(true)
  64. abar?.setHomeButtonEnabled(true)
  65.  
  66.  
  67.  
  68.  
  69.  
  70. rv_transaction.layoutManager = LinearLayoutManager(this)
  71. rv_recomendation_at_transaction_activity.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
  72. adapter = RvAdapterTransaction(transactionModel,
  73. { idDataMin: TransactionModel -> minAmount(idDataMin) },
  74. { idDataAdd: TransactionModel -> addAmount(idDataAdd) },
  75. { idDelete: TransactionModel -> deleteProduct(idDelete) })
  76. rv_transaction.adapter = adapter
  77.  
  78.  
  79. presenter = LogicTransaction(this,this)
  80. presenter?.addItemCart(IdUSerSession(ctx).getIdUser().toString())
  81. presenter?.addDataPromo()
  82.  
  83. showTransaction(false)
  84. setPrice()
  85.  
  86.  
  87.  
  88. btn_pay_transaction.setOnClickListener {
  89. // if (transactionModel.size != 0){
  90. // intentTo(SendProductActivity::class.java)
  91. // Bungee.slideLeft(this)
  92. // } else{
  93. // showInfoMessage("Data Kosong")
  94. // }
  95.  
  96.  
  97. val stList = (adapter as RvAdapterTransaction)
  98. .getDataCheckBox()
  99. var data =""
  100.  
  101. for (i in 0 until stList.size) {
  102. val dataListCheckbox = stList[i]
  103. if (dataListCheckbox.getChecked()==true) {
  104. data = dataListCheckbox.idLinkProduct.toString()
  105. Log.e("tag","cek "+data)
  106. }
  107. }
  108. showInfoMessage("test \n $data")
  109.  
  110.  
  111. }
  112.  
  113. id_swipe_layout_transaction.setOnRefreshListener{
  114. showTransaction(false)
  115. setPrice()
  116. }
  117.  
  118.  
  119. }
  120.  
  121. override fun stopShimmer() {
  122. shimmer_view_container_recomendation.stopShimmer()
  123. shimmer_view_container_recomendation.setVisibility(View.GONE);
  124. progressbasr_transaction.visibility = View.GONE
  125. }
  126.  
  127. override fun loadPromo(data: List<DataItemPromo>) {
  128. dataItemPromo.clear()
  129. dataItemPromo.addAll(data)
  130. if (dataItemPromo.count() != 0) {
  131. adapterRecomendation = RvAdapterRecomendation(dataItemPromo)
  132. rv_recomendation_at_transaction_activity.adapter = adapterRecomendation
  133. adapterRecomendation.notifyDataSetChanged()
  134. rv_recomendation_at_transaction_activity.setHasFixedSize(true);
  135. rv_recomendation_at_transaction_activity.setItemViewCacheSize(20);
  136. }
  137. }
  138.  
  139. override fun addItemCart() {
  140.  
  141. }
  142.  
  143.  
  144. @SuppressLint("SetTextI18n")
  145. private fun setPrice() {
  146.  
  147. val getData = showTransaction(false)
  148. val priceList = mutableListOf<Long>()
  149. getData.forEach {
  150. val amountProduct = java.lang.Long.parseLong(it.amountProduct)
  151. val priceProduct = java.lang.Long.parseLong(it.priceProduct)
  152. val priceItem = amountProduct * priceProduct
  153. Log.e("Tag ", "dapat penjumalah price item $priceItem = $amountProduct + $priceProduct")
  154. priceList.add(priceItem)
  155. }
  156. val totalAllPrice = priceList.sum()
  157. tv_price_at_transaction_activity.text = "Rp" + currencyFormatter(totalAllPrice.toString())
  158.  
  159.  
  160. }
  161.  
  162.  
  163. // private fun refreshData() {
  164. // id_swipe_layout_transaction.onRefresh {
  165. // transactionModel.clear()
  166. // showTransaction()
  167. // }
  168. // }
  169.  
  170. private fun addAmount(dataId: TransactionModel) {
  171. val rowParser = classParser<TransactionModel>()
  172. queryData = database.use {
  173. select(TransactionModel.TABLE_TRANSACTION_PRODUCT)
  174. .whereArgs("(ID_PRODUCT_LINK = {id_product_link})",
  175. "id_product_link" to dataId.idLinkProduct.toString()).exec {
  176. parseList(rowParser).get(0)
  177.  
  178. }
  179. }
  180. val idLink = queryData?.idLinkProduct
  181. val addAmount = Integer.valueOf(queryData?.amountProduct) + 1
  182. val totalPrice = Integer.valueOf(addAmount) * Integer.valueOf(queryData?.priceProduct)
  183. Log.e("Tag", "data amount " + addAmount + " / " + totalPrice + " / " + idLink)
  184.  
  185.  
  186. if (hasNetwork(ctx) == true) {
  187. presenter?.editDataCart(idLink.toString(),
  188. IdUSerSession(ctx).getIdUser().toString(),
  189. dataId?.idProduct.toString(),
  190. addAmount.toString(),
  191. totalPrice.toString())
  192.  
  193.  
  194. database.use {
  195. update(TransactionModel.TABLE_TRANSACTION_PRODUCT, "amount_product" to addAmount.toString())
  196. .whereSimple("id_product_link = ?", idLink.toString())
  197. .exec()
  198. }
  199.  
  200.  
  201. } else {
  202. showErrorMessage("Aktivkan Internet")
  203. }
  204.  
  205. showTransaction(false)
  206. setPrice()
  207.  
  208.  
  209. }
  210.  
  211. private fun minAmount(dataId: TransactionModel) {
  212. val rowParser = classParser<TransactionModel>()
  213. queryData = database.use {
  214. select(TransactionModel.TABLE_TRANSACTION_PRODUCT)
  215. .whereArgs("(ID_PRODUCT = {id_product})",
  216. "id_product" to dataId.idProduct.toString()).exec {
  217. parseList(rowParser).get(0)
  218.  
  219. }
  220. }
  221. if (queryData?.amountProduct.equals("1")) {
  222. MDToast.makeText(this, "Tidak bisa kurang dari ${queryData?.amountProduct}", MDToast.LENGTH_SHORT, MDToast.TYPE_INFO).show()
  223. } else {
  224. // val minAmount = Integer.valueOf(queryData?.amountProduct) - 1
  225. // database.use {
  226. // update(TransactionModel.TABLE_TRANSACTION_PRODUCT, "amount_product" to minAmount.toString())
  227. // .whereSimple("id_product = ?", dataId?.idProduct.toString())
  228. // .exec()
  229. //
  230. //
  231. // }
  232.  
  233. val idLink = queryData?.idLinkProduct
  234. val addAmount = Integer.valueOf(queryData?.amountProduct) - 1
  235. val totalPrice = Integer.valueOf(addAmount) * Integer.valueOf(queryData?.priceProduct)
  236. Log.e("Tag", "data amount " + addAmount + " / " + totalPrice + " / " + idLink)
  237.  
  238.  
  239. if (hasNetwork(ctx) == true) {
  240. presenter?.editDataCart(idLink.toString(),
  241. IdUSerSession(ctx).getIdUser().toString(),
  242. dataId?.idProduct.toString(),
  243. addAmount.toString(),
  244. totalPrice.toString())
  245.  
  246.  
  247. database.use {
  248. update(TransactionModel.TABLE_TRANSACTION_PRODUCT, "amount_product" to addAmount.toString())
  249. .whereSimple("id_product_link = ?", idLink.toString())
  250. .exec()
  251. }
  252.  
  253.  
  254. } else {
  255. showErrorMessage("Aktivkan Internet")
  256. }
  257.  
  258. }
  259.  
  260. showTransaction(false)
  261. setPrice()
  262. }
  263.  
  264.  
  265. private fun showTransaction(isSelect: Boolean): List<TransactionModel> {
  266.  
  267. // id_swipe_layout_transaction.isRefreshing = false
  268. presenter?.addItemCart(IdUSerSession(ctx).getIdUser().toString())
  269. database.use {
  270. try {
  271. val idUser = IdUSerSession(ctx).getIdUser().toString()
  272. val result = select(TransactionModel.TABLE_TRANSACTION_PRODUCT).whereArgs("id_user = $idUser")
  273. val transaction = result.parseList(parser = rowParser { id: Long?, idProduct: String?, idLinkProduct: String?, productcode: String?, titleProduct: String, priceProduct: String?, photoProduct: String?, amountProduct: String?, sellerProduct: String?, emailUser: String?, idUser: String?, flag: String?, nameCategory : String?
  274. ->
  275. TransactionModel(id, idProduct, idLinkProduct, productcode, titleProduct, priceProduct, photoProduct, amountProduct, sellerProduct, emailUser, idUser, flag, nameCategory)
  276. })
  277. transactionModel.clear()
  278. transactionModel.addAll(transaction)
  279. adapter.notifyDataSetChanged()
  280. rv_transaction.adapter = adapter
  281.  
  282. id_swipe_layout_transaction.isRefreshing = false
  283.  
  284. } catch (e: IndexOutOfBoundsException) {
  285.  
  286. }
  287.  
  288. }
  289. Log.e("Tag","data transaction " + Gson().toJson(transactionModel))
  290. return transactionModel
  291.  
  292. }
  293.  
  294. private fun deleteProduct(idDelete: TransactionModel) {
  295.  
  296. if (hasNetwork(ctx)==true) {
  297. presenter?.deleteItemCart(idDelete.idLinkProduct.toString())
  298. database.use {
  299. delete(TransactionModel.TABLE_TRANSACTION_PRODUCT, "(ID_PRODUCT_LINK = {id_product_link})",
  300. "id_product_link" to idDelete.idLinkProduct.toString())
  301. }
  302. } else{
  303. showErrorMessage("Aktivkan internet")
  304. }
  305.  
  306. presenter?.addItemCart(IdUSerSession(ctx).getIdUser()!!)
  307. showTransaction(false)
  308. setPrice()
  309.  
  310. }
  311.  
  312. override fun onStart() {
  313. super.onStart()
  314. shimmer_view_container_recomendation.startShimmer()
  315. // sendData = intent.getStringExtra("1")
  316. // if (sendData != null){
  317. // showTransaction(false)
  318. // }
  319. // showTransaction(false)
  320.  
  321. }
  322.  
  323. override fun onPause() {
  324. super.onPause()
  325. Handler().postDelayed({
  326. showTransaction(false)
  327. }, 1000)
  328.  
  329.  
  330. }
  331.  
  332.  
  333. override fun onRestart() {
  334. super.onRestart()
  335.  
  336. }
  337.  
  338. override fun onResume() {
  339. super.onResume()
  340. sendData = intent.getStringExtra("1")
  341. if (sendData != null){
  342. Handler().postDelayed({
  343. showTransaction(false)
  344. }, 1000)
  345.  
  346. }
  347.  
  348.  
  349.  
  350. }
  351. override fun onBackPressed() {
  352. super.onBackPressed()
  353. Bungee.slideLeft(this) //fire the slide left animation
  354. }
  355.  
  356. override fun onOptionsItemSelected(item: MenuItem?): Boolean {
  357. val i = item?.getItemId()
  358. if (i == android.R.id.home) {
  359. onBackPressed()
  360. }
  361. return super.onOptionsItemSelected(item)
  362. }
  363.  
  364. override fun onConfigurationChanged(newConfig: Configuration?) {
  365. super.onConfigurationChanged(newConfig)
  366. }
  367.  
  368.  
  369.  
  370. }
  371.  
  372.  
  373.  
  374. private fun <E> MutableList<E>.addAll(elements: List<Unit>) {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement