Skip to content

Commit

Permalink
Merge pull request #81 from unflowhq/release/1.12.1
Browse files Browse the repository at this point in the history
v1.12.1
  • Loading branch information
David Newell authored Oct 5, 2022
2 parents 3d69b4c + 155389c commit ad57f3b
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 24 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ dependencies {
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.appcompat:appcompat:1.3.1"
implementation "com.unflow:unflow-ui:1.12.0"
implementation "com.unflow:unflow-ui:1.12.1"
}
4 changes: 2 additions & 2 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
UnflowReactNative_kotlinVersion=1.5.31
UnflowReactNative_compileSdkVersion=31
UnflowReactNative_kotlinVersion=1.7.10
UnflowReactNative_compileSdkVersion=32
UnflowReactNative_targetSdkVersion=31

android.useAndroidX=true
80 changes: 76 additions & 4 deletions android/src/main/java/com/unflow/reactnative/UnflowModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.unflow.reactnative
import android.util.Log
import com.facebook.react.bridge.*
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
import com.unflow.analytics.AnalyticsListener
import com.unflow.analytics.domain.model.UnflowEvent
import com.unflow.androidsdk.UnflowSdk
import com.unflow.androidsdk.ui.theme.Fonts
import kotlinx.coroutines.*
Expand All @@ -14,6 +16,7 @@ class UnflowModule(
) : ReactContextBaseJavaModule(reactContext) {

private var openerJobs: MutableSet<Job> = mutableSetOf()
private var listener: AnalyticsListener = UnflowAnalyticsListener(reactContext)

override fun getName(): String {
return "Unflow"
Expand All @@ -30,7 +33,7 @@ class UnflowModule(
UnflowSdk.initialize(
application = application,
config = UnflowSdk.Config(apiKey, enableLogging),
analyticsListener = null,
analyticsListener = listener,
activityProvider = CurrentActivityProvider { currentActivity }
)
}
Expand All @@ -40,7 +43,7 @@ class UnflowModule(
fun sync() {
UnflowSdk.client().sync()
}

@ReactMethod
fun close() {
UnflowSdk.client().close()
Expand Down Expand Up @@ -173,10 +176,79 @@ class UnflowModule(
private fun ReadableMap.getFontResId(key: String): Int? {
if (!hasKey(key)) return null

val fontId = reactContext.resources.getIdentifier(getString(key), "font", reactContext.packageName)
val fontFamily: String?;
when (getType(key)) {
ReadableType.String -> {
fontFamily = getString(key)
}
ReadableType.Map -> {
fontFamily = getMap(key)?.getString("family")
}
else -> { return null }
}

val fontId = reactContext.resources.getIdentifier(fontFamily, "font", reactContext.packageName)
if (fontId != 0) return fontId

val fontsId = reactContext.resources.getIdentifier(getString(key), "fonts", reactContext.packageName)
val fontsId = reactContext.resources.getIdentifier(fontFamily, "fonts", reactContext.packageName)
return if (fontsId != 0) fontsId else null
}
}

private class UnflowAnalyticsListener(
private val reactContext: ReactApplicationContext,
): AnalyticsListener {
override fun onEvent(event: UnflowEvent) {
emitEvent(event)
}

@ReactMethod
fun emitEvent(event: UnflowEvent) {
val eventMap = WritableNativeMap()

eventMap.putString("id", event.id)
eventMap.putString("name", event.name)
event.occurredAt?.let { eventMap.putDouble("occurredAt", it.toDouble()) }

val metadataMap = WritableNativeMap()
event.metadata.forEach {
when(it.value) {
is Long -> { metadataMap.putDouble(it.key, ((it.value as? Long) ?: 0).toDouble()) }
is Int -> { metadataMap.putInt(it.key, ((it.value as? Int) ?: 0)) }
is String -> { metadataMap.putString(it.key, (it.value as? String)) }
is Boolean -> { metadataMap.putBoolean(it.key, (it.value as? Boolean) ?: false) }
null -> { metadataMap.putNull(it.key) }
is List<*> -> { metadataMap.putArray(it.key, makeNativeArray(it.value)) }
}
}
eventMap.putMap("metadata", metadataMap)

reactContext
.getJSModule(RCTDeviceEventEmitter::class.java)
.emit("EventReceived", eventMap)
}

private fun makeNativeArray(value: Any?): WritableNativeArray {
val array = WritableNativeArray()
(value as? List<*>)?.forEach { arrayValue ->
when(arrayValue) {
is Long -> {
array.pushDouble(((arrayValue as? Long) ?: 0).toDouble())
}
is Int -> {
array.pushInt(((arrayValue as? Int) ?: 0))
}
is String -> {
array.pushString((arrayValue as? String))
}
is Boolean -> {
array.pushBoolean(arrayValue as? Boolean ?: false)
}
null -> {
array.pushNull()
}
}
}
return array
}
}
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
buildscript {
ext {
minSdkVersion = 21
compileSdkVersion = 31
compileSdkVersion = 32
targetSdkVersion = 31
ndkVersion = "21.4.7075529"
}
Expand Down
10 changes: 5 additions & 5 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@ PODS:
- React-RCTImage
- RNSVG (12.3.0):
- React-Core
- Unflow (1.12.0)
- unflow-react-native (1.12.0):
- Unflow (1.12.1)
- unflow-react-native (1.12.1):
- React-Core
- Unflow (= 1.12.0)
- Unflow (= 1.12.1)
- Yoga (1.14.0)
- YogaKit (1.18.1):
- Yoga (~> 1.14)
Expand Down Expand Up @@ -562,8 +562,8 @@ SPEC CHECKSUMS:
RNGestureHandler: 6e757e487a4834e7280e98e9bac66d2d9c575e9c
RNScreens: 40a2cb40a02a609938137a1e0acfbf8fc9eebf19
RNSVG: 302bfc9905bd8122f08966dc2ce2d07b7b52b9f8
Unflow: c533856531e3a4041516b10267ad97bb32a6c975
unflow-react-native: d05cf97fa26efda0ff69aa0a8ea5cf03e599d24d
Unflow: 6cdf0cea5e2ff605321d73b663b7e5ddce8aa19e
unflow-react-native: e20f7d3479b716544eccff572ec10b69ca3127bb
Yoga: 5cbf25add73edb290e1067017690f7ebf56c5468
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

Expand Down
13 changes: 11 additions & 2 deletions example/src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'react-native-gesture-handler';
import * as React from 'react';
import React, { useEffect } from 'react';
import Unflow from 'unflow-react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
Expand All @@ -20,11 +20,20 @@ export default function App() {
Unflow.initialize('ae469c05ed6d8f7c1d46d12901f661c9', true);
Unflow.setUserId('unflow-mirror_react-native');
Unflow.sync();
Unflow.setCustomFonts({ button: 'shadowsintolight_regular' });
// Unflow.setCustomFonts({ button: 'shadowsintolight_regular' });
Unflow.setCustomFonts({
button: { family: 'shadowsintolight_regular', size: 26 },
});

const unflowAnalyticsListener = (event) => {
console.log(event);
};

useEffect(() => {
let subscription = Unflow.addAnalyticsListener(unflowAnalyticsListener);
return () => Unflow.removeAnalyticsListener(subscription);
}, []);

return (
<GestureHandlerRootView style={styles.container}>
<NavigationContainer>
Expand Down
22 changes: 18 additions & 4 deletions ios/Unflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ import Combine

class UnflowAnalyticsListener: UnflowUI.AnalyticsListener {
func onEvent(event: UnflowUI.UnflowEvent) {
print("\(event.name) metadata: \(event.metadata)")
print("UnflowEvent: \(event.name) metadata: \(event.metadata)")
if #available(iOS 13, *) {
EventEmitter.sharedInstance.dispatch(
name: EventName.eventReceived.key,
body: [
"id": event.id,
"name": event.name,
"metadata": event.metadata,
"occurredAt": event.occuredAt
]
)
}
}
}


class EventMetadata: Codable {}

fileprivate extension Decodable {
Expand All @@ -21,12 +33,14 @@ fileprivate extension Decodable {
@objc(Unflow)
class Unflow: NSObject {

private let listener = UnflowAnalyticsListener()

@objc(initialize:withEnableLogging:)
func initialize(apiKey: String, enableLogging: Bool) -> Void {
if #available(iOS 13.0, *) {
_ = UnflowSDK.initialize(
config: UnflowSDK.Config(apiKey: apiKey, enableLogging: enableLogging),
analyticsListener: UnflowAnalyticsListener()
analyticsListener: listener
)
}
}
Expand Down Expand Up @@ -140,7 +154,7 @@ class Unflow: NSObject {
] as [String : Any?]
}
EventEmitter.sharedInstance.dispatch(
name: "OpenersChanged",
name: EventName.openersChanged.key,
body: [spaceKey: mappedOpeners]
)
}
Expand Down Expand Up @@ -169,7 +183,7 @@ class Unflow: NSObject {
]
}
EventEmitter.sharedInstance.dispatch(
name: "SpacesChanged",
name: EventName.spacesChanged.key,
body: mappedSpaces
)
}
Expand Down
17 changes: 16 additions & 1 deletion ios/UnflowEventEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,29 @@ class UnflowEventEmitter: RCTEventEmitter {
}

@objc open override func supportedEvents() -> [String] {
["OpenersChanged", "SpacesChanged"]
EventName.allCases.map(\.key)
}

@objc override static func requiresMainQueueSetup() -> Bool {
return true
}
}

@objc enum EventName: Int, CaseIterable {
case openersChanged, spacesChanged, eventReceived

var key: String {
switch self {
case .openersChanged:
return "OpenersChanged"
case .spacesChanged:
return "SpacesChanged"
case .eventReceived:
return "EventReceived"
}
}
}

@available(iOS 13.0, *)
class EventEmitter {

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unflow-react-native",
"version": "1.12.0",
"version": "1.12.1",
"description": "Tired of building the same simple screens over and over again? Empower your product team to create and ship content using the Unflow mobile SDK.",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
19 changes: 19 additions & 0 deletions src/analytics-listener.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { EmitterSubscription } from 'react-native';
import { EventEmitter } from './native-emitter';
import type { UnflowEvent } from './types';

export function addAnalyticsListener(
callback: (event: UnflowEvent) => void
): EmitterSubscription | undefined {
let subscription: EmitterSubscription | undefined;
if (EventEmitter) {
subscription = EventEmitter.addListener('EventReceived', callback);
}
return subscription;
}

export function removeAnalyticsListener(
subscription: EmitterSubscription
): void {
if (subscription) subscription.remove();
}
11 changes: 10 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { NativeModules } from 'react-native';
import {
addAnalyticsListener,
removeAnalyticsListener,
} from './analytics-listener';
import OpenerView, { useSpace } from './opener-view';
import SpacesView, { useSpaces } from './spaces-view';
import type { UnflowType } from './types';

const { Unflow } = NativeModules;

export { OpenerView, SpacesView, useSpace, useSpaces };
export default Unflow as UnflowType;

export default {
...Unflow,
addAnalyticsListener: addAnalyticsListener,
removeAnalyticsListener: removeAnalyticsListener,
} as UnflowType;
Loading

0 comments on commit ad57f3b

Please sign in to comment.