Skip to content

Commit

Permalink
Viewmodel, states and events for CompanyListings created
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonelZalegas committed Jul 20, 2024
1 parent 2f7b457 commit ad6e8a1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ root = true
ktlint_standard_final-newline = disabled
ktlint_standard_no-wildcard-imports = disabled
ktlint_standard_function-naming = disabled
ktlint_standard_package-name = disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.stockmarketcheck.mainFeature.presentation.company_listings

sealed class CompanyListingsEvent {
object Refresh : CompanyListingsEvent()

data class OnSearchQueryChange(val query: String) : CompanyListingsEvent()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.stockmarketcheck.mainFeature.presentation.company_listings

import com.example.stockmarketcheck.mainFeature.domain.model.CompanyListing

data class CompanyListingsState(
val companies: List<CompanyListing> = emptyList(),
val isLoading: Boolean = false,
val isRefreshing: Boolean = false,
val searchQuery: String = "",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.example.stockmarketcheck.mainFeature.presentation.company_listings

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.stockmarketcheck.mainFeature.domain.repository.StockRepository
import com.example.stockmarketcheck.util.Resource
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CompanyListingsViewModel
@Inject
constructor(
private val repository: StockRepository,
) : ViewModel() {
// el estado es del tipo CompanyListingsState
var state by mutableStateOf(CompanyListingsState())

private var searchJob: Job? = null

fun onEvent(event: CompanyListingsEvent) {
when (event) {
is CompanyListingsEvent.Refresh -> {
getCompanyListings(fetchFromRemote = true)
}
is CompanyListingsEvent.OnSearchQueryChange -> { // esto se triggea con cada letra que escribamos
state = state.copy(searchQuery = event.query)
searchJob?.cancel()
searchJob =
viewModelScope.launch {
delay(500L)
getCompanyListings()
}
} // https://www.notion.so/StockMarket-app-fd555472e30c45ef8586565dc35d7d42?pvs=4#03a95fa091684af5b925f58e961b2c91
}
}

private fun getCompanyListings(
query: String = state.searchQuery.lowercase(),
fetchFromRemote: Boolean = false,
) {
viewModelScope.launch {
repository
.getCompanyListings(fetchFromRemote, query)
.collect { result ->
when (result) {
is Resource.Success -> {
result.data?.let { listings ->
state =
state.copy(
companies = listings,
)
}
} // https://www.notion.so/StockMarket-app-fd555472e30c45ef8586565dc35d7d42?pvs=4#302e2f3f8f404d058bd758d15ed998b8
is Resource.Error -> Unit
is Resource.Loading -> {
state = state.copy(isLoading = result.isLoading) // aca hace referencia a la clase especifica de Loading
} // (ya q el campo isLoading es especifico de Loading y no es algo q hereda de Resources )
} // es como el field Breed de perro, aca le hace un Smart Cast por eso no se ve bien
}
}
}
}

0 comments on commit ad6e8a1

Please sign in to comment.