Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error in the network base #23

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/main/java/com/modarb/android/network/ApiResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ package com.modarb.android.network

sealed class ApiResult<out T> {
data class Success<out T>(val data: T) : ApiResult<T>()
data class Error<out T>(val data: T) : ApiResult<T>()

data class Failure(val exception: Throwable) : ApiResult<Nothing>()
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/modarb/android/network/NetworkHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.modarb.android.network

import android.content.Context
import android.widget.Toast
import com.modarb.android.R
import com.modarb.android.network.models.BaseResponse

class NetworkHelper {

companion object {
fun showErrorMessage(context: Context, errorResponse: BaseResponse) {
val defaultErrorMessage = context.getString(R.string.an_error_occurred)
val message =
errorResponse.errors?.firstOrNull() ?: errorResponse.error ?: defaultErrorMessage
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.lifecycle.lifecycleScope
import com.modarb.android.R
import com.modarb.android.databinding.FragmentHomeBinding
import com.modarb.android.network.ApiResult
import com.modarb.android.network.NetworkHelper
import com.modarb.android.ui.home.HomeActivity
import com.modarb.android.ui.home.helpers.WorkoutData
import com.modarb.android.ui.home.ui.home.domain.models.HomePageResponse
Expand Down Expand Up @@ -73,23 +74,29 @@ class HomeFragment : Fragment() {
homeViewModel.homeResponse.collect {
when (it) {
is ApiResult.Success<*> -> handleHomeSuccess(it.data as HomePageResponse)
is ApiResult.Failure -> handleHomeError(it.exception)
is ApiResult.Error -> handleHomeError(it.data as HomePageResponse)
is ApiResult.Failure -> handleHomeFail(it.exception)
else -> {}
}
}
}

}

private fun handleHomeFail(exception: Throwable) {
Toast.makeText(requireContext(), exception.message, Toast.LENGTH_SHORT).show()
binding.progressView.progressOverlay.visibility = View.GONE
}

private fun handleHomeSuccess(res: HomePageResponse) {
WorkoutData.workoutId = res.data.myWorkout.id
setData(res)
getPlanData()
binding.progressView.progressOverlay.visibility = View.GONE
}

private fun handleHomeError(exception: Throwable) {
Toast.makeText(requireContext(), exception.message, Toast.LENGTH_SHORT).show()
private fun handleHomeError(errorResponse: HomePageResponse) {
NetworkHelper.showErrorMessage(requireContext(), errorResponse)
binding.progressView.progressOverlay.visibility = View.GONE
}

Expand All @@ -99,7 +106,8 @@ class HomeFragment : Fragment() {
planViewModel.planResponse.collect {
when (it) {
is ApiResult.Success<*> -> handlePlanSuccess(it.data as PlanPageResponse)
is ApiResult.Failure -> handlePlanError(it.exception)
is ApiResult.Error -> handlePlanError(it.data)
is ApiResult.Failure -> handlePlanFail(it.exception)
else -> {}
}
}
Expand All @@ -110,14 +118,12 @@ class HomeFragment : Fragment() {

}

private fun handlePlanError(exception: Throwable) {

// TODO handle plan error
// val defaultErrorMessage = getString(R.string.an_error_occurred)
// val message = errResponse.errors?.firstOrNull() ?: errResponse.error ?: defaultErrorMessage
// Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()

private fun handlePlanFail(exception: Throwable) {
Toast.makeText(requireContext(), exception.message, Toast.LENGTH_SHORT).show()
}

private fun handlePlanError(errorResponse: PlanPageResponse) {
NetworkHelper.showErrorMessage(requireContext(), errorResponse)
}

private fun handlePlanSuccess(res: PlanPageResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HomeRepositoryImpl(private val apiService: ApiService) : HomeRepository {
ApiResult.Success(it)
} ?: ApiResult.Failure(Throwable("Response body is null"))
} else {
ApiResult.Failure(Throwable(response.errorBody()?.string() ?: "Unknown error"))
ApiResult.Error(response.body() as HomePageResponse)
}
} catch (E: Exception) {
ApiResult.Failure(E)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PlanRepositoryImp(private val apiService: ApiService) : MyPlanRepository {
ApiResult.Success(it)
} ?: ApiResult.Failure(Throwable("Response body is null"))
} else {
ApiResult.Failure(Throwable(response.errorBody()?.string() ?: "Unknown error"))
ApiResult.Error(response.body() as PlanPageResponse)
}
} catch (e: Exception) {
ApiResult.Failure(e)
Expand Down
Loading