Skip to content

Commit

Permalink
UPD: simplify use-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-kichatov committed Nov 5, 2023
1 parent 8e7a218 commit d8e161b
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
49 changes: 0 additions & 49 deletions app/src/main/java/com/mkchtv/cleantemplate/di/UseCaseModule.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +16,7 @@ import javax.inject.Inject
@ExperimentalCoroutinesApi
@HiltViewModel
class ElementsListViewModel @Inject constructor(
getAllElements: GetAllElementsUseCase,
getAllElements: GetAllElements,
) : ViewModel() {

val elementsState: StateFlow<List<ElementItem>> = getAllElements()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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<List<Element>>
}

class GetAllElements @Inject constructor(
private val repository: ElementsRepository
) : GetAllElementsUseCase {
override operator fun invoke() = repository.elementsFlow()
) {
operator fun invoke() = repository.elementsFlow()
}
Original file line number Diff line number Diff line change
@@ -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<AuthState>
}

class GetAuthStateFlow @Inject constructor(
private val repository: AuthRepository
) : GetAuthStateFlowUseCase {
override operator fun invoke() = repository.authStateFlow
) {
operator fun invoke() = repository.authStateFlow
}
Original file line number Diff line number Diff line change
@@ -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<Element>
}

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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit d8e161b

Please sign in to comment.