Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix javaintercompatibility #29

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 39 additions & 1 deletion README.md
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
@@ -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