Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.udacoding.wisatapedia.control.ui.activity.transaction
- import android.annotation.SuppressLint
- import android.content.res.Configuration
- import android.os.Bundle
- import android.support.v7.app.ActionBar
- import android.support.v7.widget.LinearLayoutManager
- import android.util.Log
- import android.view.Gravity
- import android.view.MenuItem
- import android.view.View
- import android.widget.TextView
- import com.blogbasbas.yukkotlin.util.currencyFormatter
- import com.blogbasbas.yukkotlin.util.hasNetwork
- import com.google.gson.Gson
- import com.udacoding.wisatapedia.control.base.BaseActivity
- import com.udacoding.wisatapedia.db.database
- import com.udacoding.wisatapedia.db.model_db.TransactionModel
- import com.udacoding.wisatapedia.model.promo.DataItemPromo
- import com.udacoding.wisatapedia.util.pref.IdUSerSession
- import com.valdesekamdem.library.mdtoast.MDToast
- import kotlinx.android.synthetic.main.activity_transaction.*
- import org.jetbrains.anko.ctx
- import org.jetbrains.anko.db.*
- import spencerstudios.com.bungeelib.Bungee
- import android.os.Handler
- class TransactionActivity : BaseActivity(), InterfaceTransactionActivity.View {
- override fun loadShummer() {
- progressbasr_transaction.visibility = View.VISIBLE
- }
- private var presenter: InterfaceTransactionActivity.Presenter? = null
- private var dataItemPromo: MutableList<DataItemPromo> = mutableListOf()
- lateinit var adapter: RvAdapterTransaction
- lateinit var adapterRecomendation: RvAdapterRecomendation
- val transactionModel: MutableList<TransactionModel> = mutableListOf()
- var queryData: TransactionModel? = null
- var data: TransactionModel? = null
- var sendData : String? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(com.udacoding.wisatapedia.R.layout.activity_transaction)
- getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);
- getSupportActionBar()?.setHomeButtonEnabled(true);
- val abar = supportActionBar
- val viewActionBar = layoutInflater.inflate(com.udacoding.wisatapedia.R.layout.app_bar, null)
- val params = ActionBar.LayoutParams(//Center the textview in the ActionBar !
- ActionBar.LayoutParams.WRAP_CONTENT,
- ActionBar.LayoutParams.MATCH_PARENT,
- Gravity.CENTER)
- val textviewTitle = viewActionBar.findViewById(com.udacoding.wisatapedia.R.id.titleTextView) as TextView
- textviewTitle.text = getString(com.udacoding.wisatapedia.R.string.shpoing_cart)
- abar?.setCustomView(viewActionBar, params)
- abar?.setDisplayShowCustomEnabled(true)
- abar?.setDisplayShowTitleEnabled(false)
- abar?.setDisplayHomeAsUpEnabled(true)
- abar?.setHomeButtonEnabled(true)
- rv_transaction.layoutManager = LinearLayoutManager(this)
- rv_recomendation_at_transaction_activity.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
- adapter = RvAdapterTransaction(transactionModel,
- { idDataMin: TransactionModel -> minAmount(idDataMin) },
- { idDataAdd: TransactionModel -> addAmount(idDataAdd) },
- { idDelete: TransactionModel -> deleteProduct(idDelete) })
- rv_transaction.adapter = adapter
- presenter = LogicTransaction(this,this)
- presenter?.addItemCart(IdUSerSession(ctx).getIdUser().toString())
- presenter?.addDataPromo()
- showTransaction(false)
- setPrice()
- btn_pay_transaction.setOnClickListener {
- // if (transactionModel.size != 0){
- // intentTo(SendProductActivity::class.java)
- // Bungee.slideLeft(this)
- // } else{
- // showInfoMessage("Data Kosong")
- // }
- val stList = (adapter as RvAdapterTransaction)
- .getDataCheckBox()
- var data =""
- for (i in 0 until stList.size) {
- val dataListCheckbox = stList[i]
- if (dataListCheckbox.getChecked()==true) {
- data = dataListCheckbox.idLinkProduct.toString()
- Log.e("tag","cek "+data)
- }
- }
- showInfoMessage("test \n $data")
- }
- id_swipe_layout_transaction.setOnRefreshListener{
- showTransaction(false)
- setPrice()
- }
- }
- override fun stopShimmer() {
- shimmer_view_container_recomendation.stopShimmer()
- shimmer_view_container_recomendation.setVisibility(View.GONE);
- progressbasr_transaction.visibility = View.GONE
- }
- override fun loadPromo(data: List<DataItemPromo>) {
- dataItemPromo.clear()
- dataItemPromo.addAll(data)
- if (dataItemPromo.count() != 0) {
- adapterRecomendation = RvAdapterRecomendation(dataItemPromo)
- rv_recomendation_at_transaction_activity.adapter = adapterRecomendation
- adapterRecomendation.notifyDataSetChanged()
- rv_recomendation_at_transaction_activity.setHasFixedSize(true);
- rv_recomendation_at_transaction_activity.setItemViewCacheSize(20);
- }
- }
- override fun addItemCart() {
- }
- @SuppressLint("SetTextI18n")
- private fun setPrice() {
- val getData = showTransaction(false)
- val priceList = mutableListOf<Long>()
- getData.forEach {
- val amountProduct = java.lang.Long.parseLong(it.amountProduct)
- val priceProduct = java.lang.Long.parseLong(it.priceProduct)
- val priceItem = amountProduct * priceProduct
- Log.e("Tag ", "dapat penjumalah price item $priceItem = $amountProduct + $priceProduct")
- priceList.add(priceItem)
- }
- val totalAllPrice = priceList.sum()
- tv_price_at_transaction_activity.text = "Rp" + currencyFormatter(totalAllPrice.toString())
- }
- // private fun refreshData() {
- // id_swipe_layout_transaction.onRefresh {
- // transactionModel.clear()
- // showTransaction()
- // }
- // }
- private fun addAmount(dataId: TransactionModel) {
- val rowParser = classParser<TransactionModel>()
- queryData = database.use {
- select(TransactionModel.TABLE_TRANSACTION_PRODUCT)
- .whereArgs("(ID_PRODUCT_LINK = {id_product_link})",
- "id_product_link" to dataId.idLinkProduct.toString()).exec {
- parseList(rowParser).get(0)
- }
- }
- val idLink = queryData?.idLinkProduct
- val addAmount = Integer.valueOf(queryData?.amountProduct) + 1
- val totalPrice = Integer.valueOf(addAmount) * Integer.valueOf(queryData?.priceProduct)
- Log.e("Tag", "data amount " + addAmount + " / " + totalPrice + " / " + idLink)
- if (hasNetwork(ctx) == true) {
- presenter?.editDataCart(idLink.toString(),
- IdUSerSession(ctx).getIdUser().toString(),
- dataId?.idProduct.toString(),
- addAmount.toString(),
- totalPrice.toString())
- database.use {
- update(TransactionModel.TABLE_TRANSACTION_PRODUCT, "amount_product" to addAmount.toString())
- .whereSimple("id_product_link = ?", idLink.toString())
- .exec()
- }
- } else {
- showErrorMessage("Aktivkan Internet")
- }
- showTransaction(false)
- setPrice()
- }
- private fun minAmount(dataId: TransactionModel) {
- val rowParser = classParser<TransactionModel>()
- queryData = database.use {
- select(TransactionModel.TABLE_TRANSACTION_PRODUCT)
- .whereArgs("(ID_PRODUCT = {id_product})",
- "id_product" to dataId.idProduct.toString()).exec {
- parseList(rowParser).get(0)
- }
- }
- if (queryData?.amountProduct.equals("1")) {
- MDToast.makeText(this, "Tidak bisa kurang dari ${queryData?.amountProduct}", MDToast.LENGTH_SHORT, MDToast.TYPE_INFO).show()
- } else {
- // val minAmount = Integer.valueOf(queryData?.amountProduct) - 1
- // database.use {
- // update(TransactionModel.TABLE_TRANSACTION_PRODUCT, "amount_product" to minAmount.toString())
- // .whereSimple("id_product = ?", dataId?.idProduct.toString())
- // .exec()
- //
- //
- // }
- val idLink = queryData?.idLinkProduct
- val addAmount = Integer.valueOf(queryData?.amountProduct) - 1
- val totalPrice = Integer.valueOf(addAmount) * Integer.valueOf(queryData?.priceProduct)
- Log.e("Tag", "data amount " + addAmount + " / " + totalPrice + " / " + idLink)
- if (hasNetwork(ctx) == true) {
- presenter?.editDataCart(idLink.toString(),
- IdUSerSession(ctx).getIdUser().toString(),
- dataId?.idProduct.toString(),
- addAmount.toString(),
- totalPrice.toString())
- database.use {
- update(TransactionModel.TABLE_TRANSACTION_PRODUCT, "amount_product" to addAmount.toString())
- .whereSimple("id_product_link = ?", idLink.toString())
- .exec()
- }
- } else {
- showErrorMessage("Aktivkan Internet")
- }
- }
- showTransaction(false)
- setPrice()
- }
- private fun showTransaction(isSelect: Boolean): List<TransactionModel> {
- // id_swipe_layout_transaction.isRefreshing = false
- presenter?.addItemCart(IdUSerSession(ctx).getIdUser().toString())
- database.use {
- try {
- val idUser = IdUSerSession(ctx).getIdUser().toString()
- val result = select(TransactionModel.TABLE_TRANSACTION_PRODUCT).whereArgs("id_user = $idUser")
- 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?
- ->
- TransactionModel(id, idProduct, idLinkProduct, productcode, titleProduct, priceProduct, photoProduct, amountProduct, sellerProduct, emailUser, idUser, flag, nameCategory)
- })
- transactionModel.clear()
- transactionModel.addAll(transaction)
- adapter.notifyDataSetChanged()
- rv_transaction.adapter = adapter
- id_swipe_layout_transaction.isRefreshing = false
- } catch (e: IndexOutOfBoundsException) {
- }
- }
- Log.e("Tag","data transaction " + Gson().toJson(transactionModel))
- return transactionModel
- }
- private fun deleteProduct(idDelete: TransactionModel) {
- if (hasNetwork(ctx)==true) {
- presenter?.deleteItemCart(idDelete.idLinkProduct.toString())
- database.use {
- delete(TransactionModel.TABLE_TRANSACTION_PRODUCT, "(ID_PRODUCT_LINK = {id_product_link})",
- "id_product_link" to idDelete.idLinkProduct.toString())
- }
- } else{
- showErrorMessage("Aktivkan internet")
- }
- presenter?.addItemCart(IdUSerSession(ctx).getIdUser()!!)
- showTransaction(false)
- setPrice()
- }
- override fun onStart() {
- super.onStart()
- shimmer_view_container_recomendation.startShimmer()
- // sendData = intent.getStringExtra("1")
- // if (sendData != null){
- // showTransaction(false)
- // }
- // showTransaction(false)
- }
- override fun onPause() {
- super.onPause()
- Handler().postDelayed({
- showTransaction(false)
- }, 1000)
- }
- override fun onRestart() {
- super.onRestart()
- }
- override fun onResume() {
- super.onResume()
- sendData = intent.getStringExtra("1")
- if (sendData != null){
- Handler().postDelayed({
- showTransaction(false)
- }, 1000)
- }
- }
- override fun onBackPressed() {
- super.onBackPressed()
- Bungee.slideLeft(this) //fire the slide left animation
- }
- override fun onOptionsItemSelected(item: MenuItem?): Boolean {
- val i = item?.getItemId()
- if (i == android.R.id.home) {
- onBackPressed()
- }
- return super.onOptionsItemSelected(item)
- }
- override fun onConfigurationChanged(newConfig: Configuration?) {
- super.onConfigurationChanged(newConfig)
- }
- }
- private fun <E> MutableList<E>.addAll(elements: List<Unit>) {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement