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/update unavailable dependency PhotoView #242

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

<config-file parent="/manifest" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" android:minSdkVersion="33" />
</config-file>

<source-file src="src/android/PhotoViewer.java" target-dir="src/com/sarriaroman/PhotoViewer" />
Expand Down
23 changes: 18 additions & 5 deletions src/android/PhotoViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
Expand All @@ -22,6 +23,7 @@ public class PhotoViewer extends CordovaPlugin {

public static final String WRITE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE;
public static final String READ_IMAGES = Manifest.permission.READ_MEDIA_IMAGES;

public static final int REQ_CODE = 0;

Expand All @@ -33,19 +35,30 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
if (action.equals("show")) {
this.args = args;
this.callbackContext = callbackContext;

if (cordova.hasPermission(READ) && cordova.hasPermission(WRITE)) {
this.launchActivity();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (cordova.hasPermission(READ_IMAGES)) {
this.launchActivity();
} else {
this.getPermission();
}
} else {
this.getPermission();
if (cordova.hasPermission(READ) && cordova.hasPermission(WRITE)) {
this.launchActivity();
} else {
this.getPermission();
}
}
return true;
}
return false;
}

protected void getPermission() {
cordova.requestPermissions(this, REQ_CODE, new String[]{WRITE, READ});
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
cordova.requestPermissions(this, REQ_CODE, new String[]{READ_IMAGES});
} else {
cordova.requestPermissions(this, REQ_CODE, new String[]{WRITE, READ});
}
}

//
Expand Down
3 changes: 2 additions & 1 deletion src/android/photoviewer.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
repositories{
jcenter()
maven { url "https://www.jitpack.io" }
}

dependencies {
implementation 'com.commit451:PhotoView:1.2.4'
implementation 'com.github.chrisbanes:PhotoView:v1.2.4'
implementation 'com.squareup.picasso:picasso:2.71828'
}

Expand Down
26 changes: 15 additions & 11 deletions src/ios/PhotoViewer.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ - (void)show:(CDVInvokedUrlCommand*)command
showCloseBtn = [[command.arguments objectAtIndex:3] boolValue];
copyToReference = [[command.arguments objectAtIndex:4] boolValue];
headers = [self headers:[command.arguments objectAtIndex:5]];

if ([url rangeOfString:@"http"].location == 0) {
copyToReference = true;
}
Expand Down Expand Up @@ -140,9 +140,8 @@ - (void)show:(CDVInvokedUrlCommand*)command

- (NSURL *)localFileURLForImage:(NSString *)image
{
Boolean isFirebase = [image rangeOfString:@"firebase"].length > 0;
NSString* webStringURL = image;
if (!isFirebase) {
if (![self isUrlAlreadyEncoded:webStringURL]) {
webStringURL = [image stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
}
NSURL* fileURL = [NSURL URLWithString:webStringURL];
Expand All @@ -157,7 +156,6 @@ - (NSURL *)localFileURLForImage:(NSString *)image
}
if (error)
return nil;

if( data ) {
// save this image to a temp folder
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
Expand Down Expand Up @@ -247,20 +245,20 @@ - (void)showFullScreen:(NSURL *)url andTitle:(NSString *)title {
[closeBtn setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6]];
[closeBtn addTarget:self action:@selector(closeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.viewController.view addSubview:closeBtn];

imageLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, viewHeight - 50, viewWidth - 120, 50)];
imageLabel.numberOfLines = 0;
imageLabel.lineBreakMode = NSLineBreakByWordWrapping;
imageLabel.minimumScaleFactor = 0.5;
imageLabel.adjustsFontSizeToFitWidth = YES;

[imageLabel setTextAlignment:NSTextAlignmentCenter];
[imageLabel setTextColor:[UIColor whiteColor]];
[imageLabel setBackgroundColor:[UIColor clearColor]];
[imageLabel setFont:[UIFont fontWithName: @"San Fransisco" size: 14.0f]];
[imageLabel setText:title];
[self.viewController.view addSubview:imageLabel];

} else {
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fullimagetapped:)];
singleTap.numberOfTapsRequired = 1;
Expand All @@ -277,7 +275,7 @@ - (void)fullimagetapped:(UIGestureRecognizer *)gestureRecognizer {
- (void)closeButtonPressed:(UIButton *)button {
[closeBtn removeFromSuperview];
[imageLabel removeFromSuperview];

closeBtn = nil;
imageLabel = nil;
[self closeImage];
Expand Down Expand Up @@ -340,7 +338,6 @@ - (NSDictionary *)headers:(NSString *)headerString {
if (headerString == nil || [headerString length] == 0) {
return nil;
}

NSData *jsonData = [headerString dataUsingEncoding:NSUTF8StringEncoding];
// Note that JSONObjectWithData will return either an NSDictionary or an NSArray, depending whether your JSON string represents an a dictionary or an array.
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
Expand All @@ -351,17 +348,24 @@ - (NSDictionary *)headers:(NSString *)headerString {
- (NSData *)imageDataFromURLWithHeaders:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

for(NSString *key in headers) {
NSString *value = [headers objectForKey:key];
[request setValue:value forHTTPHeaderField:key];
}

NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:nil];
return data;
}

- (Boolean)isUrlAlreadyEncoded:(NSString *)url {
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"%[2-9A-F][0-9A-F]"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSUInteger numMatches = [regex numberOfMatchesInString:url options:0 range:NSMakeRange(0, [url length])];
return numMatches > 0;
}

@end