Realization of the API call in the Detail Page
continuous-integration/drone/push Build is passing Details

pull/8/head
Emre KARTAL 2 years ago
parent 31d8f6d0cf
commit 64d75971b0

@ -66,7 +66,7 @@ class ApiManager : DataManager() {
override suspend fun getItemsByCompetition(id: Int): List<Match> = override suspend fun getItemsByCompetition(id: Int): List<Match> =
coroutineScope { coroutineScope {
val matches = footballApi.getMatches() val matches = footballApi.getMatchesByCompetition(id)
return@coroutineScope matches.matches.map { it.toModel() } return@coroutineScope matches.matches.map { it.toModel() }
} }

@ -25,11 +25,11 @@ interface FootballApi {
@GET("persons/{id}") @GET("persons/{id}")
suspend fun getPlayer(@Path("id") playerId: Int): PlayerResponse suspend fun getPlayer(@Path("id") playerId: Int): PlayerResponse
@Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @Headers("X-Auth-Token: 621ef06e148542f98b4993a5442421eb")
@GET("competitions") @GET("competitions")
suspend fun getCompetitions(): CompetitionResponse suspend fun getCompetitions(): CompetitionResponse
@Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @Headers("X-Auth-Token: 8f51b43de0444026bd3ec3484f082575")
@GET("competitions/{id}") @GET("competitions/{id}")
suspend fun getCompetition(@Path("id") id: Int): CompetitionResult suspend fun getCompetition(@Path("id") id: Int): CompetitionResult
@ -37,12 +37,12 @@ interface FootballApi {
@GET("matches") @GET("matches")
suspend fun getMatches(): MatchResponse suspend fun getMatches(): MatchResponse
@Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @Headers("X-Auth-Token: 621ef06e148542f98b4993a5442421eb")
@GET("matches/{id}") @GET("matches/{id}")
suspend fun getMatch(@Path("id") id: Int): MatchResult suspend fun getMatch(@Path("id") id: Int): MatchResult
@Headers("X-Auth-Token: 7814ffe5b0314b5291a287d32a178e57") @Headers("X-Auth-Token: b002ff114afa41a590e2baef63d8c689")
@GET("matches/{id}/matches") @GET("competitions/{id}/matches")
suspend fun getMatchesByCompetition(@Path("id") id: Int): MatchResponse suspend fun getMatchesByCompetition(@Path("id") id: Int): MatchResponse
} }

@ -8,10 +8,16 @@ import android.widget.ImageButton
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import uca.iut.clermont.R import uca.iut.clermont.R
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import uca.iut.clermont.model.Competition import uca.iut.clermont.model.Competition
import uca.iut.clermont.view.adapter.MatchesAdapter
import uca.iut.clermont.view.viewModel.DetailViewModel
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@ -19,6 +25,8 @@ class DetailFragment : Fragment() {
private var isLiked = false private var isLiked = false
private lateinit var competition: Competition private lateinit var competition: Competition
private val viewModel: DetailViewModel by viewModels()
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -30,10 +38,33 @@ class DetailFragment : Fragment() {
val id = arguments?.getInt("idItem")!! val id = arguments?.getInt("idItem")!!
//competition = (activity as MainActivity).manager.competitionsMgr.getItemById(id)!!
viewModel.competition.observe(viewLifecycleOwner, Observer { comp ->
comp?.let {
competition = comp
initializeView(view) initializeView(view)
initRecyclerView(view) initRecyclerView(view)
}
})
viewModel.loadCurrentCompetition(id)
viewModel.nbCompetitionMatches.observe(viewLifecycleOwner, Observer { comp ->
comp?.let {
initNumberMatches(view)
}
})
viewModel.loadNumberMatches(id)
viewModel.competitionMatches.observe(viewLifecycleOwner, Observer { competitions ->
competitions?.let {
initRecyclerView(view)
}
})
viewModel.loadMatches(id)
return view; return view;
} }
@ -76,21 +107,24 @@ class DetailFragment : Fragment() {
dateStart.text = formattedDate dateStart.text = formattedDate
/*nbMatches.text =
(activity as MainActivity).manager.matchesMgr.getNbItemsByCompetition(competition.name)
.toString()*/
} }
private fun initNumberMatches(view: View) {
val nbMatches = view.findViewById<TextView>(R.id.nbMatches)
nbMatches.text = viewModel.nbCompetitionMatches.value.toString()
}
private fun initRecyclerView(view: View) { private fun initRecyclerView(view: View) {
/*val recyclerViewMatches = view.findViewById<RecyclerView>(R.id.listRecentsMatches) val recyclerViewMatches = view.findViewById<RecyclerView>(R.id.listRecentsMatches)
with(recyclerViewMatches) { with(recyclerViewMatches) {
layoutManager = LinearLayoutManager(view.context) layoutManager = LinearLayoutManager(view.context)
adapter = MatchesAdapter( adapter = viewModel.competitionMatches.value?.toList()?.let {
(activity as MainActivity).manager.matchesMgr.getItemsByCompetition(competition.name) MatchesAdapter(
.toList().toTypedArray() it.toTypedArray()
) )
}*/ }
}
} }
} }

@ -6,6 +6,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.ImageButton import android.widget.ImageButton
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -19,7 +20,7 @@ import uca.iut.clermont.view.viewModel.FavoriteViewModel
class FavoriteFragment : Fragment(), FavoritesAdapter.OnItemClickListener { class FavoriteFragment : Fragment(), FavoritesAdapter.OnItemClickListener {
private val viewModel: FavoriteViewModel by viewModels<FavoriteViewModel>() private val viewModel: FavoriteViewModel by viewModels()
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -70,8 +71,8 @@ class FavoriteFragment : Fragment(), FavoritesAdapter.OnItemClickListener {
} }
override fun onItemClick(position: Int) { override fun onItemClick(position: Int) {
/*val competitions = viewModel.competitions val competitions = viewModel.competitions.value!!
val bundle = bundleOf("idItem" to competitions[position].id) val bundle = bundleOf("idItem" to competitions[position].id)
findNavController().navigate(R.id.action_favoriteFragment_to_detailFragment, bundle)*/ findNavController().navigate(R.id.action_favoriteFragment_to_detailFragment, bundle)
} }
} }

@ -19,7 +19,7 @@ import uca.iut.clermont.view.viewModel.HomeViewModel
class HomeFragment : Fragment() { class HomeFragment : Fragment() {
val viewModel: HomeViewModel by viewModels<HomeViewModel>() val viewModel: HomeViewModel by viewModels()
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -38,19 +38,19 @@ class HomeFragment : Fragment() {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
initRecyclerView(view, it) initRecyclerView(view, it)
} else { } else {
text.setText("No games started yet!") text.setText(R.string.noMatches)
} }
} }
}) })
displayMatches(view) displayMatches()
buttonFavorite.setOnClickListener { buttonFavorite.setOnClickListener {
findNavController().navigate(R.id.favoriteFragment) findNavController().navigate(R.id.favoriteFragment)
} }
restartMatches.setOnClickListener { restartMatches.setOnClickListener {
displayMatches(view) displayMatches()
} }
return view return view
@ -61,12 +61,10 @@ class HomeFragment : Fragment() {
with(recyclerViewMatches) { with(recyclerViewMatches) {
layoutManager = LinearLayoutManager(view.context) layoutManager = LinearLayoutManager(view.context)
adapter = MatchesAdapter(matches.toList().toTypedArray()) adapter = MatchesAdapter(matches.toList().toTypedArray())
} }
} }
private fun displayMatches(view: View) { private fun displayMatches() {
viewModel.loadMatches() viewModel.loadMatches()
} }

@ -36,6 +36,16 @@ class MatchesAdapter(private val recentMatches: Array<Match>) :
val formatter = SimpleDateFormat("dd/MM/yyyy' 'HH:mm", Locale.US) val formatter = SimpleDateFormat("dd/MM/yyyy' 'HH:mm", Locale.US)
val formattedDate = formatter.format(date.time) val formattedDate = formatter.format(date.time)
with(holder.iconStatus) {
setImageResource(R.drawable.mi_temp)
layoutParams.width = 0
layoutParams.height = 0
(layoutParams as ViewGroup.MarginLayoutParams).apply {
topMargin = 8
bottomMargin = 7
}
}
if (recentMatches[position].status == "IN_PLAY") { if (recentMatches[position].status == "IN_PLAY") {
with(holder.iconStatus) { with(holder.iconStatus) {
setImageResource(R.drawable.live) setImageResource(R.drawable.live)
@ -46,7 +56,6 @@ class MatchesAdapter(private val recentMatches: Array<Match>) :
bottomMargin = 0 bottomMargin = 0
} }
} }
} }
if (recentMatches[position].status == "PAUSED") { if (recentMatches[position].status == "PAUSED") {
@ -57,7 +66,8 @@ class MatchesAdapter(private val recentMatches: Array<Match>) :
} }
} }
holder.dateCompetition.text = recentMatches[position].competition.name.plus(" : ").plus(formattedDate) holder.dateCompetition.text =
recentMatches[position].competition.name.plus(" : ").plus(formattedDate)
Glide.with(holder.itemView.context) Glide.with(holder.itemView.context)
.load(recentMatches[position].homeTeam.crest) .load(recentMatches[position].homeTeam.crest)

@ -1,4 +1,37 @@
package uca.iut.clermont.view.viewModel package uca.iut.clermont.view.viewModel
class DetailViewModel { import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import uca.iut.clermont.api.ApiManager
import uca.iut.clermont.model.Competition
import uca.iut.clermont.model.Match
import java.util.*
class DetailViewModel : ViewModel() {
val manager = ApiManager()
val competition = MutableLiveData<Competition?>()
val competitionMatches = MutableLiveData<List<Match>>()
val nbCompetitionMatches = MutableLiveData<Int>()
fun loadCurrentCompetition(id: Int) = viewModelScope.launch {
val result = manager.competitionsMgr.getItemById(id)
competition.value = result
}
fun loadMatches(id: Int) = viewModelScope.launch {
val matchResults = manager.matchesMgr.getItemsByCompetition(id)
competitionMatches.value = matchResults.filter { it.status != "TIMED" && it.status != "SCHEDULED" }
.apply { forEach { it.date.add(Calendar.HOUR_OF_DAY, 2) } }
.sortedBy { it.competition.name }
.sortedByDescending { it.date }
}
fun loadNumberMatches(id: Int) = viewModelScope.launch {
val nb = manager.matchesMgr.getNbItemsByCompetition(id)
nbCompetitionMatches.value = nb
}
} }

@ -20,7 +20,8 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:src="@drawable/arrow" /> android:src="@drawable/arrow"
android:contentDescription="@string/imageNotFound" />
<Button <Button
android:id="@+id/buttonTextHome" android:id="@+id/buttonTextHome"

@ -19,7 +19,7 @@
android:id="@+id/textViewTitle" android:id="@+id/textViewTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Hello" android:text="@string/hello"
android:textColor="@color/title" android:textColor="@color/title"
android:textSize="20sp" android:textSize="20sp"
android:textStyle="bold" /> android:textStyle="bold" />
@ -28,10 +28,11 @@
android:id="@+id/buttonFavorite" android:id="@+id/buttonFavorite"
android:layout_width="34dp" android:layout_width="34dp"
android:layout_height="34dp" android:layout_height="34dp"
android:layout_gravity="right" android:layout_gravity="end"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:scaleType="fitCenter" android:scaleType="fitCenter"
android:src="@drawable/icon_like" /> android:src="@drawable/icon_like"
android:contentDescription="@string/imageNotFound" />
</FrameLayout> </FrameLayout>

@ -19,8 +19,8 @@
android:layout_width="64dp" android:layout_width="64dp"
android:layout_height="64dp" android:layout_height="64dp"
android:layout_marginHorizontal="15dp" android:layout_marginHorizontal="15dp"
android:contentDescription="" android:layout_marginVertical="10dp"
android:layout_marginVertical="10dp" /> android:contentDescription="@string/imageNotFound"/>
<TextView <TextView
android:id="@+id/textFavorites" android:id="@+id/textFavorites"

@ -24,9 +24,10 @@
android:id="@+id/iconStatus" android:id="@+id/iconStatus"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_gravity="right" android:layout_gravity="end"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_marginBottom="7dp" /> android:layout_marginBottom="7dp"
android:contentDescription="@string/imageNotFound" />
</FrameLayout> </FrameLayout>

@ -8,11 +8,10 @@
<ImageView <ImageView
android:id="@+id/imageDetail" android:id="@+id/imageDetail"
android:layout_width="170dp" android:layout_width="220dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginTop="30dp" android:layout_marginTop="30dp"
android:scaleType="centerCrop" android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.25" app:layout_constraintHeight_percent="0.25"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"

@ -22,7 +22,8 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:src="@drawable/arrow" /> android:src="@drawable/arrow"
android:contentDescription="@string/imageNotFound" />
<Button <Button
android:id="@+id/buttonTextHome" android:id="@+id/buttonTextHome"

@ -20,7 +20,7 @@
android:id="@+id/textViewTitle" android:id="@+id/textViewTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Hello" android:text="@string/hello"
android:textColor="@color/title" android:textColor="@color/title"
android:textSize="20sp" android:textSize="20sp"
android:textStyle="bold" /> android:textStyle="bold" />
@ -29,10 +29,11 @@
android:id="@+id/buttonFavorite" android:id="@+id/buttonFavorite"
android:layout_width="34dp" android:layout_width="34dp"
android:layout_height="34dp" android:layout_height="34dp"
android:layout_gravity="right" android:layout_gravity="end"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:scaleType="fitCenter" android:scaleType="fitCenter"
android:src="@drawable/icon_like" /> android:src="@drawable/icon_like"
android:contentDescription="@string/imageNotFound" />
</FrameLayout> </FrameLayout>

@ -13,7 +13,8 @@
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent"
android:contentDescription="@string/imageNotFound" />
<ImageView <ImageView
android:id="@+id/shadow" android:id="@+id/shadow"

@ -1,3 +1,7 @@
<resources> <resources>
<string name="app_name">Scor_It</string> <string name="app_name">Scor_It</string>
<string name="noMatches">No games started yet!</string>
<string name="imageNotFound">Image not found</string>
<string name="hello">Hello</string>
</resources> </resources>
Loading…
Cancel
Save