diff --git a/src/app/src/main/java/uca/iut/clermont/api/ApiManager.kt b/src/app/src/main/java/uca/iut/clermont/api/ApiManager.kt index 0259afc..442ca43 100644 --- a/src/app/src/main/java/uca/iut/clermont/api/ApiManager.kt +++ b/src/app/src/main/java/uca/iut/clermont/api/ApiManager.kt @@ -10,8 +10,6 @@ import kotlinx.coroutines.coroutineScope import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import uca.iut.clermont.model.* -import java.text.SimpleDateFormat -import java.util.* val retrofit: Retrofit = Retrofit.Builder() .baseUrl("https://api.football-data.org/v4/") @@ -29,29 +27,26 @@ class ApiManager : DataManager() { class ApiAreaManager : AreaManager { - override fun getItemsByName(substring: String): List { - TODO("Not yet implemented") + override suspend fun getItemsByName(substring: String): List = coroutineScope { + val areas = footballApi.getAreas() + return@coroutineScope areas.areas.filter { it.name.contains(substring) } } - override suspend fun getItems(): List { - TODO("Not yet implemented") + override suspend fun getItems(): List = coroutineScope { + val areas = footballApi.getAreas() + return@coroutineScope areas.areas } - override suspend fun getItemById(id: Int): Area? { - TODO("Not yet implemented") + override suspend fun getItemById(id: Int): Area = coroutineScope { + val area = footballApi.getArea(id) + return@coroutineScope area } } class ApiPeopleManager : PeopleManager { - override fun getItemsByName(substring: String): List { - TODO("Not yet implemented") - } - - override suspend fun getItems(): List { - TODO("Not yet implemented") - } + override suspend fun getItems(): List = listOf() override suspend fun getItemById(id: Int): Personne? = coroutineScope { val personne = footballApi.getPlayer(id) @@ -63,42 +58,53 @@ class ApiManager : DataManager() { } class ApiMatchesManager : MatchesManager { - override fun getNbItemsByCompetition(substring: String): Int { - TODO("Not yet implemented") - } - override fun getItemsByCompetition(substring: String): List { - TODO("Not yet implemented") + override suspend fun getNbItemsByCompetition(id: Int): Int = coroutineScope { + val matches = footballApi.getMatchesByCompetition(id) + return@coroutineScope matches.matches.size } + override suspend fun getItemsByCompetition(id: Int): List = + coroutineScope { + val matches = footballApi.getMatches() + return@coroutineScope matches.matches.map { it.toModel() } + } + override suspend fun getItems(): List = coroutineScope { val matches = footballApi.getMatches() return@coroutineScope matches.matches.map { matchResult -> matchResult.toModel() } } - override suspend fun getItemById(id: Int): Match? { - TODO("Not yet implemented") + override suspend fun getItemById(id: Int): Match = coroutineScope { + val match = footballApi.getMatch(id) + return@coroutineScope match.toModel() } } class ApiCompetitionsManager : CompetitionsManager { - override fun getItemsByName(substring: String): List { - TODO("Not yet implemented") + + override suspend fun getItemsByName(substring: String): List = coroutineScope { + val competitons = footballApi.getCompetitions() + return@coroutineScope competitons.competitions.map { competitionResult -> competitionResult.toModel() } + .filter { it.name == substring } } override suspend fun getItems(): List = coroutineScope { val competitons = footballApi.getCompetitions() - return@coroutineScope competitons.competitions.map { competitionResult -> competitionResult.toModel() }.sortedBy { it.name } + return@coroutineScope competitons.competitions.map { competitionResult -> competitionResult.toModel() } + .sortedBy { it.name } } - override suspend fun getItemById(id: Int): Competition? { - TODO("Not yet implemented") + override suspend fun getItemById(id: Int): Competition = coroutineScope { + val competition = footballApi.getCompetition(id) + return@coroutineScope competition.toModel() } } class ApiTeamsManager : TeamsManager { + override fun getItemsByName(substring: String): List { TODO("Not yet implemented") } diff --git a/src/app/src/main/java/uca/iut/clermont/api/FootballApi.kt b/src/app/src/main/java/uca/iut/clermont/api/FootballApi.kt index 4839b6e..3eaabb8 100644 --- a/src/app/src/main/java/uca/iut/clermont/api/FootballApi.kt +++ b/src/app/src/main/java/uca/iut/clermont/api/FootballApi.kt @@ -3,29 +3,46 @@ package uca.iut.clermont.api import retrofit2.http.GET import retrofit2.http.Headers import retrofit2.http.Path +import uca.iut.clermont.api.response.areaResponse.AreaResponse import uca.iut.clermont.api.response.competitionResponse.CompetitionResponse +import uca.iut.clermont.api.response.competitionResponse.CompetitionResult import uca.iut.clermont.api.response.matchResponse.MatchResponse +import uca.iut.clermont.api.response.matchResponse.MatchResult import uca.iut.clermont.model.Area import uca.iut.clermont.model.PlayerResponse interface FootballApi { + @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") + @GET("areas/{id}") + suspend fun getArea(@Path("id") id: Int): Area + + @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @GET("areas") - suspend fun getAreas(): List + suspend fun getAreas(): AreaResponse @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @GET("persons/{id}") suspend fun getPlayer(@Path("id") playerId: Int): PlayerResponse - @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") - @GET("persons") - suspend fun getPlayers(): List - @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @GET("competitions") suspend fun getCompetitions(): CompetitionResponse + @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") + @GET("competitions/{id}") + suspend fun getCompetition(@Path("id") id: Int): CompetitionResult + @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @GET("matches") suspend fun getMatches(): MatchResponse + + @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") + @GET("matches/{id}") + suspend fun getMatch(@Path("id") id: Int): MatchResult + + @Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") + @GET("matches/{id}/matches") + suspend fun getMatchesByCompetition(@Path("id") id: Int): MatchResponse + } diff --git a/src/app/src/main/java/uca/iut/clermont/api/response/areaResponse/AreaResponse.kt b/src/app/src/main/java/uca/iut/clermont/api/response/areaResponse/AreaResponse.kt new file mode 100644 index 0000000..a8c71b9 --- /dev/null +++ b/src/app/src/main/java/uca/iut/clermont/api/response/areaResponse/AreaResponse.kt @@ -0,0 +1,8 @@ +package uca.iut.clermont.api.response.areaResponse + +import uca.iut.clermont.model.Area + +data class AreaResponse( + val areas: List, + val count: Int +) \ No newline at end of file diff --git a/src/app/src/main/java/uca/iut/clermont/data/StubData.kt b/src/app/src/main/java/uca/iut/clermont/data/StubData.kt index 95d1e4b..862cd53 100644 --- a/src/app/src/main/java/uca/iut/clermont/data/StubData.kt +++ b/src/app/src/main/java/uca/iut/clermont/data/StubData.kt @@ -246,7 +246,7 @@ class StubData : DataManager() { ) ) - private val random = java.util.Random() + private val random = Random() val matchList: MutableList = mutableListOf() fun initMatches() { @@ -421,7 +421,7 @@ class StubData : DataManager() { class StubAreaManager(private val parent: StubData) : AreaManager { - override fun getItemsByName(substring: String) = + override suspend fun getItemsByName(substring: String) = parent.areaList.filter { it.name.contains(substring, ignoreCase = true) } override suspend fun getItems(): List = parent.areaList @@ -431,8 +431,6 @@ class StubData : DataManager() { } class StubPeopleManager(private val parent: StubData) : PeopleManager { - override fun getItemsByName(substring: String) = - parent.peopleList.filter { it.name.contains(substring, ignoreCase = true) } override suspend fun getItems() = parent.peopleList @@ -440,11 +438,12 @@ class StubData : DataManager() { } class StubMatchesManager(private val parent: StubData) : MatchesManager { - override fun getNbItemsByCompetition(substring: String) = - parent.matchList.filter { it.competition.name.contains(substring) }.count() - override fun getItemsByCompetition(substring: String) = - parent.matchList.filter { it.competition.name.contains(substring) } + override suspend fun getNbItemsByCompetition(id: Int) = + parent.matchList.filter { it.competition.id == id }.size + + override suspend fun getItemsByCompetition(id: Int) = + parent.matchList.filter { it.competition.id == id } override suspend fun getItems(): List = parent.matchList @@ -453,7 +452,7 @@ class StubData : DataManager() { } class StubCompetitionsManager(private val parent: StubData) : CompetitionsManager { - override fun getItemsByName(substring: String) = + override suspend fun getItemsByName(substring: String) = parent.competitionList.filter { it.name.contains(substring, ignoreCase = true) } override suspend fun getItems() = parent.competitionList diff --git a/src/app/src/main/java/uca/iut/clermont/model/DataManager.kt b/src/app/src/main/java/uca/iut/clermont/model/DataManager.kt index c62bc7a..0910fc8 100644 --- a/src/app/src/main/java/uca/iut/clermont/model/DataManager.kt +++ b/src/app/src/main/java/uca/iut/clermont/model/DataManager.kt @@ -9,22 +9,20 @@ abstract class DataManager { } interface AreaManager : GenericDataManager { - fun getItemsByName(substring: String): List + suspend fun getItemsByName(substring: String): List } -interface PeopleManager : GenericDataManager { - fun getItemsByName(substring: String): List -} +interface PeopleManager : GenericDataManager {} interface MatchesManager : GenericDataManager { - fun getNbItemsByCompetition(substring: String): Int + suspend fun getNbItemsByCompetition(id: Int): Int - fun getItemsByCompetition(substring: String): List + suspend fun getItemsByCompetition(id: Int): List } interface CompetitionsManager : GenericDataManager { - fun getItemsByName(substring: String): List + suspend fun getItemsByName(substring: String): List } interface TeamsManager : GenericDataManager { diff --git a/src/app/src/main/java/uca/iut/clermont/view/DetailFragment.kt b/src/app/src/main/java/uca/iut/clermont/view/DetailFragment.kt index 4a68b36..ab18ce7 100644 --- a/src/app/src/main/java/uca/iut/clermont/view/DetailFragment.kt +++ b/src/app/src/main/java/uca/iut/clermont/view/DetailFragment.kt @@ -79,21 +79,21 @@ class DetailFragment : Fragment() { dateStart.text = formattedDate - nbMatches.text = + /*nbMatches.text = (activity as MainActivity).manager.matchesMgr.getNbItemsByCompetition(competition.name) - .toString() + .toString()*/ } private fun initRecyclerView(view: View) { - val recyclerViewMatches = view.findViewById(R.id.listRecentsMatches) + /*val recyclerViewMatches = view.findViewById(R.id.listRecentsMatches) with(recyclerViewMatches) { layoutManager = LinearLayoutManager(view.context) adapter = MatchesAdapter( (activity as MainActivity).manager.matchesMgr.getItemsByCompetition(competition.name) .toList().toTypedArray() ) - } + }*/ } } \ No newline at end of file