Skip to content

Commit

Permalink
Apply spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Jan 21, 2025
1 parent 0b75b7a commit 4ef54fb
Show file tree
Hide file tree
Showing 32 changed files with 175 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import com.skydoves.sandwichdemo.network.DisneyService
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

class MainRepository(
private val disneyService: DisneyService,
) {
class MainRepository(private val disneyService: DisneyService) {

fun fetchPostersFlow(): Flow<ApiResponse<List<Poster>>> = flow {
val response = disneyService.fetchDisneyPosters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,5 @@ class PosterAdapter : RecyclerView.Adapter<PosterAdapter.PosterViewHolder>() {

override fun getItemCount(): Int = items.size

class PosterViewHolder(
val binding: ItemPosterBinding,
) : RecyclerView.ViewHolder(binding.root)
class PosterViewHolder(val binding: ItemPosterBinding) : RecyclerView.ViewHolder(binding.root)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.skydoves.sandwichdemo.model.ErrorMessage
*/
object ErrorEnvelopeMapper : ApiErrorModelMapper<ErrorMessage> {

override fun map(apiErrorResponse: ApiResponse.Failure.Error): ErrorMessage {
return ErrorMessage(apiErrorResponse.statusCode.code, apiErrorResponse.message())
}
override fun map(apiErrorResponse: ApiResponse.Failure.Error): ErrorMessage =
ErrorMessage(apiErrorResponse.statusCode.code, apiErrorResponse.message())
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.skydoves.sandwichdemo.model.Poster
*/
object SuccessPosterMapper : ApiSuccessModelMapper<List<Poster>, Poster?> {

override fun map(apiSuccessResponse: ApiResponse.Success<List<Poster>>): Poster? {
return apiSuccessResponse.data.firstOrNull()
}
override fun map(apiSuccessResponse: ApiResponse.Success<List<Poster>>): Poster? =
apiSuccessResponse.data.firstOrNull()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,4 @@ import kotlinx.serialization.Serializable
* @param message Http error response body message.
*/
@Serializable
data class ErrorMessage(
val code: Int,
val message: String,
)
data class ErrorMessage(val code: Int, val message: String)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@ package com.skydoves.sandwichdemo.model
import kotlinx.serialization.Serializable

@Serializable
data class Pokemon(
val name: String,
val url: String,
)
data class Pokemon(val name: String, val url: String)
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ import timber.log.Timber
/**
* A global response operator for handling [ApiResponse]s regardless of its type.
*/
class GlobalResponseOperator<T>(
private val application: Application,
) : ApiResponseSuspendOperator<T>() {
class GlobalResponseOperator<T>(private val application: Application) :
ApiResponseSuspendOperator<T>() {

// handle the case when the API request gets a success response.
override suspend fun onSuccess(apiResponse: ApiResponse.Success<T>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import com.skydoves.sandwichdemo.model.Poster
import com.skydoves.sandwichdemo.network.DisneyService
import timber.log.Timber

class MainOperatorViewModel(
disneyService: DisneyService,
) : AndroidViewModel(SandwichDemoApp.sandwichApp) {
class MainOperatorViewModel(disneyService: DisneyService) :
AndroidViewModel(SandwichDemoApp.sandwichApp) {

val posterListLiveData: LiveData<List<Poster>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import kotlin.jvm.JvmSynthetic
*
* @return A [StatusCode] from the network callback response.
*/
public fun HttpResponse.getStatusCode(): StatusCode {
return StatusCode.entries.find { it.code == status.value }
?: StatusCode.Unknown
public fun HttpResponse.getStatusCode(): StatusCode = StatusCode.entries.find {
it.code == status.value
}
?: StatusCode.Unknown

@PublishedApi
internal val <T> ApiResponse.Success<T>.tagResponse: HttpResponse
Expand All @@ -59,9 +59,8 @@ internal val ApiResponse.Failure.Error.payloadResponse: HttpResponse
)

/** The de-serialized response body of a successful data. */
public suspend inline fun <reified T> ApiResponse.Success<T>.body(): T {
return tagResponse.body() ?: throw NoContentException(tagResponse.getStatusCode().code)
}
public suspend inline fun <reified T> ApiResponse.Success<T>.body(): T =
tagResponse.body() ?: throw NoContentException(tagResponse.getStatusCode().code)

/** [StatusCode] is Hypertext Transfer Protocol (HTTP) response status codes. */
public val <T> ApiResponse.Success<T>.statusCode: StatusCode
Expand All @@ -77,17 +76,14 @@ public val <T> ApiResponse.Success<T>.httpResponse: HttpResponse

/**
* The [ByteReadChannel] can be consumed only once. */
public suspend fun ApiResponse.Failure.Error.bodyChannel(): ByteReadChannel {
return payloadResponse.bodyAsChannel()
}
public suspend fun ApiResponse.Failure.Error.bodyChannel(): ByteReadChannel =
payloadResponse.bodyAsChannel()

/**
* The [ByteReadChannel] can be consumed only once. */
public suspend fun ApiResponse.Failure.Error.bodyString(
fallbackCharset: Charset = Charsets.UTF_8,
): String {
return payloadResponse.bodyAsText(fallbackCharset = fallbackCharset)
}
): String = payloadResponse.bodyAsText(fallbackCharset = fallbackCharset)

/** [StatusCode] is Hypertext Transfer Protocol (HTTP) response status codes. */
public val ApiResponse.Failure.Error.statusCode: StatusCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ import io.ktor.http.Url
*/
public suspend inline fun <reified T> HttpClient.requestApiResponse(
builder: HttpRequestBuilder = HttpRequestBuilder(),
): ApiResponse<T> {
return apiResponseOf { HttpStatement(builder, this).execute() }
}
): ApiResponse<T> = apiResponseOf { HttpStatement(builder, this).execute() }

/**
* Executes an [HttpClient]'s request with the parameters specified in [block].
Expand All @@ -42,9 +40,7 @@ public suspend inline fun <reified T> HttpClient.requestApiResponse(
*/
public suspend inline fun <reified T> HttpClient.requestApiResponse(
crossinline block: HttpRequestBuilder.() -> Unit,
): ApiResponse<T> {
return apiResponseOf { request(HttpRequestBuilder().apply(block)) }
}
): ApiResponse<T> = apiResponseOf { request(HttpRequestBuilder().apply(block)) }

/**
* Executes an [HttpClient]'s request with the [urlString] and the parameters configured in [block].
Expand All @@ -54,12 +50,10 @@ public suspend inline fun <reified T> HttpClient.requestApiResponse(
public suspend inline fun <reified T> HttpClient.requestApiResponse(
urlString: String,
crossinline block: HttpRequestBuilder.() -> Unit = {},
): ApiResponse<T> {
return apiResponseOf {
request {
url(urlString)
block()
}
): ApiResponse<T> = apiResponseOf {
request {
url(urlString)
block()
}
}

Expand All @@ -71,12 +65,10 @@ public suspend inline fun <reified T> HttpClient.requestApiResponse(
public suspend inline fun <reified T> HttpClient.requestApiResponse(
url: Url,
crossinline block: HttpRequestBuilder.() -> Unit = {},
): ApiResponse<T> {
return apiResponseOf {
request {
url(url)
block()
}
): ApiResponse<T> = apiResponseOf {
request {
url(url)
block()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import de.jensklingenberg.ktorfit.converter.KtorfitResult
import de.jensklingenberg.ktorfit.converter.TypeData
import io.ktor.client.call.body
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import kotlin.jvm.JvmStatic

/**
Expand Down Expand Up @@ -75,8 +74,6 @@ public class ApiResponseConverterFactory internal constructor() : Converter.Fact

public companion object {
@JvmStatic
public fun create(): ApiResponseConverterFactory {
return ApiResponseConverterFactory()
}
public fun create(): ApiResponseConverterFactory = ApiResponseConverterFactory()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ public class ResponseDataSource<T> : DataSource<T> {
// runnable for retry response data from disk thread.
private val retryRunnable = Runnable {
if (retryCount > 0) {
synchronized(retryCount--) {
enqueue()
}
retryCount--
enqueue()
}
}

Expand Down Expand Up @@ -257,14 +256,12 @@ public class ResponseDataSource<T> : DataSource<T> {
* this live data can be observable from the network requests.
*/
@Suppress("UNCHECKED_CAST")
public fun asLiveData(): LiveData<T> {
return MutableLiveData<T>().apply {
liveData = this
if (data != empty) {
val data = data as ApiResponse<T>
if (data is ApiResponse.Success<T>) {
postValue((data.tag as Response<T>).body())
}
public fun asLiveData(): LiveData<T> = MutableLiveData<T>().apply {
liveData = this
if (data != empty) {
val data = data as ApiResponse<T>
if (data is ApiResponse.Success<T>) {
postValue((data.tag as Response<T>).body())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,13 @@ public inline fun <T> Call<T>.suspendCombineDataSource(
@JvmSynthetic
internal inline fun <T> getCallbackFromOnResult(
crossinline onResult: (response: ApiResponse<T>) -> Unit,
): Callback<T> {
return object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
onResult(ApiResponse.responseOf { response })
}
): Callback<T> = object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
onResult(ApiResponse.responseOf { response })
}

override fun onFailure(call: Call<T>, throwable: Throwable) {
onResult(ApiResponse.exception(throwable))
}
override fun onFailure(call: Call<T>, throwable: Throwable) {
onResult(ApiResponse.exception(throwable))
}
}

Expand All @@ -102,18 +100,16 @@ internal inline fun <T> getCallbackFromOnResult(
internal inline fun <T> getCallbackFromOnResultOnCoroutinesScope(
coroutineScope: CoroutineScope,
crossinline onResult: suspend (response: ApiResponse<T>) -> Unit,
): Callback<T> {
return object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
coroutineScope.launch {
onResult(ApiResponse.responseOf { response })
}
): Callback<T> = object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
coroutineScope.launch {
onResult(ApiResponse.responseOf { response })
}
}

override fun onFailure(call: Call<T>, throwable: Throwable) {
coroutineScope.launch {
onResult(ApiResponse.exception(throwable))
}
override fun onFailure(call: Call<T>, throwable: Throwable) {
coroutineScope.launch {
onResult(ApiResponse.exception(throwable))
}
}
}
Expand All @@ -132,20 +128,18 @@ internal inline fun <T> getCallbackFromOnResultOnCoroutinesScope(
internal inline fun <T> getCallbackFromOnResultWithContext(
context: CoroutineContext = EmptyCoroutineContext,
crossinline onResult: suspend (response: ApiResponse<T>) -> Unit,
): Callback<T> {
return object : Callback<T> {
val supervisorJob = SupervisorJob(context[Job])
val scope = CoroutineScope(context + supervisorJob)
override fun onResponse(call: Call<T>, response: Response<T>) {
scope.launch {
onResult(ApiResponse.responseOf { response })
}
): Callback<T> = object : Callback<T> {
val supervisorJob = SupervisorJob(context[Job])
val scope = CoroutineScope(context + supervisorJob)
override fun onResponse(call: Call<T>, response: Response<T>) {
scope.launch {
onResult(ApiResponse.responseOf { response })
}
}

override fun onFailure(call: Call<T>, throwable: Throwable) {
scope.launch {
onResult(ApiResponse.exception(throwable))
}
override fun onFailure(call: Call<T>, throwable: Throwable) {
scope.launch {
onResult(ApiResponse.exception(throwable))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import retrofit2.Response
* CallDelegate is a delegate [Call] proxy for handling and transforming one to another generic types
* between the two different types of [Call] requests.
*/
internal abstract class CallDelegate<TIn, TOut>(
protected val proxy: Call<TIn>,
) : Call<TOut> {
internal abstract class CallDelegate<TIn, TOut>(protected val proxy: Call<TIn>) : Call<TOut> {
final override fun enqueue(callback: Callback<TOut>) = enqueueImpl(callback)
final override fun execute(): Response<TOut> = executeImpl()
final override fun clone(): Call<TOut> = cloneImpl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@ import java.lang.reflect.Type
*
* request API network call asynchronously and returns [DataSource].
*/
internal class DataSourceCallAdapter constructor(
private val responseType: Type,
) : CallAdapter<Type, Call<DataSource<Type>>> {
internal class DataSourceCallAdapter constructor(private val responseType: Type) :
CallAdapter<Type, Call<DataSource<Type>>> {

override fun responseType(): Type {
return responseType
}
override fun responseType(): Type = responseType

override fun adapt(call: Call<Type>): Call<DataSource<Type>> {
return DataSourceCallDelegate(call)
}
override fun adapt(call: Call<Type>): Call<DataSource<Type>> = DataSourceCallDelegate(call)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ import java.lang.reflect.Type
*
* request API network call asynchronously and returns [DataSource].
*/
internal class DataSourceRawCallAdapter<R>(
private val responseType: Type,
) : CallAdapter<R, DataSource<R>> {
internal class DataSourceRawCallAdapter<R>(private val responseType: Type) :
CallAdapter<R, DataSource<R>> {

override fun responseType(): Type {
return responseType
}
override fun responseType(): Type = responseType

override fun adapt(call: Call<R>): DataSource<R> {
val responseDataSource: ResponseDataSource<R> = ResponseDataSource()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ internal abstract class ApiAbstract<T> {
mockWebServer.enqueue(mockResponse.setBody(source.readString(StandardCharsets.UTF_8)))
}

fun createService(clazz: Class<T>): T {
return Retrofit.Builder()
.baseUrl(mockWebServer.url("/"))
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(ApiResponseCallAdapterFactory.create())
.build()
.create(clazz)
}
fun createService(clazz: Class<T>): T = Retrofit.Builder()
.baseUrl(mockWebServer.url("/"))
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(ApiResponseCallAdapterFactory.create())
.build()
.create(clazz)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@
package com.skydoves.sandwich.retrofit.serialization

@kotlinx.serialization.Serializable
internal data class ErrorMessage(
val code: Int,
val message: String,
)
internal data class ErrorMessage(val code: Int, val message: String)
Loading

0 comments on commit 4ef54fb

Please sign in to comment.