Skip to content

Commit

Permalink
feat: Also create Android part
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jan 15, 2025
1 parent e4b80d0 commit e96b672
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/nitrogen/src/views/ViewComponentShadowNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface ViewComponentNames {
shadowNodeClassName: `${string}ShadowNode`
descriptorClassName: `${string}ComponentDescriptor`
component: `${string}Component`
manager: `${string}Manager`
}

export function getViewComponentNames(
Expand All @@ -25,6 +26,7 @@ export function getViewComponentNames(
shadowNodeClassName: `${name.HybridT}ShadowNode`,
descriptorClassName: `${name.HybridT}ComponentDescriptor`,
component: `${spec.name}Component`,
manager: `${spec.name}Manager`,
}
}

Expand Down
53 changes: 51 additions & 2 deletions packages/nitrogen/src/views/kotlin/KotlinHybridViewManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,57 @@
import type { SourceFile } from '../../syntax/SourceFile.js'
import type { HybridObjectSpec } from '../../syntax/HybridObjectSpec.js'
import {
createViewComponentShadowNodeFiles,
getViewComponentNames,
} from '../ViewComponentShadowNode.js'
import { createFileMetadataString } from '../../syntax/helpers.js'
import { NitroConfig } from '../../config/NitroConfig.js'

export function createKotlinHybridViewManager(
_: HybridObjectSpec
spec: HybridObjectSpec
): SourceFile[] {
return []
const cppFiles = createViewComponentShadowNodeFiles(spec)
const javaNamespace = NitroConfig.getAndroidPackage('java/kotlin', 'views')
const { manager } = getViewComponentNames(spec)

const mmFile = `
${createFileMetadataString(`${manager}.kt`)}
package ${javaNamespace}
import android.view.View
import com.facebook.react.fabric.StateWrapperImpl
import com.facebook.react.uimanager.ReactStylesDiffMap
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.uimanager.ThemedReactContext
class ${manager} extends SimpleViewManager<View> {
override fun getName(): String {
return "${spec.name}"
}
override fun createViewInstance(reactContext: ThemedReactContext): View {
throw Error("Not yet implemented!")
}
override fun updateState(view: View, props: ReactStylesDiffMap, stateWrapper: StateWrapper): Any {
val stateWrapperImpl = stateWrapper as? StateWrapperImpl ?: throw Error("StateWrapper uses a different implementation!")
// TODO: Get props from stateWrapperImpl and update them in HybridView
return super.updateState(view, props, stateWrapper)
}
}
`

return [
...cppFiles,
{
content: mmFile,
language: 'kotlin',
name: `${manager}.kt`,
platform: 'android',
subdirectory: [...javaNamespace.split('.')],
},
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
///
/// TestViewManager.kt
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © 2025 Marc Rousavy @ Margelo
///

package com.margelo.nitro.image.views

import android.view.View
import com.facebook.react.fabric.StateWrapperImpl
import com.facebook.react.uimanager.ReactStylesDiffMap
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.uimanager.ThemedReactContext

class TestViewManager extends SimpleViewManager<View> {
override fun getName(): String {
return "TestView"
}

override fun createViewInstance(reactContext: ThemedReactContext): View {
throw Error("Not yet implemented!")
}

override fun updateState(view: View, props: ReactStylesDiffMap, stateWrapper: StateWrapper): Any {
val stateWrapperImpl = stateWrapper as? StateWrapperImpl ?: throw Error("StateWrapper uses a different implementation!")
// TODO: Get props from stateWrapperImpl and update them in HybridView

return super.updateState(view, props, stateWrapper)
}
}

0 comments on commit e96b672

Please sign in to comment.