Skip to content

Commit

Permalink
Merge 991999f into 356f1eb
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamMc331 committed Apr 24, 2022
2 parents 356f1eb + 991999f commit 27f53bd
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 208 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.adammcneilly.pocketleague.shared.screens

import com.adammcneilly.pocketleague.shared.data.AppDependencies
import com.adammcneilly.pocketleague.shared.data.Repository

/**
* Creates an instance of a [DKMPViewModel] to be used within an Android application.
*/
fun DKMPViewModel.Factory.getAndroidInstance(): DKMPViewModel {
return DKMPViewModel(
dependencies = AppDependencies(),
dependencies = Repository(),
)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.adammcneilly.pocketleague.shared.data

import com.adammcneilly.pocketleague.shared.data.remote.octanegg.services.OctaneGGEventService
import com.adammcneilly.pocketleague.shared.data.remote.octanegg.services.OctaneGGGameService
import com.adammcneilly.pocketleague.shared.data.remote.octanegg.services.OctaneGGMatchService
import com.adammcneilly.pocketleague.shared.data.repositories.EventRepository
import com.adammcneilly.pocketleague.shared.data.repositories.GameRepository
import com.adammcneilly.pocketleague.shared.data.repositories.MatchRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
* The main entry point to various data layers inside the application.
*
* @property[useDefaultDispatcher] Most often true, meaning repo calls should be run on the
* Dispatchers.Default dispatcher. We can toggle this false for tests, though.
*/
class Repository(
private val useDefaultDispatcher: Boolean = true,
) {
internal val eventRepository: EventRepository by lazy {
OctaneGGEventService()
}

internal val matchRepository: MatchRepository by lazy {
OctaneGGMatchService()
}

internal val gameRepository: GameRepository by lazy {
OctaneGGGameService()
}

/**
* Runs the supplied [block] inside the scope for this repo, dependent
* oon the [useDefaultDispatcher] flag.
*/
suspend fun <T> withRepoContext(block: suspend () -> T): T {
return if (useDefaultDispatcher) {
withContext(Dispatchers.Default) {
block()
}
} else {
block()
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.adammcneilly.pocketleague.shared.screens

import com.adammcneilly.pocketleague.shared.data.AppDependencies
import com.adammcneilly.pocketleague.shared.data.Repository
import kotlinx.coroutines.flow.StateFlow

/**
* Root view model for the application that exposes a [stateFlow] of our application's state.
*/
class DKMPViewModel(
private val dependencies: AppDependencies
private val dependencies: Repository
) {

private val stateManager by lazy {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.adammcneilly.pocketleague.shared.screens

import com.adammcneilly.pocketleague.shared.data.AppDependencies
import com.adammcneilly.pocketleague.shared.data.Repository

/**
* This class manages all of the event handling for various screens, specifically
Expand All @@ -9,8 +9,8 @@ import com.adammcneilly.pocketleague.shared.data.AppDependencies
class Events(
val stateManager: StateManager,
) {
val dependencies: AppDependencies
get() = stateManager.dependencies
val repository: Repository
get() = stateManager.repository

/**
* This will run the supplied [block] inside the coroutine scope of the current
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.adammcneilly.pocketleague.shared.screens

import com.adammcneilly.pocketleague.shared.data.AppDependencies
import com.adammcneilly.pocketleague.shared.data.Repository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand All @@ -16,7 +16,7 @@ import kotlin.reflect.KClass
*/
@Suppress("TooManyFunctions")
class StateManager(
val dependencies: AppDependencies,
val repository: Repository,
) {
internal val mutableStateFlow = MutableStateFlow(AppState())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
package com.adammcneilly.pocketleague.shared.screens.feed

import com.adammcneilly.pocketleague.shared.data.DataState
import com.adammcneilly.pocketleague.shared.data.models.EventListRequest
import com.adammcneilly.pocketleague.shared.data.models.MatchListRequest
import com.adammcneilly.pocketleague.shared.displaymodels.toSummaryDisplayModel
import com.adammcneilly.pocketleague.shared.models.Event
import com.adammcneilly.pocketleague.shared.screens.Events
import kotlinx.coroutines.flow.collect
import kotlinx.datetime.Clock
import kotlinx.datetime.DateTimeUnit
import kotlinx.datetime.TimeZone
import kotlinx.datetime.atStartOfDayIn
import kotlinx.datetime.minus
import kotlinx.datetime.toLocalDateTime

const val NUM_DAYS_RECENT_MATCHES = 3

/**
* Loads the information for the feed state.
*/
fun Events.loadFeed() = screenCoroutine {
val upcomingEventsFlow = dependencies.getUpcomingEventsUseCase.invoke()
val recentMatchesFlow = dependencies.getRecentMatchesUseCase.invoke(NUM_DAYS_RECENT_MATCHES)
val today = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())

upcomingEventsFlow.collect { useCaseResult ->
val upcomingEventsRequest = EventListRequest(
after = today,
)

repository.eventRepository.fetchEvents(
upcomingEventsRequest,
).collect { repoResult ->
stateManager.updateScreen(FeedViewState::class) {
val mappedResult = when (useCaseResult) {
val mappedResult = when (repoResult) {
is DataState.Loading -> {
DataState.Loading
}
is DataState.Success -> {
DataState.Success(
data = useCaseResult.data.map(Event::toSummaryDisplayModel)
data = repoResult.data.map(Event::toSummaryDisplayModel)
)
}
is DataState.Error -> {
DataState.Error(useCaseResult.error)
DataState.Error(repoResult.error)
}
}

Expand All @@ -37,10 +50,19 @@ fun Events.loadFeed() = screenCoroutine {
}
}

recentMatchesFlow.collect { useCaseResult ->
val recentMatchesRequest = MatchListRequest(
before = today,
after = today.date.minus(NUM_DAYS_RECENT_MATCHES, DateTimeUnit.DAY)
.atStartOfDayIn(TimeZone.currentSystemDefault())
.toLocalDateTime(TimeZone.currentSystemDefault()),
)

repository.matchRepository.fetchMatches(
request = recentMatchesRequest,
).collect { repoResult ->
stateManager.updateScreen(FeedViewState::class) {
it.copy(
recentMatchesState = useCaseResult,
recentMatchesState = repoResult,
)
}
}
Expand Down
Loading

0 comments on commit 27f53bd

Please sign in to comment.