feat: Add native module and package for custom functionality

This commit is contained in:
2025-12-12 23:45:12 +05:30
parent 801725edf3
commit 8613fd7d30
4 changed files with 51 additions and 6 deletions

View File

@@ -5,7 +5,7 @@ import {
StyleSheet, StyleSheet,
Text, Text,
TouchableOpacity, TouchableOpacity,
Alert, Alert, NativeModules
} from 'react-native'; } from 'react-native';
import { SafeAreaProvider } from 'react-native-safe-area-context'; import { SafeAreaProvider } from 'react-native-safe-area-context';
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
@@ -14,6 +14,7 @@ import Register from './src/components/Register';
import { authAPI } from './src/services/authAPI'; import { authAPI } from './src/services/authAPI';
import { networkService } from './src/services/networkService'; import { networkService } from './src/services/networkService';
type Screen = 'login' | 'register' | 'home'; type Screen = 'login' | 'register' | 'home';
const { MyNativeModule } = NativeModules;
function App() { function App() {
const isDarkMode = useColorScheme() === 'dark'; const isDarkMode = useColorScheme() === 'dark';
@@ -25,6 +26,10 @@ function App() {
// Initialize app // Initialize app
useEffect(() => { useEffect(() => {
initializeApp(); initializeApp();
console.log("MyNativeModule", MyNativeModule);
MyNativeModule.greet("John").then((msg: any) => {
console.log(msg);
});
// Listen for network changes // Listen for network changes
const unsubscribe = networkService.addListener((networkState) => { const unsubscribe = networkService.addListener((networkState) => {

View File

@@ -10,13 +10,15 @@ import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
class MainApplication : Application(), ReactApplication { class MainApplication : Application(), ReactApplication {
override val reactHost: ReactHost by lazy { override val reactHost: ReactHost by lazy {
val packages = PackageList(this).packages.toMutableList()
// ✅ Add your custom package HERE (Kotlin syntax)
packages.add(NativeModulesPackage())
getDefaultReactHost( getDefaultReactHost(
context = applicationContext, context = applicationContext,
packageList = packageList = packages,
PackageList(this).packages.apply {
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
},
) )
} }

View File

@@ -0,0 +1,19 @@
package com.lynkeduppro
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
class MyNativeModule(private val reactContext: ReactApplicationContext)
: ReactContextBaseJavaModule(reactContext) {
override fun getName(): String {
return "MyNativeModule" // JS module name
}
@ReactMethod
fun greet(name: String, promise: Promise) {
promise.resolve("Hello $name from Android(Kotlin)!")
}
}

View File

@@ -0,0 +1,19 @@
package com.lynkeduppro
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager
class NativeModulesPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(
MyNativeModule(reactContext)
)
}
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return emptyList()
}
}