added offline cart repo
This commit is contained in:
parent
00799f0830
commit
250b805126
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@ -15,6 +15,7 @@
|
||||
<option value="$PROJECT_DIR$/cocart" />
|
||||
<option value="$PROJECT_DIR$/core" />
|
||||
<option value="$PROJECT_DIR$/firebasecart" />
|
||||
<option value="$PROJECT_DIR$/offlinecart" />
|
||||
<option value="$PROJECT_DIR$/woodroid" />
|
||||
</set>
|
||||
</option>
|
||||
|
||||
@ -200,6 +200,9 @@ dependencies {
|
||||
implementation project(path: ':firebasecart')
|
||||
implementation project(path: ':core')
|
||||
implementation project(path: ':cocart')
|
||||
implementation project(path: ':offlinecart')
|
||||
|
||||
debugImplementation 'im.dino:dbinspector:3.4.1@aar'
|
||||
|
||||
implementation 'org.fabiomsr:moneytextview:1.1.0'
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ import dagger.Provides
|
||||
import me.gilo.woodroid.app.Config
|
||||
import me.gilo.woodroid.app.WcApp
|
||||
import me.gilo.woodroid.Woocommerce
|
||||
import me.gilo.woodroid.offlinecart.repo.RoomCartRepository
|
||||
|
||||
import javax.inject.Singleton
|
||||
|
||||
@ -35,4 +36,11 @@ class AppModule {
|
||||
.build()
|
||||
}
|
||||
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
internal fun providesRoomCartRepository(): RoomCartRepository = RoomCartRepository(app!!.baseContext)
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -7,14 +7,16 @@ import com.google.firebase.auth.FirebaseAuth
|
||||
import com.google.firebase.firestore.CollectionReference
|
||||
import com.google.firebase.firestore.DocumentReference
|
||||
import com.google.firebase.firestore.FirebaseFirestore
|
||||
import me.gilo.cocart.model.CartItem
|
||||
import me.gilo.woodroid.app.common.CompletionGenericLiveData
|
||||
import me.gilo.woodroid.app.common.QueryLiveData
|
||||
import me.gilo.woodroid.app.common.WooLiveData
|
||||
import me.gilo.woodroid.app.models.CartLineItem
|
||||
import me.gilo.woodroid.Woocommerce
|
||||
import me.gilo.woodroid.core.cart.Cart
|
||||
import me.gilo.woodroid.core.cart.CartItem
|
||||
|
||||
import me.gilo.woodroid.models.Product
|
||||
import me.gilo.woodroid.offlinecart.repo.RoomCartRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
open class CartRepository @Inject
|
||||
@ -72,33 +74,4 @@ constructor() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun addToCart(product: Product): CompletionGenericLiveData<DocumentReference> {
|
||||
val completion = CompletionGenericLiveData<DocumentReference>()
|
||||
|
||||
val lineItem = CartLineItem()
|
||||
lineItem.setProductId(product.id)
|
||||
lineItem.product = product
|
||||
lineItem.setQuantity(1)
|
||||
|
||||
cart.add(lineItem).addOnCompleteListener(completion)
|
||||
|
||||
return completion
|
||||
|
||||
}
|
||||
|
||||
fun addToCart(context: Context, productId: Int, quantity: Int): WooLiveData<CartItem> {
|
||||
val callBack = WooLiveData<CartItem>()
|
||||
woocommerce.CartRepository(context).addToCart(productId, quantity).enqueue(callBack)
|
||||
|
||||
return callBack
|
||||
}
|
||||
|
||||
fun cart(context: Context, customerId: String): WooLiveData<Map<String, CartItem>> {
|
||||
val callBack = WooLiveData<Map<String, CartItem>>()
|
||||
woocommerce.CartRepository(context).cart(customerId).enqueue(callBack)
|
||||
|
||||
return callBack
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ constructor() {
|
||||
fun create(category: Category): WooLiveData<Category> {
|
||||
val callBack = WooLiveData<Category>()
|
||||
|
||||
woocommerce!!.CategoryRepository().create(category).enqueue(callBack)
|
||||
woocommerce.CategoryRepository().create(category).enqueue(callBack)
|
||||
return callBack
|
||||
}
|
||||
|
||||
@ -26,42 +26,42 @@ constructor() {
|
||||
fun category(id: Int): WooLiveData<Category> {
|
||||
val callBack = WooLiveData<Category>()
|
||||
|
||||
woocommerce!!.CategoryRepository().category(id).enqueue(callBack)
|
||||
woocommerce.CategoryRepository().category(id).enqueue(callBack)
|
||||
return callBack
|
||||
}
|
||||
|
||||
fun categories(): WooLiveData<List<Category>> {
|
||||
val callBack = WooLiveData<List<Category>>()
|
||||
|
||||
woocommerce!!.CategoryRepository().categories().enqueue(callBack)
|
||||
woocommerce.CategoryRepository().categories().enqueue(callBack)
|
||||
return callBack
|
||||
}
|
||||
|
||||
fun categories(productCategoryFilter: ProductCategoryFilter): WooLiveData<List<Category>> {
|
||||
val callBack = WooLiveData<List<Category>>()
|
||||
|
||||
woocommerce!!.CategoryRepository().categories(productCategoryFilter).enqueue(callBack)
|
||||
woocommerce.CategoryRepository().categories(productCategoryFilter).enqueue(callBack)
|
||||
return callBack
|
||||
}
|
||||
|
||||
fun update(id: Int, category: Category): WooLiveData<Category> {
|
||||
val callBack = WooLiveData<Category>()
|
||||
|
||||
woocommerce!!.CategoryRepository().update(id, category).enqueue(callBack)
|
||||
woocommerce.CategoryRepository().update(id, category).enqueue(callBack)
|
||||
return callBack
|
||||
}
|
||||
|
||||
fun delete(id: Int): WooLiveData<Category> {
|
||||
val callBack = WooLiveData<Category>()
|
||||
|
||||
woocommerce!!.CategoryRepository().delete(id).enqueue(callBack)
|
||||
woocommerce.CategoryRepository().delete(id).enqueue(callBack)
|
||||
return callBack
|
||||
}
|
||||
|
||||
fun delete(id: Int, force: Boolean): WooLiveData<Category> {
|
||||
val callBack = WooLiveData<Category>()
|
||||
|
||||
woocommerce!!.CategoryRepository().delete(id, force).enqueue(callBack)
|
||||
woocommerce.CategoryRepository().delete(id, force).enqueue(callBack)
|
||||
return callBack
|
||||
}
|
||||
|
||||
|
||||
@ -23,7 +23,9 @@ import me.gilo.woodroid.app.models.CartLineItem
|
||||
import me.gilo.woodroid.app.ui.state.ProgressDialogFragment
|
||||
import me.gilo.woodroid.app.utils.AppUtils
|
||||
import me.gilo.woodroid.app.viewmodels.ProductViewModel
|
||||
import me.gilo.woodroid.core.cart.CartItem
|
||||
import me.gilo.woodroid.models.Product
|
||||
import me.gilo.woodroid.offlinecart.repo.RoomCartRepository
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
|
||||
|
||||
@ -57,57 +59,22 @@ class ProductActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
cart()
|
||||
viewCart(AppUtils(baseContext).cartSession)
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private fun addToCart(product: Product) {
|
||||
viewModel.addToCart(baseContext, product.id, 1).observe(this, Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
RoomCartRepository(baseContext).addToCart(
|
||||
CartItem(
|
||||
productId = product.id,
|
||||
productImage = product.getFeatureImage(),
|
||||
quantity = 1,
|
||||
productPrice = product.price
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
Status.SUCCESS -> {
|
||||
toast("success!")
|
||||
val cartItem = response.data()
|
||||
AppUtils(baseContext).saveCartSession(cartItem.key, "")
|
||||
}
|
||||
|
||||
Status.ERROR -> {
|
||||
toast("error : " + response.error().message)
|
||||
}
|
||||
|
||||
Status.EMPTY -> {
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun viewCart(customerId: String) {
|
||||
viewModel.cart(baseContext, customerId).observe(this, Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
|
||||
}
|
||||
|
||||
Status.SUCCESS -> {
|
||||
toast("success!")
|
||||
|
||||
}
|
||||
|
||||
Status.ERROR -> {
|
||||
toast("error : " + response.error().message)
|
||||
}
|
||||
|
||||
Status.EMPTY -> {
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
private fun removeFromCart(cartLineItem: CartLineItem) {
|
||||
|
||||
@ -137,26 +137,6 @@ class ShopActivity : BaseActivity() {
|
||||
|
||||
private fun cart() {
|
||||
val cartKey = AppUtils(baseContext).cartSession
|
||||
viewModel.cart(baseContext, cartKey).observe(this, androidx.lifecycle.Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
}
|
||||
|
||||
Status.SUCCESS -> {
|
||||
val cartResponse = response.data()
|
||||
}
|
||||
|
||||
Status.ERROR -> {
|
||||
|
||||
|
||||
}
|
||||
|
||||
Status.EMPTY -> {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
private fun search(query : String) {
|
||||
|
||||
@ -31,10 +31,6 @@ public final class CartViewModel extends ViewModel {
|
||||
this.customerRepository = customerRepository;
|
||||
}
|
||||
|
||||
public CompletionGenericLiveData<DocumentReference> addToCart(Product product) {
|
||||
return cartRepository.addToCart(product);
|
||||
}
|
||||
|
||||
public QueryLiveData<CartLineItem> cart() {
|
||||
return cartRepository.cart();
|
||||
}
|
||||
@ -51,10 +47,6 @@ public final class CartViewModel extends ViewModel {
|
||||
return cartRepository.setQuantity(cartLineItem, quantity);
|
||||
}
|
||||
|
||||
// public WooLiveData<Map<String, CartLineItem>> cart(Context context) {
|
||||
// return cartRepository.cart(context);
|
||||
// }
|
||||
|
||||
public WooLiveData<Order> createOrder(Order order) {
|
||||
return orderRepository.create(order);
|
||||
}
|
||||
|
||||
@ -2,10 +2,6 @@ package me.gilo.woodroid.app.viewmodels;
|
||||
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import android.content.Context;
|
||||
import com.google.firebase.firestore.DocumentReference;
|
||||
|
||||
import me.gilo.cocart.model.CartItem;
|
||||
import me.gilo.woodroid.app.common.CompletionGenericLiveData;
|
||||
import me.gilo.woodroid.app.common.QueryLiveData;
|
||||
import me.gilo.woodroid.app.common.WooLiveData;
|
||||
@ -13,7 +9,7 @@ import me.gilo.woodroid.app.models.CartLineItem;
|
||||
import me.gilo.woodroid.app.repo.CartRepository;
|
||||
import me.gilo.woodroid.app.repo.OrderRepository;
|
||||
import me.gilo.woodroid.app.repo.ProductRepository;
|
||||
import me.gilo.woodroid.models.LineItem;
|
||||
|
||||
import me.gilo.woodroid.models.Order;
|
||||
import me.gilo.woodroid.models.Product;
|
||||
import me.gilo.woodroid.models.ProductReview;
|
||||
@ -52,9 +48,6 @@ public final class ProductViewModel extends ViewModel {
|
||||
return productRepository.products();
|
||||
}
|
||||
|
||||
public CompletionGenericLiveData<DocumentReference> addToCart(Product product) {
|
||||
return cartRepository.addToCart(product);
|
||||
}
|
||||
|
||||
public WooLiveData<Order> addToCart(int productId) {
|
||||
return orderRepository.addToCart(productId);
|
||||
@ -76,14 +69,6 @@ public final class ProductViewModel extends ViewModel {
|
||||
return cartRepository.setQuantity(cartLineItem, quantity);
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, CartItem>> cart(Context context, String customerId) {
|
||||
return cartRepository.cart(context, customerId);
|
||||
}
|
||||
|
||||
public WooLiveData<CartItem> addToCart(Context context, int productId, int quantity) {
|
||||
return cartRepository.addToCart(context, productId, quantity);
|
||||
}
|
||||
|
||||
public WooLiveData<List<Product>> products(ProductFilter filter) {
|
||||
return productRepository.products(filter);
|
||||
}
|
||||
|
||||
16
core/src/main/java/me/gilo/woodroid/core/cart/CartItem.kt
Normal file
16
core/src/main/java/me/gilo/woodroid/core/cart/CartItem.kt
Normal file
@ -0,0 +1,16 @@
|
||||
package me.gilo.woodroid.core.cart
|
||||
|
||||
|
||||
data class CartItem (
|
||||
var id: Int? = 0,
|
||||
var createdAt: Long? = null,
|
||||
var modifiedAt: Long? = null,
|
||||
var productId: Int = 0,
|
||||
var variationId: Int? = 0,
|
||||
var quantity: Int = 0,
|
||||
|
||||
var productName: String? = "",
|
||||
var productPrice: String? = "",
|
||||
var productImage: String? = ""
|
||||
|
||||
)
|
||||
@ -8,10 +8,10 @@ abstract class CartRepository{
|
||||
|
||||
abstract fun clear(): LiveData<String>
|
||||
abstract fun count(id: Int): LiveData<Int>
|
||||
abstract fun cart(): LiveData<List<LineItem>>
|
||||
abstract fun addToCart(lineItem: LineItem): LiveData<LineItem>
|
||||
abstract fun cart(): LiveData<Cart>
|
||||
abstract fun addToCart(cartItem: CartItem)
|
||||
abstract fun delete(cartId: String): LiveData<String>
|
||||
abstract fun restore(cartId: String): LiveData<String>
|
||||
abstract fun update(cartId: String, quantity: Int): LiveData<LineItem>
|
||||
abstract fun update(cartId: String, quantity: Int)
|
||||
|
||||
}
|
||||
|
||||
@ -5,47 +5,17 @@ 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) : CartRepository() {
|
||||
open class FirebaseCartRepository(userId : String){
|
||||
|
||||
private val cart: CollectionReference = FirebaseFirestore.getInstance()
|
||||
.collection("users")
|
||||
.document(userId)
|
||||
.collection("cart")
|
||||
|
||||
|
||||
override fun cart(): LiveData<List<LineItem>> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
override fun clear(): LiveData<String> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
override fun count(id: Int): LiveData<Int> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
override fun addToCart(lineItem: LineItem): LiveData<LineItem> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
override fun delete(cartId: String): LiveData<String> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
override fun restore(cartId: String): LiveData<String> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
override fun update(cartId: String, quantity: Int): LiveData<LineItem> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
60
offlinecart/build.gradle
Normal file
60
offlinecart/build.gradle
Normal file
@ -0,0 +1,60 @@
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
apply plugin: 'kotlin-kapt'
|
||||
|
||||
|
||||
android {
|
||||
compileSdkVersion 29
|
||||
buildToolsVersion "29.0.2"
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 29
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles 'consumer-rules.pro'
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = '1.8'
|
||||
targetCompatibility = '1.8'
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||||
|
||||
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
|
||||
kapt "androidx.lifecycle:lifecycle-compiler:2.3.0-alpha01"
|
||||
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.2.0'
|
||||
implementation 'androidx.paging:paging-runtime-ktx:2.1.1'
|
||||
|
||||
// Room components
|
||||
implementation "androidx.room:room-runtime:2.2.4"
|
||||
kapt "androidx.room:room-compiler:2.2.4"
|
||||
androidTestImplementation "androidx.room:room-testing:2.2.4"
|
||||
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3"
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3"
|
||||
|
||||
implementation project(path: ':core')
|
||||
|
||||
|
||||
implementation 'androidx.core:core-ktx:1.2.0'
|
||||
testImplementation 'junit:junit:4.13'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
}
|
||||
0
offlinecart/consumer-rules.pro
Normal file
0
offlinecart/consumer-rules.pro
Normal file
21
offlinecart/proguard-rules.pro
vendored
Normal file
21
offlinecart/proguard-rules.pro
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@ -0,0 +1,24 @@
|
||||
package me.gilo.woodroid.offlinecart
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ExampleInstrumentedTest {
|
||||
@Test
|
||||
fun useAppContext() {
|
||||
// Context of the app under test.
|
||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
assertEquals("me.gilo.woodroid.offlinecart.test", appContext.packageName)
|
||||
}
|
||||
}
|
||||
2
offlinecart/src/main/AndroidManifest.xml
Normal file
2
offlinecart/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1,2 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="me.gilo.woodroid.offlinecart" />
|
||||
@ -0,0 +1,5 @@
|
||||
package me.gilo.woodroid.offlinecart.config;
|
||||
|
||||
public class Config {
|
||||
public static String DB_NAME = "db_woodroid_cart";
|
||||
}
|
||||
26
offlinecart/src/main/java/me/gilo/woodroid/offlinecart/dao/CartItemDao.kt
Executable file
26
offlinecart/src/main/java/me/gilo/woodroid/offlinecart/dao/CartItemDao.kt
Executable file
@ -0,0 +1,26 @@
|
||||
package me.gilo.woodroid.offlinecart.dao
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.room.*
|
||||
import me.gilo.woodroid.offlinecart.entity.CartItemEntity
|
||||
|
||||
@Dao
|
||||
interface CartItemDao {
|
||||
@Insert
|
||||
fun insert(category: CartItemEntity?): Long?
|
||||
|
||||
@Query("SELECT * FROM cart_item ORDER BY created_at desc")
|
||||
fun fetchAll(): List<CartItemEntity>
|
||||
|
||||
@Query("SELECT COUNT(*) FROM cart_item")
|
||||
fun count(): LiveData<Int>
|
||||
|
||||
@Query("SELECT * FROM cart_item WHERE id =:id")
|
||||
operator fun get(id: Int): LiveData<CartItemEntity>
|
||||
|
||||
@Update
|
||||
fun update(category: CartItemEntity?)
|
||||
|
||||
@Delete
|
||||
fun delete(category: CartItemEntity?)
|
||||
}
|
||||
15
offlinecart/src/main/java/me/gilo/woodroid/offlinecart/db/AppDatabase.kt
Executable file
15
offlinecart/src/main/java/me/gilo/woodroid/offlinecart/db/AppDatabase.kt
Executable file
@ -0,0 +1,15 @@
|
||||
package me.gilo.woodroid.offlinecart.db
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import me.gilo.woodroid.offlinecart.dao.CartItemDao
|
||||
import me.gilo.woodroid.offlinecart.entity.CartItemEntity
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
CartItemEntity::class
|
||||
], version = 1, exportSchema = false
|
||||
)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
abstract fun cartItemDao(): CartItemDao
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package me.gilo.woodroid.offlinecart.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import me.gilo.woodroid.core.cart.CartItem
|
||||
|
||||
@Entity(tableName = "cart_item")
|
||||
data class CartItemEntity (
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
var id: Int? = 0,
|
||||
|
||||
@ColumnInfo(name = "created_at")
|
||||
var createdAt: Long? = null,
|
||||
|
||||
@ColumnInfo(name = "modified_at")
|
||||
var modifiedAt: Long? = null,
|
||||
|
||||
@ColumnInfo(name = "product_id")
|
||||
var productId: Int = 0,
|
||||
|
||||
@ColumnInfo(name = "quantity")
|
||||
var quantity: Int = 0,
|
||||
|
||||
@ColumnInfo(name = "variation_id")
|
||||
var variationId: Int? = 0,
|
||||
|
||||
@ColumnInfo(name = "product_name")
|
||||
var productName: String? = "",
|
||||
|
||||
@ColumnInfo(name = "product_price")
|
||||
var productPrice: String? = "",
|
||||
|
||||
@ColumnInfo(name = "product_image")
|
||||
var productImage: String? = ""
|
||||
)
|
||||
|
||||
fun CartItemEntity.toCartItem() = CartItem(
|
||||
id = this.id,
|
||||
createdAt = this.createdAt,
|
||||
modifiedAt = this.modifiedAt,
|
||||
productId = this.productId,
|
||||
quantity = this.quantity,
|
||||
variationId = this.variationId,
|
||||
productName = this.productName,
|
||||
productPrice = this.productPrice,
|
||||
productImage = this.productImage
|
||||
)
|
||||
|
||||
fun CartItem.toCartItemEntity() = CartItemEntity(
|
||||
id = this.id,
|
||||
createdAt = this.createdAt,
|
||||
modifiedAt = this.modifiedAt,
|
||||
productId = this.productId,
|
||||
quantity = this.quantity,
|
||||
variationId = this.variationId,
|
||||
productName = this.productName,
|
||||
productPrice = this.productPrice,
|
||||
productImage = this.productImage
|
||||
)
|
||||
@ -0,0 +1,63 @@
|
||||
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.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
|
||||
|
||||
class RoomCartRepository(val context: Context){
|
||||
|
||||
private val appDatabase: AppDatabase = Room
|
||||
.databaseBuilder(context, AppDatabase::class.java, Config.DB_NAME)
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
|
||||
|
||||
fun addToCart(cartItem: CartItem) {
|
||||
if (cartItem.createdAt == null) {
|
||||
cartItem.createdAt = AppUtils.currentDateTime.time
|
||||
}
|
||||
|
||||
InsertCartItem(appDatabase, cartItem).execute()
|
||||
}
|
||||
|
||||
fun update(cartItem: CartItem) {
|
||||
cartItem.modifiedAt = AppUtils.currentDateTime.time
|
||||
UpdateCartItem(appDatabase, cartItem).execute()
|
||||
|
||||
}
|
||||
|
||||
fun count(): LiveData<Int> {
|
||||
return appDatabase.cartItemDao().count()
|
||||
}
|
||||
|
||||
fun delete(cartItem: CartItem) {
|
||||
DeleteCartItem(appDatabase, cartItem).execute()
|
||||
}
|
||||
|
||||
fun cartItem(id: Int): LiveData<CartItem> {
|
||||
return Transformations.map(
|
||||
appDatabase.cartItemDao().get(id)
|
||||
) { cartItemEntity: CartItemEntity? -> cartItemEntity?.toCartItem()
|
||||
}
|
||||
}
|
||||
|
||||
fun items(): LiveData<List<CartItem>> {
|
||||
val data: MutableLiveData<List<CartItem>> = MutableLiveData()
|
||||
RetrieveCartItems(appDatabase, data).execute()
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
package me.gilo.moneta.room.task
|
||||
|
||||
import android.os.AsyncTask
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import me.gilo.woodroid.core.cart.CartItem
|
||||
import me.gilo.woodroid.offlinecart.db.AppDatabase
|
||||
import me.gilo.woodroid.offlinecart.entity.toCartItem
|
||||
import me.gilo.woodroid.offlinecart.entity.toCartItemEntity
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
|
||||
class InsertCartItem(
|
||||
private val appDatabase: AppDatabase,
|
||||
private val cartItem: CartItem
|
||||
) :
|
||||
AsyncTask<Void, Void, Long?>() {
|
||||
|
||||
override fun doInBackground(vararg voids: Void): Long? {
|
||||
|
||||
return appDatabase.cartItemDao()
|
||||
.insert(cartItem.toCartItemEntity())
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
) :
|
||||
AsyncTask<Void, Void, Unit>() {
|
||||
|
||||
override fun doInBackground(vararg voids: Void) {
|
||||
return appDatabase.cartItemDao().update(cartItem.toCartItemEntity())
|
||||
}
|
||||
}
|
||||
|
||||
class DeleteCartItem(
|
||||
private val appDatabase: AppDatabase,
|
||||
private val cartItem: CartItem
|
||||
) :
|
||||
AsyncTask<Void, Void, Unit>() {
|
||||
|
||||
override fun doInBackground(vararg voids: Void) {
|
||||
return appDatabase.cartItemDao().delete(cartItem.toCartItemEntity())
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package me.gilo.woodroid.offlinecart.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.Toast
|
||||
import androidx.annotation.StringRes
|
||||
import java.text.ParseException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
class AppUtils(internal var context: Context) {
|
||||
internal var token: String? = null
|
||||
internal var expiry: String? = null
|
||||
|
||||
companion object {
|
||||
val currentDateTime: Date
|
||||
get() = Calendar.getInstance().time
|
||||
|
||||
fun getFormattedDateString(date: Date?): String? {
|
||||
try {
|
||||
var spf = SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy")
|
||||
val dateString = spf.format(date)
|
||||
val newDate = spf.parse(dateString)
|
||||
spf = SimpleDateFormat("dd MMM yyyy HH:mm:ss")
|
||||
return spf.format(newDate)
|
||||
} catch (e: ParseException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
19
offlinecart/src/main/java/me/gilo/woodroid/offlinecart/utils/Converters.kt
Executable file
19
offlinecart/src/main/java/me/gilo/woodroid/offlinecart/utils/Converters.kt
Executable file
@ -0,0 +1,19 @@
|
||||
package me.gilo.woodroid.offlinecart.utils
|
||||
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import java.util.*
|
||||
|
||||
class Converters {
|
||||
|
||||
@TypeConverter
|
||||
public fun fromTimestamp(value: Long?): Date? {
|
||||
return if (value == null) null else Date(value)
|
||||
}
|
||||
|
||||
@TypeConverter
|
||||
public fun dateToTimestamp(date: Date?): Long? {
|
||||
return date?.time
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package me.gilo.woodroid.offlinecart
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
class ExampleUnitTest {
|
||||
@Test
|
||||
fun addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2)
|
||||
}
|
||||
}
|
||||
@ -1,2 +1,3 @@
|
||||
include ':app', ':woodroid', ':firebasecart', ':core'
|
||||
include ':cocart'
|
||||
include ':offlinecart'
|
||||
|
||||
@ -68,6 +68,13 @@ class Product : Serializable {
|
||||
var menu_order: Int = 0
|
||||
lateinit var meta_data: ArrayList<Metadata>
|
||||
lateinit var images: ArrayList<Image>
|
||||
|
||||
fun getFeatureImage(): String{
|
||||
if(this.images.isEmpty()){
|
||||
return ""
|
||||
}
|
||||
|
||||
return this.images.first().src!!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user