WIP Add to cart struggling with cookies
This commit is contained in:
parent
ed0f31421f
commit
11ffd08276
@ -1,10 +1,11 @@
|
||||
package me.gilo.wc.repo;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import me.gilo.wc.common.WooLiveData;
|
||||
import me.gilo.wc.utils.AppUtils;
|
||||
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;
|
||||
@ -20,21 +21,29 @@ public class CartRepository {
|
||||
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> addToCart(int productId) {
|
||||
public WooLiveData<Map<String, LineItem>> addToCart(Context context, 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);
|
||||
woocommerce.CartRepository(context).addToCart(lineItem).enqueue(callBack);
|
||||
return callBack;
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> cart() {
|
||||
public WooLiveData<Map<String, LineItem>> cart(Context context) {
|
||||
final WooLiveData<Map<String, LineItem>> callBack = new WooLiveData();
|
||||
woocommerce.CartRepository().cart().enqueue(callBack);
|
||||
woocommerce.CartRepository(context).cart().enqueue(callBack);
|
||||
|
||||
return callBack;
|
||||
}
|
||||
|
||||
public void saveSession(Context context, String session, String expiry) {
|
||||
AppUtils appUtils = new AppUtils(context);
|
||||
appUtils.saveCartSession(session, expiry);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,20 +1,12 @@
|
||||
package me.gilo.wc.ui.product
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.design.widget.Snackbar
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.text.Html
|
||||
import android.text.Spannable
|
||||
import android.text.SpannableString
|
||||
import android.text.style.ForegroundColorSpan
|
||||
import android.view.View
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import com.squareup.picasso.Picasso
|
||||
import me.gilo.wc.R
|
||||
|
||||
import kotlinx.android.synthetic.main.activity_product.*
|
||||
import kotlinx.android.synthetic.main.content_product.*
|
||||
import me.gilo.wc.R
|
||||
import me.gilo.wc.common.BaseActivity
|
||||
import me.gilo.wc.common.Status
|
||||
import me.gilo.wc.ui.state.ProgressDialogFragment
|
||||
@ -46,7 +38,7 @@ class ProductActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun addToCart(productId: Int) {
|
||||
viewModel.addToCart(productId).observe(this, android.arch.lifecycle.Observer { response ->
|
||||
viewModel.addToCart(baseContext, productId).observe(this, android.arch.lifecycle.Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
|
||||
|
||||
@ -6,7 +6,6 @@ import android.support.v4.view.GravityCompat
|
||||
import android.support.v7.widget.GridLayoutManager
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.widget.Filter
|
||||
import android.widget.Toast
|
||||
import io.github.inflationx.viewpump.ViewPumpContextWrapper
|
||||
import kotlinx.android.synthetic.main.activity_shop.*
|
||||
@ -119,7 +118,7 @@ class ShopActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
private fun cart() {
|
||||
viewModel.cart().observe(this, android.arch.lifecycle.Observer { response ->
|
||||
viewModel.cart(baseContext).observe(this, android.arch.lifecycle.Observer { response ->
|
||||
when (response!!.status()) {
|
||||
Status.LOADING -> {
|
||||
}
|
||||
|
||||
@ -53,6 +53,19 @@ public class AppUtils {
|
||||
return prefs.getString("token", null);
|
||||
}
|
||||
|
||||
public void saveCartSession(String sessionId, String expiry){
|
||||
SharedPreferences.Editor editor = context.getSharedPreferences(MY_PREFS_NAME, context.MODE_PRIVATE).edit();
|
||||
editor.putString("cartSession", sessionId);
|
||||
editor.putString("expiry", expiry);
|
||||
editor.putBoolean("hasSession", true);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public String getCartSession() {
|
||||
SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, context.MODE_PRIVATE);
|
||||
return prefs.getString("cartSession", null);
|
||||
}
|
||||
|
||||
public String getExpiry() {
|
||||
SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, context.MODE_PRIVATE);
|
||||
return prefs.getString("expiry", null);
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
package me.gilo.wc.viewmodels;
|
||||
|
||||
import android.arch.lifecycle.ViewModel;
|
||||
import android.content.Context;
|
||||
import me.gilo.wc.common.WooLiveData;
|
||||
import me.gilo.wc.repo.CartRepository;
|
||||
import me.gilo.wc.repo.OrderRepository;
|
||||
import me.gilo.wc.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.filters.ProductFilter;
|
||||
|
||||
@ -34,12 +34,12 @@ public final class ProductViewModel extends ViewModel {
|
||||
return productRepository.products();
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> addToCart(int productId) {
|
||||
return cartRepository.addToCart(productId);
|
||||
public WooLiveData<Map<String, LineItem>> addToCart(Context context, int productId) {
|
||||
return cartRepository.addToCart(context, productId);
|
||||
}
|
||||
|
||||
public WooLiveData<Map<String, LineItem>> cart() {
|
||||
return cartRepository.cart();
|
||||
public WooLiveData<Map<String, LineItem>> cart(Context context) {
|
||||
return cartRepository.cart(context);
|
||||
}
|
||||
|
||||
public WooLiveData<List<Product>> products(ProductFilter filter) {
|
||||
|
||||
@ -50,4 +50,6 @@ dependencies {
|
||||
|
||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
|
||||
|
||||
implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.0.0-RC1'
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package me.gilo.woodroid;
|
||||
|
||||
import android.content.Context;
|
||||
import me.gilo.woodroid.repo.*;
|
||||
import me.gilo.woodroid.repo.order.OrderNoteRepository;
|
||||
import me.gilo.woodroid.repo.order.RefundRepository;
|
||||
@ -165,7 +166,8 @@ public class Woocommerce {
|
||||
return reportsRepository;
|
||||
}
|
||||
|
||||
public CartRepository CartRepository() {
|
||||
public CartRepository CartRepository(Context context) {
|
||||
cartRepository.turnOnCookies(context);
|
||||
return cartRepository;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
package me.gilo.woodroid.callback;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class WooCallback<T> implements Callback<T> {
|
||||
@Override
|
||||
public void onResponse(Call<T> call, Response<T> response) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<T> call, Throwable t) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
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;
|
||||
|
||||
public class AddCookiesInterceptor implements Interceptor {
|
||||
|
||||
public static final String PREF_COOKIES = "PREF_COOKIES";
|
||||
// We're storing our stuff in a database made just for cookies called PREF_COOKIES.
|
||||
// I reccomend you do this, and don't change this default value.
|
||||
private Context context;
|
||||
|
||||
public AddCookiesInterceptor(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(Interceptor.Chain chain) throws IOException {
|
||||
Request.Builder builder = chain.request().newBuilder();
|
||||
|
||||
HashSet<String> preferences = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet(PREF_COOKIES, new HashSet<String>());
|
||||
for (String cookie : preferences) {
|
||||
builder.addHeader("Cookie", cookie);
|
||||
}
|
||||
|
||||
return chain.proceed(builder.build());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
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;
|
||||
|
||||
public class ReceivedCookiesInterceptor implements Interceptor {
|
||||
|
||||
private Context context;
|
||||
public ReceivedCookiesInterceptor(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Response originalResponse = chain.proceed(chain.request());
|
||||
|
||||
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
|
||||
HashSet<String> cookies = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context).getStringSet("PREF_COOKIES", new HashSet<String>());
|
||||
|
||||
for (String header : originalResponse.headers("Set-Cookie")) {
|
||||
cookies.add(header);
|
||||
}
|
||||
|
||||
SharedPreferences.Editor memes = PreferenceManager.getDefaultSharedPreferences(context).edit();
|
||||
memes.putStringSet("PREF_COOKIES", cookies).apply();
|
||||
memes.commit();
|
||||
}
|
||||
|
||||
return originalResponse;
|
||||
}
|
||||
}
|
||||
@ -1,31 +1,32 @@
|
||||
package me.gilo.woodroid.repo;
|
||||
|
||||
import android.content.Context;
|
||||
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.data.cookie.AddCookiesInterceptor;
|
||||
import me.gilo.woodroid.data.cookie.ReceivedCookiesInterceptor;
|
||||
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;
|
||||
CartAPI apiService;
|
||||
Retrofit retrofit;
|
||||
|
||||
String baseUrl;
|
||||
|
||||
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)
|
||||
@ -39,6 +40,30 @@ public class CartRepository{
|
||||
.client(client)
|
||||
.build();
|
||||
|
||||
this.baseUrl = baseUrl;
|
||||
|
||||
apiService = retrofit.create(CartAPI.class);
|
||||
}
|
||||
|
||||
public void turnOnCookies(Context context){
|
||||
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
|
||||
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.addInterceptor(new AddCookiesInterceptor(context))
|
||||
.addInterceptor(new ReceivedCookiesInterceptor(context))
|
||||
.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);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user