Fix Api exception and searchList 🐛

For_Me_Teams_Search
Emre KARTAL 2 years ago
parent 2905584410
commit 630ab5ef1e

@ -13,6 +13,8 @@ import uca.iut.clermont.model.PlayerResponse
interface FootballApi { interface FootballApi {
// Area
@Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57")
@GET("areas/{id}") @GET("areas/{id}")
suspend fun getArea(@Path("id") id: Int): Area suspend fun getArea(@Path("id") id: Int): Area
@ -21,10 +23,14 @@ interface FootballApi {
@GET("areas") @GET("areas")
suspend fun getAreas(): AreaResponse suspend fun getAreas(): AreaResponse
// Person
@Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57")
@GET("persons/{id}") @GET("persons/{id}")
suspend fun getPlayer(@Path("id") playerId: Int): PlayerResponse suspend fun getPlayer(@Path("id") playerId: Int): PlayerResponse
// Competition
@Headers("X-Auth-Token: 621ef06e148542f98b4993a5442421eb") @Headers("X-Auth-Token: 621ef06e148542f98b4993a5442421eb")
@GET("competitions") @GET("competitions")
suspend fun getCompetitions(): CompetitionResponse suspend fun getCompetitions(): CompetitionResponse
@ -33,6 +39,21 @@ interface FootballApi {
@GET("competitions/{id}") @GET("competitions/{id}")
suspend fun getCompetition(@Path("id") id: Int): CompetitionResult suspend fun getCompetition(@Path("id") id: Int): CompetitionResult
// Team
@Headers("X-Auth-Token: 9983c8c887464274bed01ff046775bed")
@GET("teams?limit=1000")
suspend fun getTeams(): CompetitionResponse
@Headers("X-Auth-Token: 84c47f4211244a0d9b3cb9376e00f4aa")
@GET("competitions/{id}/teams")
suspend fun getTeamsByCompetition(): CompetitionResponse
@Headers("X-Auth-Token: 9983c8c887464274bed01ff046775bed")
@GET("teams/{id}")
suspend fun getTeam(): CompetitionResponse
// Match
@Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57")
@GET("matches") @GET("matches")
suspend fun getMatches(): MatchResponse suspend fun getMatches(): MatchResponse

@ -28,6 +28,8 @@ class HomeFragment : Fragment(), CompetitionsAdapter.OnItemClickListener {
private val viewModel: HomeViewModel by viewModels() private val viewModel: HomeViewModel by viewModels()
private lateinit var searchBar: EditText private lateinit var searchBar: EditText
private lateinit var searchAdapter: RecyclerView private lateinit var searchAdapter: RecyclerView
private var searchList: MutableList<Competition> = mutableListOf()
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -85,16 +87,12 @@ class HomeFragment : Fragment(), CompetitionsAdapter.OnItemClickListener {
} }
private fun searchNames(query: String) { private fun searchNames(query: String) {
val filteredCompetition = mutableListOf<Competition>() val filteredCompetition = viewModel.competitions.value.orEmpty()
if (query.isNotEmpty()) { .asSequence()
for (competition in viewModel.competitions.value!!) { .filter { it.name.lowercase().contains(query.lowercase()) }
if (competition.name.lowercase() .take(3)
.contains(query.lowercase()) && filteredCompetition.size < 3 .toMutableList()
) { searchList = filteredCompetition
filteredCompetition.add(competition)
}
}
}
searchList(filteredCompetition, this) searchList(filteredCompetition, this)
} }
@ -129,9 +127,8 @@ class HomeFragment : Fragment(), CompetitionsAdapter.OnItemClickListener {
} }
override fun onItemClick(position: Int) { override fun onItemClick(position: Int) {
val competitions = viewModel.competitions.value!!
val bundle = bundleOf( val bundle = bundleOf(
"idItem" to competitions[position].id, "idItem" to searchList[position].id,
"fragmentId" to R.id.homeFragment "fragmentId" to R.id.homeFragment
) )
findNavController().navigate(R.id.action_homeFragment_to_detailFragment, bundle) findNavController().navigate(R.id.action_homeFragment_to_detailFragment, bundle)

@ -1,9 +1,11 @@
package uca.iut.clermont.view.viewModel package uca.iut.clermont.view.viewModel
import android.util.Log
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import retrofit2.HttpException
import uca.iut.clermont.api.ApiManager import uca.iut.clermont.api.ApiManager
import uca.iut.clermont.model.Competition import uca.iut.clermont.model.Competition
import uca.iut.clermont.model.Match import uca.iut.clermont.model.Match
@ -25,21 +27,32 @@ class DetailViewModel(
*/ */
fun loadCurrentCompetition(id: Int) = viewModelScope.launch { fun loadCurrentCompetition(id: Int) = viewModelScope.launch {
val result = manager.competitionsMgr.getItemById(id) try {
competition.value = result val result = manager.competitionsMgr.getItemById(id)
competition.value = result
} catch (e: HttpException) {
Log.d(e.toString(), ": too many requests")
}
} }
fun loadMatches(id: Int) = viewModelScope.launch { fun loadMatches(id: Int) = viewModelScope.launch {
try {
val matchResults = manager.matchesMgr.getItemsByCompetition(id) val matchResults = manager.matchesMgr.getItemsByCompetition(id)
competitionMatches.value = competitionMatches.value =
matchResults.filter { it.status != "TIMED" && it.status != "SCHEDULED" && it.status != "POSTPONED" } matchResults.filter { it.status != "TIMED" && it.status != "SCHEDULED" && it.status != "POSTPONED" }
.apply { forEach { it.date.add(Calendar.HOUR_OF_DAY, 2) } } .apply { forEach { it.date.add(Calendar.HOUR_OF_DAY, 2) } }
.sortedBy { it.competition.name } .sortedBy { it.competition.name }
.sortedByDescending { it.date } .sortedByDescending { it.date }
} catch (e: HttpException) {
Log.d(e.toString(), ": too many requests")
}
} }
fun loadNumberMatches() = viewModelScope.launch { fun loadNumberMatches() = viewModelScope.launch {
nbCompetitionMatches.value = competitionMatches.value?.size try {
nbCompetitionMatches.value = competitionMatches.value?.size
} catch (e: HttpException) {
Log.d(e.toString(), ": too many requests")
}
} }
} }

@ -1,9 +1,11 @@
package uca.iut.clermont.view.viewModel package uca.iut.clermont.view.viewModel
import android.util.Log
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import retrofit2.HttpException
import uca.iut.clermont.api.ApiManager import uca.iut.clermont.api.ApiManager
import uca.iut.clermont.model.Competition import uca.iut.clermont.model.Competition
import uca.iut.clermont.model.Match import uca.iut.clermont.model.Match
@ -16,16 +18,24 @@ class HomeViewModel : ViewModel() {
val competitions = MutableLiveData<List<Competition>>() val competitions = MutableLiveData<List<Competition>>()
fun loadMatches() = viewModelScope.launch { fun loadMatches() = viewModelScope.launch {
val matchResults = manager.matchesMgr.getItems() try {
matches.value = matchResults.filter { it.status != "TIMED" } val matchResults = manager.matchesMgr.getItems()
.apply { forEach { it.date.add(Calendar.HOUR_OF_DAY, 2) } } matches.value = matchResults.filter { it.status != "TIMED" }
.sortedBy { it.competition.name } .apply { forEach { it.date.add(Calendar.HOUR_OF_DAY, 2) } }
.sortedByDescending { it.date } .sortedBy { it.competition.name }
.sortedByDescending { it.date }
} catch (e: HttpException) {
Log.d(e.toString(),": too many requests")
}
} }
fun loadCompetitions() = viewModelScope.launch { fun loadCompetitions() = viewModelScope.launch {
val result = manager.competitionsMgr.getItems() try {
competitions.value = result val result = manager.competitionsMgr.getItems()
competitions.value = result
} catch (e: HttpException) {
Log.d(e.toString(),": too many requests")
}
} }
} }
Loading…
Cancel
Save