Repository navigation
[1/2] Add highlights networking and data layer #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
65b010a
Add highlights networking and data layer
amjiao f12b996
Fix apostrophe type error
amjiao 528a8ff
Fix lint failure
amjiao 4b35210
Reformat Highlights.kt
amjiao ef24271
Clean up files/reorganize
amjiao 21ab730
Address pr comments
amjiao 556d868
Merge branch 'main' of https://github.com/cuappdev/score-android into…
amjiao f65fb1b
Readability
amjiao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| query Highlights($sportsType: String) { | ||
| articles(sportsType: $sportsType) { | ||
| title | ||
| image | ||
| sportsType | ||
| publishedAt | ||
| url | ||
| } | ||
| youtubeVideos { | ||
| title | ||
| thumbnail | ||
| url | ||
| publishedAt | ||
| duration | ||
| sportsType | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 65 additions & 6 deletions
71
app/src/main/java/com/cornellappdev/score/model/Highlights.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,92 @@ | ||
| package com.cornellappdev.score.model | ||
|
|
||
| import com.cornellappdev.score.util.isValidSport | ||
| import com.cornellappdev.score.util.outputFormatter | ||
| import com.cornellappdev.score.util.parseIsoDateToLocalDateOrNull | ||
| import com.example.score.HighlightsQuery | ||
| import java.time.LocalDate | ||
|
|
||
| data class VideoHighlightData( | ||
| val title: String, | ||
| val thumbnailImageUrl: String, | ||
| val videoUrl: String, | ||
| val date: String, | ||
| val sport: Sport, | ||
| val gender: GenderDivision, | ||
| val gender: GenderDivision?, | ||
| val duration: String? = null, | ||
| val finalScore: String? = null, | ||
| val photoCredit: String? = null | ||
| val photoCredit: String? = null, | ||
| val date: LocalDate?, | ||
| val dateString: String, | ||
| val sport: Sport?, | ||
| ) | ||
|
|
||
| data class ArticleHighlightData( | ||
| val title: String, | ||
| val imageUrl: String, | ||
| val articleUrl: String, | ||
| val date: String, | ||
| val sport: Sport | ||
| val date: LocalDate?, | ||
| val dateString: String, | ||
| val sport: Sport? | ||
| ) | ||
|
|
||
| sealed class HighlightData { | ||
| abstract val title: String | ||
| abstract val date: LocalDate? | ||
| abstract val dateString: String | ||
| abstract val sport: Sport? | ||
|
|
||
| data class Video(val data: VideoHighlightData) : HighlightData() { | ||
| override val title = data.title | ||
| override val date = data.date | ||
| override val dateString = data.dateString | ||
| override val sport = data.sport | ||
| } | ||
|
|
||
| data class Article(val data: ArticleHighlightData) : HighlightData() { | ||
| override val title = data.title | ||
| override val date = data.date | ||
| override val dateString = data.dateString | ||
| override val sport = data.sport | ||
| } | ||
| } | ||
|
|
||
| fun HighlightsQuery.YoutubeVideo.toHighlightData(): HighlightData? { | ||
| val sportName = sportsType ?: return null | ||
| if (!isValidSport(sportName)) return null | ||
|
|
||
| val date = parseIsoDateToLocalDateOrNull(publishedAt) | ||
|
|
||
| return HighlightData.Video( | ||
| VideoHighlightData( | ||
| title = title, | ||
| thumbnailImageUrl = thumbnail, | ||
| videoUrl = url, | ||
| date = date, | ||
| dateString = date?.format(outputFormatter) ?: "-/-/-", | ||
| sport = Sport.fromDisplayName(sportName), | ||
| gender = if (title.contains("Men")) { | ||
| GenderDivision.MALE | ||
| } else { | ||
| GenderDivision.FEMALE | ||
| }, | ||
| duration = duration | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| fun HighlightsQuery.Article.toHighlightData(): HighlightData? { | ||
| val sportName = sportsType | ||
| if (!isValidSport(sportName)) return null | ||
|
|
||
| val date = parseIsoDateToLocalDateOrNull(publishedAt) | ||
|
|
||
| return HighlightData.Article( | ||
| ArticleHighlightData( | ||
| title = title, | ||
| imageUrl = image ?: return null, | ||
| articleUrl = url, | ||
| date = date, | ||
| dateString = date?.format(outputFormatter) ?: "-/-/-", | ||
| sport = Sport.fromDisplayName(sportName) | ||
| ) | ||
| ) | ||
| } |
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/cornellappdev/score/model/HighlightsRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package com.cornellappdev.score.model | ||
|
|
||
| import android.util.Log | ||
| import com.apollographql.apollo.ApolloClient | ||
| import com.example.score.HighlightsQuery | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.CoroutineStart | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.TimeoutCancellationException | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.withTimeout | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import kotlin.coroutines.cancellation.CancellationException | ||
| import kotlin.time.Duration.Companion.milliseconds | ||
|
|
||
| private const val TIMEOUT_TIME_MILLIS = 5000L | ||
|
|
||
| @Singleton | ||
| class HighlightsRepository @Inject constructor( | ||
| private val apolloClient: ApolloClient, | ||
| private val appScope: CoroutineScope, | ||
| ) { | ||
| private val _highlightsFlow = | ||
| MutableStateFlow<ApiResponse<List<HighlightData>>>(ApiResponse.Loading) | ||
| val highlightsFlow = _highlightsFlow.asStateFlow() | ||
|
|
||
| //track the in-flight job so we can cancel it before starting a new one | ||
| private var currentFetchJob: Job? = null | ||
|
|
||
|
|
||
| /** | ||
| * Asynchronously fetches the list of highlights from the API. Once finished, will send down | ||
| * `upcomingGamesFlow` to be observed. | ||
| */ | ||
| fun fetchHighlights(): Job { | ||
| currentFetchJob?.cancel() | ||
| val fetchJob = appScope.launch(start = CoroutineStart.LAZY) { | ||
|
|
||
| _highlightsFlow.value = ApiResponse.Loading | ||
|
|
||
| try { | ||
| val result = | ||
| withTimeout(TIMEOUT_TIME_MILLIS.milliseconds) { | ||
| apolloClient.query(HighlightsQuery()).execute().toResult() | ||
| } | ||
|
|
||
| if (result.isSuccess) { | ||
| val highlights = result.getOrNull() | ||
| val highlightsList = | ||
| highlights?.articles.orEmpty().mapNotNull { it?.toHighlightData() } + | ||
| highlights?.youtubeVideos.orEmpty().mapNotNull { it?.toHighlightData() } | ||
|
|
||
| _highlightsFlow.value = ApiResponse.Success(highlightsList) | ||
| } else { | ||
| _highlightsFlow.value = ApiResponse.Error | ||
| } | ||
|
|
||
| } catch (e: TimeoutCancellationException) { | ||
| Log.e("HighlightsRepository", "Highlights request timed out", e) | ||
| _highlightsFlow.value = ApiResponse.Error | ||
| } catch (e: CancellationException) { | ||
| throw e | ||
| } catch (e: Exception) { | ||
| Log.e("HighlightsRepository", "Error fetching highlights", e) | ||
| _highlightsFlow.value = ApiResponse.Error | ||
| } | ||
| } | ||
| currentFetchJob = fetchJob | ||
| fetchJob.start() | ||
| return fetchJob | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.