Last active
February 5, 2019 11:31
-
-
Save ildar2/806715ce1c26c4d274fe532e2e7532e4 to your computer and use it in GitHub Desktop.
Glide SVG issue workaround: https://github.com/bumptech/glide/issues/3246
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package my.app.kz.ui.main.offers; | |
import android.graphics.drawable.PictureDrawable; | |
import android.os.AsyncTask; | |
import android.support.annotation.NonNull; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.LruCache; | |
import android.view.LayoutInflater; | |
import android.view.ViewGroup; | |
import com.bumptech.glide.request.RequestListener; | |
import com.pixplicity.sharp.Sharp; | |
import com.pixplicity.sharp.SharpDrawable; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import my.beeline.kz.data.model.DashboardBundle; | |
import my.beeline.kz.databinding.ViewholderPriceplanBundleBinding; | |
import my.beeline.kz.utils.RecyclerViewAdapter; | |
import my.beeline.kz.utils.svg.SvgSoftwareLayerSetter; | |
import timber.log.Timber; | |
public class __OfferBundleAdapter extends RecyclerViewAdapter<__OfferBundleAdapter.BalanceViewHolder, DashboardBundle> { | |
private ClickListener mListener; | |
private static LruCache<String, SharpDrawable> cache = new LruCache<>(100); | |
public __OfferBundleAdapter(ClickListener listener) { | |
super(); | |
mListener = listener; | |
} | |
@NonNull | |
@Override | |
public BalanceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); | |
ViewholderPriceplanBundleBinding binding = ViewholderPriceplanBundleBinding.inflate(layoutInflater, parent, false); | |
return new BalanceViewHolder(binding, mListener); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull BalanceViewHolder holder, int position) { | |
holder.bind(items.get(position)); | |
} | |
static class BalanceViewHolder extends RecyclerView.ViewHolder { | |
private ViewholderPriceplanBundleBinding binding; | |
public BalanceViewHolder(ViewholderPriceplanBundleBinding binding, ClickListener listener) { | |
super(binding.getRoot()); | |
this.binding = binding; | |
} | |
void bind(DashboardBundle item) { | |
binding.setBundle(item); | |
String url = item.getIconUrl(); | |
if (url == null || url.isEmpty()) return; | |
SharpDrawable cached = cache.get(url); | |
if (cached != null) { | |
binding.bundleIcon.setImageDrawable(cached); | |
} else { | |
new HttpImageRequestTask().execute(url);//todo find better solution asap! | |
} | |
// GlideApp.with(binding.bundleIcon) | |
// .as(PictureDrawable.class) | |
// .listener(applyListener(item.getIconUrl())) | |
// .load(item.getIconUrl()) | |
// .placeholder(R.drawable.ic_server_error) | |
// .into(binding.bundleIcon); | |
} | |
private RequestListener<PictureDrawable> applyListener(String url) { | |
return url != null && url.endsWith(".svg") ? new SvgSoftwareLayerSetter() : null; | |
} | |
private class HttpImageRequestTask extends AsyncTask<String, Void, SharpDrawable> { | |
String path; | |
@Override | |
protected SharpDrawable doInBackground(String... params) { | |
try { | |
path = params[0]; | |
final URL url = new URL(path); | |
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | |
InputStream is = urlConnection.getInputStream(); | |
SharpDrawable drawable = Sharp.loadInputStream(is) | |
.getDrawable(); | |
is.close(); | |
return drawable; | |
} catch (Exception e) { | |
Timber.w(e); | |
return null; | |
} | |
} | |
@Override | |
protected void onPostExecute(SharpDrawable drawable) { | |
if (drawable != null) { | |
cache.put(path, drawable); | |
binding.bundleIcon.setImageDrawable(drawable); | |
} | |
} | |
} | |
} | |
public interface ClickListener { | |
void onItemClick(DashboardBundle item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment