Skip to content
Open
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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ dependencies {
implementation platform('com.google.firebase:firebase-bom:29.0.2')
implementation 'com.google.firebase:firebase-crashlytics'
implementation 'com.google.firebase:firebase-analytics'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.9'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.example.dispatcher.model.network.articles

import com.example.dispatcher.model.ArticlesResponse
import com.example.dispatcher.model.network.articles.ArticlesApiConstants.END_POINT
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.QueryMap

interface ArticlesApi {
@GET(END_POINT)
fun getArticles(@QueryMap params: Map<String, String>): Call<ArticlesResponse>
fun getArticles(@QueryMap params: Map<String, String>): Response<ArticlesResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.example.dispatcher.model.network.articles.ArticlesApiConstants.API__E
import com.example.dispatcher.model.network.articles.ArticlesApiConstants.API__EVERYTHING_TO__KEY
import com.example.dispatcher.model.network.articles.ArticlesApiConstants.ENDPOINT_EVERYTHING
import com.example.dispatcher.model.network.manager.ApiController
import retrofit2.Callback
import retrofit2.Response
import java.util.HashMap

class ArticlesApiController: ApiController<ArticlesApi>() {
Expand All @@ -26,18 +26,17 @@ class ArticlesApiController: ApiController<ArticlesApi>() {
return ArticlesApi::class.java
}

fun getArticles(callback: Callback<ArticlesResponse>,
q: String? = null,
qInTitle: String? = null,
sources: String? = null,
domains: String? = null,
excludeDomains: String? = null,
from: String? = null,
to: String? = null,
language: String? = null,
sortBy: String? = null,
pageSize: Int? = null,
page: Int? = null) {
suspend fun getArticles(q: String? = null,
qInTitle: String? = null,
sources: String? = null,
domains: String? = null,
excludeDomains: String? = null,
from: String? = null,
to: String? = null,
language: String? = null,
sortBy: String? = null,
pageSize: Int? = null,
page: Int? = null): Response<ArticlesResponse> {
val params: MutableMap<String, String> = HashMap()
when {
!q.isNullOrEmpty() -> params[API__EVERYTHING_Q__KEY] = q
Expand All @@ -53,6 +52,6 @@ class ArticlesApiController: ApiController<ArticlesApi>() {
page != null -> params[API__EVERYTHING_PAGE__KEY] = "$page"
}

getApi().getArticles(params).enqueue(callback)
return getApi().getArticles(params)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.example.dispatcher.model.network.headlines

import com.example.dispatcher.model.ArticlesResponse
import com.example.dispatcher.model.network.headlines.TopHeadlinesConstants.END_POINT
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.QueryMap

interface TopHeadlinesApi {
@GET(END_POINT)
fun getTopArticles(@QueryMap params: Map<String, String>): Call<ArticlesResponse>
suspend fun getArticles(@QueryMap params: Map<String, String>): Response<ArticlesResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.example.dispatcher.model.network.headlines.TopHeadlinesConstants.API_
import com.example.dispatcher.model.network.headlines.TopHeadlinesConstants.API__HEADLINES_SOURCES__KEY
import com.example.dispatcher.model.network.headlines.TopHeadlinesConstants.ENDPOINT_TOP
import com.example.dispatcher.model.network.manager.ApiController
import retrofit2.Callback
import retrofit2.Response
import java.util.HashMap

class TopHeadlinesApiController: ApiController<TopHeadlinesApi>() {
Expand All @@ -21,13 +21,12 @@ class TopHeadlinesApiController: ApiController<TopHeadlinesApi>() {
return TopHeadlinesApi::class.java
}

fun getTopArticles(callback: Callback<ArticlesResponse>,
country: String? = null,
suspend fun getTopArticles(country: String? = null,
category: String? = null,
sources: String? = null,
q: String? = null,
pageSize: Int? = null,
page: Int? = null){
page: Int? = null): Response<ArticlesResponse> {
val params: MutableMap<String, String> = HashMap()
when {
!country.isNullOrEmpty() -> params[API__HEADLINES_COUNTRY__KEY] = country
Expand All @@ -38,6 +37,6 @@ class TopHeadlinesApiController: ApiController<TopHeadlinesApi>() {
page != null -> params[API__HEADLINES_PAGE__KEY] = "$page"
}

getApi().getTopArticles(params).enqueue(callback)
return getApi().getArticles(params)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.example.dispatcher.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.dispatcher.model.Article
import com.example.dispatcher.model.ArticlesResponse
import com.example.dispatcher.model.network.headlines.TopHeadlinesApiController
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.lang.Exception
import java.util.*
import kotlin.collections.ArrayList

Expand All @@ -19,18 +19,14 @@ class DashboardViewModel : ViewModel() {
}

private fun getArticles() {
TopHeadlinesApiController()
.getTopArticles(object: Callback<ArticlesResponse> {
override fun onResponse(
call: Call<ArticlesResponse>,
response: Response<ArticlesResponse>) {
if (response.isSuccessful && response.body() != null)
articles.value = response.body()!!.articles
}

override fun onFailure(call: Call<ArticlesResponse>, t: Throwable) {
t.printStackTrace()
}
}, Locale.getDefault().country)
viewModelScope.launch(Dispatchers.IO) {
try {
val response = TopHeadlinesApiController().getTopArticles(Locale.getDefault().country)
if (response.isSuccessful && response.body() != null)
articles.postValue(response.body()!!.articles)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}