generated from AdamMc331/AndroidAppTemplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...commonMain/kotlin/com/adammcneilly/pocketleague/shared/app/data/match/MatchListRequest.kt
This file contains 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,30 @@ | ||
package com.adammcneilly.pocketleague.shared.app.data.match | ||
|
||
/** | ||
* Defines an enumeration of the ways we can request a list of matches | ||
* in the PocketLeague app. | ||
*/ | ||
sealed interface MatchListRequest { | ||
/** | ||
* Requests matches (should only be one) with the given [matchId]. | ||
*/ | ||
data class Id( | ||
val matchId: String, | ||
) : MatchListRequest | ||
|
||
/** | ||
* Request a list of matches within a given date range. | ||
*/ | ||
data class DateRange( | ||
val startDateUTC: String, | ||
val endDateUTC: String, | ||
) : MatchListRequest | ||
|
||
/** | ||
* Finds a list of matches with a given [eventId] and [stageId] combination. | ||
*/ | ||
data class EventStage( | ||
val eventId: String, | ||
val stageId: String, | ||
) : MatchListRequest | ||
} |
16 changes: 16 additions & 0 deletions
16
.../commonMain/kotlin/com/adammcneilly/pocketleague/shared/app/data/match/MatchRepository.kt
This file contains 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,16 @@ | ||
package com.adammcneilly.pocketleague.shared.app.data.match | ||
|
||
import com.adammcneilly.pocketleague.shared.app.core.models.Match | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Defines the data layer for any match related requests. | ||
*/ | ||
interface MatchRepository { | ||
/** | ||
* Stream a list of [Match] entities that apply to the supplied [request]. | ||
*/ | ||
fun getMatches( | ||
request: MatchListRequest, | ||
): Flow<List<Match>> | ||
} |
95 changes: 95 additions & 0 deletions
95
...n/com/adammcneilly/pocketleague/shared/app/data/octanegg/match/OctaneGGMatchRepository.kt
This file contains 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,95 @@ | ||
package com.adammcneilly.pocketleague.shared.app.data.octanegg.match | ||
|
||
import com.adammcneilly.pocketleague.shared.app.core.models.Match | ||
import com.adammcneilly.pocketleague.shared.app.data.match.MatchListRequest | ||
import com.adammcneilly.pocketleague.shared.app.data.match.MatchRepository | ||
import com.adammcneilly.pocketleague.shared.app.data.octanegg.dto.OctaneGGMatch | ||
import com.adammcneilly.pocketleague.shared.app.data.octanegg.dto.OctaneGGMatchListResponse | ||
import com.adammcneilly.pocketleague.shared.app.data.remote.BaseKtorClient | ||
import com.adammcneilly.pocketleague.shared.app.data.remote.RemoteParams | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
|
||
/** | ||
* An implementation of [MatchRepository] that requests | ||
* data from the supplied [apiClient] assuming it's an octane.gg client. | ||
*/ | ||
class OctaneGGMatchRepository( | ||
private val apiClient: BaseKtorClient, | ||
) : MatchRepository { | ||
override fun getMatches( | ||
request: MatchListRequest, | ||
): Flow<List<Match>> { | ||
return when (request) { | ||
is MatchListRequest.Id -> { | ||
flow { | ||
val matchResult = apiClient.getResponse<OctaneGGMatch>( | ||
endpoint = matchByIdEndpoint(request.matchId), | ||
params = getParamsForRequest(request), | ||
).map { octaneMatch -> | ||
listOf(octaneMatch.toMatch()) | ||
} | ||
|
||
emit(matchResult.getOrNull().orEmpty()) | ||
} | ||
} | ||
|
||
else -> { | ||
flow { | ||
val matchResult = apiClient.getResponse<OctaneGGMatchListResponse>( | ||
endpoint = MATCHES_ENDPOINT, | ||
params = getParamsForRequest(request), | ||
).map { octaneMatchList -> | ||
octaneMatchList.matches?.map(OctaneGGMatch::toMatch).orEmpty() | ||
} | ||
|
||
emit(matchResult.getOrNull().orEmpty()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun getParamsForRequest( | ||
request: MatchListRequest, | ||
): RemoteParams { | ||
val initialParams = when (request) { | ||
is MatchListRequest.DateRange -> { | ||
mapOf( | ||
AFTER_KEY to request.startDateUTC, | ||
BEFORE_KEY to request.endDateUTC, | ||
) | ||
} | ||
|
||
is MatchListRequest.EventStage -> { | ||
mapOf( | ||
EVENT_KEY to request.eventId, | ||
STAGE_KEY to request.stageId, | ||
) | ||
} | ||
|
||
is MatchListRequest.Id -> { | ||
emptyMap() | ||
} | ||
} | ||
|
||
return initialParams + mapOf( | ||
GROUP_KEY to "rlcs", | ||
) | ||
} | ||
|
||
companion object { | ||
private const val MATCHES_ENDPOINT = "/matches" | ||
|
||
private fun matchByIdEndpoint( | ||
id: String, | ||
): String { | ||
return "$MATCHES_ENDPOINT/$id" | ||
} | ||
|
||
private const val BEFORE_KEY = "before" | ||
private const val AFTER_KEY = "after" | ||
private const val GROUP_KEY = "group" | ||
private const val EVENT_KEY = "event" | ||
private const val STAGE_KEY = "stage" | ||
} | ||
} |