Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass articleId instead of authorId to DefaultArticleAuthorComponent #820

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
arkivanov marked this conversation as resolved.
Show resolved Hide resolved
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")
arkivanov marked this conversation as resolved.
Show resolved Hide resolved
},
isToolbarVisible = isToolbarVisible,
selectedArticleId = null,
Expand Down
Loading