Skip to content

Commit

Permalink
fix(react-navigation): new way to handle deep link
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Feb 28, 2019
1 parent f1b5457 commit b5b65c8
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 48 deletions.
95 changes: 84 additions & 11 deletions client/react-native/common/components/App.js
Expand Up @@ -16,11 +16,87 @@ import NavigationService from './../helpers/NavigationService'
import { AppNavigator } from './Navigator/AppNavigator'
import { RelayContext } from '../relay'

const { CoreModule } = NativeModules

class HandleDeepLink extends PureComponent {
static router = AppNavigator.router

async componentDidUpdate (nextProps) {
if (nextProps.screenProps.deepLink !== this.props.screenProps.deepLink) {
this.openDeepLink()
}
}

getActiveRouteName = navigationState => {
if (!navigationState) {
return null
}
const route = navigationState.routes[navigationState.index]
// dive into nested navigators
if (route.routes) {
return this.getActiveRouteName(route)
}

// get fragment from react-navigation params
const fragment = Object.keys(route.params || {}).reduce((fragment, key) => {
const paramType = typeof route.params[key]
if (
paramType === 'string' ||
paramType === 'number' ||
paramType === 'boolean'
) {
let val = route.params[key]
if (key === 'id') {
val = atob(val)
val = val.match(/:(.*)$/)
val = val[1]
}
fragment += fragment.length > 0 ? `,${key}=${val}` : `#${key}=${val}`
}
return fragment
}, '')
return route.routeName + fragment
}

openDeepLink = () => {
const {
screenProps: { deepLink, clearDeepLink },
} = this.props

if (!deepLink) {
return
}
console.log('dispatch deepLink ', deepLink)
this.props.navigation.dispatch(NavigationActions.navigate(deepLink))
clearDeepLink()
}

render () {
return (
<AppNavigator
{...this.props}
ref={nav => {
this.navigation = nav
NavigationService.setTopLevelNavigator(nav)
}}
onNavigationStateChange={(prevState, currentState) => {
const currentRoute = this.getActiveRouteName(currentState)
const prevRoute = this.getActiveRouteName(prevState)

if (prevRoute !== currentRoute) {
CoreModule.setCurrentRoute(currentRoute)
}
}}
/>
)
}
}

let AppContainer = {}
if (Platform.OS !== 'web') {
AppContainer = createAppContainer(AppNavigator)
AppContainer = createAppContainer(HandleDeepLink)
} else {
AppContainer = AppNavigator
AppContainer = HandleDeepLink
}

export default class App extends PureComponent {
Expand Down Expand Up @@ -60,6 +136,12 @@ export default class App extends PureComponent {
componentDidMount () {
ReactNativeLanguages.addEventListener('change', this._onLanguageChange)

if (this._handleOpenURL === undefined) {
this._handleOpenURL = this.handleOpenURL.bind(this)
}

Linking.addEventListener('url', this._handleOpenURL)

Linking.getInitialURL()
.then(url => {
if (url !== null) {
Expand All @@ -68,11 +150,6 @@ export default class App extends PureComponent {
})
.catch(() => {})

if (this._handleOpenURL === undefined) {
this._handleOpenURL = this.handleOpenURL.bind(this)
}

Linking.addEventListener('url', this._handleOpenURL)
this.setState({ loading: false })
}

Expand Down Expand Up @@ -164,10 +241,6 @@ export default class App extends PureComponent {
{ !loading
? <RelayContext.Provider value={{ ...relayContext, setStateBis: this.setStateBis }}>
<AppContainer
ref={nav => {
this.navigation = nav
NavigationService.setTopLevelNavigator(nav)
}}
screenProps={{
deepLink,
setDeepLink: (deepLink) => this.setDeepLink(deepLink),
Expand Down
Expand Up @@ -14,7 +14,7 @@ const TabBarIcon = (tintColor, routeName, badgeValue) => {
chats: 'message-circle',
settings: 'settings',
}[routeName]
console.log('routeName', routeName)

return (
<Icon.Badge
name={iconName}
Expand Down
32 changes: 15 additions & 17 deletions client/react-native/common/components/Screens/Accounts/Auth.js
Expand Up @@ -56,24 +56,20 @@ class Auth extends PureComponent {
}

openDeepLink = () => {
if (this.props.screenProps === 'undefined') {
// return
}

// const {
// screenProps: {
// deepLink,
// clearDeepLink,
// },
// navigation,
// } = this.props
const {
screenProps: {
deepLink,
clearDeepLink,
},
navigation,
} = this.props

// if (!deepLink || deepLink === 'undefined' || Platform.OS === 'web') {
// return
// }
if (!deepLink || deepLink === 'undefined' || Platform.OS === 'web') {
return
}

// navigation.dispatch(NavigationActions.navigate(deepLink))
// clearDeepLink()
navigation.navigate(deepLink)
clearDeepLink()
}

getRelayContext = async () => {
Expand Down Expand Up @@ -163,7 +159,9 @@ class Auth extends PureComponent {
}
)

this.props.navigation.navigate('switch/picker', { firstLaunch })
if (this.props.screenProps !== 'undefined' && !this.props.screenProps.deepLink || this.props.screenProps.deepLink === 'undefined' || Platform.OS === 'web') {
this.props.navigation.navigate('switch/picker', { firstLaunch })
}
}

async componentDidMount () {
Expand Down
10 changes: 10 additions & 0 deletions client/react-native/ios/Berty.xcodeproj/project.pbxproj
Expand Up @@ -55,6 +55,8 @@
8D1BA426216CF8EA00FEF0AE /* libcrypto.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D1BA425216CF8EA00FEF0AE /* libcrypto.a */; };
8D1BA428216CF8F700FEF0AE /* libssl.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D1BA427216CF8F700FEF0AE /* libssl.a */; };
90E88BBFF04C4F05A6D26075 /* libRNDevMenu.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 46D4617AEDB145E1B15B2216 /* libRNDevMenu.a */; };
990E84192226DCB100751E73 /* RCTLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 990E84182226DCB100751E73 /* RCTLog.swift */; };
990E845C2226DCCB00751E73 /* RCTSwiftLog.m in Sources */ = {isa = PBXBuildFile; fileRef = 990E845B2226DCCB00751E73 /* RCTSwiftLog.m */; };
991A11A92201B169006F3200 /* fontello.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 991A116D2201B169006F3200 /* fontello.ttf */; };
993E33F82200C2F100AD98F8 /* Lottie.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 993E33EE2200C2E200AD98F8 /* Lottie.framework */; };
998B6ADC2179E1B90029B33C /* CoreBluetooth.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 998B6ADB2179E1B90029B33C /* CoreBluetooth.framework */; };
Expand Down Expand Up @@ -670,6 +672,9 @@
8D1BA425216CF8EA00FEF0AE /* libcrypto.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libcrypto.a; path = ../../common/openssl/built/ios/libcrypto.a; sourceTree = "<group>"; };
8D1BA427216CF8F700FEF0AE /* libssl.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libssl.a; path = ../../common/openssl/built/ios/libssl.a; sourceTree = "<group>"; };
8E1D0314925B45DFBA2E4B6B /* Lottie.xcodeproj */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = "wrapper.pb-project"; name = Lottie.xcodeproj; path = "../node_modules/lottie-ios/Lottie.xcodeproj"; sourceTree = "<group>"; };
990E84182226DCB100751E73 /* RCTLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RCTLog.swift; sourceTree = "<group>"; };
990E845A2226DCCB00751E73 /* RCTSwiftLog.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTSwiftLog.h; sourceTree = "<group>"; };
990E845B2226DCCB00751E73 /* RCTSwiftLog.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCTSwiftLog.m; sourceTree = "<group>"; };
991A116D2201B169006F3200 /* fontello.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; name = fontello.ttf; path = ../common/static/svg/fontello.ttf; sourceTree = "<group>"; };
9977408CAA8E4D17AF46013D /* RNInstabug.xcodeproj */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = "wrapper.pb-project"; name = RNInstabug.xcodeproj; path = "../node_modules/instabug-reactnative/ios/RNInstabug.xcodeproj"; sourceTree = "<group>"; };
998B6ADB2179E1B90029B33C /* CoreBluetooth.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreBluetooth.framework; path = System/Library/Frameworks/CoreBluetooth.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -863,6 +868,9 @@
13B07FB51A68108700A75B9A /* Images.xcassets */,
13B07FB61A68108700A75B9A /* Info.plist */,
13B07FB11A68108700A75B9A /* LaunchScreen.xib */,
990E84182226DCB100751E73 /* RCTLog.swift */,
990E845A2226DCCB00751E73 /* RCTSwiftLog.h */,
990E845B2226DCCB00751E73 /* RCTSwiftLog.m */,
);
name = Berty;
sourceTree = "<group>";
Expand Down Expand Up @@ -2094,6 +2102,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
990E845C2226DCCB00751E73 /* RCTSwiftLog.m in Sources */,
990E84192226DCB100751E73 /* RCTLog.swift in Sources */,
F844DEC121E8C5FD00A810A1 /* AppDelegate.swift in Sources */,
F8799D7B2136F44E003B9FD7 /* CoreModule.swift in Sources */,
F8799D7A2136F44B003B9FD7 /* CoreModule.m in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions client/react-native/ios/Berty/Berty-Bridging-Header.h
Expand Up @@ -9,3 +9,5 @@
#import "React/RCTRootView.h"
#import "React/RCTLinkingManager.h"
#import "React/RCTPushNotificationManager.h"
#import "React/RCTLog.h"
#import "RCTSwiftLog.h"
31 changes: 22 additions & 9 deletions client/react-native/ios/RCTLog.swift
@@ -1,9 +1,22 @@
//
// RCTLog.swift
// Berty
//
// Created by sacha on 27/02/2019.
// Copyright © 2019 Berty Technologies. All rights reserved.
//

import Foundation



func RCTLogError(_ message: String, _ file: String=#file, _ line: UInt=#line) {
RCTSwiftLog.error(message, file: file, line: line)
}

func RCTLogWarn(_ message: String, _ file: String=#file, _ line: UInt=#line) {
RCTSwiftLog.warn(message, file: file, line: line)
}

func RCTLogInfo(_ message: String, _ file: String=#file, _ line: UInt=#line) {
RCTSwiftLog.info(message, file: file, line: line)
}

func RCTLog(_ message: String, _ file: String=#file, _ line: UInt=#line) {
RCTSwiftLog.log(message, file: file, line: line)
}

func RCTLogTrace(_ message: String, _ file: String=#file, _ line: UInt=#line) {
RCTSwiftLog.trace(message, file: file, line: line)
}
16 changes: 9 additions & 7 deletions client/react-native/ios/RCTSwiftLog.h
@@ -1,17 +1,19 @@
//
// RCTSwiftLog.h
// Berty
//
// Created by sacha on 27/02/2019.
// Copyright © 2019 Berty Technologies. All rights reserved.
//
// Created by Jimmy Dee on 4/5/17.
//
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface RCTSwiftLog : NSObject

@end
+ (void)error:(NSString * _Nonnull)message file:(NSString * _Nonnull)file line:(NSUInteger)line;
+ (void)warn:(NSString * _Nonnull)message file:(NSString * _Nonnull)file line:(NSUInteger)line;
+ (void)info:(NSString * _Nonnull)message file:(NSString * _Nonnull)file line:(NSUInteger)line;
+ (void)log:(NSString * _Nonnull)message file:(NSString * _Nonnull)file line:(NSUInteger)line;
+ (void)trace:(NSString * _Nonnull)message file:(NSString * _Nonnull)file line:(NSUInteger)line;

NS_ASSUME_NONNULL_END
@end
33 changes: 30 additions & 3 deletions client/react-native/ios/RCTSwiftLog.m
@@ -1,13 +1,40 @@
//
// RCTSwiftLog.m
// Berty
//
// Created by sacha on 27/02/2019.
// Copyright © 2019 Berty Technologies. All rights reserved.
//
// Created by Jimmy Dee on 4/5/17.
//
//

#import <React/RCTLog.h>

#import "RCTSwiftLog.h"

@implementation RCTSwiftLog

+ (void)info:(NSString *)message file:(NSString *)file line:(NSUInteger)line
{
_RCTLogNativeInternal(RCTLogLevelInfo, file.UTF8String, (int)line, @"%@", message);
}

+ (void)warn:(NSString *)message file:(NSString *)file line:(NSUInteger)line
{
_RCTLogNativeInternal(RCTLogLevelWarning, file.UTF8String, (int)line, @"%@", message);
}

+ (void)error:(NSString *)message file:(NSString *)file line:(NSUInteger)line
{
_RCTLogNativeInternal(RCTLogLevelError, file.UTF8String, (int)line, @"%@", message);
}

+ (void)log:(NSString *)message file:(NSString *)file line:(NSUInteger)line
{
_RCTLogNativeInternal(RCTLogLevelInfo, file.UTF8String, (int)line, @"%@", message);
}

+ (void)trace:(NSString *)message file:(NSString *)file line:(NSUInteger)line
{
_RCTLogNativeInternal(RCTLogLevelTrace, file.UTF8String, (int)line, @"%@", message);
}

@end

0 comments on commit b5b65c8

Please sign in to comment.