Skip to content

Commit

Permalink
Improving codes
Browse files Browse the repository at this point in the history
  • Loading branch information
LinX64 committed Oct 22, 2023
1 parent 11746b3 commit a2f637a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 40 deletions.
15 changes: 12 additions & 3 deletions shared/src/commonMain/kotlin/data/dataSource/NewsDataSourceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ import kotlinx.serialization.json.Json
class NewsDataSourceImpl(private val client: HttpClient) : NewsDataSource {

override suspend fun getTopHeadlines(): TopHeadlinesResponse {
val response = getSpecificNewsBySource(Sources.TechCrunch.value)
return Json.decodeFromString(response)
val response = getSpecificNewsBySource(Sources.Business.value)
return decodeFromString(response)
}

override suspend fun getWallStreetJournal(): TopHeadlinesResponse {
val response = getSpecificNewsBySource(WALL_STREET_JOURNAL.value)
return Json.decodeFromString(response)
return decodeFromString(response)
}

private suspend fun getSpecificNewsBySource(source: String): String {
return client.get(Consts.BASE_URL + source + Consts.API_KEY).bodyAsText()
}

private fun decodeFromString(response: String): TopHeadlinesResponse {
val json = Json {
ignoreUnknownKeys = true
coerceInputValues = true
explicitNulls = false
}
return json.decodeFromString(response)
}
}
10 changes: 5 additions & 5 deletions shared/src/commonMain/kotlin/data/model/TopHeadlinesResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ data class TopHeadlinesResponse(
@Serializable
data class Article(
@SerialName("author")
val author: String,
val author: String? = null,
@SerialName("content")
val content: String,
val content: String? = null,
@SerialName("description")
val description: String,
val description: String? = null,
@SerialName("publishedAt")
val publishedAt: String,
@SerialName("source")
Expand All @@ -30,13 +30,13 @@ data class Article(
@SerialName("url")
val url: String,
@SerialName("urlToImage")
val urlToImage: String
val urlToImage: String? = null
)

@Serializable
data class Source(
@SerialName("id")
val id: String,
val id: String? = null,
@SerialName("name")
val name: String
)

This file was deleted.

11 changes: 0 additions & 11 deletions shared/src/commonMain/kotlin/data/repository/MainRepositoryImpl.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ class NewsRepositoryImpl(
override fun getTopHeadlinesByTopic(topic: String): Flow<TopHeadlinesResponse> = flow {
val topHeadlines = newsDataSource.getTopHeadlines()
emit(topHeadlines)
// TODO
}.flowOn(ioDispatcher)
}
1 change: 0 additions & 1 deletion shared/src/commonMain/kotlin/data/util/Sources.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package data.util

enum class Sources(val value: String) {
TechCrunch("top-headlines?sources=techcrunch&apiKey="),
Business("top-headlines?country=us&category=business&apiKey="),
WALL_STREET_JOURNAL("everything?domains=wsj.com&apiKey="),
TESLA("everything?q=tesla&from=2023-09-21&sortBy=publishedAt&apiKey="),
Expand Down
7 changes: 4 additions & 3 deletions shared/src/commonMain/kotlin/ui/main/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ internal fun MainScreen(
LazyVerticalStaggeredGrid(
columns = StaggeredGridCells.Adaptive(300.dp),
contentPadding = PaddingValues(16.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalItemSpacing = 24.dp,
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalItemSpacing = 8.dp,
modifier = Modifier.testTag("home:feed"),
state = state,
state = state
) {
mainContent(topHeadlinesState)
}
Expand All @@ -64,6 +64,7 @@ private fun LazyStaggeredGridScope.mainContent(
is TopHeadlinesState.Loading -> Unit
is TopHeadlinesState.Success -> {
val articles = topHeadlinesState.articles

items(articles.size) { index ->
val article = articles[index]
ArticleItem(article)
Expand Down
21 changes: 10 additions & 11 deletions shared/src/commonMain/kotlin/ui/main/components/ArticleItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,21 @@ import org.jetbrains.compose.resources.painterResource
@Composable
internal fun ArticleItem(article: Article) {
Card(
modifier = Modifier.fillMaxWidth()
.padding(8.dp),
modifier = Modifier.padding(8.dp)
.fillMaxWidth(),
elevation = CardDefaults.cardElevation(
defaultElevation = 3.dp
)
) {
Image(
modifier = Modifier.fillMaxWidth(),
painter = painterResource("compose.png"),
contentDescription = null
)

Column(
modifier = Modifier
.fillMaxSize(),
modifier = Modifier.fillMaxSize(),
) {
Image(
modifier = Modifier.fillMaxWidth(),
painter = painterResource("compose.png"),
contentDescription = null
)

Spacer(modifier = Modifier.height(5.dp))

Column(modifier = Modifier.padding(16.dp)) {
Expand All @@ -51,7 +50,7 @@ internal fun ArticleItem(article: Article) {
Spacer(modifier = Modifier.padding(4.dp))

Text(
text = article.description,
text = article.description ?: "",
style = MaterialTheme.typography.bodySmall
)
}
Expand Down

0 comments on commit a2f637a

Please sign in to comment.