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

AndroidX Support #201

Open
wants to merge 6 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
2 changes: 1 addition & 1 deletion PhotoView.android.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { requireNativeComponent, View } from 'react-native';
import ViewPropTypes from 'react-native/Libraries/Components/View/ViewPropTypes';
import { ViewPropTypes } from 'deprecated-react-native-prop-types'

const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource');

Expand Down
2 changes: 1 addition & 1 deletion PhotoView.ios.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { requireNativeComponent, View } from 'react-native';
import ViewPropTypes from 'react-native/Libraries/Components/View/ViewPropTypes';
import { ViewPropTypes } from 'deprecated-react-native-prop-types'

const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource');

Expand Down
17 changes: 8 additions & 9 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:4.2.2'
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
minSdkVersion 21
targetSdkVersion 30
versionCode 1
}
lintOptions {
Expand All @@ -29,11 +29,10 @@ android {
repositories {
mavenCentral()
google()
jcenter()
}

dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'com.facebook.fresco:fresco:1.12.1'
implementation 'me.relex:photodraweeview:1.1.3'
implementation 'com.facebook.fresco:fresco:2.6.0'
implementation 'me.relex:photodraweeview:2.1.0'
}
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

package com.reactnative.photoview;

import android.support.annotation.IntDef;
import androidx.annotation.IntDef;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.RCTEventEmitter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.NonNull;
import androidx.annotation.NonNull;
import android.util.Log;
import android.view.View;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
Expand Down Expand Up @@ -269,4 +269,3 @@ private int getMaxTextureSize() {
return Math.max(maximumTextureSize, IMAGE_MAX_BITMAP_DIMENSION);
}
}

4 changes: 2 additions & 2 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
Expand All @@ -14,8 +14,8 @@ buildscript {

allprojects {
repositories {
mavenCentral()
mavenLocal()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
Expand Down
122 changes: 65 additions & 57 deletions ios/RNPhotoView.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#import <React/RCTUtils.h>
#import <React/UIView+React.h>
#import <React/RCTImageLoader.h>
#import <SDWebImage/SDAnimatedImageView+WebCache.h>
#import <SDWebImage/SDWebImageDownloader.h>
#import <SDWebImage/SDWebImage.h>
#import <SDWebImagePhotosPlugin/SDImagePhotosLoader.h>

@interface RNPhotoView()

Expand Down Expand Up @@ -90,12 +94,12 @@ - (void)imageView:(UIImageView *)imageView singleTapDetected:(UITouch *)touch {

if (_onPhotoViewerTap) {
_onPhotoViewerTap(@{
@"point": @{
@"x": @(touchX),
@"y": @(touchY),
},
@"target": self.reactTag
});
@"point": @{
@"x": @(touchX),
@"y": @(touchY),
},
@"target": self.reactTag
});
}
}

Expand All @@ -117,12 +121,12 @@ - (void)view:(UIView *)view singleTapDetected:(UITouch *)touch {

if (_onPhotoViewerViewTap) {
_onPhotoViewerViewTap(@{
@"point": @{
@"x": @(touchX),
@"y": @(touchY),
},
@"target": self.reactTag,
});
@"point": @{
@"x": @(touchX),
@"y": @(touchY),
},
@"target": self.reactTag,
});
}
}

Expand Down Expand Up @@ -248,9 +252,9 @@ - (void)layoutSubviews {
_photoImageView.frame = frameToCenter;
if (_onPhotoViewerScale) {
_onPhotoViewerScale(@{
@"scale": @(self.zoomScale),
@"target": self.reactTag
});
@"scale": @(self.zoomScale),
@"target": self.reactTag
});
}
}

Expand Down Expand Up @@ -286,27 +290,28 @@ - (void)displayWithImage:(UIImage*)image {
#pragma mark - Setter

- (void)setSource:(NSDictionary *)source {
__weak RNPhotoView *weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if ([_source isEqualToDictionary:source]) {
if ([weakSelf.source isEqualToDictionary:source]) {
return;
}
NSString *uri = source[@"uri"];
if (!uri) {
return;
}
_source = source;
weakSelf.source = source;
NSURL *imageURL = [NSURL URLWithString:uri];

if (![[uri substringToIndex:4] isEqualToString:@"http"]) {
@try {
UIImage *image = RCTImageFromLocalAssetURL(imageURL);
if (image) { // if local image
[self setImage:image];
if (_onPhotoViewerLoad) {
_onPhotoViewerLoad(nil);
if (weakSelf.onPhotoViewerLoad) {
weakSelf.onPhotoViewerLoad(nil);
}
if (_onPhotoViewerLoadEnd) {
_onPhotoViewerLoadEnd(nil);
if (weakSelf.onPhotoViewerLoadEnd) {
weakSelf.onPhotoViewerLoadEnd(nil);
}
return;
}
Expand All @@ -319,6 +324,10 @@ - (void)setSource:(NSDictionary *)source {
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:imageURL];

if (source[@"headers"]) {
// Set headers.
[source[@"headers"] enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString* header, BOOL *stop) {
[[SDWebImageDownloader sharedDownloader] setValue:header forHTTPHeaderField:key];
}];
NSMutableURLRequest *mutableRequest = [request mutableCopy];

NSDictionary *headers = source[@"headers"];
Expand All @@ -328,47 +337,43 @@ - (void)setSource:(NSDictionary *)source {
[mutableRequest addValue:[headers objectForKey:key] forHTTPHeaderField:key];
request = [mutableRequest copy];
}

__weak RNPhotoView *weakSelf = self;
if (_onPhotoViewerLoadStart) {
_onPhotoViewerLoadStart(nil);
if (weakSelf.onPhotoViewerLoadStart) {
weakSelf.onPhotoViewerLoadStart(nil);
}

// use default values from [imageLoader loadImageWithURLRequest:request callback:callback] method
[[_bridge moduleForClass:[RCTImageLoader class]] loadImageWithURLRequest:request
size:CGSizeZero
scale:1
clipped:YES
resizeMode:RCTResizeModeStretch
progressBlock:^(int64_t progress, int64_t total) {
if (_onPhotoViewerProgress) {
_onPhotoViewerProgress(@{
@"loaded": @((double)progress),
@"total": @((double)total),
});
}
}
partialLoadBlock:nil
completionBlock:^(NSError *error, UIImage *image) {
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf setImage:image];
});
if (_onPhotoViewerLoad) {
_onPhotoViewerLoad(nil);
}
} else {
if (_onPhotoViewerError) {
_onPhotoViewerError(nil);
}
}
if (_onPhotoViewerLoadEnd) {
_onPhotoViewerLoadEnd(nil);
}
}];
SDWebImageOptions options = SDWebImageRetryFailed;
[weakSelf downloadImage:imageURL options:options];
});
}

- (void)downloadImage:(NSURL *) url options:(SDWebImageOptions) options {
__weak typeof(self) weakSelf = self; // Always use a weak reference to self in blocks
[[SDWebImageManager sharedManager] loadImageWithURL:url options:options progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
if (weakSelf.onPhotoViewerProgress) {
weakSelf.onPhotoViewerProgress(@{
@"loaded": @(receivedSize),
@"total": @(expectedSize)
});
}
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
if (error) {
if (weakSelf.onPhotoViewerError) {
weakSelf.onPhotoViewerError(nil);
}
} else if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf setImage:image];
});
if (weakSelf.onPhotoViewerLoad) {
weakSelf.onPhotoViewerLoad(nil);
}
}
if (weakSelf.onPhotoViewerLoadEnd) {
weakSelf.onPhotoViewerLoadEnd(nil);
}
}];
}

- (void)setLoadingIndicatorSrc:(NSString *)loadingIndicatorSrc {
if (!loadingIndicatorSrc) {
return;
Expand Down Expand Up @@ -413,6 +418,9 @@ - (void)initView {
_minZoomScale = 1.0;
_maxZoomScale = 5.0;

[SDImageLoadersManager.sharedManager addLoader:SDImagePhotosLoader.sharedLoader];
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.sharedManager;

// Setup
self.backgroundColor = [UIColor clearColor];
self.delegate = self;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"type": "git",
"url": "git://github.com/alwx/react-native-photo-view.git"
},
"dependencies": {
"prop-types": "^15.5.10"
"peerDependencies": {
"deprecated-react-native-prop-types": "*",
"prop-types": "*"
}
}
21 changes: 12 additions & 9 deletions react-native-photo-view.podspec
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
require "json"
require 'json'

package = JSON.parse(File.read(File.join(__dir__, "package.json")))
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
s.name = "react-native-photo-view"
s.version = package["version"]
s.name = 'react-native-photo-view'
s.version = package['version']
s.summary = package['description']
s.author = package['author']
s.homepage = package['homepage']
s.license = package['license']
s.ios.deployment_target = "7.0"
s.tvos.deployment_target = "9.0"
s.source = { :git => "https://github.com/alwx/react-native-photo-view.git", :tag => "v#{s.version}" }
s.source_files = "ios/*.{h,m}"
s.dependency "React-Core"
s.ios.deployment_target = '10.0'
s.tvos.deployment_target = '10.0'
s.source = { git: 'https://github.com/alwx/react-native-photo-view.git', tag: "v#{s.version}" }
s.source_files = 'ios/*.{h,m}'
s.dependency 'React-Core'
s.dependency 'SDWebImage'
s.dependency 'SDWebImageWebPCoder'
s.dependency 'SDWebImagePhotosPlugin'
end