-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
409 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
androidApp/src/androidMain/kotlin/com/client/news/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
androidApp/src/androidMain/kotlin/com/client/news/ui/data/model/TopHeadlinesResponse.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import androidx.compose.runtime.Composable | ||
import ui.NewsApp | ||
|
||
@Composable | ||
fun MainView() { | ||
|
8 changes: 8 additions & 0 deletions
8
shared/src/commonMain/kotlin/data/dataSource/NewsDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package data.dataSource | ||
|
||
import data.model.TopHeadlinesResponse | ||
|
||
interface NewsDataSource { | ||
suspend fun getTopHeadlines(): TopHeadlinesResponse | ||
suspend fun getWallStreetJournal(): TopHeadlinesResponse | ||
} |
27 changes: 27 additions & 0 deletions
27
shared/src/commonMain/kotlin/data/dataSource/NewsDataSourceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package data.dataSource | ||
|
||
import data.model.TopHeadlinesResponse | ||
import data.util.Consts | ||
import data.util.Sources | ||
import data.util.Sources.WALL_STREET_JOURNAL | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
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) | ||
} | ||
|
||
override suspend fun getWallStreetJournal(): TopHeadlinesResponse { | ||
val response = getSpecificNewsBySource(WALL_STREET_JOURNAL.value) | ||
return Json.decodeFromString(response) | ||
} | ||
|
||
private suspend fun getSpecificNewsBySource(source: String): String { | ||
return client.get(Consts.BASE_URL + source + Consts.API_KEY).bodyAsText() | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
shared/src/commonMain/kotlin/data/model/TopHeadlinesResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package data.model | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class TopHeadlinesResponse( | ||
@SerialName("articles") | ||
val articles: List<Article>, | ||
@SerialName("status") | ||
val status: String, | ||
@SerialName("totalResults") | ||
val totalResults: Int | ||
) | ||
|
||
@Serializable | ||
data class Article( | ||
@SerialName("author") | ||
val author: String, | ||
@SerialName("content") | ||
val content: String, | ||
@SerialName("description") | ||
val description: String, | ||
@SerialName("publishedAt") | ||
val publishedAt: String, | ||
@SerialName("source") | ||
val source: Source, | ||
@SerialName("title") | ||
val title: String, | ||
@SerialName("url") | ||
val url: String, | ||
@SerialName("urlToImage") | ||
val urlToImage: String | ||
) | ||
|
||
@Serializable | ||
data class Source( | ||
@SerialName("id") | ||
val id: String, | ||
@SerialName("name") | ||
val name: String | ||
) |
9 changes: 9 additions & 0 deletions
9
shared/src/commonMain/kotlin/data/repository/NewsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package data.repository | ||
|
||
import data.model.TopHeadlinesResponse | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface NewsRepository { | ||
fun getTopHeadlines(): Flow<TopHeadlinesResponse> | ||
fun getTopHeadlinesByTopic(topic: String): Flow<TopHeadlinesResponse> | ||
} |
28 changes: 28 additions & 0 deletions
28
shared/src/commonMain/kotlin/data/repository/NewsRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package data.repository | ||
|
||
import data.dataSource.NewsDataSource | ||
import data.dataSource.NewsDataSourceImpl | ||
import data.model.TopHeadlinesResponse | ||
import data.util.Sources | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.IO | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
|
||
class NewsRepositoryImpl( | ||
private val newsDataSource: NewsDataSource, | ||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO | ||
) : NewsRepository { | ||
|
||
override fun getTopHeadlines(): Flow<TopHeadlinesResponse> = flow { | ||
val topHeadlines = newsDataSource.getTopHeadlines() | ||
emit(topHeadlines) | ||
}.flowOn(ioDispatcher) | ||
|
||
override fun getTopHeadlinesByTopic(topic: String): Flow<TopHeadlinesResponse> = flow { | ||
val topHeadlines = newsDataSource.getTopHeadlines() | ||
emit(topHeadlines) | ||
}.flowOn(ioDispatcher) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package data.util | ||
|
||
object Consts { | ||
const val BASE_URL = "https://newsapi.org/v2/" | ||
const val API_KEY = "048261763a0240babf29ff0a2e567780" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package data.util | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onStart | ||
|
||
sealed interface Result<out T> { | ||
data class Success<T>(val data: T) : Result<T> | ||
data class Error(val exception: Throwable? = null) : Result<Nothing> | ||
data object Loading : Result<Nothing> | ||
} | ||
|
||
fun <T> Flow<T>.asResult(): Flow<Result<T>> { | ||
return this | ||
.map<T, Result<T>> { | ||
Result.Success(it) | ||
} | ||
.onStart { emit(Result.Loading) } | ||
.catch { emit(Result.Error(it)) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
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="), | ||
APPLE("everything?q=apple&from=2023-10-20&to=2023-10-20&sortBy=popularity&apiKey=") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
package di | ||
|
||
import data.repository.MainRepositoryImpl | ||
import data.dataSource.NewsDataSource | ||
import data.dataSource.NewsDataSourceImpl | ||
import data.repository.NewsRepository | ||
import data.repository.NewsRepositoryImpl | ||
import io.ktor.client.HttpClient | ||
import org.koin.dsl.module | ||
import ui.MainViewModel | ||
|
||
fun appModule() = module { | ||
single { MainRepositoryImpl() } | ||
val appModule = module { | ||
viewModelDefinition { MainViewModel(get()) } | ||
} | ||
|
||
viewModelDefinition { MainViewModel() } | ||
val networkModule = module { | ||
factory { HttpClient() } | ||
single<NewsDataSource> { NewsDataSourceImpl(get()) } | ||
|
||
// Repository | ||
single<NewsRepository> { NewsRepositoryImpl(get()) } | ||
} |
Oops, something went wrong.