-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add image caching to SmallImageCard #319
Add image caching to SmallImageCard #319
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## feature/content-cards #319 +/- ##
===========================================================
- Coverage 82.78% 82.23% -0.56%
Complexity 800 800
===========================================================
Files 78 79 +1
Lines 3701 3765 +64
Branches 572 583 +11
===========================================================
+ Hits 3064 3096 +32
- Misses 459 483 +24
- Partials 178 186 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
UIUtils.downloadImage(imageUrl) { | ||
val contentCardImageManager = ContentCardImageManager() | ||
|
||
contentCardImageManager.getContentCardImageBitmap(imageUrl) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we define the getContentCardImageBitmap
function in a companion object within ContentCardImageManager
so there is no need to create a ContentCardImageManager
object just to call getContentCardImageBitmap
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
import java.io.InputStream | ||
import java.nio.ByteBuffer | ||
|
||
class ContentCardImageManager() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the ()
can be removed after the class name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
it.onSuccess { bitmap -> | ||
imageBitmap = bitmap | ||
isLoading = false | ||
} | ||
it.onFailure { | ||
// TODO once we have a default image, we can use that here | ||
// todo - confirm default image bitmap to be used here | ||
// imageBitmap = contentCardManager.getDefaultImageBitmap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function can also be added to the companion object so it can be called statically.
byteArray.inputStream() // Create InputStream from byte array | ||
} | ||
|
||
val cacheEntry = CacheEntry(imageInputStream, CacheExpiry.never(), null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should set a cache expiry here as content cards will be refreshed often. @navratan-soni and @PravinPK what do you guys think is the ideal expiry time we should set? i'm thinking a 7 day expiry would be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Log.warning( | ||
MessagingConstants.LOG_TAG, | ||
SELF_TAG, | ||
"getImageBitmapFromCache: Unable to read cached data into a bitmap due to error: $e" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we usually use a -
between the function name tag and the log message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Log.warning( | ||
MessagingConstants.LOG_TAG, | ||
SELF_TAG, | ||
"getImageBitmapFromCache: Unable to read cached data as the inputStream is null" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we usually use a -
between the function name tag and the log message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
cacheService?.set(cacheName, imageName, cacheEntry) | ||
|
||
return true | ||
} catch (ex: java.lang.Exception) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no need to provide the full Exception package path here:
} catch (e: Exception) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Log.warning( | ||
MessagingConstants.LOG_TAG, | ||
SELF_TAG, | ||
"cacheImage - An unexpected error occurred while caching the downloaded image: \n %s", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use the kotlin string interpolation format:
Log.warning(
MessagingConstants.LOG_TAG,
SELF_TAG,
"cacheImage - An unexpected error occurred while caching the downloaded image: $e"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -21,6 +21,8 @@ public final class MessagingConstants { | |||
static final String CACHE_BASE_DIR = "messaging"; | |||
static final String PROPOSITIONS_CACHE_SUBDIRECTORY = "propositions"; | |||
static final String IMAGES_CACHE_SUBDIRECTORY = "images"; | |||
static final String CONTENT_CARD_CACHE_SUBDIRECTORY = "contentCardImages"; | |||
static final String CONTENT_CARD_TEST_CACHE_SUBDIRECTORY = "contentCardTestImages"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's add these constants to MessagingTestConstants
instead of here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
@Test | ||
fun get_image_first_time_when_not_in_cache() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we name the tests with more readable names like try getting an image when it is not in the cache
? back ticks can be used to add spaces to test method names: https://kotlinlang.org/docs/coding-conventions.html#names-for-test-methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
import java.io.InputStream | ||
import java.nio.ByteBuffer | ||
|
||
class ContentCardImageManager { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whole class can be made as an object instead of class, and we can remove the companion object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two points here.
- If all the methods are in companion object, can we remove the companion object and just create the whole class as Object.
- My preferred approach would be to create instance of this Manager class, which takes the input of cache name in constructor itself. That way we will not need to pass the name of cache in all the methods.
the |
ff6d1b5
into
adobe:feature/content-cards
Description
Previously the images were always downloaded from the url, added cache functionality. Now, the image will be fetched from cache if present, otherwise it will be downloaded and cached for future use.
Related Issue
Motivation and Context
How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Checklist: