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

feature: Extend Resource to have an Error Payload and separate the Er… #22

Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nagpal.shivam.vtucslab.core

sealed class ErrorType {
object NoActiveInternetConnection : ErrorType()
object SomeErrorOccurred : ErrorType()
enum class ErrorType {
NoActiveInternetConnection,
SomeErrorOccurred,
}
8 changes: 4 additions & 4 deletions app/src/main/java/com/nagpal/shivam/vtucslab/core/Resource.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.nagpal.shivam.vtucslab.core

sealed class Resource<T> {
class Loading<T>(val data: T? = null) : Resource<T>()
class Success<T>(val data: T?) : Resource<T>()
class Error<T>(val errorType: ErrorType? = null) : Resource<T>()
sealed class Resource<D, E> {
class Loading<D, E>(val data: D? = null) : Resource<D, E>()
class Success<D, E>(val data: D?) : Resource<D, E>()
class Error<D, E>(val error: E) : Resource<D, E>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.nagpal.shivam.vtucslab.core

data class UIMessage(val messageType: UIMessageType, val args: List<Any> = emptyList())
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.nagpal.shivam.vtucslab.core

enum class UIMessageType {
NoActiveInternetConnection,
SomeErrorOccurred,
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.nagpal.shivam.vtucslab.repositories

import com.nagpal.shivam.vtucslab.core.ErrorType
import com.nagpal.shivam.vtucslab.core.Resource
import com.nagpal.shivam.vtucslab.models.LaboratoryExperimentResponse
import com.nagpal.shivam.vtucslab.models.LaboratoryResponse
import kotlinx.coroutines.flow.Flow

interface VtuCsLabRepository {
fun fetchLaboratories(url: String): Flow<Resource<LaboratoryResponse>>
fun fetchLaboratories(url: String): Flow<Resource<LaboratoryResponse, ErrorType>>

fun fetchExperiments(url: String): Flow<Resource<LaboratoryExperimentResponse>>
fun fetchExperiments(url: String): Flow<Resource<LaboratoryExperimentResponse, ErrorType>>

fun fetchContent(url: String): Flow<Resource<String>>
fun fetchContent(url: String): Flow<Resource<String, ErrorType>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,32 @@ import kotlinx.coroutines.flow.flow
private val LOG_TAG: String = VtuCsLabRepositoryImpl::class.java.name

class VtuCsLabRepositoryImpl(
val application: Application,
private val application: Application,
private val vtuCsLabService: VtuCsLabService
) : VtuCsLabRepository {
override fun fetchLaboratories(url: String): Flow<Resource<LaboratoryResponse>> = flow {
fetch(url) {
vtuCsLabService.getLaboratoryResponse(it)
override fun fetchLaboratories(url: String): Flow<Resource<LaboratoryResponse, ErrorType>> =
flow {
fetch(url) {
vtuCsLabService.getLaboratoryResponse(it)
}
}
}

override fun fetchExperiments(url: String): Flow<Resource<LaboratoryExperimentResponse>> =
override fun fetchExperiments(url: String): Flow<Resource<LaboratoryExperimentResponse, ErrorType>> =
flow {
fetch(url) {
vtuCsLabService.getLaboratoryExperimentsResponse(it)
}
}

override fun fetchContent(url: String): Flow<Resource<String>> = flow {
override fun fetchContent(url: String): Flow<Resource<String, ErrorType>> = flow {
fetch(url) {
vtuCsLabService.fetchRawResponse(it)
}
}

private suspend fun <T : Any> FlowCollector<Resource<T>>.fetch(
private suspend fun <D : Any> FlowCollector<Resource<D, ErrorType>>.fetch(
url: String,
executable: suspend (String) -> ApiResult<T>
executable: suspend (String) -> ApiResult<D>
) {
emit(Resource.Loading())
if (!NetworkUtils.isNetworkConnected(application)) {
Expand All @@ -53,11 +54,11 @@ class VtuCsLabRepositoryImpl(
}
is ApiResult.ApiError -> {
StaticMethods.logNetworkResultError(LOG_TAG, url, apiResult.code, apiResult.message)
emit(Resource.Error())
emit(Resource.Error(ErrorType.SomeErrorOccurred))
}
is ApiResult.ApiException -> {
StaticMethods.logNetworkResultException(LOG_TAG, url, apiResult.throwable)
emit(Resource.Error())
emit(Resource.Error(ErrorType.SomeErrorOccurred))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.nagpal.shivam.vtucslab.screens

import com.nagpal.shivam.vtucslab.core.ErrorType
import com.nagpal.shivam.vtucslab.core.UIMessage

data class ContentState<T>(
val stage: String,
val data: T? = null,
val errorType: ErrorType? = null,
val errorMessage: UIMessage? = null,
val baseUrl: String? = null,
)
19 changes: 13 additions & 6 deletions app/src/main/java/com/nagpal/shivam/vtucslab/screens/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.Context
import com.nagpal.shivam.vtucslab.R
import com.nagpal.shivam.vtucslab.core.ErrorType
import com.nagpal.shivam.vtucslab.core.Resource
import com.nagpal.shivam.vtucslab.core.UIMessage
import com.nagpal.shivam.vtucslab.core.UIMessageType
import com.nagpal.shivam.vtucslab.utilities.Stages
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -16,7 +18,7 @@ object Utils {
uiStateFlow: MutableStateFlow<ContentState<T>>,
fetchJob: Job?,
viewModelScope: CoroutineScope,
fetchExecutable: (String) -> Flow<Resource<T>>,
fetchExecutable: (String) -> Flow<Resource<T, ErrorType>>,
getBaseUrl: (T) -> String?,
url: String
): Job? {
Expand All @@ -43,9 +45,13 @@ object Utils {
}
is Resource.Error -> {
uiStateFlow.update {
val uiMessage: UIMessage = when (resource.error) {
ErrorType.NoActiveInternetConnection -> UIMessage(UIMessageType.NoActiveInternetConnection)
ErrorType.SomeErrorOccurred -> UIMessage(UIMessageType.SomeErrorOccurred)
}
ContentState(
Stages.FAILED,
errorType = resource.errorType,
errorMessage = uiMessage,
)
}
}
Expand All @@ -61,10 +67,11 @@ object Utils {
uiStateFlow.update { initialState }
}

fun mapErrorTypeToString(context: Context, errorType: ErrorType?): String {
return when (errorType) {
ErrorType.NoActiveInternetConnection -> context.getString(R.string.no_internet_connection)
else -> context.getString(R.string.error_occurred)
fun UIMessage?.asString(context: Context): String {
return when (this?.messageType) {
UIMessageType.NoActiveInternetConnection -> context.getString(R.string.no_internet_connection)
UIMessageType.SomeErrorOccurred -> context.getString(R.string.error_occurred)
null -> context.getString(R.string.error_occurred)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import androidx.navigation.fragment.navArgs
import com.nagpal.shivam.vtucslab.R
import com.nagpal.shivam.vtucslab.databinding.FragmentDisplayBinding
import com.nagpal.shivam.vtucslab.screens.UiEvent
import com.nagpal.shivam.vtucslab.screens.Utils
import com.nagpal.shivam.vtucslab.screens.Utils.asString
import com.nagpal.shivam.vtucslab.utilities.Constants
import com.nagpal.shivam.vtucslab.utilities.Stages
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -59,8 +59,7 @@ class DisplayFragment : Fragment() {
}, 500)
}
Stages.FAILED -> {
val message: String =
Utils.mapErrorTypeToString(requireContext(), it.errorType)
val message: String = it.errorMessage.asString(requireContext())
showErrorMessage(message)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.nagpal.shivam.vtucslab.adapters.ContentAdapter
import com.nagpal.shivam.vtucslab.databinding.FragmentProgramBinding
import com.nagpal.shivam.vtucslab.models.ContentFile
import com.nagpal.shivam.vtucslab.screens.UiEvent
import com.nagpal.shivam.vtucslab.screens.Utils
import com.nagpal.shivam.vtucslab.screens.Utils.asString
import com.nagpal.shivam.vtucslab.utilities.Stages
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -57,8 +57,7 @@ class ProgramFragment : Fragment() {
}
}
Stages.FAILED -> {
val message: String =
Utils.mapErrorTypeToString(requireContext(), it.errorType)
val message: String = it.errorMessage.asString(requireContext())
showErrorMessage(message)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.nagpal.shivam.vtucslab.adapters.NavigationAdapter
import com.nagpal.shivam.vtucslab.databinding.FragmentRepositoryBinding
import com.nagpal.shivam.vtucslab.models.Laboratory
import com.nagpal.shivam.vtucslab.screens.UiEvent
import com.nagpal.shivam.vtucslab.screens.Utils
import com.nagpal.shivam.vtucslab.screens.Utils.asString
import com.nagpal.shivam.vtucslab.utilities.Constants
import com.nagpal.shivam.vtucslab.utilities.Stages
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -57,8 +57,7 @@ class RepositoryFragment : Fragment() {
}
}
Stages.FAILED -> {
val message: String =
Utils.mapErrorTypeToString(requireContext(), it.errorType)
val message: String = it.errorMessage.asString(requireContext())
showErrorMessage(message)
}
}
Expand Down