๐Ÿ“ฆ manideepk90 / react-native-hyperswitch-sdk

๐Ÿ“„ HyperSwitchSDK.kt ยท 119 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119package io.hyperswitch

import android.app.Activity
import android.app.Application
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.facebook.react.PackageList
import com.facebook.react.ReactFragment
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeApplicationEntryPoint.loadReactNative
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.defaults.DefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.react.runtime.hermes.HermesInstance

class HyperSwitchSDK(
    private val reactNativeHost: ReactNativeHost, private val reactHost: ReactHost
) {

    public fun getReactHost(): ReactHost {
        return reactHost
    }

    public fun getReactNativeHost(): ReactNativeHost {
        return reactNativeHost
    }

    fun presentFragment(activity: Activity) {
        val propsBundle = Bundle().apply {
            putString("type", "payment")
            putString("from", "rn")
        }
        val hyperSwitchFragment = HyperswitchFragment.Builder().setComponentName("hyperSwitch").setLaunchOptions(propsBundle).build()

        val fragmentManager: FragmentManager = (activity as FragmentActivity).supportFragmentManager
        val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.add(android.R.id.content,hyperSwitchFragment, "HyperPaymentSheet")
        fragmentTransaction.addToBackStack("HyperPaymentSheet")
        fragmentTransaction.commit()
    }

    companion object {
        lateinit var shared: HyperSwitchSDK

        private var isInitialized = false

        private var isSoLoaderInit = false

        private fun initReactNative(application: Application) {
            if (!isSoLoaderInit) {
                loadReactNative(application)
                isSoLoaderInit = true
            }
        }

        fun init(application: Application, reactHost: ReactHost, reactNativeHost: ReactNativeHost) {
            if (!isInitialized) {
                initReactNative(application)
                shared = HyperSwitchSDK(reactNativeHost, reactHost)
                isInitialized = true
            }
        }

        /*
        Remember to call the InitReactNative before creating the ReactHost and reactNativeHost
         */
        fun init(application: Application, packageList: List<ReactPackage>, enableOTA: Boolean) {
            initReactNative(application)

            val useDeveloperSupport = BuildConfig.DEBUG

            val reactHost = DefaultReactHost.getDefaultReactHost(
                application,
                packageList,
                "index",
                "hyperswitch",
                "assets://hyperswitch.bundle", // TODO need to OTA here
                HermesInstance(),
                useDeveloperSupport
            )
            val reactNativeHost = object : DefaultReactNativeHost(application) {
                override fun getPackages(): List<ReactPackage> = packageList

                override fun getJSMainModuleName(): String = "index"

                override fun getJSBundleFile(): String? {
                    return "assets://hyperswitch.bundle"

                }

                override fun getUseDeveloperSupport(): Boolean = useDeveloperSupport

                override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
                override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED

//                @Deprecated(
//                    "Setting isHermesEnabled inside `ReactNativeHost` is deprecated and this field will be ignored. If this field is set to true, you can safely remove it. If this field is set to false, please follow the setup on https://github.com/react-native-community/javascriptcore to continue using JSC",
//                    replaceWith = ReplaceWith("")
//                )
//                override val isHermesEnabled: Boolean = true
            }
            init(application, reactHost, reactNativeHost)

        }

        fun init(application: Application, enableOTA: Boolean = true) {
            init(application, packageList = PackageList(application).packages, enableOTA)
        }

        fun init(application: Application) {
            init(application, enableOTA = true)
        }
    }


}