Changed o to 0. Some renaming. Checked for nullability on string.isEmpty() function to prevent crashes

This commit is contained in:
NoodleMage 2016-01-27 11:39:03 +01:00
parent df3dde422b
commit 2c293734f4
3 changed files with 59 additions and 40 deletions

View File

@ -69,7 +69,7 @@ public class ChapterCache {
PARAMETER_CACHE_SIZE
);
} catch (IOException e) {
// Do Nothing. TODO error handling?
// Do Nothing. TODO error handling.
}
}

View File

@ -28,17 +28,24 @@ import eu.kanade.tachiyomi.util.DiskUtils;
*/
public class CoverCache {
/** Name of cache directory. */
/**
* Name of cache directory.
*/
private static final String PARAMETER_CACHE_DIRECTORY = "cover_disk_cache";
/** Interface to global information about an application environment. */
/**
* Interface to global information about an application environment.
*/
private final Context context;
/** Cache class used for cache management. */
/**
* Cache class used for cache management.
*/
private final File cacheDir;
/**
* Constructor of CoverCache.
*
* @param context application environment interface.
*/
public CoverCache(Context context) {
@ -53,18 +60,18 @@ public class CoverCache {
/**
* Check if cache dir exist if not create directory.
* @return true if cache dir exist or is created.
*
* @return true if cache dir does exist and 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 and update the call made to this method with ,null?
*
* @param thumbnailUrl url of thumbnail.
* @param headers headers included in Glide request.
* @param headers headers included in Glide request.
*/
public void save(String thumbnailUrl, LazyHeaders headers) {
save(thumbnailUrl, headers, null);
@ -72,14 +79,15 @@ public class CoverCache {
/**
* 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.
* @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))
if (thumbnailUrl == null || TextUtils.isEmpty(thumbnailUrl))
// Do not try and create the string. Instead... only try to realize the truth. There is no string.
return;
@ -92,7 +100,7 @@ public class CoverCache {
public void onResourceReady(File resource, GlideAnimation<? super File> anim) {
try {
// Copy the cover from Glide's cache to local cache.
add(thumbnailUrl, resource);
copyToLocalCache(thumbnailUrl, resource);
// Check if imageView isn't null and show picture in imageView.
if (imageView != null) {
@ -108,18 +116,19 @@ public class CoverCache {
/**
* Copy the cover from Glide's cache to local cache.
* //TODO rename add => copyToLocalCache?
*
* @param thumbnailUrl url of thumbnail.
* @param source the cover image.
* @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.
private void copyToLocalCache(String thumbnailUrl, File source) throws IOException {
// Create cache directory and check if directory exist
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();
@ -143,25 +152,26 @@ public class CoverCache {
}
}
/**
* Returns the cover from cache.
* TODO rename get => getCoverFromCache
*
* @param thumbnailUrl the thumbnail url.
* @return cover image.
*/
private File get(String thumbnailUrl) {
private File getCoverFromCache(String thumbnailUrl) {
return new File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl));
}
/**
* 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) {
public boolean deleteCoverFromCache(String thumbnailUrl) {
// Check if url is empty.
if (TextUtils.isEmpty(thumbnailUrl))
if (thumbnailUrl == null || TextUtils.isEmpty(thumbnailUrl))
return false;
// Remove file.
@ -171,13 +181,14 @@ public class CoverCache {
/**
* Save or load the image from cache
* @param imageView imageView where picture should be displayed.
*
* @param imageView imageView where picture should be displayed.
* @param thumbnailUrl the thumbnail url.
* @param headers headers included in Glide request.
* @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);
File localCover = getCoverFromCache(thumbnailUrl);
if (localCover.exists()) {
loadFromCache(imageView, localCover);
} else {
@ -188,13 +199,14 @@ public class CoverCache {
/**
* 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 imageView imageView where picture should be displayed.
* @param thumbnailUrl url of thumbnail.
* @param headers headers included in Glide request.
* @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);
File localCover = getCoverFromCache(thumbnailUrl);
if (localCover.exists()) {
loadFromCache(imageView, localCover);
} else {
@ -203,9 +215,10 @@ public class CoverCache {
}
/**
* Helper method to load the cover from the cache directory into the specified image view.
* 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!.
* @param file file to load. Must exist!.
*/
private void loadFromCache(ImageView imageView, File file) {
Glide.with(context)
@ -218,11 +231,16 @@ public class CoverCache {
/**
* 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 imageView imageView where picture should be displayed.
* @param thumbnailUrl url of thumbnail.
* @param headers headers included in Glide request.
* @param headers headers included in Glide request.
*/
public void loadFromNetwork(ImageView imageView, String thumbnailUrl, LazyHeaders headers) {
// Check if url is empty.
if (thumbnailUrl == null || TextUtils.isEmpty(thumbnailUrl))
return;
GlideUrl url = new GlideUrl(thumbnailUrl, headers);
Glide.with(context)
.load(url)

View File

@ -19,17 +19,18 @@ import rx.schedulers.Schedulers;
public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
@Inject DatabaseHelper db;
@Inject SourceManager sourceManager;
@Inject CoverCache coverCache;
private Manga manga;
protected Source source;
private int count = -1;
private static final int GET_MANGA = 1;
private static final int GET_CHAPTER_COUNT = 2;
private static final int FETCH_MANGA_INFO = 3;
protected Source source;
@Inject
DatabaseHelper db;
@Inject
SourceManager sourceManager;
@Inject
CoverCache coverCache;
private Manga manga;
private int count = -1;
@Override
protected void onCreate(Bundle savedState) {
@ -111,7 +112,7 @@ public class MangaInfoPresenter extends BasePresenter<MangaInfoFragment> {
if (isFavorite) {
coverCache.save(manga.thumbnail_url, source.getGlideHeaders());
} else {
coverCache.delete(manga.thumbnail_url);
coverCache.deleteCoverFromCache(manga.thumbnail_url);
}
}