Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit a78bb7f

Browse files
committed
feat: add retrofit no connection view state contract
1 parent a5a3184 commit a78bb7f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

retrofit/src/main/java/com/crazylegend/retrofit/viewstate/ViewStateExtensions.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.crazylegend.retrofit.viewstate
22

33
import androidx.lifecycle.SavedStateHandle
44
import com.crazylegend.retrofit.apiresult.*
5+
import com.crazylegend.retrofit.throwables.isNoConnectionException
56
import okhttp3.ResponseBody
67

78
/**
@@ -52,7 +53,8 @@ fun SavedStateHandle.handleApiErrorFromSavedState(errorBody: ResponseBody?): Str
5253
return get<String>(errorStateKey)
5354
}
5455

55-
fun handleApiError(savedStateHandle: SavedStateHandle, errorBody: ResponseBody?): String? = savedStateHandle.handleApiErrorFromSavedState(errorBody)
56+
fun handleApiError(savedStateHandle: SavedStateHandle, errorBody: ResponseBody?): String? =
57+
savedStateHandle.handleApiErrorFromSavedState(errorBody)
5658

5759

5860
val <T> ViewStateContract<T>.showEmptyDataOnErrorsOrSuccess: Boolean
@@ -86,3 +88,11 @@ val <T> ViewStateContract<T>.showEmptyDataOnSuccess: Boolean
8688
return isDataNotLoaded && retrofitResult.isSuccess
8789
}
8890

91+
val <T> ViewStateContract<T>.shouldShowEmptyDataOnNoConnection: Boolean
92+
get() {
93+
val retrofitResult = data.value
94+
return isDataNotLoaded &&
95+
retrofitResult.isError &&
96+
retrofitResult.asError().throwable.isNoConnectionException
97+
}
98+

0 commit comments

Comments
 (0)