Skip to content

Commit

Permalink
fetch and display single proposal by ID
Browse files Browse the repository at this point in the history
This commit implements the functionality to fetch and display a single proposal by its ID in the ProposalViewModel.

-
 It introduces a new function `fetchProposalById` to retrieve a proposal based on its ID.
- The UI state is updated to handle loading, error, and success states.
- Upon successful retrieval, the proposal details are parsed and displayed.
- Logging is added for debugging and tracking the response and potential errors
.
  • Loading branch information
OkelloSam21 committed Aug 18, 2024
1 parent df604e4 commit e8624d3
Showing 1 changed file with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.samuelokello.kazihub.presentation.business

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.samuelokello.kazihub.domain.model.proposal.proposalResponse.ProposalResponseItem
import com.samuelokello.kazihub.domain.model.proposal.proposalResponse.ProposalData
import com.samuelokello.kazihub.domain.repositpry.ProposalRepository
import com.samuelokello.kazihub.utils.Resource
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -13,45 +14,61 @@ import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ProposalViewmodel @Inject constructor(private val proposalRepository: ProposalRepository): ViewModel() {
val _uiState = MutableStateFlow(ProposalUiState())
class ProposalViewModel @Inject constructor(private val repository: ProposalRepository) : ViewModel() {

private val _uiState = MutableStateFlow(ProposalUiState())
val uiState = _uiState.asStateFlow()

private fun fetchProposalsBYId(id: Int) {
fun fetchProposalById(id: Int) {
viewModelScope.launch {
when (val result = proposalRepository.getProposalById(id)) {
is Resource.Error -> {
_uiState.update {
it.copy(
isLoading = false,
error = result.message
)
}
}
is Resource.Loading -> {
_uiState.update {
it.copy(
isLoading = true,
error = null
)
}
}
is Resource.Success -> {
_uiState.update {
it.copy(
isLoading = false,
error = null,
proposals = result.data ?: emptyList()
)
_uiState.update { it.copy(isLoading = true) }
try {
when (val response = repository.getProposalById(id)) {
is Resource.Error -> {
_uiState.update { it.copy(isLoading = false, error = response.message) }
Log.e("ProposalViewModel", "Error: ${response.message}")
}
is Resource.Loading -> {
_uiState.update { it.copy(isLoading = true) }
}
is Resource.Success -> {
Log.d("ProposalViewModel", "Response: ${response.data}")
val proposalData = response.data?.data
if (proposalData != null) {
val proposal = ProposalData(
amount = proposalData.amount,
cv = proposalData.cv,
id = proposalData.id,
jobId = proposalData.jobId,
message = proposalData.message,
status = proposalData.status,
userId = proposalData.userId
)
_uiState.update { it.copy(
isLoading = false,
proposals = listOf(proposal)
) }
Log.d("ProposalViewModel", "Proposal parsed: $proposal")
} else {
_uiState.update { it.copy(
isLoading = false,
message = response.data?.message ?: "No proposal data found"
) }
Log.d("ProposalViewModel", "No proposal data, message: ${response.data?.message}")
}
}
}
} catch (e: Exception) {
_uiState.update { it.copy(isLoading = false, error = e.message) }
Log.e("ProposalViewModel", "Exception: ${e.message}", e)
}
}
}
}

data class ProposalUiState (
data class ProposalUiState(
val isLoading: Boolean = false,
val error: String? = null,
val proposals: List<ProposalResponseItem> = emptyList()
)
val proposals: List<ProposalData> = emptyList(),
val message: String? = null
)

0 comments on commit e8624d3

Please sign in to comment.