package com.sheep.gamegroup.model.cookie;

import android.content.Context;

import com.sheep.gamegroup.util.ListUtil;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;

import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

/**
 * Created by realicing on 2018/8/29.
 * realicing@sina.com
 */
public class CookieManager implements CookieJar {


    private static PersistentCookieStore cookieStore;

    public CookieManager(Context context) {
        if (cookieStore == null) {
            cookieStore = new PersistentCookieStore(context);
        }
    }


    @Override
    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        if (!ListUtil.isEmpty(cookies)) {
            cookieStore.add(url, cookies);
        }
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl url) {
        return cookieStore.get(url);
    }

}
