From 1d6e018bc4407fadc491d4a9d1184b31f0828bde Mon Sep 17 00:00:00 2001 From: Cutiful <113351087+Syaroful@users.noreply.github.com> Date: Thu, 10 Jul 2025 18:20:00 +0700 Subject: [PATCH] feat: add GrowthRecipeViewModel for fetching graphic data --- .../recipe/GrowthRecipeViewModel.kt | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 agrilinkvocpro/growth_recipe_feature/src/main/java/com/syaroful/agrilinkvocpro/growth_recipe_feature/presentation/recipe/GrowthRecipeViewModel.kt diff --git a/agrilinkvocpro/growth_recipe_feature/src/main/java/com/syaroful/agrilinkvocpro/growth_recipe_feature/presentation/recipe/GrowthRecipeViewModel.kt b/agrilinkvocpro/growth_recipe_feature/src/main/java/com/syaroful/agrilinkvocpro/growth_recipe_feature/presentation/recipe/GrowthRecipeViewModel.kt new file mode 100644 index 0000000..70c54ea --- /dev/null +++ b/agrilinkvocpro/growth_recipe_feature/src/main/java/com/syaroful/agrilinkvocpro/growth_recipe_feature/presentation/recipe/GrowthRecipeViewModel.kt @@ -0,0 +1,65 @@ +package com.syaroful.agrilinkvocpro.growth_recipe_feature.presentation.recipe + +import android.util.Log +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.syaroful.agrilinkvocpro.core.utils.ResultState +import com.syaroful.agrilinkvocpro.core.utils.extention.mapToUserFriendlyError +import com.syaroful.agrilinkvocpro.core.utils.extention.toFormattedString +import com.syaroful.agrilinkvocpro.data.UserPreferences +import com.syaroful.agrilinkvocpro.growth_recipe_feature.data.model.NpkGraphicDayResponse +import com.syaroful.agrilinkvocpro.growth_recipe_feature.data.repository.GraphicDataRepository +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch +import java.util.Date + + +private const val TAG = "GrowthRecipeViewModel" +class GrowthRecipeViewModel( + private val userPreferences: UserPreferences, + private val growthRecipeRepository: GraphicDataRepository +) : ViewModel() { + + private val _getGraphicState = + MutableStateFlow>(ResultState.Idle) + val getGraphicState: StateFlow> = _getGraphicState.asStateFlow() + + private val today = Date() + private val tenDaysAgo = Date(today.time - 9 * 24 * 60 * 60 * 1000) + + fun getGraphicData( + sensor: String + ) { + _getGraphicState.value = ResultState.Loading + viewModelScope.launch { + val token = userPreferences.tokenFlow.first() + val authHeader = "Bearer $token" + val dateNow = today.toFormattedString() + val dateTenDaysAgo = tenDaysAgo.toFormattedString() + + try { + val response = growthRecipeRepository.getNpkDataSensor( + authHeader = authHeader, + startDate = dateTenDaysAgo, + endDate = dateNow, + timeRange = "DAILY", + sensor = sensor, + ) + if (response.isSuccessful) { + response.body()?.let { body -> + _getGraphicState.value = ResultState.Success(body) + } ?: run { + _getGraphicState.value = ResultState.Error("Data tidak ditemukan") + } + } + } catch (e: Exception) { + val errorMessage = mapToUserFriendlyError(e) + _getGraphicState.value = ResultState.Error(errorMessage) + Log.d(TAG, "Failed to fetch data: ${e.message}") + } + } + } +} \ No newline at end of file