Skip to content

Commit

Permalink
Merge pull request #35 from fanhero/feature/settimeout
Browse files Browse the repository at this point in the history
Exposed 'timeout' property on both platforms
  • Loading branch information
AndreaVitale authored Oct 30, 2017
2 parents 7ae3df0 + 9c8dac3 commit 3751a89
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ About the enabled `contentMode`, you can learn more about this [here](https://de
| clipsToBound | More details [here](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/clipsToBounds) | iOS only |
| requestHeader | An object used to define extra http request header fields | |
| rounded | Enable or disable the circle transformation that automatically render the image as a perfect circle | Android only |
| timeout | Set timeout for requests | |

### Extra methods

Expand All @@ -123,13 +124,15 @@ About the enabled `contentMode`, you can learn more about this [here](https://de
| setClipsToBound | Set the `clipToBounds` property | __iOS only__ |
| setRequestHeader | Set the `requestHeader` property | |
| setRounded | Set the `rounded` property | __Android only__ |
| setTimeout | Set timeout for requests | |
| getContentMode | Get the value of `contentMode` property |
| getLoadingIndicator | Get the value of `loadingIndicator` property | |
| getDefaultImage | Get the value of `defaultImage` property | |
| getBrokenLinkImage | Get the value of `brokenLinkImage` property | |
| getClipsToBound | Get the value of `clipToBounds` property | __iOS only__ |
| getRequestHeader | Get the `requestHeader` property | |
| getRounded | Get the `rounded` property | __Android only__ |
| getTimeout | Get current timeout of requests | |

### Events

Expand Down
Binary file not shown.
Binary file added android/android/lib/okhttp-3.9.0.jar
Binary file not shown.
Binary file added android/android/lib/okio-1.13.0.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion android/android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 2.1.3
version: 2.2.0
apiversion: 3
architectures: armeabi-v7a x86
description: andreavitale-imageview
Expand Down
29 changes: 28 additions & 1 deletion android/android/src/av/imageview/AVImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import av.imageview.utils.ImageLoader;
import av.imageview.utils.glide.GlideUrlBuilder;
import av.imageview.utils.glide.GlideCircleTransform;
import av.imageview.utils.glide.StreamModelLoaderWrapper;

import android.app.Activity;
import android.graphics.Bitmap;
Expand All @@ -17,6 +18,8 @@
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import okhttp3.OkHttpClient;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GifRequestBuilder;
import com.bumptech.glide.DrawableRequestBuilder;
Expand All @@ -28,6 +31,8 @@
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.stream.StreamModelLoader;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.KrollModule;
Expand All @@ -44,6 +49,7 @@

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class AVImageView extends TiUIView {
private static final String LCAT = "AVImageView";
Expand All @@ -54,6 +60,7 @@ public class AVImageView extends TiUIView {
private ImageView imageView;
private ProgressBar progressBar;
private RelativeLayout layout;
private OkHttpClient okHttpClient;

//Config variables
private boolean loadingIndicator;
Expand All @@ -75,6 +82,10 @@ public AVImageView(TiViewProxy proxy) {
this.loadingIndicator = true;
this.contentMode = ImageViewModule.CONTENT_MODE_ASPECT_FIT;
this.memoryCache = true;
this.okHttpClient = new OkHttpClient.Builder()// default timeouts are 5 seconds
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.build();

//Setting up layout and imageview
layout = new RelativeLayout(this.proxy.getActivity());
Expand Down Expand Up @@ -130,6 +141,12 @@ public void processProperties(KrollDict d) {
this.setBlob((TiBlob) uri);
}
}
if (d.containsKey("timeout")) {
this.okHttpClient = new OkHttpClient.Builder()
.connectTimeout(d.getInt("timeout"), TimeUnit.MILLISECONDS)
.readTimeout(d.getInt("timeout"), TimeUnit.MILLISECONDS)
.build();
}
}

@Override
Expand Down Expand Up @@ -193,7 +210,10 @@ public void startRequest(String url, Boolean loadingIndicator) {
if (url.startsWith("file://"))
drawableRequest = Glide.with(this.proxy.getActivity().getBaseContext()).load(url);
else
drawableRequest = Glide.with(this.proxy.getActivity().getBaseContext()).load(GlideUrlBuilder.build(url, this.requestHeader));
drawableRequest = Glide
.with(this.proxy.getActivity().getBaseContext())
.using(new StreamModelLoaderWrapper<GlideUrl>(new OkHttpUrlLoader(okHttpClient)))
.load(GlideUrlBuilder.build(url, this.requestHeader));

//Handling GIF
if (this.getMimeType(url) != null && this.getMimeType(url) == "image/gif") {
Expand Down Expand Up @@ -231,6 +251,13 @@ public void startRequest(String url, Boolean loadingIndicator) {
}
}

public void setTimeout(int timeout) {
this.okHttpClient = new OkHttpClient.Builder()
.connectTimeout(timeout, TimeUnit.MILLISECONDS)
.readTimeout(timeout, TimeUnit.MILLISECONDS)
.build();
}

private String sanitizeUrl(String url) {
if (url == null || !(url instanceof String))
return null;
Expand Down
13 changes: 13 additions & 0 deletions android/android/src/av/imageview/ImageViewProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class ImageViewProxy extends TiViewProxy
private static final int MSG_SET_BROKEN_IMAGE = MSG_FIRST_ID+1004;
private static final int MSG_SET_CONTENT_MODE = MSG_FIRST_ID+1005;
private static final int MSG_SET_REQUEST_HEADER = MSG_FIRST_ID+1006;
private static final int MSG_SET_TIMEOUT = MSG_FIRST_ID+1007;

private Activity activity;

Expand Down Expand Up @@ -106,6 +107,12 @@ public boolean handleMessage(Message message) {
getView().setRequestHeader((HashMap)result.getArg());
result.setResult(null);

return true;
case MSG_SET_TIMEOUT:
int timeout = message.arg1;
getView().setTimeout(timeout);
result.setResult(null);

return true;
default:
return super.handleMessage(message);
Expand Down Expand Up @@ -228,4 +235,10 @@ public void setRequestHeader(HashMap requestHeader) {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_REQUEST_HEADER), requestHeader);
}
}

@Kroll.setProperty
@Kroll.method
public void setTimeout(int timeout) {
getView().setTimeout(timeout);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package av.imageview.utils.glide;

import java.io.InputStream;

import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.stream.StreamModelLoader;

/**
* Workaround for <a href="https://github.com/bumptech/glide/issues/1832">bumptech/glide#1832</a>.
*
* @deprecated not needed after 3.8.0
*/
@Deprecated
public class StreamModelLoaderWrapper<GlideUrl> implements StreamModelLoader<GlideUrl> {
public final ModelLoader<GlideUrl, InputStream> wrapped;

public StreamModelLoaderWrapper(ModelLoader<GlideUrl, InputStream> wrapped) {
this.wrapped = wrapped;
}

@Override
public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) {
return wrapped.getResourceFetcher(model, width, height);
}
}
3 changes: 2 additions & 1 deletion ios/example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ var default_template = {
contentMode: AvImageview.CONTENT_MODE_ASPECT_FILL,
requestHeader: {
'Authorization': "Bearer YOU_CAN_PLACE_YOUR_ACCESS_TOKEN_HERE"
}
}.
timeout: 2500
}
}]
};
Expand Down
3 changes: 3 additions & 0 deletions ios/iphone/Classes/AvImageviewImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
NSString *brokenLinkImagePath;

NSDictionary *requestHeader;

NSTimeInterval timeout;
}

-(void)setWidth_:(id)width;
Expand All @@ -40,5 +42,6 @@
-(void)setContentMode_:(id)args;
-(void)setClipsToBounds_:(id)clips;
-(void)setRequestHeader_:(id)args;
-(void)setTimeout_:(id)args;

@end
6 changes: 6 additions & 0 deletions ios/iphone/Classes/AvImageviewImageView.m
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ -(void)setRequestHeader_:(id)args {
requestHeader = args;
}

-(void)setTimeout_:(id)args {
NSTimeInterval timeout = [TiUtils doubleValue:args def:5000] / 1000;
[[SDWebImageDownloader sharedDownloader] setDownloadTimeout:timeout];

}

#pragma mark utility methodds
-(CGFloat)contentWidthForWidth:(CGFloat)suggestedWidth {
if (autoWidth > 0) {
Expand Down
2 changes: 1 addition & 1 deletion ios/iphone/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.1.0
version: 1.2.0
apiversion: 2
architectures: armv7 arm64 i386 x86_64
description: andreavitale-imageview
Expand Down

0 comments on commit 3751a89

Please sign in to comment.