Skip to content

Commit

Permalink
activity : P3C1-1
Browse files Browse the repository at this point in the history
  • Loading branch information
JustJerem committed Feb 19, 2024
1 parent 2764ea8 commit 9d24f0a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 17 deletions.
Expand Up @@ -4,10 +4,12 @@ import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.openclassrooms.icerush.databinding.ActivityMainBinding
import com.openclassrooms.icerush.domain.model.SnowReportModel
import com.openclassrooms.icerush.presentation.home.HomeViewModel
Expand Down Expand Up @@ -36,6 +38,11 @@ class MainActivity : AppCompatActivity(), WeatherAdapter.OnItemClickListener {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect {
updateCurrentWeather(it.forecast)
binding.progressBar.isVisible = it.isViewLoading
if (it.errorMessage?.isNotBlank() == true) {
Snackbar.make(binding.root, it.errorMessage, Snackbar.LENGTH_LONG)
.show()
}
}
}
}
Expand Down
@@ -0,0 +1,11 @@
package com.openclassrooms.icerush.data.repository

sealed class Result<out T> {
object Loading : Result<Nothing>()
data class Failure(
val message: String? = null,
) : Result<Nothing>()


data class Success<out R>(val value: R) : Result<R>()
}
Expand Up @@ -6,19 +6,24 @@ import com.openclassrooms.icerush.domain.model.SnowReportModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import com.openclassrooms.icerush.data.repository.Result


class SnowRepository(private val dataService: WeatherClient) {
private val API_KEY = "617aafb37c40e369ec5a9d14ca06b4c5"


fun fetchForecastData(lat: Double, lng: Double): Flow<List<SnowReportModel>> = flow {
val result = dataService.getWeatherByPosition(lat, lng, API_KEY)
val model = result.body()?.toDomainModel() ?: throw Exception("Invalid data")


emit(model)
}.catch { error ->
Log.e("WeatherRepository", error.message ?: "")
}
fun fetchForecastData(lat: Double, lng: Double): Flow<Result<List<SnowReportModel>>> =
flow {
emit(Result.Loading)
val result = dataService.getWeatherByPosition(
latitude = lat,
longitude = lng,
apiKey = API_KEY
)
val model = result.body()?.toDomainModel() ?: throw Exception("Invalid data")
emit(Result.Success(model))
}.catch { error ->
emit(Result.Failure(error.message))
}
}
Expand Up @@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
import javax.inject.Inject

import com.openclassrooms.icerush.data.repository.Result

@HiltViewModel
class HomeViewModel @Inject constructor(private val dataRepository: SnowRepository) :
Expand All @@ -31,17 +31,35 @@ class HomeViewModel @Inject constructor(private val dataRepository: SnowReposito
val latitude = 48.844304
val longitude = 2.374377
dataRepository.fetchForecastData(latitude, longitude).onEach { forecastUpdate ->
_uiState.update { currentState ->
currentState.copy(
forecast = forecastUpdate,
)
when (forecastUpdate) {
is Result.Failure -> _uiState.update { currentState ->
currentState.copy(
isViewLoading = false,
errorMessage = forecastUpdate.message
)
}

Result.Loading -> _uiState.update { currentState ->
currentState.copy(
isViewLoading = true,
errorMessage = null,
)
}

is Result.Success -> _uiState.update { currentState ->
currentState.copy(
forecast = forecastUpdate.value,
isViewLoading = false,
errorMessage = null,
)
}
}
}.launchIn(viewModelScope)

}
}

data class HomeUiState(
val forecast: List<SnowReportModel> = emptyList(),
)

val isViewLoading: Boolean = false,
val errorMessage: String? = null
)
6 changes: 6 additions & 0 deletions activity/app/src/main/res/layout/activity_main.xml
Expand Up @@ -5,6 +5,12 @@
android:layout_height="match_parent"
android:background="#ADD8E6">

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
Expand Down

0 comments on commit 9d24f0a

Please sign in to comment.