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

Switcher and Bottom Tabs Animations and UI Performance Improvements #13470

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Implement React Native Reanimated V2
  • Loading branch information
Parveshdhull committed Jun 28, 2022
commit d9d3f2bb734105c4b291ae9e66079d5df97943b6
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"presets": [
"module:metro-react-native-babel-preset"
],
"plugins": [
"react-native-reanimated/plugin"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import im.status.ethereum.pushnotifications.PushNotificationPackage;
import im.status.ethereum.StatusOkHttpClientFactory;

import com.facebook.react.bridge.JSIModulePackage;
import com.swmansion.reanimated.ReanimatedJSIModulePackage;

public class MainApplication extends NavigationApplication {

private final ReactNativeHost mReactNativeHost = new NavigationReactNativeHost(this) {
Expand All @@ -48,6 +51,11 @@ protected List<ReactPackage> getPackages() {
protected String getJSMainModuleName() {
return "index";
}

@Override
protected JSIModulePackage getJSIModulePackage() {
return new ReanimatedJSIModulePackage();
}
};

@Override
Expand Down
4 changes: 2 additions & 2 deletions nix/mobile/android/release.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ in stdenv.mkDerivation rec {
filter = lib.mkFilter {
root = path;
include = [
"package.json" "yarn.lock" "metro.config.js"
"resources/.*" "translations/.*"
"package.json" "yarn.lock" "metro.config.js" ".babelrc"
"resources/.*" "translations/.*" "src/js/worklet_factory.js"
"modules/react-native-status/android.*" "android/.*"
envFileName "VERSION" ".watchmanconfig"
"status-go-version.json" "react-native.config.js"
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"app:android": "react-native run-android"
},
"dependencies": {
"@babel/preset-typescript": "^7.17.12",
"@react-native-community/async-storage": "^1.11.0",
"@react-native-community/audio-toolkit": "git+https://github.com/tbenr/react-native-audio-toolkit.git#refs/tags/v2.0.3-status-v6",
"@react-native-community/blur": "^3.6.0",
Expand Down Expand Up @@ -60,7 +61,7 @@
"react-native-navigation": "^7.27.1",
"react-native-permissions": "^2.1.5",
"react-native-randombytes": "^3.6.1",
"react-native-reanimated": "^2.1.0",
"react-native-reanimated": "2.3.3",
"react-native-redash": "^16.0.11",
"react-native-safe-area-context": "^2.0.0",
"react-native-shake": "^3.3.1",
Expand All @@ -75,12 +76,12 @@
"web3-utils": "^1.2.1"
},
"devDependencies": {
"@mapbox/node-pre-gyp": "^1.0.9",
"@babel/generator": "7.0.0",
"@babel/helper-builder-react-jsx": "7.0.0",
"@babel/plugin-transform-block-scoping": "7.0.0",
"@babel/preset-env": "7.1.0",
"@babel/register": "7.0.0",
"@mapbox/node-pre-gyp": "^1.0.9",
"clj-kondo": "^2020.1.13",
"coveralls": "^3.0.4",
"jest": "^25.1.0",
Expand Down
69 changes: 69 additions & 0 deletions src/js/worklet_factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useDerivedValue } from 'react-native-reanimated';

// Generic Worklets

export function applyAnimationsToStyle(animations, style) {
return function() {
'worklet'

var animatedStyle = {}

for (var key in animations) {
if (key == "transform") {
var transforms = animations[key];
var animatedTransforms = []

for (var transform of transforms) {
var transformKey = Object.keys(transform)[0];
animatedTransforms.push({
[transformKey]: transform[transformKey].value
})
}

animatedStyle[key] = animatedTransforms;
} else {
animatedStyle[key] = animations[key].value;
}
}

return Object.assign(animatedStyle, style);
};
};

// Switcher Worklets

export function switcherCloseButtonOpacity (switcherButtonOpacity) {
return useDerivedValue(
function () {
'worklet'
return 1 - switcherButtonOpacity.value;
}
);
}

export function switcherScreenRadius (switcherScreenSize) {
return useDerivedValue(
function () {
'worklet'
return switcherScreenSize.value/2;
}
);
}

export function switcherScreenBottomPosition (switcherScreenRadius, switcherPressedRadius, initalPosition) {
return useDerivedValue(
function () {
'worklet'
return initalPosition + switcherPressedRadius - switcherScreenRadius.value;
}
);
}

export function switcherContainerBottomPosition (switcherScreenBottom, heightOffset) {
return useDerivedValue(
function () {
'worklet'
return - (switcherScreenBottom.value + heightOffset);
}
);
}
92 changes: 51 additions & 41 deletions src/mocks/js_dependencies.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
:ValueXY (fn [])
:View {}
:FlatList {}
:Image {}
:ScrollView {}
:Text {}}
:Easing {:bezier (fn [])
Expand Down Expand Up @@ -152,46 +151,53 @@

(def react-native-haptic-feedback #js {:default #js {:trigger nil}})

(def react-native-reanimated #js {:default #js {:createAnimatedComponent identity
:eq nil
:greaterOrEq nil
:greaterThan nil
:lessThan nil
:lessOrEq nil
:add nil
:diff nil
:divide nil
:sub nil
:multiply nil
:abs nil
:min nil
:max nil
:neq nil
:and nil
:or nil
:not nil
:set nil
:startClock nil
:stopClock nil
:Value nil
:Clock nil
:debug nil
:log nil
:event nil
:cond nil
:block nil
:interpolateNode nil
:call nil
:timing nil
:onChange nil
:View #js {}
:ScrollView #js {}
:Text #js {}
:Extrapolate #js {:CLAMP nil}
:Code #js {}}
:EasingNode #js {:bezier identity
:linear identity}
:clockRunning nil})
(def react-native-reanimated #js {:default #js {:createAnimatedComponent identity
:eq nil
:greaterOrEq nil
:greaterThan nil
:lessThan nil
:lessOrEq nil
:add nil
:diff nil
:divide nil
:sub nil
:multiply nil
:abs nil
:min nil
:max nil
:neq nil
:and nil
:or nil
:not nil
:set nil
:startClock nil
:stopClock nil
:Value nil
:Clock nil
:debug nil
:log nil
:event nil
:cond nil
:block nil
:interpolateNode nil
:call nil
:timing nil
:onChange nil
:View #js {}
:Image #js {}
:ScrollView #js {}
:Text #js {}
:Extrapolate #js {:CLAMP nil}
:Code #js {}}
:EasingNode #js {:bezier identity
:linear identity}
:clockRunning nil
:useSharedValue (fn [])
:useAnimatedStyle (fn [])
:withTiming (fn [])
:withDelay (fn [])
:Easing #js {:bezier identity}
:Keyframe (fn [])})
(def react-native-gesture-handler #js {:default #js {}
:State #js {:BEGAN nil
:ACTIVE nil
Expand Down Expand Up @@ -256,6 +262,9 @@
:proposal nil
:updated nil}}})

(def worklet-factory
#js {:applyAnimationsToStyle (fn [])})

;; Update i18n_resources.cljs
(defn mock [module]
(case module
Expand Down Expand Up @@ -299,6 +308,7 @@
"react-native-share" react-native-share
"@react-native-community/async-storage" async-storage
"react-native-svg" react-native-svg
"../src/js/worklet_factory.js" worklet-factory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

./js/worklet_factory.js ?

"./fleets.js" default-fleets
"./chats.js" default-chats
"@walletconnect/client" wallet-connect-client
Expand Down
69 changes: 69 additions & 0 deletions src/quo2/reanimated.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(ns quo2.reanimated
(:require ["react-native" :as rn]
[reagent.core :as reagent]
[clojure.string :as string]
["react-native-reanimated" :default reanimated
:refer (useSharedValue useAnimatedStyle withTiming withDelay Easing Keyframe)]))

;; Animated Components
(def create-animated-component (comp reagent/adapt-react-class (.-createAnimatedComponent reanimated)))

(def view (reagent/adapt-react-class (.-View reanimated)))
(def image (reagent/adapt-react-class (.-Image reanimated)))
(def touchable-opacity (create-animated-component (.-TouchableOpacity ^js rn)))

;; Hooks
(def use-shared-value useSharedValue)
(def use-animated-style useAnimatedStyle)

;; Animations
(def with-timing withTiming)
(def with-delay withDelay)
(def key-frame Keyframe)

;; Easings
(def bezier (.-bezier ^js Easing))

(def easings {:linear (bezier 0 0 1 1)
:easing1 (bezier 0.25 0.1 0.25 1) ;; TODO(parvesh) - rename easing functions, (design team input)
:easing2 (bezier 0 0.3 0.6 0.9)
:easing3 (bezier 0.3 0.3 0.3 0.9)})

;; Helper functions
(defn get-shared-value [anim]
(.-value anim))

(defn set-shared-value [anim val]
(set! (.-value anim) val))

(defn kebab-case->camelCase [k]
(let [words (string/split (name k) #"-")]
(->> (map string/capitalize (rest words))
(apply str (first words))
keyword)))

(defn map-keys [f m]
(->> (map (fn [[k v]] [(f k) v]) m)
(into {})))

;; Worklets
(def worklet-factory (js/require "../src/js/worklet_factory.js"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

./js/worklet_factory.js ?

Copy link
Member Author

@Parveshdhull Parveshdhull Jun 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason js/require is only working with ../src/js/worklet_factory.js

"https://user-images.githubusercontent.com/17097240/175988135-909b2ee2-a4fe-4ad5-ae84-1aaf30bdc953.mp4"

Github video player is unable to play this video, only playing locally


;;;; Component Animations

;; kebab-case styles are not working for worklets
;; so first convert kebab case styles into camel case styles
(defn apply-animations-to-style [animations style]
(let [animations (map-keys kebab-case->camelCase animations)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you elaborate why map-keys and kebab-case->camelCase

Copy link
Member Author

@Parveshdhull Parveshdhull Jun 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I think kebab case only works with clojure due to reagent, but as worklets are inside js file which is directly processed by metro instead of shadow-cljs (due to js/require for directives) only camel case works there

style (apply dissoc (map-keys kebab-case->camelCase style) (keys animations))]
(use-animated-style
(.applyAnimationsToStyle ^js worklet-factory (clj->js animations) (clj->js style)))))

;; Animators
(defn animate-shared-value-with-timing [anim val duration easing]
(set-shared-value anim (with-timing val (js-obj "duration" duration
"easing" (get easings easing)))))

(defn animate-shared-value-with-delay [anim val duration easing delay]
(set-shared-value anim (with-delay delay (with-timing val (js-obj "duration" duration
"easing" (get easings easing))))))
Loading