generated from AdamMc331/AndroidAppTemplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Adding FeedViewModel. #562
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
5 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
root = true | ||
|
||
[*.{kt,kts}] | ||
max_line_length = 120 | ||
max_line_length = 140 | ||
indent_size = 4 | ||
insert_final_newline = true | ||
ij_kotlin_allow_trailing_comma = true | ||
ij_kotlin_allow_trailing_comma_on_call_site = true | ||
ktlint_function_naming_ignore_when_annotated_with = Composable | ||
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 1 | ||
ktlint_standard_property-naming = disabled | ||
ktlint_standard_max-line-length = disabled | ||
ktlint_standard_chain-method-continuation = disabled | ||
ktlint_standard_function-expression-body = disabled | ||
ktlint_standard_multiline-expression-wrapping = disabled | ||
ktlint_standard_string-template-indent = disabled |
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
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
30 changes: 30 additions & 0 deletions
30
...ed/app/src/commonMain/kotlin/com/adammcneilly/pocketleague/shared/app/feed/FeedUiState.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,30 @@ | ||
package com.adammcneilly.pocketleague.shared.app.feed | ||
|
||
import com.adammcneilly.pocketleague.core.displaymodels.EventGroupDisplayModel | ||
import com.adammcneilly.pocketleague.core.displaymodels.MatchDetailDisplayModel | ||
|
||
data class FeedUiState( | ||
val recentMatches: List<MatchDetailDisplayModel>, | ||
val ongoingEvents: List<EventGroupDisplayModel>, | ||
val upcomingEvents: List<EventGroupDisplayModel>, | ||
) { | ||
companion object { | ||
private const val PLACEHOLDER_LIST_COUNT = 3 | ||
|
||
/** | ||
* Returns a default instance of [FeedUiState] where all display models are set | ||
* to placeholders to display during a default loading state. | ||
*/ | ||
fun placeholderState(): FeedUiState { | ||
val recentMatches = List(PLACEHOLDER_LIST_COUNT) { | ||
MatchDetailDisplayModel.placeholder | ||
} | ||
|
||
return FeedUiState( | ||
recentMatches = recentMatches, | ||
ongoingEvents = EventGroupDisplayModel.placeholder, | ||
upcomingEvents = EventGroupDisplayModel.placeholder, | ||
) | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
.../app/src/commonMain/kotlin/com/adammcneilly/pocketleague/shared/app/feed/FeedViewModel.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,88 @@ | ||
package com.adammcneilly.pocketleague.shared.app.feed | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.adammcneilly.pocketleague.core.datetime.TimeProvider | ||
import com.adammcneilly.pocketleague.core.displaymodels.EventGroupDisplayModel | ||
import com.adammcneilly.pocketleague.core.displaymodels.toDetailDisplayModel | ||
import com.adammcneilly.pocketleague.core.displaymodels.toSummaryDisplayModel | ||
import com.adammcneilly.pocketleague.core.models.Event | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
|
||
class FeedViewModel( | ||
private val getPastWeeksMatchesUseCase: GetPastWeeksMatchesUseCase, | ||
private val getOngoingEventsUseCase: GetOngoingEventsUseCase, | ||
private val getUpcomingEventsUseCase: GetUpcomingEventsUseCase, | ||
private val timeProvider: TimeProvider, | ||
) : ViewModel() { | ||
private val mutableState = MutableStateFlow(FeedUiState.placeholderState()) | ||
val state = mutableState.asStateFlow() | ||
|
||
init { | ||
observePastWeeksMatches() | ||
observeOngoingEvents() | ||
observeUpcomingEvents() | ||
} | ||
|
||
private fun observePastWeeksMatches() { | ||
val matchFlow = getPastWeeksMatchesUseCase | ||
.invoke() | ||
.map { matchList -> | ||
matchList.map { match -> | ||
match.toDetailDisplayModel(timeProvider) | ||
} | ||
} | ||
|
||
viewModelScope.launch { | ||
matchFlow.collect { matchList -> | ||
mutableState.update { currentState -> | ||
currentState.copy( | ||
recentMatches = matchList, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun observeOngoingEvents() { | ||
val eventFlow = getOngoingEventsUseCase | ||
.invoke() | ||
.map { eventList -> | ||
eventList.map(Event::toSummaryDisplayModel) | ||
} | ||
.map(EventGroupDisplayModel.Companion::mapFromEventList) | ||
|
||
viewModelScope.launch { | ||
eventFlow.collect { eventList -> | ||
mutableState.update { currentState -> | ||
currentState.copy( | ||
ongoingEvents = eventList, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun observeUpcomingEvents() { | ||
val eventFlow = getUpcomingEventsUseCase | ||
.invoke() | ||
.map { eventList -> | ||
eventList.map(Event::toSummaryDisplayModel) | ||
} | ||
.map(EventGroupDisplayModel.Companion::mapFromEventList) | ||
|
||
viewModelScope.launch { | ||
eventFlow.collect { eventList -> | ||
mutableState.update { currentState -> | ||
currentState.copy( | ||
ongoingEvents = eventList, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ommonMain/kotlin/com/adammcneilly/pocketleague/shared/app/feed/GetOngoingEventsUseCase.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,26 @@ | ||
package com.adammcneilly.pocketleague.shared.app.feed | ||
|
||
import com.adammcneilly.pocketleague.core.datetime.TimeProvider | ||
import com.adammcneilly.pocketleague.core.models.Event | ||
import com.adammcneilly.pocketleague.data.event.api.EventListRequest | ||
import com.adammcneilly.pocketleague.data.event.api.EventRepository | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Return an observable type of events that are currently happening on today's date. | ||
*/ | ||
class GetOngoingEventsUseCase( | ||
private val eventRepository: EventRepository, | ||
private val timeProvider: TimeProvider, | ||
) { | ||
/** | ||
* @see [GetOngoingEventsUseCase] | ||
*/ | ||
fun invoke(): Flow<List<Event>> { | ||
val request = EventListRequest.OnDate( | ||
dateUtc = timeProvider.now(), | ||
) | ||
|
||
return eventRepository.stream(request) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...mmonMain/kotlin/com/adammcneilly/pocketleague/shared/app/feed/GetUpcomingEventsUseCase.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,26 @@ | ||
package com.adammcneilly.pocketleague.shared.app.feed | ||
|
||
import com.adammcneilly.pocketleague.core.datetime.TimeProvider | ||
import com.adammcneilly.pocketleague.core.models.Event | ||
import com.adammcneilly.pocketleague.data.event.api.EventListRequest | ||
import com.adammcneilly.pocketleague.data.event.api.EventRepository | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Return an observable type of events that are upcoming from today's date. | ||
*/ | ||
class GetUpcomingEventsUseCase( | ||
private val eventRepository: EventRepository, | ||
private val timeProvider: TimeProvider, | ||
) { | ||
/** | ||
* @see [GetUpcomingEventsUseCase] | ||
*/ | ||
fun invoke(): Flow<List<Event>> { | ||
val request = EventListRequest.AfterDate( | ||
dateUtc = timeProvider.now(), | ||
) | ||
|
||
return eventRepository.stream(request) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mapping can probably go into the use case.