Skip to content

Commit

Permalink
Merge pull request #29 from EmmanuelMess/fix-javaintercompatibility
Browse files Browse the repository at this point in the history
Fix javaintercompatibility
  • Loading branch information
EmmanuelMess authored Dec 21, 2018
2 parents 2a8f0c5 + ed08bca commit 727d357
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileMetadata>
}
Expand All @@ -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!
Expand Down
15 changes: 15 additions & 0 deletions lib/src/main/java/com/amaze/filepreloaderlibrary/FilePreloader.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,6 +40,20 @@ object FilePreloader {
*
* @see [with].
*/
fun <D: DataContainer>with(clazz: Class<D>): SpecializedPreloader<D> {
//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 <D: DataContainer>with(clazz: Class<D>, f: FetcherFunction<D>): SpecializedPreloader<D> {
val v = SpecializedPreloader(clazz, f)
weakList.add(WeakReference(v))
Expand Down

0 comments on commit 727d357

Please sign in to comment.