From e6ad2c2f5fbad0dbd1a30662f5926beb4d515c97 Mon Sep 17 00:00:00 2001 From: gonojuarez Date: Thu, 27 Oct 2022 12:41:54 -0300 Subject: [PATCH] url new function with firebase --- app/pictogramslibrary/build.gradle | 9 ++- .../CloudStorageManager.java | 21 +++++++ .../utils/GlideAttatcher.java | 58 +++++++++++++++++-- 3 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/CloudStorageManager.java diff --git a/app/pictogramslibrary/build.gradle b/app/pictogramslibrary/build.gradle index 7788e9b..dd6326e 100644 --- a/app/pictogramslibrary/build.gradle +++ b/app/pictogramslibrary/build.gradle @@ -28,14 +28,19 @@ android { dependencies { - implementation 'androidx.appcompat:appcompat:1.5.0' + implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.google.android.material:material:1.6.1' + implementation 'com.google.android.material:material:1.7.0' implementation 'com.github.bumptech.glide:glide:4.13.1' annotationProcessor 'com.github.bumptech.glide:compiler:4.13.2' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + implementation platform('com.google.firebase:firebase-bom:30.5.0') + + // Add the dependency for the Cloud Storage library + // When using the BoM, you don't specify versions in Firebase library dependencies + implementation 'com.google.firebase:firebase-storage' } project.afterEvaluate { diff --git a/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/CloudStorageManager.java b/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/CloudStorageManager.java new file mode 100644 index 0000000..54ac033 --- /dev/null +++ b/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/CloudStorageManager.java @@ -0,0 +1,21 @@ +package com.stonefacesoft.pictogramslibrary; + +import com.google.firebase.storage.FirebaseStorage; + +public class CloudStorageManager { + private FirebaseStorage storage; + private static CloudStorageManager _CloudStorageManager; + public synchronized static CloudStorageManager getInstance(){ + if(_CloudStorageManager == null) + _CloudStorageManager = new CloudStorageManager(); + return _CloudStorageManager; + } + + public void setStorage(FirebaseStorage storage){ + this.storage = storage; + } + + public FirebaseStorage getStorage() { + return storage; + } +} diff --git a/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/GlideAttatcher.java b/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/GlideAttatcher.java index c4c3abd..356e881 100644 --- a/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/GlideAttatcher.java +++ b/app/pictogramslibrary/src/main/java/com/stonefacesoft/pictogramslibrary/utils/GlideAttatcher.java @@ -19,6 +19,10 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.bumptech.glide.request.target.Target; import com.bumptech.glide.signature.ObjectKey; +import com.google.android.gms.tasks.OnSuccessListener; +import com.google.firebase.storage.FirebaseStorage; +import com.google.firebase.storage.StorageReference; +import com.stonefacesoft.pictogramslibrary.CloudStorageManager; import com.stonefacesoft.pictogramslibrary.Interfaces.GlideModelTypes; import com.stonefacesoft.pictogramslibrary.R; @@ -142,8 +146,31 @@ public Object loadDrawable(@Nullable String string, ImageView imageView) { @NonNull @Override public Object loadDrawable(@Nullable Uri uri, ImageView imageView) { - if(isValidContext(mContext)) - glideScaleItem(overrideMethod(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(uri).error(R.drawable.ic_baseline_cloud_download_24)))))).into(imageView).waitForLayout(); + + if(isValidContext(mContext)){ + if(uri.toString().contains("firebasestorage")){ + String aux = uri.toString(); + if(aux.contains("?alt")){ + aux = aux.substring(0,aux.lastIndexOf("?alt")); + } + if(CloudStorageManager.getInstance().getStorage()!=null){ + StorageReference mRef = CloudStorageManager.getInstance().getStorage().getReferenceFromUrl(aux); + mRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Uri uriResult) { + glideScaleItem(overrideMethod(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(uriResult).error(R.drawable.ic_baseline_cloud_download_24)))))).into(imageView).waitForLayout(); + } + }); + }else{ + glideScaleItem(overrideMethod(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(uri).error(R.drawable.ic_baseline_cloud_download_24)))))).into(imageView).waitForLayout(); + } + + + } + else{ + glideScaleItem(overrideMethod(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(uri).error(R.drawable.ic_baseline_cloud_download_24)))))).into(imageView).waitForLayout(); + } + } return this; } @@ -165,9 +192,30 @@ public Object loadDrawable(@Nullable Integer resourceId, ImageView imageView) { @Override public Object loadDrawable(@Nullable URL url, ImageView imageView) { - if(isValidContext(mContext)) - glideScaleItem(overrideMethod(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(url).signature(new ObjectKey(url)).error(R.drawable.ic_baseline_cloud_download_24)))))).into(imageView).waitForLayout(); - return this; + if(isValidContext(mContext)){ + if(url.toString().contains("firebasestorage")){ + String aux = url.toString(); + if(aux.contains("?alt")){ + aux = aux.substring(0,aux.lastIndexOf("?alt")); + } + if(CloudStorageManager.getInstance().getStorage()!=null){ + StorageReference mRef = CloudStorageManager.getInstance().getStorage().getReferenceFromUrl(aux); + mRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Uri uriResult) { + glideScaleItem(overrideMethod(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(uriResult).error(R.drawable.ic_baseline_cloud_download_24)))))).into(imageView).waitForLayout(); + } + }); + }else{ + glideScaleItem(overrideMethod(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(url).error(R.drawable.ic_baseline_cloud_download_24)))))).into(imageView).waitForLayout(); + } + + + } + else{ + glideScaleItem(overrideMethod(cornerRadious(useDecodeFormat(useDiskCacheStrategic(getGlide().getRequestManagerRetriever().get(mContext).load(url).error(R.drawable.ic_baseline_cloud_download_24)))))).into(imageView).waitForLayout(); + } + } return this; } @NonNull