android:optimize code

This commit is contained in:
pengfei.zhou 2021-10-25 14:13:05 +08:00 committed by osborn
parent 6402522946
commit 4bd4f42f52
8 changed files with 15 additions and 25 deletions

View File

@ -37,6 +37,6 @@ public class DoricAndroidLoader implements DoricResourceLoader {
@Override @Override
public DoricResource load(DoricContext doricContext, String identifier) { public DoricResource load(DoricContext doricContext, String identifier) {
return new DoricAndroidResource(this.defType, identifier, doricContext); return new DoricAndroidResource(this.defType, doricContext, identifier);
} }
} }

View File

@ -27,12 +27,10 @@ import pub.doric.async.AsyncResult;
*/ */
public class DoricAndroidResource extends DoricResource { public class DoricAndroidResource extends DoricResource {
private final String defType; private final String defType;
private final String identifier;
public DoricAndroidResource(String defType, String identifier, DoricContext doricContext) { public DoricAndroidResource(String defType, DoricContext doricContext, String identifier) {
super(doricContext); super(doricContext, identifier);
this.defType = defType; this.defType = defType;
this.identifier = identifier;
} }
@Override @Override

View File

@ -30,6 +30,6 @@ public class DoricAssetsLoader implements DoricResourceLoader {
@Override @Override
public DoricResource load(DoricContext doricContext, String identifier) { public DoricResource load(DoricContext doricContext, String identifier) {
return new DoricAssetsResource(identifier, doricContext); return new DoricAssetsResource(doricContext, identifier);
} }
} }

View File

@ -27,18 +27,15 @@ import pub.doric.async.AsyncResult;
* @CreateDate: 2021/10/20 * @CreateDate: 2021/10/20
*/ */
public class DoricAssetsResource extends DoricResource { public class DoricAssetsResource extends DoricResource {
private final String path; public DoricAssetsResource(DoricContext doricContext, String identifier) {
super(doricContext, identifier);
public DoricAssetsResource(String path, DoricContext doricContext) {
super(doricContext);
this.path = path;
} }
@Override @Override
public AsyncResult<InputStream> asInputStream() { public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> result = new AsyncResult<>(); AsyncResult<InputStream> result = new AsyncResult<>();
try { try {
InputStream inputStream = doricContext.getContext().getAssets().open(path); InputStream inputStream = doricContext.getContext().getAssets().open(identifier);
result.setResult(inputStream); result.setResult(inputStream);
} catch (IOException e) { } catch (IOException e) {
result.setError(e); result.setError(e);

View File

@ -32,11 +32,9 @@ import pub.doric.utils.DoricUtils;
* @CreateDate: 2021/10/22 * @CreateDate: 2021/10/22
*/ */
class DoricBase64Resource extends DoricResource { class DoricBase64Resource extends DoricResource {
private final String identifier;
public DoricBase64Resource(DoricContext doricContext, String identifier) { public DoricBase64Resource(DoricContext doricContext, String identifier) {
super(doricContext); super(doricContext, identifier);
this.identifier = identifier;
} }
@Override @Override

View File

@ -28,18 +28,16 @@ import pub.doric.async.AsyncResult;
* @CreateDate: 2021/10/20 * @CreateDate: 2021/10/20
*/ */
public class DoricLocalResource extends DoricResource { public class DoricLocalResource extends DoricResource {
private final String filePath;
public DoricLocalResource(DoricContext doricContext, String identifier) { public DoricLocalResource(DoricContext doricContext, String identifier) {
super(doricContext); super(doricContext, identifier);
this.filePath = identifier;
} }
@Override @Override
public AsyncResult<InputStream> asInputStream() { public AsyncResult<InputStream> asInputStream() {
AsyncResult<InputStream> result = new AsyncResult<>(); AsyncResult<InputStream> result = new AsyncResult<>();
try { try {
result.setResult(new FileInputStream(filePath)); result.setResult(new FileInputStream(identifier));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
result.setError(e); result.setError(e);
} }

View File

@ -37,17 +37,14 @@ import pub.doric.async.AsyncResult;
*/ */
public class DoricRemoteResource extends DoricResource { public class DoricRemoteResource extends DoricResource {
private final String url;
public DoricRemoteResource(DoricContext doricContext, String identifier) { public DoricRemoteResource(DoricContext doricContext, String identifier) {
super(doricContext); super(doricContext, identifier);
this.url = identifier;
} }
@Override @Override
public AsyncResult<InputStream> asInputStream() { public AsyncResult<InputStream> asInputStream() {
final AsyncResult<InputStream> result = new AsyncResult<>(); final AsyncResult<InputStream> result = new AsyncResult<>();
Glide.with(doricContext.getContext()).download(url) Glide.with(doricContext.getContext()).download(identifier)
.listener(new RequestListener<File>() { .listener(new RequestListener<File>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {

View File

@ -28,9 +28,11 @@ import pub.doric.async.AsyncResult;
*/ */
public abstract class DoricResource { public abstract class DoricResource {
protected final DoricContext doricContext; protected final DoricContext doricContext;
protected final String identifier;
public DoricResource(DoricContext doricContext) { public DoricResource(DoricContext doricContext, String identifier) {
this.doricContext = doricContext; this.doricContext = doricContext;
this.identifier = identifier;
} }
public abstract AsyncResult<InputStream> asInputStream(); public abstract AsyncResult<InputStream> asInputStream();