diff --git a/README.md b/README.md index 92ca036..f6c1569 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,12 @@ dependencies { ## Usage +### Kotlin: ```kotlin 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 } @@ -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).preloadFrom(externalDir.getAbsolutePath()); +} + +public void load(File externalDir) { + FILE_PRELOADER.with(FileMetadata.class).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! 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))