From d8e161b00eb81dc583cdd756ba4ecbb769d8de20 Mon Sep 17 00:00:00 2001 From: mkchtv Date: Sun, 5 Nov 2023 18:17:36 +0200 Subject: [PATCH] UPD: simplify use-cases --- .../auth/AuthProtectedViewModel.kt | 4 +- .../cleantemplate/auth/AuthViewModel.kt | 4 +- .../details/ElementDetailsViewModel.kt | 12 ++--- .../mkchtv/cleantemplate/di/UseCaseModule.kt | 49 ------------------- .../list/ElementsListViewModel.kt | 4 +- .../domain/usecase/CreateOrUpdateElement.kt | 8 +-- .../domain/usecase/DeleteElement.kt | 8 +-- .../domain/usecase/GetAllElements.kt | 10 +--- .../domain/usecase/GetAuthStateFlow.kt | 10 +--- .../domain/usecase/GetElement.kt | 10 +--- .../domain/usecase/OnAuthenticated.kt | 8 +-- 11 files changed, 24 insertions(+), 103 deletions(-) delete mode 100644 app/src/main/java/com/mkchtv/cleantemplate/di/UseCaseModule.kt diff --git a/app/src/main/java/com/mkchtv/cleantemplate/auth/AuthProtectedViewModel.kt b/app/src/main/java/com/mkchtv/cleantemplate/auth/AuthProtectedViewModel.kt index dff82be..d84223a 100644 --- a/app/src/main/java/com/mkchtv/cleantemplate/auth/AuthProtectedViewModel.kt +++ b/app/src/main/java/com/mkchtv/cleantemplate/auth/AuthProtectedViewModel.kt @@ -1,13 +1,13 @@ package com.mkchtv.cleantemplate.auth import androidx.lifecycle.ViewModel -import com.mkchtv.cleantemplate.domain.usecase.GetAuthStateFlowUseCase +import com.mkchtv.cleantemplate.domain.usecase.GetAuthStateFlow import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject @HiltViewModel class AuthProtectedViewModel @Inject constructor( - getAuthStateFlow: GetAuthStateFlowUseCase, + getAuthStateFlow: GetAuthStateFlow, ) : ViewModel() { val authStateFlow = getAuthStateFlow() } \ No newline at end of file diff --git a/app/src/main/java/com/mkchtv/cleantemplate/auth/AuthViewModel.kt b/app/src/main/java/com/mkchtv/cleantemplate/auth/AuthViewModel.kt index 31d2fee..63c07a3 100644 --- a/app/src/main/java/com/mkchtv/cleantemplate/auth/AuthViewModel.kt +++ b/app/src/main/java/com/mkchtv/cleantemplate/auth/AuthViewModel.kt @@ -2,14 +2,14 @@ package com.mkchtv.cleantemplate.auth import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.mkchtv.cleantemplate.domain.usecase.OnAuthenticatedUseCase +import com.mkchtv.cleantemplate.domain.usecase.OnAuthenticated import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel class AuthViewModel @Inject constructor( - private val onAuthenticated: OnAuthenticatedUseCase + private val onAuthenticated: OnAuthenticated ) : ViewModel() { fun onAuthClick() = viewModelScope.launch { diff --git a/app/src/main/java/com/mkchtv/cleantemplate/details/ElementDetailsViewModel.kt b/app/src/main/java/com/mkchtv/cleantemplate/details/ElementDetailsViewModel.kt index 2d92239..6b0e01d 100644 --- a/app/src/main/java/com/mkchtv/cleantemplate/details/ElementDetailsViewModel.kt +++ b/app/src/main/java/com/mkchtv/cleantemplate/details/ElementDetailsViewModel.kt @@ -5,9 +5,9 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.mkchtv.cleantemplate.domain.common.Constants import com.mkchtv.cleantemplate.domain.di.AppIoScope -import com.mkchtv.cleantemplate.domain.usecase.CreateOrUpdateElementUseCase -import com.mkchtv.cleantemplate.domain.usecase.DeleteElementUseCase -import com.mkchtv.cleantemplate.domain.usecase.GetElementUseCase +import com.mkchtv.cleantemplate.domain.usecase.CreateOrUpdateElement +import com.mkchtv.cleantemplate.domain.usecase.DeleteElement +import com.mkchtv.cleantemplate.domain.usecase.GetElement import com.mkchtv.cleantemplate.list.ElementItem import com.mkchtv.cleantemplate.mapper.toUiItem import com.mkchtv.cleantemplate.util.getIntOrDefault @@ -23,9 +23,9 @@ import javax.inject.Inject @HiltViewModel class ElementDetailsViewModel @Inject constructor( - getElement: GetElementUseCase, - private val createOrUpdateElement: CreateOrUpdateElementUseCase, - private val deleteElement: DeleteElementUseCase, + getElement: GetElement, + private val createOrUpdateElement: CreateOrUpdateElement, + private val deleteElement: DeleteElement, @AppIoScope private val appIoScope: CoroutineScope, private val savedStateHandle: SavedStateHandle ) : ViewModel() { diff --git a/app/src/main/java/com/mkchtv/cleantemplate/di/UseCaseModule.kt b/app/src/main/java/com/mkchtv/cleantemplate/di/UseCaseModule.kt deleted file mode 100644 index 18b2e38..0000000 --- a/app/src/main/java/com/mkchtv/cleantemplate/di/UseCaseModule.kt +++ /dev/null @@ -1,49 +0,0 @@ -package com.mkchtv.cleantemplate.di - -import com.mkchtv.cleantemplate.domain.usecase.CreateOrUpdateElement -import com.mkchtv.cleantemplate.domain.usecase.CreateOrUpdateElementUseCase -import com.mkchtv.cleantemplate.domain.usecase.DeleteElement -import com.mkchtv.cleantemplate.domain.usecase.DeleteElementUseCase -import com.mkchtv.cleantemplate.domain.usecase.GetAllElements -import com.mkchtv.cleantemplate.domain.usecase.GetAllElementsUseCase -import com.mkchtv.cleantemplate.domain.usecase.GetAuthStateFlow -import com.mkchtv.cleantemplate.domain.usecase.GetAuthStateFlowUseCase -import com.mkchtv.cleantemplate.domain.usecase.GetElement -import com.mkchtv.cleantemplate.domain.usecase.GetElementUseCase -import com.mkchtv.cleantemplate.domain.usecase.OnAuthenticated -import com.mkchtv.cleantemplate.domain.usecase.OnAuthenticatedUseCase -import dagger.Binds -import dagger.Module -import dagger.hilt.InstallIn -import dagger.hilt.android.components.ViewModelComponent -import dagger.hilt.android.scopes.ViewModelScoped - -@Module -@InstallIn(ViewModelComponent::class) -abstract class UseCaseModule { - - @Binds - @ViewModelScoped - abstract fun bindGetAllElementsUseCase(useCase: GetAllElements): GetAllElementsUseCase - - @Binds - @ViewModelScoped - abstract fun bindGetElementUseCase(useCase: GetElement): GetElementUseCase - - @Binds - @ViewModelScoped - abstract fun bindCreateOrUpdateElementUseCase(useCase: CreateOrUpdateElement): CreateOrUpdateElementUseCase - - @Binds - @ViewModelScoped - abstract fun bindDeleteElementUseCase(useCase: DeleteElement): DeleteElementUseCase - - @Binds - @ViewModelScoped - abstract fun bindGetAuthStateFlowUseCase(useCase: GetAuthStateFlow): GetAuthStateFlowUseCase - - @Binds - @ViewModelScoped - abstract fun bindOnAuthenticatedUseCase(useCase: OnAuthenticated): OnAuthenticatedUseCase - -} \ No newline at end of file diff --git a/app/src/main/java/com/mkchtv/cleantemplate/list/ElementsListViewModel.kt b/app/src/main/java/com/mkchtv/cleantemplate/list/ElementsListViewModel.kt index fdf6370..8fdc194 100644 --- a/app/src/main/java/com/mkchtv/cleantemplate/list/ElementsListViewModel.kt +++ b/app/src/main/java/com/mkchtv/cleantemplate/list/ElementsListViewModel.kt @@ -2,7 +2,7 @@ package com.mkchtv.cleantemplate.list import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.mkchtv.cleantemplate.domain.usecase.GetAllElementsUseCase +import com.mkchtv.cleantemplate.domain.usecase.GetAllElements import com.mkchtv.cleantemplate.mapper.toUiItemsList import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -16,7 +16,7 @@ import javax.inject.Inject @ExperimentalCoroutinesApi @HiltViewModel class ElementsListViewModel @Inject constructor( - getAllElements: GetAllElementsUseCase, + getAllElements: GetAllElements, ) : ViewModel() { val elementsState: StateFlow> = getAllElements() diff --git a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/CreateOrUpdateElement.kt b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/CreateOrUpdateElement.kt index b2cee62..40ca098 100644 --- a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/CreateOrUpdateElement.kt +++ b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/CreateOrUpdateElement.kt @@ -4,13 +4,9 @@ import com.mkchtv.cleantemplate.domain.entity.Element import com.mkchtv.cleantemplate.domain.repository.ElementsRepository import javax.inject.Inject -interface CreateOrUpdateElementUseCase { - suspend operator fun invoke(id: Int, name: String, description: String) -} - class CreateOrUpdateElement @Inject constructor( private val repository: ElementsRepository -) : CreateOrUpdateElementUseCase { - override suspend operator fun invoke(id: Int, name: String, description: String) = +) { + suspend operator fun invoke(id: Int, name: String, description: String) = repository.createOrUpdate(Element(id = id, name = name, description = description)) } \ No newline at end of file diff --git a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/DeleteElement.kt b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/DeleteElement.kt index 371af54..f10ddb8 100644 --- a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/DeleteElement.kt +++ b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/DeleteElement.kt @@ -3,12 +3,8 @@ package com.mkchtv.cleantemplate.domain.usecase import com.mkchtv.cleantemplate.domain.repository.ElementsRepository import javax.inject.Inject -interface DeleteElementUseCase { - suspend operator fun invoke(vararg ids: Int) -} - class DeleteElement @Inject constructor( private val repository: ElementsRepository -) : DeleteElementUseCase { - override suspend operator fun invoke(vararg ids: Int) = repository.delete(ids = ids) +) { + suspend operator fun invoke(vararg ids: Int) = repository.delete(ids = ids) } \ No newline at end of file diff --git a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetAllElements.kt b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetAllElements.kt index 3c8781b..978e32e 100644 --- a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetAllElements.kt +++ b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetAllElements.kt @@ -1,16 +1,10 @@ package com.mkchtv.cleantemplate.domain.usecase -import com.mkchtv.cleantemplate.domain.entity.Element import com.mkchtv.cleantemplate.domain.repository.ElementsRepository -import kotlinx.coroutines.flow.Flow import javax.inject.Inject -interface GetAllElementsUseCase { - operator fun invoke(): Flow> -} - class GetAllElements @Inject constructor( private val repository: ElementsRepository -) : GetAllElementsUseCase { - override operator fun invoke() = repository.elementsFlow() +) { + operator fun invoke() = repository.elementsFlow() } \ No newline at end of file diff --git a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetAuthStateFlow.kt b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetAuthStateFlow.kt index b325f42..60104d4 100644 --- a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetAuthStateFlow.kt +++ b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetAuthStateFlow.kt @@ -1,16 +1,10 @@ package com.mkchtv.cleantemplate.domain.usecase -import com.mkchtv.cleantemplate.domain.common.AuthState import com.mkchtv.cleantemplate.domain.repository.AuthRepository -import kotlinx.coroutines.flow.Flow import javax.inject.Inject -interface GetAuthStateFlowUseCase { - operator fun invoke(): Flow -} - class GetAuthStateFlow @Inject constructor( private val repository: AuthRepository -) : GetAuthStateFlowUseCase { - override operator fun invoke() = repository.authStateFlow +) { + operator fun invoke() = repository.authStateFlow } \ No newline at end of file diff --git a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetElement.kt b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetElement.kt index 7298b7d..51327c6 100644 --- a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetElement.kt +++ b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/GetElement.kt @@ -1,16 +1,10 @@ package com.mkchtv.cleantemplate.domain.usecase -import com.mkchtv.cleantemplate.domain.entity.Element import com.mkchtv.cleantemplate.domain.repository.ElementsRepository -import kotlinx.coroutines.flow.Flow import javax.inject.Inject -interface GetElementUseCase { - operator fun invoke(elementId: Int): Flow -} - class GetElement @Inject constructor( private val repository: ElementsRepository -) : GetElementUseCase { - override operator fun invoke(elementId: Int) = repository.elementFlow(elementId) +) { + operator fun invoke(elementId: Int) = repository.elementFlow(elementId) } \ No newline at end of file diff --git a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/OnAuthenticated.kt b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/OnAuthenticated.kt index 174ca36..35a70f3 100644 --- a/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/OnAuthenticated.kt +++ b/domain/src/main/java/com/mkchtv/cleantemplate/domain/usecase/OnAuthenticated.kt @@ -3,12 +3,8 @@ package com.mkchtv.cleantemplate.domain.usecase import com.mkchtv.cleantemplate.domain.repository.AuthRepository import javax.inject.Inject -interface OnAuthenticatedUseCase { - suspend operator fun invoke() -} - class OnAuthenticated @Inject constructor( private val repository: AuthRepository -) : OnAuthenticatedUseCase { - override suspend operator fun invoke() = repository.onAuthenticated() +) { + suspend operator fun invoke() = repository.onAuthenticated() } \ No newline at end of file