Add to cart function using cocart
@ -15,6 +15,7 @@ import javax.inject.Singleton;
|
|||||||
AndroidSupportInjectionModule.class,
|
AndroidSupportInjectionModule.class,
|
||||||
ViewModelModule.class,
|
ViewModelModule.class,
|
||||||
ActivitiesModule.class,
|
ActivitiesModule.class,
|
||||||
|
AppModule.class
|
||||||
})
|
})
|
||||||
interface AppComponent extends AndroidInjector<DaggerApplication> {
|
interface AppComponent extends AndroidInjector<DaggerApplication> {
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package me.gilo.wc.di;
|
|||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
import dagger.Provides;
|
import dagger.Provides;
|
||||||
import me.gilo.wc.WcApp;
|
import me.gilo.wc.WcApp;
|
||||||
|
import me.gilo.woodroid.Woocommerce;
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
@ -21,4 +22,17 @@ public class AppModule {
|
|||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
Woocommerce providesWoocommerce() {
|
||||||
|
Woocommerce woocommerce = new Woocommerce.Builder()
|
||||||
|
.setSiteUrl("http://157.230.131.179")
|
||||||
|
.setApiVersion(Woocommerce.API_V3)
|
||||||
|
.setConsumerKey("ck_26c61abd7eeff238d87dc56585bf26cb2d1a1ec3")
|
||||||
|
.setConsumerSecret("cs_062e8e3a7ae0ce08fdebc0c39f8f834d5e87598e")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return woocommerce;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
40
app/src/main/java/me/gilo/wc/repo/CartRepository.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package me.gilo.wc.repo;
|
||||||
|
|
||||||
|
|
||||||
|
import me.gilo.wc.common.WooLiveData;
|
||||||
|
import me.gilo.woodroid.Woocommerce;
|
||||||
|
import me.gilo.woodroid.models.LineItem;
|
||||||
|
import me.gilo.woodroid.models.Order;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CartRepository {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Woocommerce woocommerce;
|
||||||
|
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public CartRepository() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public WooLiveData<Map<String, LineItem>> addToCart(int productId) {
|
||||||
|
final WooLiveData<Map<String, LineItem>> callBack = new WooLiveData();
|
||||||
|
|
||||||
|
LineItem lineItem = new LineItem();
|
||||||
|
lineItem.setProductId(productId);
|
||||||
|
lineItem.setQuantity(1);
|
||||||
|
|
||||||
|
woocommerce.CartRepository().addToCart(lineItem).enqueue(callBack);
|
||||||
|
return callBack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WooLiveData<Map<String, LineItem>> cart() {
|
||||||
|
final WooLiveData<Map<String, LineItem>> callBack = new WooLiveData();
|
||||||
|
woocommerce.CartRepository().cart().enqueue(callBack);
|
||||||
|
return callBack;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -2,14 +2,16 @@ package me.gilo.wc.repo;
|
|||||||
|
|
||||||
|
|
||||||
import me.gilo.wc.common.WooLiveData;
|
import me.gilo.wc.common.WooLiveData;
|
||||||
|
import me.gilo.woodroid.Woocommerce;
|
||||||
import me.gilo.woodroid.models.Order;
|
import me.gilo.woodroid.models.Order;
|
||||||
import me.gilo.woodroid.models.Product;
|
|
||||||
import me.gilo.woodroid.models.filters.ProductFilter;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class OrderRepository extends WoocommerceRepository {
|
public class OrderRepository {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Woocommerce woocommerce;
|
||||||
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public OrderRepository() {
|
public OrderRepository() {
|
||||||
|
|||||||
@ -2,17 +2,20 @@ package me.gilo.wc.repo;
|
|||||||
|
|
||||||
|
|
||||||
import me.gilo.wc.common.WooLiveData;
|
import me.gilo.wc.common.WooLiveData;
|
||||||
|
import me.gilo.woodroid.Woocommerce;
|
||||||
import me.gilo.woodroid.models.Product;
|
import me.gilo.woodroid.models.Product;
|
||||||
import me.gilo.woodroid.models.filters.ProductFilter;
|
import me.gilo.woodroid.models.filters.ProductFilter;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ProductRepository extends WoocommerceRepository {
|
public class ProductRepository {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Woocommerce woocommerce;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ProductRepository() {
|
public ProductRepository() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public WooLiveData<List<Product>> products() {
|
public WooLiveData<List<Product>> products() {
|
||||||
|
|||||||
@ -1,13 +0,0 @@
|
|||||||
package me.gilo.wc.repo;
|
|
||||||
|
|
||||||
import me.gilo.woodroid.Woocommerce;
|
|
||||||
|
|
||||||
public class WoocommerceRepository {
|
|
||||||
|
|
||||||
Woocommerce woocommerce = new Woocommerce.Builder()
|
|
||||||
.setSiteUrl("http://157.230.131.179")
|
|
||||||
.setApiVersion(Woocommerce.API_V3)
|
|
||||||
.setConsumerKey("ck_26c61abd7eeff238d87dc56585bf26cb2d1a1ec3")
|
|
||||||
.setConsumerSecret("cs_062e8e3a7ae0ce08fdebc0c39f8f834d5e87598e")
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
@ -54,6 +54,7 @@ class ShopActivity : BaseActivity() {
|
|||||||
rvShop.adapter = adapter
|
rvShop.adapter = adapter
|
||||||
|
|
||||||
products()
|
products()
|
||||||
|
cart()
|
||||||
|
|
||||||
bFilter.setOnClickListener{filter()}
|
bFilter.setOnClickListener{filter()}
|
||||||
}
|
}
|
||||||
@ -117,6 +118,30 @@ class ShopActivity : BaseActivity() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun cart() {
|
||||||
|
viewModel.cart().observe(this, android.arch.lifecycle.Observer { response ->
|
||||||
|
when (response!!.status()) {
|
||||||
|
Status.LOADING -> {
|
||||||
|
}
|
||||||
|
|
||||||
|
Status.SUCCESS -> {
|
||||||
|
val cartResponse = response.data()
|
||||||
|
toast("Cart items are " + cartResponse.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
Status.ERROR -> {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Status.EMPTY -> {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
private fun search(query : String) {
|
private fun search(query : String) {
|
||||||
viewModel.search(query).observe(this, android.arch.lifecycle.Observer { response ->
|
viewModel.search(query).observe(this, android.arch.lifecycle.Observer { response ->
|
||||||
when (response!!.status()) {
|
when (response!!.status()) {
|
||||||
|
|||||||
@ -2,32 +2,44 @@ package me.gilo.wc.viewmodels;
|
|||||||
|
|
||||||
import android.arch.lifecycle.ViewModel;
|
import android.arch.lifecycle.ViewModel;
|
||||||
import me.gilo.wc.common.WooLiveData;
|
import me.gilo.wc.common.WooLiveData;
|
||||||
|
import me.gilo.wc.repo.CartRepository;
|
||||||
import me.gilo.wc.repo.OrderRepository;
|
import me.gilo.wc.repo.OrderRepository;
|
||||||
import me.gilo.wc.repo.ProductRepository;
|
import me.gilo.wc.repo.ProductRepository;
|
||||||
|
import me.gilo.woodroid.models.LineItem;
|
||||||
import me.gilo.woodroid.models.Order;
|
import me.gilo.woodroid.models.Order;
|
||||||
import me.gilo.woodroid.models.Product;
|
import me.gilo.woodroid.models.Product;
|
||||||
import me.gilo.woodroid.models.filters.ProductFilter;
|
import me.gilo.woodroid.models.filters.ProductFilter;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
public final class ProductViewModel extends ViewModel {
|
public final class ProductViewModel extends ViewModel {
|
||||||
private final ProductRepository productRepository;
|
private final ProductRepository productRepository;
|
||||||
private final OrderRepository orderRepository;
|
private final OrderRepository orderRepository;
|
||||||
|
private final CartRepository cartRepository;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ProductViewModel(ProductRepository productRepository, OrderRepository orderRepository) {
|
ProductViewModel(ProductRepository productRepository,
|
||||||
|
OrderRepository orderRepository,
|
||||||
|
CartRepository cartRepository
|
||||||
|
) {
|
||||||
this.productRepository = productRepository;
|
this.productRepository = productRepository;
|
||||||
this.orderRepository = orderRepository;
|
this.orderRepository = orderRepository;
|
||||||
|
this.cartRepository = cartRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WooLiveData<List<Product>> products() {
|
public WooLiveData<List<Product>> products() {
|
||||||
return productRepository.products();
|
return productRepository.products();
|
||||||
}
|
}
|
||||||
|
|
||||||
public WooLiveData<Order> addToCart(int productId) {
|
public WooLiveData<Map<String, LineItem>> addToCart(int productId) {
|
||||||
return orderRepository.addToCart(productId);
|
return cartRepository.addToCart(productId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WooLiveData<Map<String, LineItem>> cart() {
|
||||||
|
return cartRepository.cart();
|
||||||
}
|
}
|
||||||
|
|
||||||
public WooLiveData<List<Product>> products(ProductFilter filter) {
|
public WooLiveData<List<Product>> products(ProductFilter filter) {
|
||||||
|
|||||||
0
app/src/main/res/drawable-hdpi/baseline_sort_by_alpha_white_18.png
Normal file → Executable file
|
Before Width: | Height: | Size: 345 B After Width: | Height: | Size: 345 B |
0
app/src/main/res/drawable-hdpi/baseline_sort_by_alpha_white_24.png
Normal file → Executable file
|
Before Width: | Height: | Size: 424 B After Width: | Height: | Size: 424 B |
0
app/src/main/res/drawable-hdpi/baseline_sort_by_alpha_white_36.png
Normal file → Executable file
|
Before Width: | Height: | Size: 573 B After Width: | Height: | Size: 573 B |
0
app/src/main/res/drawable-hdpi/baseline_sort_by_alpha_white_48.png
Normal file → Executable file
|
Before Width: | Height: | Size: 767 B After Width: | Height: | Size: 767 B |
0
app/src/main/res/drawable-mdpi/baseline_sort_by_alpha_white_18.png
Normal file → Executable file
|
Before Width: | Height: | Size: 273 B After Width: | Height: | Size: 273 B |
0
app/src/main/res/drawable-mdpi/baseline_sort_by_alpha_white_24.png
Normal file → Executable file
|
Before Width: | Height: | Size: 329 B After Width: | Height: | Size: 329 B |
0
app/src/main/res/drawable-mdpi/baseline_sort_by_alpha_white_36.png
Normal file → Executable file
|
Before Width: | Height: | Size: 424 B After Width: | Height: | Size: 424 B |
0
app/src/main/res/drawable-mdpi/baseline_sort_by_alpha_white_48.png
Normal file → Executable file
|
Before Width: | Height: | Size: 567 B After Width: | Height: | Size: 567 B |
0
app/src/main/res/drawable-xhdpi/baseline_sort_by_alpha_white_18.png
Normal file → Executable file
|
Before Width: | Height: | Size: 424 B After Width: | Height: | Size: 424 B |
0
app/src/main/res/drawable-xhdpi/baseline_sort_by_alpha_white_24.png
Normal file → Executable file
|
Before Width: | Height: | Size: 567 B After Width: | Height: | Size: 567 B |
0
app/src/main/res/drawable-xhdpi/baseline_sort_by_alpha_white_36.png
Normal file → Executable file
|
Before Width: | Height: | Size: 767 B After Width: | Height: | Size: 767 B |
0
app/src/main/res/drawable-xhdpi/baseline_sort_by_alpha_white_48.png
Normal file → Executable file
|
Before Width: | Height: | Size: 909 B After Width: | Height: | Size: 909 B |
0
app/src/main/res/drawable-xxhdpi/baseline_sort_by_alpha_white_18.png
Normal file → Executable file
|
Before Width: | Height: | Size: 573 B After Width: | Height: | Size: 573 B |
0
app/src/main/res/drawable-xxhdpi/baseline_sort_by_alpha_white_24.png
Normal file → Executable file
|
Before Width: | Height: | Size: 767 B After Width: | Height: | Size: 767 B |
0
app/src/main/res/drawable-xxhdpi/baseline_sort_by_alpha_white_36.png
Normal file → Executable file
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
0
app/src/main/res/drawable-xxhdpi/baseline_sort_by_alpha_white_48.png
Normal file → Executable file
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
0
app/src/main/res/drawable-xxxhdpi/baseline_sort_by_alpha_white_18.png
Normal file → Executable file
|
Before Width: | Height: | Size: 767 B After Width: | Height: | Size: 767 B |
0
app/src/main/res/drawable-xxxhdpi/baseline_sort_by_alpha_white_24.png
Normal file → Executable file
|
Before Width: | Height: | Size: 909 B After Width: | Height: | Size: 909 B |
0
app/src/main/res/drawable-xxxhdpi/baseline_sort_by_alpha_white_36.png
Normal file → Executable file
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
0
app/src/main/res/drawable-xxxhdpi/baseline_sort_by_alpha_white_48.png
Normal file → Executable file
|
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 1.7 KiB |
10
app/src/main/res/drawable/baseline_sort_by_alpha_24.xml
Executable file
@ -0,0 +1,10 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24.0"
|
||||||
|
android:viewportHeight="24.0"
|
||||||
|
android:tint="?attr/colorControlNormal">
|
||||||
|
<path
|
||||||
|
android:fillColor="#ffffff"
|
||||||
|
android:pathData="M14.94,4.66h-4.72l2.36,-2.36zM10.25,19.37h4.66l-2.33,2.33zM6.1,6.27L1.6,17.73h1.84l0.92,-2.45h5.11l0.92,2.45h1.84L7.74,6.27L6.1,6.27zM4.97,13.64l1.94,-5.18 1.94,5.18L4.97,13.64zM15.73,16.14h6.12v1.59h-8.53v-1.29l5.92,-8.56h-5.88v-1.6h8.3v1.26l-5.93,8.6z"/>
|
||||||
|
</vector>
|
||||||
@ -8,7 +8,7 @@ buildscript {
|
|||||||
|
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:3.4.0-alpha03'
|
classpath 'com.android.tools.build:gradle:3.5.0-alpha05'
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
// NOTE: Do not place your application dependencies here; they belong
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
// in the individual module build.gradle files
|
// in the individual module build.gradle files
|
||||||
|
|||||||
4
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,6 @@
|
|||||||
#Sun Jan 27 08:51:51 EAT 2019
|
#Fri Mar 01 19:09:10 EAT 2019
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-milestone-1-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
|
||||||
|
|||||||
@ -28,6 +28,8 @@ public class Woocommerce {
|
|||||||
|
|
||||||
final ReportsRepository reportsRepository;
|
final ReportsRepository reportsRepository;
|
||||||
|
|
||||||
|
final CartRepository cartRepository;
|
||||||
|
|
||||||
|
|
||||||
enum ApiVersion {
|
enum ApiVersion {
|
||||||
API_VERSION1{
|
API_VERSION1{
|
||||||
@ -53,6 +55,8 @@ public class Woocommerce {
|
|||||||
public Woocommerce(String siteUrl, ApiVersion apiVerion, String consumerKey, String consumerSecret) {
|
public Woocommerce(String siteUrl, ApiVersion apiVerion, String consumerKey, String consumerSecret) {
|
||||||
String baseUrl = siteUrl + "/wp-json/wc/v" + apiVerion + "/";
|
String baseUrl = siteUrl + "/wp-json/wc/v" + apiVerion + "/";
|
||||||
|
|
||||||
|
String cartBaseUrl = siteUrl + "/wp-json/wc/v" + 2 + "/";
|
||||||
|
|
||||||
orderNoteRepository = new OrderNoteRepository(baseUrl, consumerKey, consumerSecret);
|
orderNoteRepository = new OrderNoteRepository(baseUrl, consumerKey, consumerSecret);
|
||||||
refundRepository = new RefundRepository(baseUrl, consumerKey, consumerSecret);
|
refundRepository = new RefundRepository(baseUrl, consumerKey, consumerSecret);
|
||||||
attributeRepository = new AttributeRepository(baseUrl, consumerKey, consumerSecret);
|
attributeRepository = new AttributeRepository(baseUrl, consumerKey, consumerSecret);
|
||||||
@ -68,6 +72,8 @@ public class Woocommerce {
|
|||||||
|
|
||||||
reportsRepository = new ReportsRepository(baseUrl, consumerKey, consumerSecret);
|
reportsRepository = new ReportsRepository(baseUrl, consumerKey, consumerSecret);
|
||||||
|
|
||||||
|
cartRepository = new CartRepository(cartBaseUrl, consumerKey, consumerSecret);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -158,4 +164,8 @@ public class Woocommerce {
|
|||||||
public ReportsRepository ReportsRepository() {
|
public ReportsRepository ReportsRepository() {
|
||||||
return reportsRepository;
|
return reportsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CartRepository CartRepository() {
|
||||||
|
return cartRepository;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,40 @@
|
|||||||
|
package me.gilo.woodroid.data.api;
|
||||||
|
|
||||||
|
|
||||||
|
import me.gilo.woodroid.models.Coupon;
|
||||||
|
import me.gilo.woodroid.models.LineItem;
|
||||||
|
import me.gilo.woodroid.models.filters.CartFilter;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.http.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface CartAPI {
|
||||||
|
|
||||||
|
@Headers("Content-Type: application/json")
|
||||||
|
@POST("clear")
|
||||||
|
Call<String> clear();
|
||||||
|
|
||||||
|
@GET("count-items")
|
||||||
|
Call<Integer> count();
|
||||||
|
|
||||||
|
@GET("cart")
|
||||||
|
Call<Map<String, LineItem>> list();
|
||||||
|
|
||||||
|
@Headers("Content-Type: application/json")
|
||||||
|
@POST("cart/add")
|
||||||
|
Call<Map<String, LineItem>> addToCart(@Body LineItem body);
|
||||||
|
|
||||||
|
@DELETE("cart/cart-item")
|
||||||
|
Call<String> delete(@Body CartFilter body);
|
||||||
|
|
||||||
|
@GET("cart/cart-item")
|
||||||
|
Call<String> restore(@Body CartFilter body);
|
||||||
|
|
||||||
|
@Headers("Content-Type: application/json")
|
||||||
|
@POST("cart/cart-item")
|
||||||
|
Call<String> update(@Body CartFilter body);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
package me.gilo.woodroid.models.filters;
|
||||||
|
|
||||||
|
public class CartFilter {
|
||||||
|
|
||||||
|
String cart_item_key;
|
||||||
|
int quantity;
|
||||||
|
|
||||||
|
public CartFilter(String cart_item_key) {
|
||||||
|
this.cart_item_key = cart_item_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCart_item_key() {
|
||||||
|
return cart_item_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCart_item_key(String cart_item_key) {
|
||||||
|
this.cart_item_key = cart_item_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getQuantity() {
|
||||||
|
return quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQuantity(int quantity) {
|
||||||
|
this.quantity = quantity;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,80 @@
|
|||||||
|
package me.gilo.woodroid.repo;
|
||||||
|
|
||||||
|
import me.gilo.woodroid.data.api.CartAPI;
|
||||||
|
import me.gilo.woodroid.data.api.CouponAPI;
|
||||||
|
import me.gilo.woodroid.data.auth.AuthIntercepter;
|
||||||
|
import me.gilo.woodroid.models.Coupon;
|
||||||
|
import me.gilo.woodroid.models.LineItem;
|
||||||
|
import me.gilo.woodroid.models.filters.CartFilter;
|
||||||
|
import me.gilo.woodroid.models.filters.CouponFilter;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class CartRepository{
|
||||||
|
|
||||||
|
private final CartAPI apiService;
|
||||||
|
Retrofit retrofit;
|
||||||
|
|
||||||
|
public CartRepository(String baseUrl, String consumerKey, String consumerSecret) {
|
||||||
|
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
|
||||||
|
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
|
.addInterceptor(loggingInterceptor)
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.connectTimeout(15, TimeUnit.SECONDS)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
retrofit = new Retrofit.Builder()
|
||||||
|
.baseUrl(baseUrl)
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.client(client)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
apiService = retrofit.create(CartAPI.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<String> clear() {
|
||||||
|
return apiService.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Call<Integer> count(int id) {
|
||||||
|
return apiService.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Map<String, LineItem>> cart() {
|
||||||
|
return apiService.list();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Map<String, LineItem>> addToCart(LineItem lineItem) {
|
||||||
|
return apiService.addToCart(lineItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<String> delete(String cardId) {
|
||||||
|
CartFilter cartFilter = new CartFilter(cardId);
|
||||||
|
return apiService.delete(cartFilter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<String> restore(String cardId) {
|
||||||
|
CartFilter cartFilter = new CartFilter(cardId);
|
||||||
|
return apiService.restore(cartFilter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<String> update(String cardId, int quantity) {
|
||||||
|
CartFilter cartFilter = new CartFilter(cardId);
|
||||||
|
cartFilter.setQuantity(quantity);
|
||||||
|
|
||||||
|
return apiService.update(cartFilter);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||