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
13 changes: 13 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,23 @@ android {
"BASE_URL",
"\"${secrets.getProperty("API_URL_DEV")}\""
)
buildConfigField(
"String",
"SOCKET_URL",
"\"${secrets.getProperty("SOCKET_URL_DEV")}\""
)
}
release {
buildConfigField(
"String",
"BASE_URL",
"\"${secrets.getProperty("API_URL_PROD")}\""
)
buildConfigField(
"String",
"SOCKET_URL",
"\"${secrets.getProperty("SOCKET_URL_PROD")}\""
)
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
Expand Down Expand Up @@ -98,6 +108,9 @@ dependencies {
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.1")
implementation("io.socket:socket.io-client:2.1.1") {
exclude(group = "org.json", module = "json")
}
implementation("com.apollographql.apollo:apollo-runtime:4.0.0")
implementation("io.coil-kt.coil3:coil-compose:3.1.0")
implementation("io.coil-kt.coil3:coil-network-okhttp:3.1.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.cornellappdev.score.util.CORNELL_TEAM_NAME
import com.cornellappdev.score.R
import com.cornellappdev.score.model.GameCardData
import com.cornellappdev.score.theme.AmbientColor
Expand Down Expand Up @@ -155,7 +156,7 @@ private fun TeamScore(
if (isCornell) {
Image(
painter = painterResource(R.drawable.cornell_logo),
contentDescription = "Cornell Logo",
contentDescription = "$CORNELL_TEAM_NAME Logo",
modifier = Modifier
.height(27.dp)
.padding(horizontal = 2.dp, vertical = 4.dp)
Expand All @@ -171,7 +172,7 @@ private fun TeamScore(
}
Spacer(modifier = Modifier.width(4.dp))
Text(
text = if (isCornell) "Cornell" else team,
text = if (isCornell) CORNELL_TEAM_NAME else team,
style = heading2,
color = if (winningTeam) GrayPrimary else GrayLight
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.cornellappdev.score.util.CORNELL_TEAM_NAME
import com.cornellappdev.score.R
import com.cornellappdev.score.components.ScorePreview
import com.cornellappdev.score.model.ScoreEvent
Expand Down Expand Up @@ -100,7 +101,7 @@ fun ScoreEventItem(event: ScoreEvent) {
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = homeScore.toString(),
style = if (event.team.name == "Cornell") metricSemibold else metricNormal, // TODO: Check name
style = if (event.team.name == CORNELL_TEAM_NAME) metricSemibold else metricNormal, // TODO: Check name
color = GrayPrimary,
textAlign = TextAlign.Center
)
Expand All @@ -112,7 +113,7 @@ fun ScoreEventItem(event: ScoreEvent) {
)
Text(
text = awayScore.toString(),
style = if (event.team.name != "Cornell") metricSemibold else metricNormal,
style = if (event.team.name != CORNELL_TEAM_NAME) metricSemibold else metricNormal,
color = GrayPrimary,
textAlign = TextAlign.Center
)
Expand Down
67 changes: 53 additions & 14 deletions app/src/main/java/com/cornellappdev/score/model/Game.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cornellappdev.score.model

import androidx.compose.ui.graphics.Color
import com.cornellappdev.score.util.CORNELL_TEAM_NAME
import com.cornellappdev.score.R
import com.cornellappdev.score.util.convertScores
import com.cornellappdev.score.util.formatDateTimeDisplay
Expand Down Expand Up @@ -43,16 +44,17 @@ data class GameDetailsTeam(
val name: String
)

@Serializable
data class GameDetailsBoxScore(
val team: String?,
val period: String?,
val time: String?,
val description: String?,
val scorer: String?,
val assist: String?,
val scoreBy: String?,
val corScore: Int?,
val oppScore: Int?
val team: String? = null,
val period: String? = null,
val time: String? = null,
val description: String? = null,
val scorer: String? = null,
val assist: String? = null,
val scoreBy: String? = null,
val corScore: Int? = null,
val oppScore: Int? = null
)

data class GameDetailsGame(
Expand Down Expand Up @@ -127,7 +129,8 @@ data class DetailsCardData(
val daysUntilGame: Int?,
val hoursUntilGame: Int?,
val homeScore: Int,
val oppScore: Int
val oppScore: Int,
val result: String = ""
)

// Scoring information by round of a game, used in the box score
Expand Down Expand Up @@ -272,7 +275,7 @@ fun GameDetailsGame.toGameCardData(): DetailsCardData {
val (daysUntil, hoursUntil) = getTimeUntilStart(date, time ?: "") ?: (null to null)
val parsedScores = parseResultScore(result)
return DetailsCardData(
title = "Cornell Vs. ${team?.name ?: ""}",
title = "$CORNELL_TEAM_NAME Vs. ${team?.name ?: ""}",
opponentLogo = team?.image ?: "",
opponent = team?.name ?: "",
opponentColor = team?.color ?: Color.White,
Expand All @@ -293,7 +296,7 @@ fun GameDetailsGame.toGameCardData(): DetailsCardData {
scoreBreakdown = scoreBreakdown ?: emptyList(),
gameData = toGameData(
scoreBreakdown = scoreBreakdown,
team1 = TeamBoxScore("Cornell"),
team1 = TeamBoxScore(CORNELL_TEAM_NAME),
team2 = TeamBoxScore(team?.name ?: ""),
sport = sport,
result = result ?: ""
Expand All @@ -304,7 +307,43 @@ fun GameDetailsGame.toGameCardData(): DetailsCardData {
homeScore = convertScores(scoreBreakdown?.getOrNull(0), sport, result ?: "").second
?: parsedScores?.first ?: 0,
oppScore = convertScores(scoreBreakdown?.getOrNull(1), sport, result ?: "").second
?: parsedScores?.second ?: 0
?: parsedScores?.second ?: 0,
result = result ?: ""
)
}

/**
* Merges a live socket update into the current DetailsCardData.
* Null fields in [update] leave existing values unchanged.
*/
fun DetailsCardData.applySocketUpdate(update: SocketGameUpdateData): DetailsCardData {
val newScoreBreakdown = update.scoreBreakdown ?: scoreBreakdown
val newGameData = if (update.scoreBreakdown != null) {
toGameData(
scoreBreakdown = newScoreBreakdown,
team1 = TeamBoxScore(CORNELL_TEAM_NAME),
team2 = TeamBoxScore(opponent),
sport = sport,
result = result
)
} else gameData

val newBoxScore = update.boxScore ?: boxScore
val newScoreEvents = if (update.boxScore != null) {
newBoxScore.filterNotNull().toScoreEvents(opponentLogo)
} else scoreEvent

return copy(
homeScore = update.homeScore
?: update.scoreBreakdown?.let { convertScores(it.getOrNull(0), sport, result).second }
?: homeScore,
oppScore = update.oppScore
?: update.scoreBreakdown?.let { convertScores(it.getOrNull(1), sport, result).second }
?: oppScore,
scoreBreakdown = newScoreBreakdown,
gameData = newGameData,
boxScore = newBoxScore,
scoreEvent = newScoreEvents
)
}

Expand All @@ -328,4 +367,4 @@ fun List<GameDetailsBoxScore>.toScoreEvents(teamLogo: String): List<ScoreEvent>
description = boxScore.description
)
}
}
}
73 changes: 73 additions & 0 deletions app/src/main/java/com/cornellappdev/score/model/SocketManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.cornellappdev.score.model

import android.util.Log
import com.cornellappdev.score.BuildConfig
import io.socket.client.IO
import io.socket.client.Socket
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import org.json.JSONObject
import java.util.Collections
import javax.inject.Inject
import javax.inject.Singleton

private const val TAG = "SocketManager"

@Singleton
class SocketManager @Inject constructor(private val appScope: CoroutineScope) {

private val _gameUpdateFlow =
MutableSharedFlow<SocketGameUpdateEnvelope>(extraBufferCapacity = 16)
val gameUpdateFlow: SharedFlow<SocketGameUpdateEnvelope> = _gameUpdateFlow.asSharedFlow()

private val activeSubscriptions: MutableSet<String> =
Collections.synchronizedSet(mutableSetOf())

private val json = Json { ignoreUnknownKeys = true; isLenient = true }

private val socket: Socket by lazy {
val opts = IO.Options.builder()
.setTransports(arrayOf("websocket"))
.build()
IO.socket(BuildConfig.SOCKET_URL, opts).also { s ->
// "on" is a listener
s.on(Socket.EVENT_CONNECT) {
Log.d(TAG, "Connected")
val subscriptions = synchronized(activeSubscriptions) {
activeSubscriptions.toList()
}
subscriptions.forEach { id ->
s.emit("subscribe", JSONObject().put("gameId", id))
}
}
s.on(Socket.EVENT_DISCONNECT) { args ->
Log.d(
TAG,
"Disconnected: ${args.firstOrNull()}"
)
}
s.on(Socket.EVENT_CONNECT_ERROR) { args -> Log.e(TAG, "Error: ${args.firstOrNull()}") }
s.on("game_update") { args ->
val raw = args.firstOrNull() as? JSONObject ?: return@on
runCatching { json.decodeFromString<SocketGameUpdateEnvelope>(raw.toString()) }
.onSuccess { appScope.launch { _gameUpdateFlow.emit(it) } }
.onFailure { Log.e(TAG, "Parse error: $it") }
}
s.connect()
}
}

fun subscribe(gameId: String) {
activeSubscriptions.add(gameId)
socket.emit("subscribe", JSONObject().put("gameId", gameId))
}

fun unsubscribe(gameId: String) {
activeSubscriptions.remove(gameId)
socket.emit("unsubscribe", JSONObject().put("gameId", gameId))
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/cornellappdev/score/model/SocketModels.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.cornellappdev.score.model

import kotlinx.serialization.Serializable

@Serializable
data class SocketGameUpdateEnvelope(
val type: String,
val gameId: String,
val timestamp: String,
val data: SocketGameUpdateData
)

@Serializable
data class SocketGameUpdateData(
val homeScore: Int? = null,
val oppScore: Int? = null,
val scoreBreakdown: List<List<String?>?>? = null,
val boxScore: List<GameDetailsBoxScore?>? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun DetailsCardData.toCalendarEvent(): CalendarEvent? {
val date = this.date ?: return null

return CalendarEvent(
title = "Cornell vs. ${this.opponent}",
title = "$CORNELL_TEAM_NAME vs. ${this.opponent}",
description = "${this.sport} game (${this.gender})",
location = this.locationString,
date = date,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.cornellappdev.score.util

const val CORNELL_TEAM_NAME = "Cornell"
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ val gameList = listOf(
PRINCETON_GAME
)

val team1 = TeamBoxScore(name = "Cornell")
val team1 = TeamBoxScore(name = CORNELL_TEAM_NAME)
val team2 = TeamBoxScore(name = "Yale University")

val teamScore1 = TeamScore(
Expand Down Expand Up @@ -129,7 +129,7 @@ val longGameData = GameData(teamScores = longGameTeamScore1 to longGameTeamScore
val extraLongGameData = GameData(teamScores = extraLongGameTeamScore1 to extraLongGameTeamScore2)

val team3 = TeamGameSummary(
name = "Cornell",
name = CORNELL_TEAM_NAME,
"https://cornellbigred.com/images/logos/penn_200x200.png?width=80&height=80&mode=max"
)
val team4 = TeamGameSummary(
Expand Down
Loading
Loading