Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 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(start = 4.dp, end = 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
28 changes: 24 additions & 4 deletions app/src/main/java/com/cornellappdev/score/model/Game.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
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.parseDate
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: Int,
val gender: String,
val sport: String,
val date: String,
val city: String
)
) {
val toGameCardData =
GameCardData(
teamLogo = teamLogo,
team = teamName,
teamColor = Color(teamColor),
date = parseDate(date),
dateString = parseDate(date)?.format(outputFormatter)
?: date,
isLive = (LocalDate.now() == parseDate(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
)
}

//Data for HomeScreen game displays
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
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)
}
}
53 changes: 33 additions & 20 deletions app/src/main/java/com/cornellappdev/score/model/ScoreRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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 +33,42 @@ 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 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
)
}
val result = (apolloClient.query(GamesQuery()).execute()).toResult()

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 = formatColor(game.team.color),
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
}
}
}
}

/**
* Converts from format "#xxxxxx" to a valid hex, with alpha = 40. Ready to be passed into Color()
*/
fun formatColor(color: String): Int {
val alpha = (40 * 255 / 100)// Convert percent to hex (0-255)
val colorInt = Integer.parseInt(color.removePrefix("#"), 16)
return (alpha shl 24) or colorInt
}
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