Skip to content

Commit

Permalink
Change class ResponseResult to Response
Browse files Browse the repository at this point in the history
  • Loading branch information
Souleymane Sidibe committed Aug 13, 2021
1 parent 31baca8 commit 8dbd558
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.viewModelScope
import com.soulesidibe.domain.AddOrUpdateTodoUseCase
import com.soulesidibe.domain.GetTodoUseCase
import com.soulesidibe.domain.RemoveTodoUseCase
import com.soulesidibe.domain.ResponseResult
import com.soulesidibe.domain.Response
import com.soulesidibe.todoapp.model.TodoViewModel
import com.soulesidibe.todoapp.model.toEntity
import com.soulesidibe.todoapp.model.toTodoViewModel
Expand Down Expand Up @@ -44,8 +44,8 @@ class TodoDetailViewModel(

fun getTodoBy(id: String) = flow<ViewState<TodoViewModel>> {
when (val result = todoUseCase.execute(id)) {
is ResponseResult.Error -> emit(ViewState.failed(result.throwable))
is ResponseResult.Success -> emit(ViewState.success(result.data.toTodoViewModel()))
is Response.Error -> emit(ViewState.failed(result.throwable))
is Response.Success -> emit(ViewState.success(result.data.toTodoViewModel()))
}
}.flowOn(dispatcher.io())
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TodoListViewModel(
}
}.flowOn(dispatcher.io())
.catch {
emit(ResponseResult.failure(Exception()))
emit(Response.failure(Exception()))
}
.collect {
_todosState.emit(it.toViewState())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.soulesidibe.todoapp.viewmodel

import com.soulesidibe.domain.ResponseResult
import com.soulesidibe.domain.Response

fun <T> ResponseResult<T>.toViewState(): ViewState<T> {
fun <T> Response<T>.toViewState(): ViewState<T> {
return this.let {
when (it) {
is ResponseResult.Error -> {
is Response.Error -> {
ViewState.failed(it.throwable)
}
is ResponseResult.Success -> {
is Response.Success -> {
ViewState.success(it.data)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.soulesidibe.data.datasource.TodoLocalDataSource
import com.soulesidibe.data.model.TodoData
import com.soulesidibe.data.model.mapper.toDb
import com.soulesidibe.data.model.mapper.toEntity
import com.soulesidibe.domain.ResponseResult
import com.soulesidibe.domain.Response
import com.soulesidibe.domain.entity.TodoEntity
import com.soulesidibe.domain.exception.CannotAddOrUpdateException
import com.soulesidibe.domain.exception.NoTodosFoundException
Expand All @@ -19,12 +19,12 @@ internal class TodoRepositoryImpl(

private val transform: (TodoData) -> TodoEntity = { todoDb -> todoDb.toEntity() }

override suspend fun get(): Flow<ResponseResult<List<TodoEntity>>> {
override suspend fun get(): Flow<Response<List<TodoEntity>>> {
return dataSource.getAll().map { list ->
if (list.isEmpty()) {
ResponseResult.failure(NoTodosFoundException)
Response.failure(NoTodosFoundException)
} else {
ResponseResult.success(list.map(transform))
Response.success(list.map(transform))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import com.soulesidibe.domain.repository.TodoRepository

class AddOrUpdateTodoUseCase(private val repository: TodoRepository) :
UseCase<TodoEntity, Boolean> {
override suspend fun execute(param: TodoEntity): ResponseResult<Boolean> {
override suspend fun execute(param: TodoEntity): Response<Boolean> {
try {
repository.addOrUpdate(param)
return ResponseResult.success(true)
return Response.success(true)
} catch (e: Throwable) {
return ResponseResult.failure(e)
return Response.failure(e)
}
}
}
7 changes: 3 additions & 4 deletions domain/src/main/java/com/soulesidibe/domain/GetTodoUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package com.soulesidibe.domain
import com.soulesidibe.domain.entity.TodoEntity
import com.soulesidibe.domain.exception.NoTodoFoundException
import com.soulesidibe.domain.repository.TodoRepository
import java.lang.Exception

class GetTodoUseCase(private val repository: TodoRepository) : UseCase<String, TodoEntity> {
override suspend fun execute(param: String): ResponseResult<TodoEntity> {
val value = repository.byId(param) ?: return ResponseResult.failure(NoTodoFoundException)
return ResponseResult.success(value)
override suspend fun execute(param: String): Response<TodoEntity> {
val value = repository.byId(param) ?: return Response.failure(NoTodoFoundException)
return Response.success(value)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.soulesidibe.domain

import com.soulesidibe.domain.entity.TodoEntity
import com.soulesidibe.domain.exception.NoTodosFoundException
import com.soulesidibe.domain.repository.TodoRepository
import kotlinx.coroutines.flow.Flow

class GetTodosUseCase(private val repository: TodoRepository) :
UseCaseFlow<None, Flow<ResponseResult<List<TodoEntity>>>> {
override suspend fun execute(param: None): Flow<ResponseResult<List<TodoEntity>>> {
UseCaseFlow<None, Flow<Response<List<TodoEntity>>>> {
override suspend fun execute(param: None): Flow<Response<List<TodoEntity>>> {
return repository.get()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.soulesidibe.domain.exception.NoTodoFoundException
import com.soulesidibe.domain.repository.TodoRepository

class RemoveTodoUseCase(private val repository: TodoRepository) : UseCase<String, Boolean> {
override suspend fun execute(param: String): ResponseResult<Boolean> {
val todo = repository.byId(param) ?: return ResponseResult.failure(NoTodoFoundException)
return ResponseResult.success(repository.remove(todo))
override suspend fun execute(param: String): Response<Boolean> {
val todo = repository.byId(param) ?: return Response.failure(NoTodoFoundException)
return Response.success(repository.remove(todo))
}
}
19 changes: 19 additions & 0 deletions domain/src/main/java/com/soulesidibe/domain/Response.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.soulesidibe.domain

sealed class Response<T> {
data class Success<T>(val data: T) : Response<T>()
data class Error<T>(val throwable: Throwable) : Response<T>()

companion object {
fun <T> success(data: T) = Success(data)
fun <T> failure(throwable: Throwable) = Error<T>(throwable)
}
}


inline fun <R, T> Response<T>.map(transform: (value: T) -> R): Response<R> {
return when(this) {
is Response.Error -> Response.failure(this.throwable)
is Response.Success -> Response.success(transform(this.data))
}
}
19 changes: 0 additions & 19 deletions domain/src/main/java/com/soulesidibe/domain/ResponseResult.kt

This file was deleted.

2 changes: 1 addition & 1 deletion domain/src/main/java/com/soulesidibe/domain/UseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.soulesidibe.domain

interface UseCase<in Param, Output> {

suspend fun execute(param: Param): ResponseResult<Output>
suspend fun execute(param: Param): Response<Output>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.soulesidibe.domain.repository

import com.soulesidibe.domain.ResponseResult
import com.soulesidibe.domain.Response
import com.soulesidibe.domain.entity.TodoEntity
import com.soulesidibe.domain.exception.CannotAddOrUpdateException
import com.soulesidibe.domain.exception.NoTodosFoundException
Expand All @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.Flow
interface TodoRepository {

@Throws(NoTodosFoundException::class)
suspend fun get(): Flow<ResponseResult<List<TodoEntity>>>
suspend fun get(): Flow<Response<List<TodoEntity>>>

suspend fun byId(id: String): TodoEntity?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AddOrUpdateTodoUseCaseTest {

val usecase = AddOrUpdateTodoUseCase(repository)
val responseResult = usecase.execute(todo)
assertEquals(ResponseResult.success(true), responseResult)
assertEquals(Response.success(true), responseResult)
}

@Test
Expand All @@ -34,7 +34,7 @@ class AddOrUpdateTodoUseCaseTest {
val useCase = AddOrUpdateTodoUseCase(repository)
val responseResult = useCase.execute(todo)
assertEquals(
ResponseResult.Error<CannotAddOrUpdateException>(CannotAddOrUpdateException),
Response.Error<CannotAddOrUpdateException>(CannotAddOrUpdateException),
responseResult
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class GetTodosUseCaseTest {
@Test
fun `should get the list of todos`() = runBlockingTest {
val repository = mock(TodoRepository::class.java)
`when`(repository.get()).thenReturn(flowOf(ResponseResult.success(listOf())))
`when`(repository.get()).thenReturn(flowOf(Response.success(listOf())))

val useCase: GetTodosUseCase = GetTodosUseCase(repository)
val result = useCase.execute(None())

assertEquals(
ResponseResult.Success<List<TodoEntity>>(listOf()),
Response.Success<List<TodoEntity>>(listOf()),
result.firstOrNull()
)
}
Expand All @@ -33,13 +33,13 @@ class GetTodosUseCaseTest {
@Test
fun `should get a fail response`() = runBlockingTest {
val repository = mock(TodoRepository::class.java)
`when`(repository.get()).thenReturn(flowOf(ResponseResult.failure(NoTodosFoundException)))
`when`(repository.get()).thenReturn(flowOf(Response.failure(NoTodosFoundException)))

val useCase: GetTodosUseCase = GetTodosUseCase(repository)
val result = useCase.execute(None())

assertEquals(
ResponseResult.Error<NoTodosFoundException>(NoTodosFoundException),
Response.Error<NoTodosFoundException>(NoTodosFoundException),
result.firstOrNull()
)
}
Expand Down

0 comments on commit 8dbd558

Please sign in to comment.