Skip to content

Commit

Permalink
add auto complete in search with countries
Browse files Browse the repository at this point in the history
  • Loading branch information
HamdiBoumaiza committed Mar 29, 2020
1 parent 061ed1d commit 26a9fef
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 9 deletions.
10 changes: 8 additions & 2 deletions app/src/main/java/com/hb/covid19status/api/ApiService.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.hb.covid19status.api

import com.hb.covid19status.data.ResponseHistoryCountry
import com.hb.covid19status.data.ResponseListCountries
import com.hb.covid19status.data.ResponseListCountriesAffected
import com.hb.covid19status.data.ResponseListCountriesStats
import com.hb.covid19status.model.WorldStats
import retrofit2.http.GET
import retrofit2.http.Header
Expand All @@ -17,7 +18,7 @@ interface ApiService {
@GET(ALL_COUNTRIES_STATS_URL)
suspend fun getListCountriesStatsAsync(
@Header("x-rapidapi-key") amount: String
): ResponseListCountries
): ResponseListCountriesStats

@GET(HISTORY_BY_COUNTRIES_BY_DATE_URL)
suspend fun getHistoryByCountryAndDate(
Expand All @@ -26,4 +27,9 @@ interface ApiService {
@Query("date") date: String
): ResponseHistoryCountry

@GET(LIST_AFFECTED_COUNTRY_URL)
suspend fun getlistAffectedCountries(
@Header("x-rapidapi-key") amount: String
): ResponseListCountriesAffected

}
3 changes: 2 additions & 1 deletion app/src/main/java/com/hb/covid19status/api/NetworkConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ const val BASE_URL = "https://coronavirus-monitor.p.rapidapi.com/coronavirus/"

const val GENERAL_WORLD_STATS_URL = "worldstat.php"
const val ALL_COUNTRIES_STATS_URL = "cases_by_country.php"
const val HISTORY_BY_COUNTRIES_BY_DATE_URL = "history_by_country_and_date.php"
const val HISTORY_BY_COUNTRIES_BY_DATE_URL = "history_by_country_and_date.php"
const val LIST_AFFECTED_COUNTRY_URL = "affected.php"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hb.covid19status.data

import com.google.gson.annotations.SerializedName
import java.io.Serializable


data class ResponseListCountriesAffected(

@SerializedName("affected_countries")
val affected_countries: List<String>
) : Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.google.gson.annotations.SerializedName
import com.hb.covid19status.model.CountryStat


data class ResponseListCountries(
data class ResponseListCountriesStats(

@SerializedName("countries_stat")
val countries_stat: List<CountryStat>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.hb.covid19status.data_source.remote

import com.hb.covid19status.data.ResponseHistoryCountry
import com.hb.covid19status.data.ResponseListCountries
import com.hb.covid19status.data.ResponseListCountriesAffected
import com.hb.covid19status.data.ResponseListCountriesStats
import com.hb.covid19status.data.ResultData
import com.hb.covid19status.model.WorldStats

interface RemoteDataSource {

suspend fun worldWithStats(): ResultData<WorldStats>
suspend fun listCountriesWithStats(): ResultData<ResponseListCountries>
suspend fun listCountriesWithStats(): ResultData<ResponseListCountriesStats>
suspend fun historyByDateByCountryStats(country : String, date : String): ResultData<ResponseHistoryCountry>
suspend fun affectedCountries(): ResultData<ResponseListCountriesAffected>

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ package com.hb.covid19status.data_source.remote
import com.hb.covid19status.BuildConfig
import com.hb.covid19status.api.ApiService
import com.hb.covid19status.data.ResponseHistoryCountry
import com.hb.covid19status.data.ResponseListCountries
import com.hb.covid19status.data.ResponseListCountriesAffected
import com.hb.covid19status.data.ResponseListCountriesStats
import com.hb.covid19status.data.ResultData
import com.hb.covid19status.di.IoDispatcher
import com.hb.covid19status.model.WorldStats
Expand All @@ -31,7 +32,7 @@ class RemoteDataSourceImpl(
}


override suspend fun listCountriesWithStats(): ResultData<ResponseListCountries> =
override suspend fun listCountriesWithStats(): ResultData<ResponseListCountriesStats> =
withContext(ioDispatcher) {
val request =
api.getListCountriesStatsAsync(BuildConfig.API_KEY)
Expand All @@ -48,4 +49,11 @@ class RemoteDataSourceImpl(
ResultData.Success(request)
}

override suspend fun affectedCountries(): ResultData<ResponseListCountriesAffected> =
withContext(ioDispatcher) {
val request =
api.getlistAffectedCountries(BuildConfig.API_KEY)
ResultData.Success(request)
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hb.covid19status.repository

import com.hb.covid19status.data.ResponseHistoryCountry
import com.hb.covid19status.data.ResponseListCountriesAffected
import com.hb.covid19status.data.ResultData
import com.hb.covid19status.model.CountryStat
import com.hb.covid19status.model.WorldStats
Expand All @@ -17,4 +18,6 @@ interface AppRepository {

suspend fun historyByDateByCountryStats(country : String, date : String): ResultData<ResponseHistoryCountry>

suspend fun affectedCountries(): ResultData<ResponseListCountriesAffected>

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hb.covid19status.repository

import com.hb.covid19status.data.RemoteDataNotFoundException
import com.hb.covid19status.data.ResponseHistoryCountry
import com.hb.covid19status.data.ResponseListCountriesAffected
import com.hb.covid19status.data.ResultData
import com.hb.covid19status.data_source.local.AppDao
import com.hb.covid19status.data_source.remote.RemoteDataSource
Expand Down Expand Up @@ -88,4 +89,15 @@ class AppRepositoryImpl(
}
}

override suspend fun affectedCountries(): ResultData<ResponseListCountriesAffected> {
return when (val result = remoteDataSource.affectedCountries()) {
is ResultData.Success -> {
ResultData.Success(result.data)
}
is ResultData.Error -> {
ResultData.Error(RemoteDataNotFoundException())
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.annotation.Nullable
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
Expand All @@ -23,6 +24,7 @@ import java.text.SimpleDateFormat
import java.util.*
import javax.inject.Inject


class SearchHistoryFragment : Fragment(), View.OnClickListener {

private val appComponents by lazy { MainApplication.appComponents }
Expand Down Expand Up @@ -50,6 +52,7 @@ class SearchHistoryFragment : Fragment(), View.OnClickListener {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getViewModel().getListAffectedCountries()
initObservers()
initCalendar()
binding.edittextCalendar.setOnClickListener(this)
Expand Down Expand Up @@ -100,6 +103,12 @@ class SearchHistoryFragment : Fragment(), View.OnClickListener {
}

private fun initObservers() {
getViewModel().resulAffectedCountries.observe(this, Observer { response ->
response?.let {
setAutoCompleteData(it)
}
})

getViewModel().resultHistory.observe(this, Observer { response ->
response?.let { sendData(it) }
})
Expand All @@ -114,6 +123,17 @@ class SearchHistoryFragment : Fragment(), View.OnClickListener {
})
}

// Create the adapter and set it to the AutoCompleteTextView
private fun setAutoCompleteData(listCountries: List<String>) {
ArrayAdapter<String>(
activity!!,
android.R.layout.simple_list_item_1,
listCountries
).also { adapter ->
binding.edittextSearch.setAdapter(adapter)
}
}

private fun handleError() {
activity?.toast(getString(R.string.error_message))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class SearchHistoryViewModel @Inject constructor(
private var _resultHistory = MutableLiveData<ResponseHistoryCountry>()
var resultHistory: LiveData<ResponseHistoryCountry> = _resultHistory

private var _resulAffectedCountries = MutableLiveData<List<String>>()
var resulAffectedCountries: LiveData<List<String>> = _resulAffectedCountries

private var _errorMessage = MutableLiveData<String>()
var errorMessage: LiveData<String> = _errorMessage

Expand All @@ -43,4 +46,25 @@ class SearchHistoryViewModel @Inject constructor(
}
}
}

fun getListAffectedCountries() {
_showLoading.postValue(true)
viewModelScope.launch {
try {
when (val response = repositoryImpl.affectedCountries()) {
is ResultData.Success -> {
_showLoading.postValue(false)
_resulAffectedCountries.postValue(response.data.affected_countries)
}
is ResultData.Error -> {
_showLoading.postValue(false)
_errorMessage.postValue("")
}
}
} catch (e: Exception) {
_showLoading.postValue(false)
_errorMessage.postValue(e.message)
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_history_stats.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
app:layout_constraintStart_toStartOf="@+id/guideline_vertical10"
app:layout_constraintTop_toTopOf="parent" />

<EditText
<AutoCompleteTextView
android:id="@+id/edittext_search"
style="@style/EditText.Flat.Grey"
android:layout_width="0dp"
Expand Down

0 comments on commit 26a9fef

Please sign in to comment.