Skip to content

Commit db7abb8

Browse files
committed
calendar ticket link
1 parent 254d1c7 commit db7abb8

5 files changed

Lines changed: 92 additions & 19 deletions

File tree

app/src/main/graphql/GameById.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ query GameById($id: String!) {
2828
corScore
2929
oppScore
3030
}
31+
ticketLink
3132
}
3233
}

app/src/main/java/com/cornellappdev/score/model/Game.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ data class GameDetailsGame(
6767
val time: String?,
6868
val scoreBreakdown: List<List<String?>?>?,
6969
val team: GameDetailsTeam?,
70-
val boxScore: List<GameDetailsBoxScore>?
70+
val boxScore: List<GameDetailsBoxScore>?,
71+
val ticketUrl: String?
7172
)
7273

7374

@@ -126,7 +127,8 @@ data class DetailsCardData(
126127
val daysUntilGame: Int?,
127128
val hoursUntilGame: Int?,
128129
val homeScore: Int,
129-
val oppScore: Int
130+
val oppScore: Int,
131+
val ticketUrl: String?
130132
)
131133

132134
// Scoring information by round of a game, used in the box score
@@ -146,7 +148,7 @@ data class TeamScore(
146148
// Aggregated game data showing scores for both teams
147149
data class GameData(
148150
val teamScores: Pair<TeamScore, TeamScore>
149-
){
151+
) {
150152
val maxPeriods: Int
151153
get() =
152154
maxOf(
@@ -298,7 +300,8 @@ fun GameDetailsGame.toGameCardData(): DetailsCardData {
298300
homeScore = convertScores(scoreBreakdown?.getOrNull(0), sport).second
299301
?: parsedScores?.first ?: 0,
300302
oppScore = convertScores(scoreBreakdown?.getOrNull(1), sport).second
301-
?: parsedScores?.second ?: 0
303+
?: parsedScores?.second ?: 0,
304+
ticketUrl = ticketUrl ?: ""
302305
)
303306
}
304307

app/src/main/java/com/cornellappdev/score/model/GameByIdQueryMappers.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ fun GameByIdQuery.Game.toGameDetails(): GameDetailsGame {
1717
time = this.time,
1818
scoreBreakdown = this.scoreBreakdown,
1919
team = this.team?.toGameDetailsTeam(),
20-
boxScore = this.boxScore?.mapNotNull { it?.toGameDetailsBoxScore() }
20+
boxScore = this.boxScore?.mapNotNull { it?.toGameDetailsBoxScore() },
21+
ticketUrl = ticketLink
2122
)
2223
}
24+
2325
fun GameByIdQuery.Team.toGameDetailsTeam(): GameDetailsTeam {
2426
return GameDetailsTeam(
2527
id = this.id,
26-
color = parseColor(this.color).copy(alpha = 0.4f*255),
28+
color = parseColor(this.color).copy(alpha = 0.4f * 255),
2729
image = this.image,
2830
name = this.name
2931
)

app/src/main/java/com/cornellappdev/score/screen/GameDetailsScreen.kt

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.cornellappdev.score.screen
22

33
import ScoringSummary
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.net.Uri
7+
import android.provider.CalendarContract
48
import androidx.compose.foundation.Image
59
import androidx.compose.foundation.background
610
import androidx.compose.foundation.horizontalScroll
@@ -31,6 +35,7 @@ import androidx.compose.ui.unit.sp
3135
import androidx.hilt.navigation.compose.hiltViewModel
3236
import com.cornellappdev.score.R
3337
import com.cornellappdev.score.components.BoxScore
38+
import com.cornellappdev.score.components.ButtonPrimary
3439
import com.cornellappdev.score.components.EmptyStateBox
3540
import com.cornellappdev.score.components.ErrorState
3641
import com.cornellappdev.score.components.GameDetailsLoadingScreen
@@ -55,6 +60,10 @@ import com.cornellappdev.score.theme.Style.heading3
5560
import com.cornellappdev.score.theme.White
5661
import com.cornellappdev.score.viewmodel.GameDetailsViewModel
5762
import java.time.LocalDate
63+
import java.time.LocalTime
64+
import java.time.ZoneId
65+
import java.time.format.DateTimeFormatter
66+
import java.util.Locale
5867

5968
@Composable
6069
fun GameDetailsScreen(
@@ -220,16 +229,26 @@ fun GameDetailsContent(
220229
}
221230

222231
Spacer(modifier = Modifier.weight(1f))
223-
224-
// ButtonPrimary(
225-
// "Add to Calendar",
226-
// painterResource(R.drawable.ic_calendar),
227-
// onClick = {
228-
// gameCard.toCalendarEvent()?.let { event ->
229-
// addToCalendar(context = context, event)
230-
// }
231-
// }
232-
// )
232+
Row() {
233+
ButtonPrimary(
234+
"Buy Tickets",
235+
painterResource(R.drawable.ticket),
236+
onClick = {
237+
gameCard.ticketUrl?.let { url ->
238+
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
239+
context.startActivity(intent)
240+
}
241+
}
242+
)
243+
Spacer(Modifier.size(16.dp))
244+
ButtonPrimary(
245+
"Add to Calendar",
246+
painterResource(R.drawable.ic_calendar),
247+
onClick = {
248+
addGameToCalendar(context, gameCard)
249+
}
250+
)
251+
}
233252
}
234253

235254
}
@@ -331,7 +350,8 @@ private fun GameDetailsPreview() {
331350
daysUntilGame = 6,
332351
hoursUntilGame = 144,
333352
homeScore = 78,
334-
oppScore = 75
353+
oppScore = 75,
354+
ticketUrl = ""
335355
), navigateToGameScoreSummary = {}
336356
)
337357
}
@@ -382,7 +402,36 @@ private fun EmptyGameDetailsPreview() {
382402
daysUntilGame = 0,
383403
hoursUntilGame = 0,
384404
homeScore = 0,
385-
oppScore = 0
386-
), navigateToGameScoreSummary = {}
405+
oppScore = 0,
406+
ticketUrl = ""
407+
),
408+
navigateToGameScoreSummary = {},
387409
)
410+
}
411+
412+
// helper
413+
fun addGameToCalendar(context: Context, gameCard: DetailsCardData) {
414+
val date = gameCard.date ?: return
415+
val time = gameCard.time
416+
417+
val startDateTime = try {
418+
val formatter = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH)
419+
val localTime = LocalTime.parse(time.trim().uppercase(), formatter)
420+
date.atTime(localTime)
421+
} catch (e: Exception) {
422+
date.atStartOfDay()
423+
}
424+
425+
val zoneId = ZoneId.systemDefault()
426+
val startMillis = startDateTime.atZone(zoneId).toInstant().toEpochMilli()
427+
val endMillis = startDateTime.plusHours(2).atZone(zoneId).toInstant().toEpochMilli()
428+
429+
val intent = Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI).apply {
430+
putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startMillis)
431+
putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endMillis)
432+
putExtra(CalendarContract.Events.TITLE, gameCard.title)
433+
putExtra(CalendarContract.Events.EVENT_LOCATION, gameCard.locationString)
434+
putExtra(CalendarContract.Events.DESCRIPTION, "${gameCard.sport} - ${gameCard.gender}")
435+
}
436+
context.startActivity(intent)
388437
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:pathData="M14.551,0.866L0.862,14.677L3.307,17.122C6.362,16.266 7.218,17.977 6.362,20.299L9.051,22.866L22.74,9.055L20.296,6.611C17.167,7.491 16.792,4.777 16.996,3.311L14.551,0.866Z"
8+
android:strokeWidth="1.22222"
9+
android:fillColor="#00000000"
10+
android:strokeColor="#ffffff"
11+
android:strokeLineCap="round"/>
12+
<path
13+
android:pathData="M10.151,5.266L11.129,6.244M12.229,7.344L13.696,8.811M14.918,10.033L16.262,11.377M17.485,12.599L18.34,13.455"
14+
android:strokeWidth="1.22222"
15+
android:fillColor="#00000000"
16+
android:strokeColor="#ffffff"
17+
android:strokeLineCap="round"/>
18+
</vector>

0 commit comments

Comments
 (0)