Code optimization. Added comments. Few comment mistake fixes

This commit is contained in:
NoodleMage 2016-01-26 15:02:16 +01:00
parent 2014b228e8
commit 11b68f914f
2 changed files with 105 additions and 20 deletions

View file

@ -32,7 +32,7 @@ public class ChapterCache {
/** Name of cache directory. */
private static final String PARAMETER_CACHE_DIRECTORY = "chapter_disk_cache";
/** Application version. */
/** Application cache version. */
private static final int PARAMETER_APP_VERSION = 1;
/** The number of values per cache entry. Must be positive. */
@ -55,7 +55,6 @@ public class ChapterCache {
* @param context application environment interface.
*/
public ChapterCache(Context context) {
// Set context.
this.context = context;
// Initialize Json handler.

View file

@ -20,33 +20,70 @@ import java.io.OutputStream;
import eu.kanade.tachiyomi.util.DiskUtils;
/**
* Class used to create cover cache
* Makes us of Glide which can avoid repeating requests and save the file.
* It is not necessary to load the images to the cache.
* Names of files are created with the md5 of the thumbnailURL
*/
public class CoverCache {
/** Name of cache directory. */
private static final String PARAMETER_CACHE_DIRECTORY = "cover_disk_cache";
private Context context;
private File cacheDir;
/** Interface to global information about an application environment. */
private final Context context;
/** Cache class used for cache management. */
private final File cacheDir;
/**
* Constructor of CoverCache.
* @param context application environment interface.
*/
public CoverCache(Context context) {
this.context = context;
// Get cache directory from parameter.
cacheDir = new File(context.getCacheDir(), PARAMETER_CACHE_DIRECTORY);
// Create cache directory.
createCacheDir();
}
/**
* Check if cache dir exist if not create directory.
* @return true if cache dir exist or is created.
*/
private boolean createCacheDir() {
// TODO return value never used.
return !cacheDir.exists() && cacheDir.mkdirs();
}
/**
* Download the cover with Glide (it can avoid repeating requests) and save the file.
* TODO maybe remove?
* @param thumbnailUrl url of thumbnail.
* @param headers headers included in Glide request.
*/
public void save(String thumbnailUrl, LazyHeaders headers) {
save(thumbnailUrl, headers, null);
}
// Download the cover with Glide (it can avoid repeating requests) and save the file on this cache
// Optionally, load the image in the given image view when the resource is ready, if not null
public void save(String thumbnailUrl, LazyHeaders headers, ImageView imageView) {
/**
* Download the cover with Glide (it can avoid repeating requests) and save the file.
* @param thumbnailUrl url of thumbnail.
* @param headers headers included in Glide request.
* @param imageView imageView where picture should be displayed.
*/
private void save(String thumbnailUrl, LazyHeaders headers, ImageView imageView) {
// Check if url is empty.
if (TextUtils.isEmpty(thumbnailUrl))
// Do not try and create the string. Instead... only try to realize the truth. There is no string.
return;
// Download the cover with Glide and save the file.
GlideUrl url = new GlideUrl(thumbnailUrl, headers);
Glide.with(context)
.load(url)
@ -54,7 +91,10 @@ public class CoverCache {
@Override
public void onResourceReady(File resource, GlideAnimation<? super File> anim) {
try {
// Copy the cover from Glide's cache to local cache.
add(thumbnailUrl, resource);
// Check if imageView isn't null and show picture in imageView.
if (imageView != null) {
loadFromCache(imageView, resource);
}
@ -65,18 +105,31 @@ public class CoverCache {
});
}
// Copy the cover from Glide's cache to this cache
public void add(String thumbnailUrl, File source) throws IOException {
/**
* Copy the cover from Glide's cache to local cache.
* //TODO rename add => copyToLocalCache?
* @param thumbnailUrl url of thumbnail.
* @param source the cover image.
* @throws IOException TODO not returned atm?
*/
private void add(String thumbnailUrl, File source) throws IOException {
// Create cache directory. TODO is this needed. Already called in constructor.
createCacheDir();
// Create destination file.
File dest = new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
// Check if file already exists, if true delete it.
if (dest.exists())
dest.delete();
// Write thumbnail image to file.
InputStream in = new FileInputStream(source);
try {
OutputStream out = new FileOutputStream(dest);
try {
// Transfer bytes from in to out
// Transfer bytes from in to out.
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
@ -90,22 +143,40 @@ public class CoverCache {
}
}
// Get the cover from cache
public File get(String thumbnailUrl) {
/**
* Returns the cover from cache.
* TODO rename get => getCoverFromCache
* @param thumbnailUrl the thumbnail url.
* @return cover image.
*/
private File get(String thumbnailUrl) {
return new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
}
// Delete the cover from cache
/**
* Delete the cover file from the cache.
* TODO rename delete => deleteCoverFromCache.
* @param thumbnailUrl the thumbnail url.
* @return status of deletion.
*/
public boolean delete(String thumbnailUrl) {
// Check if url is empty.
if (TextUtils.isEmpty(thumbnailUrl))
return false;
// Remove file.
File file = new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
return file.exists() && file.delete();
}
// Save and load the image from cache
public void saveAndLoadFromCache(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
/**
* Save or load the image from cache
* @param imageView imageView where picture should be displayed.
* @param thumbnailUrl the thumbnail url.
* @param headers headers included in Glide request.
*/
public void saveOrLoadFromCache(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
// If file exist load it otherwise save it.
File localCover = get(thumbnailUrl);
if (localCover.exists()) {
loadFromCache(imageView, localCover);
@ -114,8 +185,15 @@ public class CoverCache {
}
}
// If the image is already in our cache, use it. If not, load it with glide
/**
* If the image is already in our cache, use it. If not, load it with glide.
* TODO not used atm.
* @param imageView imageView where picture should be displayed.
* @param thumbnailUrl url of thumbnail.
* @param headers headers included in Glide request.
*/
public void loadFromCacheOrNetwork(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
// If localCover exist load it from cache otherwise load it from network.
File localCover = get(thumbnailUrl);
if (localCover.exists()) {
loadFromCache(imageView, localCover);
@ -124,8 +202,11 @@ public class CoverCache {
}
}
// Helper method to load the cover from the cache directory into the specified image view
// The file must exist
/**
* Helper method to load the cover from the cache directory into the specified image view.
* @param imageView imageView where picture should be displayed.
* @param file file to load. Must exist!.
*/
private void loadFromCache(ImageView imageView, File file) {
Glide.with(context)
.load(file)
@ -134,8 +215,13 @@ public class CoverCache {
.into(imageView);
}
// Helper method to load the cover from network into the specified image view.
// It does NOT save the image in cache
/**
* Helper method to load the cover from network into the specified image view.
* It does NOT save the image in cache!
* @param imageView imageView where picture should be displayed.
* @param thumbnailUrl url of thumbnail.
* @param headers headers included in Glide request.
*/
public void loadFromNetwork(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
GlideUrl url = new GlideUrl(thumbnailUrl, headers);
Glide.with(context)