commit
00799f0830
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@ -12,6 +12,7 @@
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/app" />
|
||||
<option value="$PROJECT_DIR$/cocart" />
|
||||
<option value="$PROJECT_DIR$/core" />
|
||||
<option value="$PROJECT_DIR$/firebasecart" />
|
||||
<option value="$PROJECT_DIR$/woodroid" />
|
||||
|
||||
@ -199,6 +199,7 @@ dependencies {
|
||||
implementation project(path: ':woodroid')
|
||||
implementation project(path: ':firebasecart')
|
||||
implementation project(path: ':core')
|
||||
implementation project(path: ':cocart')
|
||||
|
||||
implementation 'org.fabiomsr:moneytextview:1.1.0'
|
||||
|
||||
|
||||
@ -7,12 +7,12 @@ 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.models.LineItem
|
||||
|
||||
import me.gilo.woodroid.models.Product
|
||||
import javax.inject.Inject
|
||||
@ -87,9 +87,16 @@ constructor() {
|
||||
|
||||
}
|
||||
|
||||
fun cart(context: Context): WooLiveData<Map<String, LineItem>> {
|
||||
val callBack = WooLiveData<Map<String, LineItem>>()
|
||||
woocommerce.CartRepository(context).cart().enqueue(callBack)
|
||||
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
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@ import android.widget.FrameLayout
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.Observer
|
||||
import kotlinx.android.synthetic.main.activity_product.*
|
||||
import kotlinx.android.synthetic.main.content_product.*
|
||||
import me.gilo.woodroid.app.R
|
||||
@ -20,6 +21,7 @@ import me.gilo.woodroid.app.common.Status
|
||||
import me.gilo.woodroid.app.events.ProductEvent
|
||||
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.models.Product
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
@ -55,32 +57,56 @@ class ProductActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
cart()
|
||||
viewCart(AppUtils(baseContext).cartSession)
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private fun addToCart(product: Product) {
|
||||
viewModel.addToCart(product.id).observe(this, androidx.lifecycle.Observer { response ->
|
||||
viewModel.addToCart(baseContext, product.id, 1).observe(this, Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
|
||||
}
|
||||
|
||||
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 -> {
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -16,6 +16,7 @@ import me.gilo.woodroid.app.adapter.ProductAdapter
|
||||
import me.gilo.woodroid.app.common.BaseActivity
|
||||
import me.gilo.woodroid.app.common.Status
|
||||
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.models.Product
|
||||
import me.gilo.woodroid.models.filters.ProductFilter
|
||||
@ -135,7 +136,8 @@ class ShopActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun cart() {
|
||||
viewModel.cart(baseContext).observe(this, androidx.lifecycle.Observer { response ->
|
||||
val cartKey = AppUtils(baseContext).cartSession
|
||||
viewModel.cart(baseContext, cartKey).observe(this, androidx.lifecycle.Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ public class AppUtils {
|
||||
|
||||
public String getCartSession() {
|
||||
SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, context.MODE_PRIVATE);
|
||||
return prefs.getString("cartSession", null);
|
||||
return prefs.getString("cartSession", "");
|
||||
}
|
||||
|
||||
public String getExpiry() {
|
||||
|
||||
@ -51,9 +51,9 @@ public final class CartViewModel extends ViewModel {
|
||||
return cartRepository.setQuantity(cartLineItem, quantity);
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> cart(Context context) {
|
||||
return cartRepository.cart(context);
|
||||
}
|
||||
// public WooLiveData<Map<String, CartLineItem>> cart(Context context) {
|
||||
// return cartRepository.cart(context);
|
||||
// }
|
||||
|
||||
public WooLiveData<Order> createOrder(Order order) {
|
||||
return orderRepository.create(order);
|
||||
|
||||
@ -4,6 +4,8 @@ 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;
|
||||
@ -74,8 +76,12 @@ public final class ProductViewModel extends ViewModel {
|
||||
return cartRepository.setQuantity(cartLineItem, quantity);
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> cart(Context context) {
|
||||
return cartRepository.cart(context);
|
||||
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) {
|
||||
|
||||
50
cocart/build.gradle
Normal file
50
cocart/build.gradle
Normal file
@ -0,0 +1,50 @@
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
compileSdkVersion 29
|
||||
|
||||
|
||||
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'
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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.core:core-ktx:1.1.0'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
|
||||
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
|
||||
|
||||
implementation 'com.jakewharton.retrofit:retrofit1-okhttp3-client:1.1.0'
|
||||
|
||||
implementation 'io.reactivex:rxandroid:1.2.1'
|
||||
implementation 'io.reactivex:rxjava:1.3.0'
|
||||
|
||||
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
|
||||
|
||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
|
||||
|
||||
|
||||
}
|
||||
0
cocart/consumer-rules.pro
Normal file
0
cocart/consumer-rules.pro
Normal file
21
cocart/proguard-rules.pro
vendored
Normal file
21
cocart/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.cocart
|
||||
|
||||
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.cocart.test", appContext.packageName)
|
||||
}
|
||||
}
|
||||
2
cocart/src/main/AndroidManifest.xml
Normal file
2
cocart/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1,2 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="me.gilo.cocart" />
|
||||
@ -0,0 +1,36 @@
|
||||
package me.gilo.cocart.callback
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
|
||||
import java.io.IOException
|
||||
|
||||
class CallBackLiveData<T> : LiveData<Resource<T>>(), Callback<T> {
|
||||
init {
|
||||
value = Resource(Status.LOADING)
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call<T>, response: Response<T>) {
|
||||
if (response.isSuccessful) {
|
||||
setValue(Resource(response.body()!!))
|
||||
} else {
|
||||
var error: String? = null
|
||||
try {
|
||||
error = response.errorBody()!!.string()
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
if (error == null) {
|
||||
error = "Something went wrong"
|
||||
}
|
||||
setValue(Resource(NetworkException(error)))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<T>, t: Throwable) {
|
||||
value = Resource(NetworkException(t))
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package me.gilo.cocart.callback
|
||||
|
||||
class NetworkException : Exception {
|
||||
|
||||
constructor() : super() {}
|
||||
|
||||
constructor(message: String) : super(message) {}
|
||||
|
||||
constructor(message: String, cause: Throwable) : super(message, cause) {}
|
||||
|
||||
constructor(cause: Throwable) : super(cause) {}
|
||||
}
|
||||
58
cocart/src/main/java/me/gilo/cocart/callback/Resource.kt
Executable file
58
cocart/src/main/java/me/gilo/cocart/callback/Resource.kt
Executable file
@ -0,0 +1,58 @@
|
||||
package me.gilo.cocart.callback
|
||||
|
||||
|
||||
class Resource<T> private constructor(private val data: T?, private val error: Exception?) {
|
||||
internal var status = Status.LOADING
|
||||
|
||||
val isSuccessful: Boolean
|
||||
get() = data != null && error == null
|
||||
|
||||
constructor(data: T) : this(data, null) {}
|
||||
|
||||
constructor(status: Status) : this(null, null) {
|
||||
this.status = status
|
||||
}
|
||||
|
||||
constructor(exception: Exception) : this(null, exception) {
|
||||
this.status = Status.ERROR
|
||||
}
|
||||
|
||||
init {
|
||||
|
||||
if (error != null) {
|
||||
status = Status.ERROR
|
||||
} else if (data != null) {
|
||||
if (data is List<*>) {
|
||||
if ((data as List<*>).size == 0) {
|
||||
status = Status.EMPTY
|
||||
} else {
|
||||
status = Status.SUCCESS
|
||||
}
|
||||
} else {
|
||||
status = Status.SUCCESS
|
||||
}
|
||||
} else {
|
||||
status = Status.LOADING
|
||||
}
|
||||
}
|
||||
|
||||
fun data(): T? {
|
||||
if (error != null) {
|
||||
throw IllegalStateException("error is not null. Call isSuccessful() first.")
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
fun error(): Exception? {
|
||||
if (data != null) {
|
||||
throw IllegalStateException("data is not null. Call isSuccessful() first.")
|
||||
}
|
||||
return error
|
||||
}
|
||||
|
||||
fun status(): Status {
|
||||
return status
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
cocart/src/main/java/me/gilo/cocart/callback/Status.kt
Normal file
11
cocart/src/main/java/me/gilo/cocart/callback/Status.kt
Normal file
@ -0,0 +1,11 @@
|
||||
package me.gilo.cocart.callback
|
||||
|
||||
enum class Status {
|
||||
EMPTY,
|
||||
SUCCESS,
|
||||
ERROR,
|
||||
LOADING;
|
||||
|
||||
val isLoading: Status
|
||||
get() = LOADING
|
||||
}
|
||||
5
cocart/src/main/java/me/gilo/cocart/callback/WooCall.kt
Normal file
5
cocart/src/main/java/me/gilo/cocart/callback/WooCall.kt
Normal file
@ -0,0 +1,5 @@
|
||||
package me.gilo.cocart.callback
|
||||
|
||||
import retrofit2.Call
|
||||
|
||||
interface WooCall<T> : Call<T>
|
||||
19
cocart/src/main/java/me/gilo/cocart/data/ApiVersion.kt
Normal file
19
cocart/src/main/java/me/gilo/cocart/data/ApiVersion.kt
Normal file
@ -0,0 +1,19 @@
|
||||
package me.gilo.cocart.data
|
||||
|
||||
enum class ApiVersion {
|
||||
API_VERSION1 {
|
||||
override fun toString(): String {
|
||||
return "1"
|
||||
}
|
||||
},
|
||||
API_VERSION2 {
|
||||
override fun toString(): String {
|
||||
return "2"
|
||||
}
|
||||
},
|
||||
API_VERSION3 {
|
||||
override fun toString(): String {
|
||||
return "3"
|
||||
}
|
||||
}
|
||||
}
|
||||
17
cocart/src/main/java/me/gilo/cocart/data/api/CartAPI.kt
Normal file
17
cocart/src/main/java/me/gilo/cocart/data/api/CartAPI.kt
Normal file
@ -0,0 +1,17 @@
|
||||
package me.gilo.cocart.data.api
|
||||
|
||||
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.*
|
||||
|
||||
interface CartAPI {
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
@POST("clear")
|
||||
fun clear(): Call<String>
|
||||
|
||||
@GET("count-items")
|
||||
fun count(): Call<Int>
|
||||
|
||||
|
||||
}
|
||||
17
cocart/src/main/java/me/gilo/cocart/data/api/FiltersAPI.kt
Normal file
17
cocart/src/main/java/me/gilo/cocart/data/api/FiltersAPI.kt
Normal file
@ -0,0 +1,17 @@
|
||||
package me.gilo.cocart.data.api
|
||||
|
||||
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.*
|
||||
|
||||
interface FiltersAPI {
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
@POST("clear")
|
||||
fun clear(): Call<String>
|
||||
|
||||
@GET("count-items")
|
||||
fun count(): Call<Int>
|
||||
|
||||
|
||||
}
|
||||
40
cocart/src/main/java/me/gilo/cocart/data/api/ItemsAPI.kt
Normal file
40
cocart/src/main/java/me/gilo/cocart/data/api/ItemsAPI.kt
Normal file
@ -0,0 +1,40 @@
|
||||
package me.gilo.cocart.data.api
|
||||
|
||||
|
||||
import me.gilo.cocart.data.requests.CartItemRequest
|
||||
import me.gilo.cocart.data.requests.CartRequest
|
||||
import me.gilo.cocart.model.CartItem
|
||||
import me.gilo.cocart.model.CartTotal
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.*
|
||||
|
||||
interface ItemsAPI {
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
@POST("add-item")
|
||||
fun addToCart(@Body body: CartItemRequest): Call<CartItem>
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
@GET("get-cart")
|
||||
fun list( @Query("thumb") thumb: Boolean = true): Call<Map<String, CartItem>>
|
||||
|
||||
@POST("get-cart/saved")
|
||||
fun getCustomerCart(@Body body: CartRequest): Call<Map<String, CartItem>>
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
@POST("clear")
|
||||
fun clear(): Call<String>
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
@GET("count-items")
|
||||
fun count(): Call<Int>
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
@POST("calculate")
|
||||
fun calculate(@Query("return") returnTotal: Boolean = true): Call<String>
|
||||
|
||||
@Headers("Content-Type: application/json")
|
||||
@GET("totals")
|
||||
fun totals(@Query("html") returnTotal: Boolean = true): Call<CartTotal>
|
||||
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package me.gilo.woodroid.data.cookie
|
||||
|
||||
import android.content.Context
|
||||
import android.preference.PreferenceManager
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
|
||||
import java.io.IOException
|
||||
import java.util.HashSet
|
||||
|
||||
class AddCookiesInterceptor(private val context: Context) : Interceptor {
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val builder = chain.request().newBuilder()
|
||||
|
||||
val preferences = PreferenceManager.getDefaultSharedPreferences(context).getStringSet(
|
||||
PREF_COOKIES,
|
||||
HashSet()
|
||||
) as HashSet<String>
|
||||
for (cookie in preferences) {
|
||||
builder.addHeader("Cookie", cookie)
|
||||
}
|
||||
|
||||
return chain.proceed(builder.build())
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
val PREF_COOKIES = "PREF_COOKIES"
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package me.gilo.woodroid.data.cookie
|
||||
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
|
||||
import java.io.IOException
|
||||
|
||||
class DemoCookieInterceptor : Interceptor {
|
||||
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val builder = chain.request().newBuilder()
|
||||
|
||||
builder.addHeader(
|
||||
"Cookie",
|
||||
"wordpress_logged_in_9073cf57240df660c1e240d327cc46cb=gilo%7C1551794958%7CJx1NTnn0f6wuYlN5a0PmTJxYcPlr1sUUqpr659EKCcG%7C802c643a30a82bf7aa6350b5fb5dd005c019b2e1b1d59566ef0c426e33126eae"
|
||||
)
|
||||
builder.addHeader("Cookie", "woocommerce_cart_hash=d9ec6c9bf0d307629c2a981362735284")
|
||||
builder.addHeader(
|
||||
"Cookie",
|
||||
"wp_woocommerce_session_9073cf57240df660c1e240d327cc46cb=1%7C%7C1551796439%7C%7C1551792839%7C%7Ce6deec897575a9a84bb4a672abf2ed72"
|
||||
)
|
||||
|
||||
|
||||
|
||||
return chain.proceed(builder.build())
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package me.gilo.woodroid.data.cookie
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.preference.PreferenceManager
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
|
||||
import java.io.IOException
|
||||
import java.util.HashSet
|
||||
|
||||
class ReceivedCookiesInterceptor(private val context: Context) : Interceptor {
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val originalResponse = chain.proceed(chain.request())
|
||||
|
||||
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
|
||||
val cookies = PreferenceManager.getDefaultSharedPreferences(context).getStringSet(
|
||||
"PREF_COOKIES",
|
||||
HashSet()
|
||||
) as HashSet<String>
|
||||
|
||||
for (header in originalResponse.headers("Set-Cookie")) {
|
||||
cookies.add(header)
|
||||
}
|
||||
|
||||
val memes = PreferenceManager.getDefaultSharedPreferences(context).edit()
|
||||
memes.putStringSet("PREF_COOKIES", cookies).apply()
|
||||
memes.commit()
|
||||
}
|
||||
|
||||
return originalResponse
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package me.gilo.cocart.data.requests
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class CartItemRequest(
|
||||
@SerializedName("product_id")
|
||||
var productId: Int,
|
||||
|
||||
@SerializedName("quantity")
|
||||
var quantity: Int,
|
||||
|
||||
@SerializedName("variation_id")
|
||||
var variationId: Int? = null,
|
||||
|
||||
@SerializedName("variation")
|
||||
var variation: Any? = null,
|
||||
|
||||
@SerializedName("cart_item_data")
|
||||
var cartItemData: Any? = null,
|
||||
|
||||
@SerializedName("refresh_totals")
|
||||
var refreshTotals: Boolean? = null,
|
||||
|
||||
@SerializedName("return_cart")
|
||||
var returnCart: Boolean? = null
|
||||
)
|
||||
@ -0,0 +1,11 @@
|
||||
package me.gilo.cocart.data.requests
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class CartRequest(
|
||||
@SerializedName("id")
|
||||
var customerId: String? = null,
|
||||
|
||||
@SerializedName("thumb")
|
||||
var thumb: Boolean?
|
||||
)
|
||||
31
cocart/src/main/java/me/gilo/cocart/model/CartItem.kt
Normal file
31
cocart/src/main/java/me/gilo/cocart/model/CartItem.kt
Normal file
@ -0,0 +1,31 @@
|
||||
package me.gilo.cocart.model
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
|
||||
class CartItem {
|
||||
|
||||
@SerializedName("product_id")
|
||||
var productId: Int = 0
|
||||
|
||||
@SerializedName("variation_id")
|
||||
var variationId: Int? = null
|
||||
|
||||
var variation: Array<Any>? = null
|
||||
|
||||
lateinit var subtotal: String
|
||||
@SerializedName("subtotal_tax")
|
||||
lateinit var subtotalTax: String
|
||||
lateinit var total: String
|
||||
lateinit var totalTax: String
|
||||
lateinit var price: String
|
||||
var quantity: Int = 0
|
||||
lateinit var taxClass: Any
|
||||
lateinit var name: String
|
||||
|
||||
|
||||
lateinit var key: String
|
||||
lateinit var sku: String
|
||||
lateinit var variations: String
|
||||
lateinit var data_hash: String
|
||||
}
|
||||
54
cocart/src/main/java/me/gilo/cocart/model/CartTotal.kt
Normal file
54
cocart/src/main/java/me/gilo/cocart/model/CartTotal.kt
Normal file
@ -0,0 +1,54 @@
|
||||
package me.gilo.cocart.model
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
|
||||
data class CartTotal (
|
||||
|
||||
@SerializedName("subtotal")
|
||||
var subtotal: String? = "",
|
||||
|
||||
@SerializedName("subtotal_tax")
|
||||
var subtotalTax: Float? = 0f,
|
||||
|
||||
@SerializedName("shipping_total")
|
||||
var shippingTotal: String? = "",
|
||||
|
||||
@SerializedName("shipping_tax")
|
||||
var shippingTax: Float? = 0f,
|
||||
|
||||
@SerializedName("shipping_taxes")
|
||||
var shippingTaxes: Map<String, Float>? = HashMap(),
|
||||
|
||||
@SerializedName("discount_total")
|
||||
var discountTotal: Float? = 0f,
|
||||
|
||||
@SerializedName("discount_tax")
|
||||
var discountTax: Float? = 0f,
|
||||
|
||||
@SerializedName("cart_contents_total")
|
||||
var cartContentsTotal: String? = "",
|
||||
|
||||
@SerializedName("cart_contents_tax")
|
||||
var cartContentsTax: Float? = 0f,
|
||||
|
||||
@SerializedName("cart_contents_taxes")
|
||||
var cartContentsTaxes: Map<String, Float>? = HashMap(),
|
||||
|
||||
@SerializedName("fee_total")
|
||||
var feeTotal: String? = "",
|
||||
|
||||
@SerializedName("fee_tax")
|
||||
var feeTax: String? = "",
|
||||
|
||||
@SerializedName("fee_taxes")
|
||||
var feeTaxes: Any,
|
||||
|
||||
@SerializedName("total")
|
||||
var total: String? = "",
|
||||
|
||||
@SerializedName("total_tax")
|
||||
var totalTax: Float? = 0f
|
||||
|
||||
|
||||
)
|
||||
60
cocart/src/main/java/me/gilo/cocart/repo/CoCartRepository.kt
Normal file
60
cocart/src/main/java/me/gilo/cocart/repo/CoCartRepository.kt
Normal file
@ -0,0 +1,60 @@
|
||||
package me.gilo.cocart.repo
|
||||
|
||||
import me.gilo.cocart.data.api.ItemsAPI
|
||||
import me.gilo.cocart.data.requests.CartItemRequest
|
||||
import me.gilo.cocart.data.requests.CartRequest
|
||||
import me.gilo.cocart.model.CartItem
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Call
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class CoCartRepository(private var baseUrl: String, consumerKey: String, consumerSecret: String) {
|
||||
|
||||
private var apiService: ItemsAPI
|
||||
private var retrofit: Retrofit
|
||||
|
||||
init {
|
||||
val loggingInterceptor = HttpLoggingInterceptor()
|
||||
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
|
||||
|
||||
val client = OkHttpClient.Builder()
|
||||
.addInterceptor(loggingInterceptor)
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
.writeTimeout(30, TimeUnit.SECONDS)
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
.build()
|
||||
|
||||
retrofit = Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.client(client)
|
||||
.build()
|
||||
|
||||
apiService = retrofit.create(ItemsAPI::class.java)
|
||||
}
|
||||
|
||||
fun addToCart(productId: Int, quantity: Int): Call<CartItem> {
|
||||
val cartItemRequest = CartItemRequest(
|
||||
productId = productId, quantity = quantity
|
||||
)
|
||||
|
||||
return apiService.addToCart(cartItemRequest)
|
||||
}
|
||||
|
||||
fun cart(): Call<Map<String, CartItem>> {
|
||||
return apiService.list()
|
||||
}
|
||||
|
||||
fun cart(customerId: Int, thumb:Boolean = false): Call<Map<String, CartItem>> {
|
||||
return apiService.list(thumb)
|
||||
}
|
||||
|
||||
fun getCustomerCart(customerId: String, thumb:Boolean = false): Call<Map<String, CartItem>> {
|
||||
return apiService.getCustomerCart(CartRequest(customerId=customerId, thumb = true))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
17
cocart/src/test/java/me/gilo/cocart/ExampleUnitTest.kt
Normal file
17
cocart/src/test/java/me/gilo/cocart/ExampleUnitTest.kt
Normal file
@ -0,0 +1,17 @@
|
||||
package me.gilo.cocart
|
||||
|
||||
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 +1,2 @@
|
||||
include ':app', ':woodroid', ':firebasecart', ':core'
|
||||
include ':cocart'
|
||||
|
||||
@ -56,6 +56,9 @@ dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
|
||||
|
||||
implementation project(path: ':cocart')
|
||||
|
||||
|
||||
}
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@ -37,7 +37,7 @@ class Woocommerce(siteUrl: String, apiVerion: ApiVersion, consumerKey: String, c
|
||||
|
||||
init {
|
||||
val baseUrl = "$siteUrl/wp-json/wc/v$apiVerion/"
|
||||
val cartBaseUrl = "$siteUrl/wp-json/wc/v2/"
|
||||
val cartBaseUrl = "$siteUrl/wp-json/cocart/v1/"
|
||||
|
||||
orderNoteRepository = OrderNoteRepository(baseUrl, consumerKey, consumerSecret)
|
||||
refundRepository = RefundRepository(baseUrl, consumerKey, consumerSecret)
|
||||
@ -129,7 +129,6 @@ class Woocommerce(siteUrl: String, apiVerion: ApiVersion, consumerKey: String, c
|
||||
}
|
||||
|
||||
fun CartRepository(context: Context): CartRepository {
|
||||
cartRepository.turnOnCookies(context)
|
||||
return cartRepository
|
||||
}
|
||||
|
||||
|
||||
@ -1,99 +1,19 @@
|
||||
package me.gilo.woodroid.repo
|
||||
|
||||
import android.content.Context
|
||||
import me.gilo.woodroid.data.api.CartAPI
|
||||
import me.gilo.woodroid.data.cookie.AddCookiesInterceptor
|
||||
import me.gilo.woodroid.data.cookie.ReceivedCookiesInterceptor
|
||||
import me.gilo.woodroid.models.LineItem
|
||||
import me.gilo.woodroid.models.filters.CartFilter
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import me.gilo.cocart.model.CartItem
|
||||
import me.gilo.cocart.repo.CoCartRepository
|
||||
import retrofit2.Call
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class CartRepository(internal var baseUrl: String, consumerKey: String, consumerSecret: String) {
|
||||
|
||||
internal var apiService: CartAPI
|
||||
internal var retrofit: Retrofit
|
||||
private var cartRepository: CoCartRepository = CoCartRepository(baseUrl, consumerKey, consumerSecret)
|
||||
|
||||
init {
|
||||
val loggingInterceptor = HttpLoggingInterceptor()
|
||||
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
|
||||
|
||||
|
||||
val client = OkHttpClient.Builder()
|
||||
.addInterceptor(loggingInterceptor)
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
.writeTimeout(30, TimeUnit.SECONDS)
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
.build()
|
||||
|
||||
retrofit = Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.client(client)
|
||||
.build()
|
||||
|
||||
apiService = retrofit.create(CartAPI::class.java)
|
||||
fun addToCart(productId: Int, quantity: Int): Call<CartItem> {
|
||||
return cartRepository.addToCart(productId, quantity)
|
||||
}
|
||||
|
||||
fun turnOnCookies(context: Context) {
|
||||
val loggingInterceptor = HttpLoggingInterceptor()
|
||||
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
|
||||
|
||||
val client = OkHttpClient.Builder()
|
||||
.addInterceptor(AddCookiesInterceptor(context))
|
||||
.addInterceptor(ReceivedCookiesInterceptor(context))
|
||||
.addInterceptor(loggingInterceptor)
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
.writeTimeout(30, TimeUnit.SECONDS)
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
.build()
|
||||
|
||||
retrofit = Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.client(client)
|
||||
.build()
|
||||
|
||||
apiService = retrofit.create(CartAPI::class.java)
|
||||
fun cart(customerId: String): Call<Map<String, CartItem>> {
|
||||
return cartRepository.getCustomerCart(customerId = customerId)
|
||||
}
|
||||
|
||||
fun clear(): Call<String> {
|
||||
return apiService.clear()
|
||||
}
|
||||
|
||||
|
||||
fun count(id: Int): Call<Int> {
|
||||
return apiService.count()
|
||||
}
|
||||
|
||||
fun cart(): Call<Map<String, LineItem>> {
|
||||
return apiService.list()
|
||||
}
|
||||
|
||||
fun addToCart(lineItem: LineItem): Call<Map<String, LineItem>> {
|
||||
return apiService.addToCart(lineItem)
|
||||
}
|
||||
|
||||
fun delete(cardId: String): Call<String> {
|
||||
val cartFilter = CartFilter(cardId)
|
||||
return apiService.delete(cartFilter)
|
||||
}
|
||||
|
||||
fun restore(cardId: String): Call<String> {
|
||||
val cartFilter = CartFilter(cardId)
|
||||
return apiService.restore(cartFilter)
|
||||
}
|
||||
|
||||
fun update(cardId: String, quantity: Int): Call<String> {
|
||||
val cartFilter = CartFilter(cardId)
|
||||
cartFilter.quantity = quantity
|
||||
|
||||
return apiService.update(cartFilter)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user