icon change on add to cart
This commit is contained in:
parent
b941c425e8
commit
b6e5a32cd3
13
.idea/compiler.xml
generated
Normal file
13
.idea/compiler.xml
generated
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel>
|
||||
<module name="app" target="1.8" />
|
||||
<module name="cocart" target="1.7" />
|
||||
<module name="core" target="1.7" />
|
||||
<module name="firebasecart" target="1.8" />
|
||||
<module name="offlinecart" target="1.8" />
|
||||
<module name="woodroid" target="1.8" />
|
||||
</bytecodeTargetLevel>
|
||||
</component>
|
||||
</project>
|
||||
@ -55,13 +55,25 @@ class ProductActivity : BaseActivity() {
|
||||
|
||||
if (productId != 0){
|
||||
product(productId)
|
||||
|
||||
checkIfExistsInCart(productId)
|
||||
}
|
||||
|
||||
cart()
|
||||
|
||||
}
|
||||
|
||||
private fun checkIfExistsInCart(productId: Int) {
|
||||
RoomCartRepository(baseContext).exists(productId).observe(this, Observer {productExists ->
|
||||
if (productExists) {
|
||||
fab.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.carnation_red))
|
||||
fab.setImageDrawable(ContextCompat.getDrawable(baseContext, R.drawable.baseline_remove_shopping_cart_24))
|
||||
}else{
|
||||
fab.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.colorPrimary))
|
||||
fab.setImageDrawable(ContextCompat.getDrawable(baseContext, R.drawable.baseline_add_shopping_cart_24))
|
||||
}
|
||||
|
||||
productInCart = productExists
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -79,27 +91,6 @@ class ProductActivity : BaseActivity() {
|
||||
|
||||
private fun removeFromCart(cartLineItem: CartLineItem) {
|
||||
|
||||
viewModel.deleteItem(cartLineItem).observe(this, androidx.lifecycle.Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
|
||||
}
|
||||
|
||||
Status.SUCCESS -> {
|
||||
|
||||
|
||||
}
|
||||
|
||||
Status.ERROR -> {
|
||||
toast("error : " + response.error().message)
|
||||
}
|
||||
|
||||
Status.EMPTY -> {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -135,7 +126,7 @@ class ProductActivity : BaseActivity() {
|
||||
|
||||
|
||||
private fun cart() {
|
||||
viewModel.cart().observe(this, androidx.lifecycle.Observer { response ->
|
||||
viewModel.cart().observe(this, Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
|
||||
@ -156,8 +147,6 @@ class ProductActivity : BaseActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
toggleFab()
|
||||
|
||||
if (cartItems.size == 0 && tvCartCounter != null){
|
||||
tvCartCounter?.visibility = View.GONE
|
||||
}else{
|
||||
@ -175,7 +164,6 @@ class ProductActivity : BaseActivity() {
|
||||
Status.EMPTY -> {
|
||||
productInCart = false
|
||||
cartItems.clear()
|
||||
toggleFab()
|
||||
|
||||
if (cartItems.size == 0 && tvCartCounter != null){
|
||||
tvCartCounter?.visibility = View.GONE
|
||||
@ -190,17 +178,6 @@ class ProductActivity : BaseActivity() {
|
||||
|
||||
}
|
||||
|
||||
private fun toggleFab() {
|
||||
if (productInCart) {
|
||||
fab.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.carnation_red))
|
||||
fab.setImageDrawable(ContextCompat.getDrawable(baseContext, R.drawable.baseline_remove_shopping_cart_24))
|
||||
}else{
|
||||
fab.backgroundTintList = ColorStateList.valueOf(resources.getColor(R.color.colorPrimary))
|
||||
fab.setImageDrawable(ContextCompat.getDrawable(baseContext, R.drawable.baseline_add_shopping_cart_24))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun setUpPage(product: Product) {
|
||||
tvTitle.text = product.name
|
||||
tvDescription.text = Html.fromHtml(product.description)
|
||||
|
||||
@ -10,7 +10,7 @@ buildscript {
|
||||
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:4.0.0-alpha04'
|
||||
classpath 'com.android.tools.build:gradle:4.1.0-alpha02'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
classpath 'com.google.gms:google-services:4.2.0'
|
||||
classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0'
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package me.gilo.woodroid.core.cart
|
||||
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import me.gilo.woodroid.core.cart.lines.FeeLine
|
||||
import me.gilo.woodroid.core.cart.lines.LineItem
|
||||
import me.gilo.woodroid.core.cart.lines.ShippingLine
|
||||
@ -32,6 +33,7 @@ data class Cart(
|
||||
|
||||
var feeLines: List<FeeLine> = ArrayList(),
|
||||
|
||||
var couponLines: List<Any> = ArrayList()
|
||||
var couponLines: List<Any> = ArrayList(),
|
||||
var items: LiveData<List<CartItem>>
|
||||
|
||||
)
|
||||
|
||||
@ -2,7 +2,7 @@ package me.gilo.woodroid.core.cart
|
||||
|
||||
|
||||
data class CartItem (
|
||||
var id: Int? = 0,
|
||||
var id: Int? = null,
|
||||
var createdAt: Long? = null,
|
||||
var modifiedAt: Long? = null,
|
||||
var productId: Int = 0,
|
||||
|
||||
@ -1,14 +1,9 @@
|
||||
package me.gilo.woodroid.firebasecart
|
||||
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import com.google.firebase.auth.FirebaseAuth
|
||||
import com.google.firebase.firestore.CollectionReference
|
||||
import com.google.firebase.firestore.FirebaseFirestore
|
||||
import me.gilo.woodroid.core.cart.Cart
|
||||
import me.gilo.woodroid.core.cart.CartItem
|
||||
import me.gilo.woodroid.core.cart.CartRepository
|
||||
import me.gilo.woodroid.core.cart.lines.LineItem
|
||||
|
||||
|
||||
|
||||
open class FirebaseCartRepository(userId : String){
|
||||
|
||||
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,6 @@
|
||||
#Sat Nov 30 11:05:14 EAT 2019
|
||||
#Tue Mar 17 11:45:23 EAT 2020
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-rc-1-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-bin.zip
|
||||
|
||||
@ -12,9 +12,15 @@ interface CartItemDao {
|
||||
@Query("SELECT * FROM cart_item ORDER BY created_at desc")
|
||||
fun fetchAll(): List<CartItemEntity>
|
||||
|
||||
@Query("SELECT * FROM cart_item ORDER BY created_at desc")
|
||||
fun items(): LiveData<List<CartItemEntity>>
|
||||
|
||||
@Query("SELECT COUNT(*) FROM cart_item")
|
||||
fun count(): LiveData<Int>
|
||||
|
||||
@Query("SELECT COUNT(*) FROM cart_item where product_id = :productId")
|
||||
fun exists(productId: Int): LiveData<Boolean>
|
||||
|
||||
@Query("SELECT * FROM cart_item WHERE id =:id")
|
||||
operator fun get(id: Int): LiveData<CartItemEntity>
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ import me.gilo.woodroid.core.cart.CartItem
|
||||
@Entity(tableName = "cart_item")
|
||||
data class CartItemEntity (
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
var id: Int? = 0,
|
||||
var id: Int?,
|
||||
|
||||
@ColumnInfo(name = "created_at")
|
||||
var createdAt: Long? = null,
|
||||
|
||||
@ -2,19 +2,18 @@ package me.gilo.woodroid.offlinecart.repo
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.Transformations
|
||||
import androidx.room.Room
|
||||
import me.gilo.moneta.room.task.DeleteCartItem
|
||||
import me.gilo.moneta.room.task.InsertCartItem
|
||||
import me.gilo.moneta.room.task.RetrieveCartItems
|
||||
import me.gilo.moneta.room.task.UpdateCartItem
|
||||
import me.gilo.woodroid.offlinecart.utils.AppUtils
|
||||
import me.gilo.woodroid.offlinecart.task.DeleteCartItem
|
||||
import me.gilo.woodroid.offlinecart.task.InsertCartItem
|
||||
import me.gilo.woodroid.offlinecart.task.UpdateCartItem
|
||||
import me.gilo.woodroid.core.cart.Cart
|
||||
import me.gilo.woodroid.core.cart.CartItem
|
||||
import me.gilo.woodroid.offlinecart.config.Config
|
||||
import me.gilo.woodroid.offlinecart.db.AppDatabase
|
||||
import me.gilo.woodroid.offlinecart.entity.CartItemEntity
|
||||
import me.gilo.woodroid.offlinecart.entity.toCartItem
|
||||
import me.gilo.woodroid.offlinecart.utils.AppUtils
|
||||
|
||||
class RoomCartRepository(val context: Context){
|
||||
|
||||
@ -32,6 +31,8 @@ class RoomCartRepository(val context: Context){
|
||||
InsertCartItem(appDatabase, cartItem).execute()
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun update(cartItem: CartItem) {
|
||||
cartItem.modifiedAt = AppUtils.currentDateTime.time
|
||||
UpdateCartItem(appDatabase, cartItem).execute()
|
||||
@ -42,6 +43,10 @@ class RoomCartRepository(val context: Context){
|
||||
return appDatabase.cartItemDao().count()
|
||||
}
|
||||
|
||||
fun exists(productId: Int): LiveData<Boolean> {
|
||||
return appDatabase.cartItemDao().exists(productId)
|
||||
}
|
||||
|
||||
fun delete(cartItem: CartItem) {
|
||||
DeleteCartItem(appDatabase, cartItem).execute()
|
||||
}
|
||||
@ -53,11 +58,12 @@ class RoomCartRepository(val context: Context){
|
||||
}
|
||||
}
|
||||
|
||||
fun items(): LiveData<List<CartItem>> {
|
||||
val data: MutableLiveData<List<CartItem>> = MutableLiveData()
|
||||
RetrieveCartItems(appDatabase, data).execute()
|
||||
|
||||
return data
|
||||
fun cart(): Cart {
|
||||
return Cart(
|
||||
items = Transformations.map(appDatabase.cartItemDao().items()){
|
||||
it.map { cartItemEntity -> cartItemEntity.toCartItem() }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
package me.gilo.moneta.room.task
|
||||
package me.gilo.woodroid.offlinecart.task
|
||||
|
||||
import android.os.AsyncTask
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import me.gilo.woodroid.core.cart.Cart
|
||||
import me.gilo.woodroid.core.cart.CartItem
|
||||
import me.gilo.woodroid.offlinecart.db.AppDatabase
|
||||
import me.gilo.woodroid.offlinecart.entity.toCartItem
|
||||
@ -23,28 +24,6 @@ class InsertCartItem(
|
||||
}
|
||||
}
|
||||
|
||||
class RetrieveCartItems(
|
||||
private val appDatabase: AppDatabase,
|
||||
|
||||
private val data: MutableLiveData<List<CartItem>>
|
||||
) :
|
||||
AsyncTask<Void, Void, Unit>() {
|
||||
|
||||
var categories = ArrayList<CartItem>()
|
||||
|
||||
override fun doInBackground(vararg voids: Void){
|
||||
val list = appDatabase.cartItemDao().fetchAll()
|
||||
for (item in list){
|
||||
categories.add(item.toCartItem())
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPostExecute(result: Unit?) {
|
||||
data.value = categories;
|
||||
super.onPostExecute(result)
|
||||
}
|
||||
}
|
||||
|
||||
class UpdateCartItem(
|
||||
private val appDatabase: AppDatabase,
|
||||
private val cartItem: CartItem
|
||||
|
||||
Loading…
Reference in New Issue
Block a user