Skip to content

Commit

Permalink
Merge pull request #820 from arkivanov/articleId
Browse files Browse the repository at this point in the history
Pass articleId instead of authorId to DefaultArticleAuthorComponent
  • Loading branch information
arkivanov authored Dec 6, 2024
2 parents f14e3f9 + 0b2380b commit a3f0869
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ internal class DefaultMultiPaneComponent(
database = database,
isToolbarVisible = navState.map { it.mode.isSingle },
selectedArticleId = navState.map { it.takeUnless { it.mode.isSingle }?.details?.articleId },
onArticleSelected = { articleId, authorId ->
onArticleSelected = { articleId ->
navigation.navigate { state ->
state.copy(
details = Details(articleId = articleId, authorId = authorId),
extra = if (state.mode == TRIPLE) Extra(authorId = authorId) else null,
details = Details(articleId = articleId),
extra = if (state.mode == TRIPLE) Extra(articleId = articleId) else null,
)
}
},
Expand All @@ -73,15 +73,15 @@ internal class DefaultMultiPaneComponent(
database = database,
articleId = config.articleId,
isToolbarVisible = navState.map { it.mode.isSingle },
onAuthorRequested = { navigation.activateExtra(extra = Extra(authorId = config.authorId)) },
onAuthorRequested = { navigation.activateExtra(extra = Extra(articleId = config.articleId)) },
onFinished = navigation::pop,
)

private fun authorComponent(config: Extra, componentContext: ComponentContext): ArticleAuthorComponent =
DefaultArticleAuthorComponent(
componentContext = componentContext,
database = database,
authorId = config.authorId,
articleId = config.articleId,
isToolbarVisible = navState.map { it.mode.isSingle },
isCloseButtonVisible = navState.map { it.mode.isDual },
onFinished = navigation::pop,
Expand All @@ -90,7 +90,7 @@ internal class DefaultMultiPaneComponent(
override fun setMode(mode: ChildPanelsMode) {
navigation.navigate { state ->
state.copy(
extra = state.takeIf { mode == TRIPLE }?.details?.authorId?.let { Extra(authorId = it) } ?: state.extra,
extra = state.takeIf { mode == TRIPLE }?.details?.articleId?.let { Extra(articleId = it) } ?: state.extra,
mode = mode,
)
}
Expand All @@ -101,8 +101,8 @@ internal class DefaultMultiPaneComponent(
}

@Serializable
private data class Details(val articleId: Long, val authorId: Long)
private data class Details(val articleId: Long)

@Serializable
private data class Extra(val authorId: Long)
private data class Extra(val articleId: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.badoo.reaktive.observable.Observable
internal class DefaultArticleAuthorComponent(
componentContext: ComponentContext,
database: ArticleDatabase,
authorId: Long,
articleId: Long,
isToolbarVisible: Observable<Boolean>,
isCloseButtonVisible: Observable<Boolean>,
private val onFinished: () -> Unit,
Expand All @@ -26,7 +26,7 @@ internal class DefaultArticleAuthorComponent(
Model(
isToolbarVisible = false,
isCloseButtonVisible = false,
author = database.getAuthor(id = authorId).toAuthor(),
author = database.getArticle(id = articleId).author.toAuthor(),
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ internal interface ArticleDatabase {
fun getArticles(): List<ArticleEntity>

fun getArticle(id: Long): ArticleEntity

fun getAuthor(id: Long): AuthorEntity
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ internal class DefaultArticleDatabase : ArticleDatabase {
)
}

private val authorMap = authors.associateBy(AuthorEntity::id)
private val articleMap = articles.associateBy(ArticleEntity::id)

override fun getArticles(): List<ArticleEntity> = articles

override fun getArticle(id: Long): ArticleEntity =
articleMap.getValue(id)

override fun getAuthor(id: Long): AuthorEntity =
authorMap.getValue(id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface ArticleListComponent {

data class Article(
val id: Long,
val authorId: Long,
val title: String
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class DefaultArticleListComponent(
database: ArticleDatabase,
isToolbarVisible: Observable<Boolean>,
selectedArticleId: Observable<Long?>,
private val onArticleSelected: (articleId: Long, authorId: Long) -> Unit,
private val onArticleSelected: (articleId: Long) -> Unit,
) : ArticleListComponent, ComponentContext by componentContext, DisposableScope by componentContext.disposableScope() {

private val _models =
Expand All @@ -44,11 +44,10 @@ internal class DefaultArticleListComponent(
private fun ArticleEntity.toArticle(): Article =
Article(
id = id,
authorId = author.id,
title = title
)

override fun onArticleClicked(article: Article) {
onArticleSelected(article.id, article.authorId)
onArticleSelected(article.id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PreviewArticleListComponent(
MutableValue(
Model(
articles = List(10) { index ->
Article(id = index.toLong(), authorId = index.toLong(), title = "Article $index")
Article(id = index.toLong(), title = "Article $index")
},
isToolbarVisible = isToolbarVisible,
selectedArticleId = null,
Expand Down

0 comments on commit a3f0869

Please sign in to comment.