Skip to content

Commit 822e270

Browse files
perf: improve ios perf, refactor code, update readme for Expo
1 parent f18e6d4 commit 822e270

File tree

8 files changed

+42
-76
lines changed

8 files changed

+42
-76
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,26 @@ With npm:
4343
With yarn:
4444
`$ yarn add react-native-loader-kit`
4545

46-
## Extra setup step for iOS
46+
## For iOS
4747
Run the following command to setup for iOS:
4848
```
4949
npx pod-install ios
5050
```
51+
## For Expo
52+
You need to run `prebuild` in order for native code takes effect:
53+
```
54+
npx expo prebuild
55+
```
5156
# Usage
5257
```js
5358
import LoaderKit from 'react-native-loader-kit'
5459

5560
<LoaderKit
5661
style={{ width: 50, height: 50 }}
5762
name={'BallPulse'} // Optional: see list of animations below
58-
size={50} // Required on iOS
5963
color={'red'} // Optional: color can be: 'red', 'green',... or '#ddd', '#ffffff',...
6064
/>
6165
```
62-
> Note: size is required for iOS and should equal to `width` and `height`
6366
# List animations
6467
As shown in the demo above, animations are as follows:
6568
## Default animations (both Android and iOS)

android/build.gradle

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ buildscript {
33
repositories {
44
google()
55
mavenCentral()
6-
jcenter()
76
}
87

98
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.5.3'
9+
classpath 'com.android.tools.build:gradle:3.5.4'
1110
}
1211
}
1312
}
@@ -19,10 +18,10 @@ def safeExtGet(prop, fallback) {
1918
}
2019

2120
android {
22-
compileSdkVersion safeExtGet('LoaderKit_compileSdkVersion', 29)
21+
compileSdkVersion safeExtGet('LoaderKit_compileSdkVersion', 30)
2322
defaultConfig {
2423
minSdkVersion safeExtGet('LoaderKit_minSdkVersion', 16)
25-
targetSdkVersion safeExtGet('LoaderKit_targetSdkVersion', 29)
24+
targetSdkVersion safeExtGet('LoaderKit_targetSdkVersion', 30)
2625
versionCode 1
2726
versionName "1.0"
2827

@@ -50,7 +49,6 @@ repositories {
5049
}
5150
google()
5251
mavenCentral()
53-
jcenter()
5452
}
5553

5654
dependencies {

android/src/main/java/com/reactnativeloaderkit/LoaderKitViewManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ public AVLoadingIndicatorView createViewInstance(ThemedReactContext context) {
2626

2727
@ReactProp(name = "name")
2828
public void setName(AVLoadingIndicatorView view, String name) {
29-
view.setIndicator(name + "Indicator");
29+
view.setIndicator(name != null ? name + "Indicator" : "BallPulseIndicator");
3030
}
3131

32-
@ReactProp(name = "color")
32+
@ReactProp(name = "color", defaultInt = Color.WHITE)
3333
public void setColor(AVLoadingIndicatorView view, int color) {
3434
view.setIndicatorColor(color);
3535
}

example/src/App.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@ import {
77
SafeAreaView,
88
Dimensions,
99
} from 'react-native';
10-
import LoaderKit from 'react-native-loader-kit';
11-
import animations from '../../src/animations';
10+
import LoaderKit, { animations } from 'react-native-loader-kit';
1211

1312
const { height } = Dimensions.get('window');
1413

1514
const App = () => {
1615
const renderLoaders = animations.map((item, index) => (
1716
<View style={{ margin: 10 }} key={index}>
1817
<Text style={{ color: 'white', textAlign: 'center' }}>{index + 1}</Text>
19-
<LoaderKit
20-
style={{ width: 50, height: 50, marginTop: 3 }}
21-
name={item}
22-
size={50}
23-
/>
18+
<LoaderKit style={{ width: 50, height: 50, marginTop: 3 }} name={item} />
2419
</View>
2520
));
2621

ios/LoaderKitViewManager.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
@interface RCT_EXTERN_MODULE(LoaderKitViewManager, RCTViewManager)
44

5-
RCT_EXPORT_VIEW_PROPERTY(size, NSNumber)
6-
75
RCT_EXPORT_VIEW_PROPERTY(name, NSString)
86

97
RCT_EXPORT_VIEW_PROPERTY(color, NSNumber)

ios/LoaderKitViewManager.swift

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,69 +49,43 @@ class LoaderKitView : UIView {
4949
"CircleStrokeSpin": NVActivityIndicatorType.circleStrokeSpin,
5050
]
5151

52-
var loader: NVActivityIndicatorView!
53-
var currentSize = 48.0
52+
private var loader = NVActivityIndicatorView(frame: .zero, type: NVActivityIndicatorView.DEFAULT_TYPE)
5453

5554
override init(frame: CGRect) {
5655
super.init(frame: frame)
5756

58-
let frameObj = CGRect(x: 0, y: 0, width: 200, height: 200)
59-
self.loader = NVActivityIndicatorView(frame: frameObj, type: NVActivityIndicatorView.DEFAULT_TYPE)
60-
self.addSubview(self.loader)
61-
self.loader.startAnimating()
57+
addSubview(loader)
58+
59+
loader.translatesAutoresizingMaskIntoConstraints = false
60+
NSLayoutConstraint.activate([
61+
loader.leadingAnchor.constraint(equalTo: leadingAnchor),
62+
loader.trailingAnchor.constraint(equalTo: trailingAnchor),
63+
loader.topAnchor.constraint(equalTo: topAnchor),
64+
loader.bottomAnchor.constraint(equalTo: bottomAnchor),
65+
])
66+
67+
loader.startAnimating()
6268
}
6369

6470
required init?(coder aDecoder: NSCoder) {
6571
fatalError("init(coder:) has not been implemented")
6672
}
6773

68-
@objc
69-
var size: NSNumber? {
70-
set {
71-
if newValue != nil {
72-
self.loader.stopAnimating()
73-
let frameObj = CGRect(x: 0, y: 0, width: newValue!.doubleValue, height: newValue!.doubleValue)
74-
self.loader.frame = frameObj
75-
self.loader.startAnimating()
76-
77-
self.currentSize = newValue!.doubleValue
78-
}
79-
}
80-
get {
81-
return nil;
82-
}
83-
}
84-
8574
@objc
8675
var name: NSString? {
87-
set {
88-
if newValue != nil && animations[newValue! as String] != nil {
89-
self.loader.stopAnimating()
90-
let frameObj = CGRect(x: 0, y: 0, width: self.currentSize, height: self.currentSize)
91-
self.loader.frame = frameObj
92-
self.loader.type = animations[newValue! as String]!
93-
self.loader.startAnimating()
94-
95-
}
96-
}
97-
get {
98-
return nil;
76+
didSet {
77+
loader.stopAnimating()
78+
loader.type = (name != nil ? animations[name! as String] : animations["BallPulse"])!
79+
loader.startAnimating()
9980
}
10081
}
10182

10283
@objc
10384
var color: NSNumber? {
104-
set {
105-
if newValue != nil {
106-
self.loader.stopAnimating()
107-
let frameObj = CGRect(x: 0, y: 0, width: self.currentSize, height: self.currentSize)
108-
self.loader.frame = frameObj
109-
self.loader.color = RCTConvert.uiColor(newValue)
110-
self.loader.startAnimating()
111-
}
112-
}
113-
get {
114-
return nil;
85+
didSet {
86+
loader.stopAnimating()
87+
loader.color = color != nil ? RCTConvert.uiColor(color) : .white
88+
loader.startAnimating()
11589
}
11690
}
11791
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-loader-kit",
3-
"version": "2.0.7",
3+
"version": "2.0.8",
44
"description": "✳️Purely native loader indicators for React Native✳️",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/index.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {
22
requireNativeComponent,
33
UIManager,
44
Platform,
5-
ViewStyle,
65
processColor,
6+
ViewProps,
77
} from 'react-native';
88
import React from 'react';
99
import PropTypes from 'prop-types';
@@ -15,19 +15,17 @@ const LINKING_ERROR =
1515
'- You rebuilt the app after installing the package\n' +
1616
'- You are not using Expo managed workflow\n';
1717

18-
type BaseProps = {
18+
interface BaseProps extends ViewProps {
1919
name?: string;
20-
size?: number;
21-
style: ViewStyle;
22-
};
20+
}
2321

24-
type LoaderKitProps = {
22+
interface LoaderKitProps extends BaseProps {
2523
color?: string;
26-
} & BaseProps;
24+
}
2725

28-
type LoaderKitNativeProps = {
26+
interface LoaderKitNativeProps extends BaseProps {
2927
color?: number;
30-
} & BaseProps;
28+
}
3129

3230
const ComponentName = 'LoaderKitView';
3331
const LoaderKitNative =
@@ -40,11 +38,11 @@ const LoaderKit = (props: LoaderKitProps) => {
4038
LoaderKit.propTypes = {
4139
name: PropTypes.oneOf(animations),
4240
color: PropTypes.string,
43-
size: Platform.OS === 'ios' ? PropTypes.number.isRequired : PropTypes.number,
4441
};
4542

4643
export default UIManager.getViewManagerConfig(ComponentName) != null
4744
? LoaderKit
4845
: () => {
4946
throw new Error(LINKING_ERROR);
5047
};
48+
export { animations };

0 commit comments

Comments
 (0)