Fmar
07/20/2023, 11:18 AMFmar
07/20/2023, 11:18 AM@HiltViewModel
class FeedViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val feedRepository: FeedRepository,
private val profileRepository: ProfileRepository,
private val activeAccountStore: ActiveAccountStore,
) : ViewModel() {
private val feedDirective: String = savedStateHandle.feedDirective ?: "network;trending"
private val _state = MutableStateFlow(
UiState(
posts = feedRepository.feedByDirective(feedDirective = feedDirective)
.map { it.map { feed -> feed.asFeedPostUi() } }
.cachedIn(viewModelScope),
)
)
(...)
Fmar
07/20/2023, 11:19 AMFmar
07/20/2023, 11:20 AMfun loadProfile(profileId: String): Flow<Profile>
Fmar
07/20/2023, 11:20 AMFmar
07/20/2023, 11:20 AMFmar
07/20/2023, 12:05 PMVlad
07/20/2023, 12:42 PMval uiState: StateFlow<UiState> = repo.getData()
.map { data ->
UiState(data.someFileds)
}
.stateIn(
scope = viewModelScope,
started = WhileUiSubscribed,
initialValue = UiState()
)
There are plenty of the examples.
https://github.com/android/architecture-samples/blob/main/app/src/main/java/com/ex[…]chitecture/blueprints/todoapp/taskdetail/TaskDetailViewModel.kt