Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions app/src/main/java/com/cornellappdev/score/components/SportCard.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cornellappdev.score.components

import android.icu.text.SimpleDateFormat
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
Expand All @@ -9,7 +8,14 @@ import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
Expand Down Expand Up @@ -40,8 +46,6 @@ import com.cornellappdev.score.theme.Style.labelsNormal
import com.cornellappdev.score.theme.Style.teamName
import com.cornellappdev.score.theme.Style.universityText
import com.cornellappdev.score.theme.saturatedGreen
import java.util.Date
import java.util.Locale

@Composable
fun SportCard(
Expand Down Expand Up @@ -101,7 +105,9 @@ fun SportCard(
) {
AsyncImage(
model = teamLogo,
modifier = Modifier.height(20.dp).padding(start = 4.dp, end = 4.dp),
modifier = Modifier
.height(20.dp)
.padding(horizontal = 4.dp),
contentDescription = ""
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ fun UpcomingGamesCarousel(games: List<GameCardData>) {
location = game.location,
modifier = Modifier,
headerModifier = Modifier,
gradientColor1 = CornellRed, //TODO: is it okay if this is hardcoded
gradientColor2 = Color(game.teamColor)
gradientColor1 = CornellRed,
gradientColor2 = game.teamColor
)
}

Expand Down
27 changes: 24 additions & 3 deletions app/src/main/java/com/cornellappdev/score/model/Game.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.cornellappdev.score.model

import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.Color
import com.cornellappdev.score.R
import com.cornellappdev.score.util.outputFormatter
import com.cornellappdev.score.util.parseDateOrNull
import java.time.LocalDate

// TODO Refactor to make easier to filter... actual gender, etc.

data class Game(
val teamName: String,
val teamLogo: String,
val teamColor: String,
val teamColor: Color,
val gender: String,
val sport: String,
val date: String,
Expand All @@ -19,7 +22,7 @@ data class Game(
data class GameCardData(
val teamLogo: String,
val team: String,
val teamColor: Int,
val teamColor: Color,
val date: LocalDate?,
val dateString: String,
val isLive: Boolean,
Expand Down Expand Up @@ -98,4 +101,22 @@ enum class GameStatus {
NOT_STARTED,
IN_PROGRESS,
COMPLETED
}

fun Game.toGameCardData(): GameCardData{
return GameCardData(
teamLogo = teamLogo,
team = teamName,
teamColor = teamColor,
date = parseDateOrNull(date),
dateString = parseDateOrNull(date)?.format(outputFormatter)
?: date,
isLive = (LocalDate.now() == parseDateOrNull(date)),
location = city,
gender = gender,
genderIcon = if (gender == "Mens") R.drawable.ic_gender_men else R.drawable.ic_gender_women,
sport = sport,
sportIcon = Sport.fromDisplayName(sport)?.emptyIcon
?: R.drawable.ic_empty_placeholder
)
}
24 changes: 24 additions & 0 deletions app/src/main/java/com/cornellappdev/score/model/Result.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cornellappdev.score.model

import com.apollographql.apollo.api.ApolloResponse
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.exception.NoDataException

/**
* Maps an ApolloResponse to a generic Kotlin result. It either provides the data with no errors, or
* a failure response containing the error message in the throwable.
*/
fun <T : Operation.Data> ApolloResponse<T>.toResult(): Result<T> {
if (hasErrors() || exception != null) {
return Result.failure(
exception?.cause ?: RuntimeException(
errors?.firstOrNull()?.message ?: "Unknown error occurred"
)
)
}
return try {
Result.success(dataOrThrow())
} catch (e: NoDataException) {
Result.failure(e)
}
}
43 changes: 24 additions & 19 deletions app/src/main/java/com/cornellappdev/score/model/ScoreRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Singleton
import com.cornellappdev.score.util.parseColor

/**
* This is a singleton responsible for fetching and caching all data for Score.
Expand All @@ -19,7 +20,6 @@ import javax.inject.Singleton
@Singleton
class ScoreRepository @Inject constructor(
private val apolloClient: ApolloClient,
// TODO use this to launch queries
private val appScope: CoroutineScope,
) {
private val _upcomingGamesFlow =
Expand All @@ -34,28 +34,33 @@ class ScoreRepository @Inject constructor(
fun fetchGames() = appScope.launch {
_upcomingGamesFlow.value = ApiResponse.Loading
try {
val response = (apolloClient.query(GamesQuery()).execute())
val games = response.data?.games ?: emptyList()
Log.d("ScoreRepository", "response fetched successfully")
val result = (apolloClient.query(GamesQuery()).execute()).toResult()

val list: List<Game> = games.mapNotNull { game ->
game?.team?.image?.let {
Game(
teamLogo = it,//game.team.image,
teamName = game.team.name,
teamColor = game.team.color,
gender = game.gender,
sport = game.sport,
date = game.date,
city = game.city
)
}
if (result.isSuccess) {
val games = result.getOrNull()

val upcomingGameslist: List<Game> =
games?.games?.mapNotNull { game ->
game?.team?.image?.let {
Game(
teamLogo = it,
teamName = game.team.name,
teamColor = parseColor(game.team.color).copy(alpha = 0.4f*255),
gender = game.gender,
sport = game.sport,
date = game.date,
city = game.city
)
}
} ?: emptyList()
_upcomingGamesFlow.value = ApiResponse.Success(upcomingGameslist)
} else {
_upcomingGamesFlow.value = ApiResponse.Error
}
Log.d("ScoreRepository", "#games: ${list.size}")
_upcomingGamesFlow.value = ApiResponse.Success(list.toList())

} catch (e: Exception) {
Log.e("ScoreRepository", "Error fetching posts: ", e)
_upcomingGamesFlow.value = ApiResponse.Error
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.cornellappdev.score.nav.root

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.hilt.navigation.compose.hiltViewModel
Expand All @@ -11,7 +9,6 @@ import androidx.navigation.compose.rememberNavController
import com.cornellappdev.score.screen.HomeScreen
import kotlinx.serialization.Serializable

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun RootNavigation(
rootNavigationViewModel: RootNavigationViewModel = hiltViewModel(),
Expand Down
Loading