Vaibhav Jaiswal
02/10/2024, 1:32 PMVaibhav Jaiswal
02/10/2024, 1:34 PMabstract class FeedComponent(
componentContext: ComponentContext,
) : BaseComponent(componentContext) {
private val pagesNavigation = PagesNavigation<FeedPagesConfig>()
internal val pages: List<FeedPagesConfig> = listOf(
FeedPagesConfig.Trending,
FeedPagesConfig.Latest
)
internal val childPages = childPages(
source = pagesNavigation,
initialPages = { Pages(pages, 0) },
serializer = FeedPagesConfig.serializer(),
childFactory = ::createChild
)
internal abstract fun createComponent(
config: FeedPagesConfig,
componentContext: ComponentContext
): FeedPageComponent
private fun createChild(
config: FeedPagesConfig,
componentContext: ComponentContext
): FeedPages {
val component = createComponent(config, componentContext)
return when(config) {
FeedPagesConfig.Trending -> FeedPages.Trending(component)
FeedPagesConfig.Latest -> FeedPages.Latest(component)
}
}
internal fun onPageSelected(index: Int) {
pagesNavigation.select(index)
}
}
And this is the child class
internal class FeedTabComponent(
componentContext: ComponentContext,
private val onTabReselected: Flow<Unit>,
val onNavEvent: (AppNavEvents) -> Unit
) : FeedComponent(componentContext) {
override fun createComponent(
config: FeedPagesConfig,
componentContext: ComponentContext
) = FeedTabPageComponent(
feedCategory = config.feedCategory,
componentContext = componentContext,
onTabReselected = onTabReselected,
onNavEvent = onNavEvent
)
}
This is the leaf component
internal class FeedTabPageComponent (
feedCategory: FeedCategory,
componentContext: ComponentContext,
val onTabReselected: Flow<Unit>,
override val onNavEvent: (AppNavEvents) -> Unit
): FeedPageComponent(componentContext) {
override val component = this
override val feed = feedItemRepo.getFeedPaged(feedCategory).pagedData
.mapLatest { it.map(FeedItemEntity::toDomain) }
.cachedIn(componentScope)
.safeCatch { setError(it) }
.toStateFlow(componentScope, PagingData.empty())
}
Now i'm getting this error that
> java.lang.NullPointerException: Parameter specified as non-null is null: method com.medial.app.ui.screens.screens.home.tabs.feed.pages.FeedTabPageComponent.init, parameter onTabReselectedVaibhav Jaiswal
02/10/2024, 1:34 PMArkadii Ivanov
02/10/2024, 4:42 PMchildStack is initialised during super (FeedComponent) constructor invocation. This also calls createChild and createComponent functions. Since the createComponent is overriden, it gets called before the extending class is fully initialised.Vaibhav Jaiswal
02/11/2024, 5:29 AMArkadii Ivanov
02/11/2024, 10:09 AMabstract functions and use IoC instead - i.e. pass the factory to the FeedComponent via constructor.
A short-term fix that may work - to make your childStack property by lazy. But I wouldn't recommend this.