From 52674e311bad9dca2afbaa18f3c67c2e5bb4e936 Mon Sep 17 00:00:00 2001 From: Emmanuel M Date: Fri, 21 Dec 2018 02:26:03 -0300 Subject: [PATCH 1/3] Added Java example to readme --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index 92ca036..936ce06 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ dependencies { ## Usage +### Kotlin: ```kotlin fun preload(externalDir: File) = FilePreloader.with(::FileMetadata).preloadFrom(externalDir.absolutePath) @@ -53,6 +54,43 @@ class FileMetadata(path: String): DataContainer(path) { } ``` +### Java: +```java +private static final FilePreloader FILE_PRELOADER = FilePreloader.INSTANCE; + +public void preload(File externalDir) { + FILE_PRELOADER.with(FileMetadata.class, FileMetadata::new).preloadFrom(externalDir.getAbsolutePath()); +} + +public void load(File externalDir) { + FILE_PRELOADER.with(FileMetadata.class, FileMetadata::new).load(externalDir.getAbsolutePath(), (fileMetadatas) -> { + show(fileMetadatas); //Do something with the data + }); +} + +private static class FileMetadata extends DataContainer { + private final String name; + private final String filePath; + private final String extension; + private final boolean isDirectory; + + + public FileMetadata(@NonNull String path){ + super(path); + File file = new File(path); + name = file.getName(); + filePath = file.getAbsolutePath(); + extension = name.substring(name.lastIndexOf('.')); + isDirectory = file.isDirectory(); + } + + @Override + public String toString() { + return "'" + name + "': {'" + filePath + "', " + isDirectory + ", *." + extension + "}"; + } +} +``` + ## Development Just import to Android Studio as normal! From 8e85bdc68fa368d8c7a9e7c8afff5c3d2f24587b Mon Sep 17 00:00:00 2001 From: Emmanuel M Date: Fri, 21 Dec 2018 02:27:42 -0300 Subject: [PATCH 2/3] Corrected function name in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 936ce06..a0b43aa 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ dependencies { fun preload(externalDir: File) = FilePreloader.with(::FileMetadata).preloadFrom(externalDir.absolutePath) -fun load() = FilePreloader.with(::FileMetadata).loadFrom(externalDir.absolutePath) { +fun load() = FilePreloader.with(::FileMetadata).load(externalDir.absolutePath) { //Do something with the data show(it) //it: List } From ed08bca5c01152ced3f1b8cce26ca4dc5e17d388 Mon Sep 17 00:00:00 2001 From: Emmanuel M Date: Fri, 21 Dec 2018 18:47:00 -0300 Subject: [PATCH 3/3] FilePreloader.with(Class) method for calling from Java --- README.md | 4 ++-- .../amaze/filepreloaderlibrary/FilePreloader.kt | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a0b43aa..f6c1569 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,11 @@ class FileMetadata(path: String): DataContainer(path) { private static final FilePreloader FILE_PRELOADER = FilePreloader.INSTANCE; public void preload(File externalDir) { - FILE_PRELOADER.with(FileMetadata.class, FileMetadata::new).preloadFrom(externalDir.getAbsolutePath()); + FILE_PRELOADER.with(FileMetadata.class).preloadFrom(externalDir.getAbsolutePath()); } public void load(File externalDir) { - FILE_PRELOADER.with(FileMetadata.class, FileMetadata::new).load(externalDir.getAbsolutePath(), (fileMetadatas) -> { + FILE_PRELOADER.with(FileMetadata.class).load(externalDir.getAbsolutePath(), (fileMetadatas) -> { show(fileMetadatas); //Do something with the data }); } diff --git a/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt b/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt index a1d9c03..9ca86c8 100644 --- a/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt +++ b/lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt @@ -1,6 +1,7 @@ package com.amaze.filepreloaderlibrary import android.app.Activity +import com.amaze.filepreloaderlibrary.FilePreloader.with import com.amaze.filepreloaderlibrary.datastructures.DataContainer import com.amaze.filepreloaderlibrary.datastructures.FetcherFunction import com.amaze.filepreloaderlibrary.utils.LIB_CONTEXT @@ -39,6 +40,20 @@ object FilePreloader { * * @see [with]. */ + fun with(clazz: Class): SpecializedPreloader { + //A constructor will obviously produce an instance of the type-parametrized type + // from which the constructor has been obtained + @Suppress("UNCHECKED_CAST") + return with(clazz, clazz.getConstructor(String::class.java)::newInstance as (String) -> D) + } + + /** + * For compatibity with Java + * + * If you want to use another method, that's not the constructor, the type still has to be (String) -> D + * + * @see [with]. + */ fun with(clazz: Class, f: FetcherFunction): SpecializedPreloader { val v = SpecializedPreloader(clazz, f) weakList.add(WeakReference(v))