Add Resource Loader for iOS

This commit is contained in:
pengfei.zhou
2021-10-25 16:01:44 +08:00
committed by osborn
parent 4bd4f42f52
commit 235549eea4
40 changed files with 979 additions and 242 deletions

View File

@@ -15,6 +15,7 @@
*/
package pub.doric.resource;
import java.io.IOException;
import java.io.InputStream;
import pub.doric.DoricContext;
@@ -34,14 +35,30 @@ public class DoricAndroidResource extends DoricResource {
}
@Override
public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> result = new AsyncResult<>();
public AsyncResult<byte[]> fetchRaw() {
AsyncResult<byte[]> result = new AsyncResult<>();
int resId = doricContext.getContext().getResources().getIdentifier(
identifier,
defType,
doricContext.getContext().getPackageName());
if (resId > 0) {
result.setResult(doricContext.getContext().getResources().openRawResource(resId));
InputStream inputStream = null;
try {
inputStream = doricContext.getContext().getResources().openRawResource(resId);
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
result.setResult(data);
} catch (Exception e) {
result.setError(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
result.setError(new Throwable("Cannot find resource for :" + identifier + ",type = " + defType));
}

View File

@@ -32,13 +32,24 @@ public class DoricAssetsResource extends DoricResource {
}
@Override
public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> result = new AsyncResult<>();
public AsyncResult<byte[]> fetchRaw() {
AsyncResult<byte[]> result = new AsyncResult<>();
InputStream inputStream = null;
try {
InputStream inputStream = doricContext.getContext().getAssets().open(identifier);
result.setResult(inputStream);
inputStream = doricContext.getContext().getAssets().open(identifier);
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
result.setResult(data);
} catch (IOException e) {
result.setError(e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}

View File

@@ -19,9 +19,6 @@ import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
import pub.doric.utils.DoricUtils;
@@ -38,8 +35,8 @@ class DoricBase64Resource extends DoricResource {
}
@Override
public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> ret = new AsyncResult<>();
public AsyncResult<byte[]> fetchRaw() {
AsyncResult<byte[]> ret = new AsyncResult<>();
Pair<String, String> result = DoricUtils.translateBase64(identifier);
if (result != null) {
String imageType = result.first;
@@ -48,7 +45,7 @@ class DoricBase64Resource extends DoricResource {
if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) {
try {
byte[] data = Base64.decode(base64, Base64.DEFAULT);
ret.setResult(new ByteArrayInputStream(data));
ret.setResult(data);
} catch (Exception e) {
e.printStackTrace();
}

View File

@@ -16,8 +16,7 @@
package pub.doric.resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
@@ -34,12 +33,24 @@ public class DoricLocalResource extends DoricResource {
}
@Override
public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> result = new AsyncResult<>();
public AsyncResult<byte[]> fetchRaw() {
AsyncResult<byte[]> result = new AsyncResult<>();
FileInputStream fis = null;
try {
result.setResult(new FileInputStream(identifier));
} catch (FileNotFoundException e) {
fis = new FileInputStream(identifier);
byte[] data = new byte[fis.available()];
fis.read(data);
result.setResult(data);
} catch (Exception e) {
result.setError(e);
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}

View File

@@ -23,8 +23,7 @@ import com.bumptech.glide.request.target.Target;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import androidx.annotation.Nullable;
import pub.doric.DoricContext;
@@ -42,8 +41,8 @@ public class DoricRemoteResource extends DoricResource {
}
@Override
public AsyncResult<InputStream> asInputStream() {
final AsyncResult<InputStream> result = new AsyncResult<>();
public AsyncResult<byte[]> fetchRaw() {
final AsyncResult<byte[]> result = new AsyncResult<>();
Glide.with(doricContext.getContext()).download(identifier)
.listener(new RequestListener<File>() {
@Override
@@ -54,10 +53,22 @@ public class DoricRemoteResource extends DoricResource {
@Override
public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
FileInputStream fis = null;
try {
result.setResult(new FileInputStream(resource));
} catch (FileNotFoundException e) {
fis = new FileInputStream(resource);
byte[] data = new byte[fis.available()];
fis.read(data);
result.setResult(data);
} catch (Exception e) {
result.setError(e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}

View File

@@ -35,5 +35,5 @@ public abstract class DoricResource {
this.identifier = identifier;
}
public abstract AsyncResult<InputStream> asInputStream();
public abstract AsyncResult<byte[]> fetchRaw();
}

View File

@@ -57,7 +57,6 @@ import java.io.InputStream;
import jp.wasabeef.glide.transformations.BlurTransformation;
import pub.doric.DoricContext;
import pub.doric.DoricSingleton;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin;
@@ -371,22 +370,10 @@ public class ImageNode extends ViewNode<ImageView> {
final String identifier = resource.getProperty("identifier").asString().value();
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(getDoricContext(), type, identifier);
if (doricResource != null) {
doricResource.asInputStream().setCallback(new AsyncResult.Callback<InputStream>() {
doricResource.fetchRaw().setCallback(new AsyncResult.Callback<byte[]>() {
@Override
public void onResult(InputStream result) {
try {
byte[] imageData = new byte[result.available()];
result.read(imageData, 0, result.available());
loadIntoTarget(Glide.with(getContext()).load(imageData));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
result.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onResult(byte[] imageData) {
loadIntoTarget(Glide.with(getContext()).load(imageData));
}
@Override