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 {
// Area
@Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57")
@GET("areas/{id}")
suspend fun getArea(@Path("id") id: Int): Area
@ -21,10 +23,14 @@ interface FootballApi {
@GET("areas")
suspend fun getAreas(): AreaResponse
// Person
@Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57")
@GET("persons/{id}")
suspend fun getPlayer(@Path("id") playerId: Int): PlayerResponse
// Competition
@Headers("X-Auth-Token: 621ef06e148542f98b4993a5442421eb")
@GET("competitions")
suspend fun getCompetitions(): CompetitionResponse
@ -33,6 +39,21 @@ interface FootballApi {
@GET("competitions/{id}")
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")
@GET("matches")
suspend fun getMatches(): MatchResponse

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

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