Skip to content

Commit

Permalink
Code review [#25] P9, P14 - added abstract UseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
E-D-W-I-N committed May 15, 2021
1 parent 921e2d5 commit 4f2ded1
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 75 deletions.
29 changes: 21 additions & 8 deletions app/src/main/java/com/edwin/weatherapp/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.edwin.weatherapp.di

import android.location.Address
import android.location.Geocoder
import android.location.Location
import com.edwin.data.device.GetLocationDataSource
import com.edwin.data.network.GeocoderDataSource
import com.edwin.data.network.RetrofitClient
import com.edwin.data.repository.WeatherRepositoryImpl
import com.edwin.domain.WeatherRepository
import com.edwin.domain.usecase.GetAddressFromGeocoderUseCase
import com.edwin.domain.usecase.GetFusedLocationUseCase
import com.edwin.domain.usecase.GetWeatherDetailsUseCase
import com.edwin.domain.model.WeatherDetails
import com.edwin.domain.usecase.UseCase
import com.edwin.domain.usecase.map.GetAddressFromGeocoderUseCase
import com.edwin.domain.usecase.map.GetFusedLocationUseCase
import com.edwin.domain.usecase.weather.GetWeatherDetailsUseCase
import com.edwin.weatherapp.presentation.map.MapViewModel
import com.edwin.weatherapp.presentation.weatherDetails.WeatherDetailsViewModel
import com.google.android.gms.location.LocationServices
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.qualifier.named
import org.koin.dsl.module

val dataModule = module {
Expand Down Expand Up @@ -41,12 +46,20 @@ val dataModule = module {
}

val useCaseModule = module {
single { GetWeatherDetailsUseCase(get()) }
single { GetFusedLocationUseCase(get()) }
single { GetAddressFromGeocoderUseCase(get()) }
single<UseCase<Location, Unit>>(
named("fusedLocation")
) { GetFusedLocationUseCase(get()) }

single<UseCase<Address, GetAddressFromGeocoderUseCase.Params>>(
named("addressFromGeocoder")
) { GetAddressFromGeocoderUseCase(get()) }

single<UseCase<WeatherDetails?, GetWeatherDetailsUseCase.Params>>(
named("weatherDetails")
) { GetWeatherDetailsUseCase(get()) }
}

val viewModelModule = module {
viewModel { MapViewModel(get(), get()) }
viewModel { WeatherDetailsViewModel(get()) }
viewModel { MapViewModel(get(named("fusedLocation")), get(named("addressFromGeocoder"))) }
viewModel { WeatherDetailsViewModel(get(named("weatherDetails"))) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import android.location.Address
import android.location.Location
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.edwin.domain.usecase.GetAddressFromGeocoderUseCase
import com.edwin.domain.usecase.GetFusedLocationUseCase
import com.edwin.domain.usecase.UseCase
import com.edwin.domain.usecase.map.GetAddressFromGeocoderUseCase.Params
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

class MapViewModel(
private val getFusedLocationUseCase: GetFusedLocationUseCase,
private val getAddressFromGeocoderUseCase: GetAddressFromGeocoderUseCase
private val getFusedLocationUseCase: UseCase<Location, Unit>,
private val getAddressFromGeocoderUseCase: UseCase<Address, Params>
) : ViewModel() {

private val _uiState = MutableStateFlow<MapUiState>(MapUiState.Default)
Expand All @@ -23,7 +23,7 @@ class MapViewModel(

fun getFusedLocation() = viewModelScope.launch {
_uiState.value = MapUiState.Loading
val location = getFusedLocationUseCase.invoke().single()
val location = getFusedLocationUseCase(Unit).single()
location.fold(
onSuccess = { _uiState.value = MapUiState.CurrentLocationLoaded(it) },
onFailure = {
Expand All @@ -35,7 +35,7 @@ class MapViewModel(

fun getAddress(latitude: Double, longitude: Double) = viewModelScope.launch {
_uiState.value = MapUiState.Loading
val address = getAddressFromGeocoderUseCase.invoke(latitude, longitude).single()
val address = getAddressFromGeocoderUseCase(Params(latitude, longitude)).single()
address.fold(
onSuccess = { _uiState.value = MapUiState.AddressLoaded(it) },
onFailure = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ package com.edwin.weatherapp.presentation.weatherDetails
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.edwin.domain.model.WeatherDetails
import com.edwin.domain.usecase.GetWeatherDetailsUseCase
import com.edwin.domain.usecase.UseCase
import com.edwin.domain.usecase.weather.GetWeatherDetailsUseCase.Params
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.single
import kotlinx.coroutines.launch

class WeatherDetailsViewModel(
private val getWeatherDetailsUseCase: GetWeatherDetailsUseCase
private val getWeatherDetailsUseCase: UseCase<WeatherDetails?, Params>
) : ViewModel() {

private val _uiState = MutableStateFlow<WeatherUiState>(WeatherUiState.Default)
val uiState: StateFlow<WeatherUiState> = _uiState.asStateFlow()

fun getWeatherDetails(latitude: Float, longitude: Float) = viewModelScope.launch {
_uiState.value = WeatherUiState.Loading
val weatherDetails = getWeatherDetailsUseCase.invoke(latitude, longitude).single()
val weatherDetails = getWeatherDetailsUseCase(Params(latitude, longitude)).single()
weatherDetails.fold(
onSuccess = { _uiState.value = WeatherUiState.WeatherDetailsLoaded(it) },
onFailure = { _uiState.value = WeatherUiState.Error(it) }
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

23 changes: 23 additions & 0 deletions domain/src/main/java/com/edwin/domain/usecase/UseCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.edwin.domain.usecase

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn

abstract class UseCase<out Type, in Params> {

abstract suspend fun run(params: Params): Type

operator fun invoke(params: Params): Flow<Result<Type>> {
return flow {
val result = try {
Result.success(run(params))
} catch (e: Exception) {
Result.failure(e)
}
emit(result)
}.flowOn(Dispatchers.IO)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.edwin.domain.usecase.map

import android.location.Address
import com.edwin.domain.WeatherRepository
import com.edwin.domain.usecase.UseCase

class GetAddressFromGeocoderUseCase(private val weatherRepository: WeatherRepository) :
UseCase<Address, GetAddressFromGeocoderUseCase.Params>() {

override suspend fun run(params: Params): Address = weatherRepository.getAddress(
params.latitude, params.longitude
)

data class Params(
val latitude: Double,
val longitude: Double
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.edwin.domain.usecase.map

import android.location.Location
import com.edwin.domain.WeatherRepository
import com.edwin.domain.usecase.UseCase

class GetFusedLocationUseCase(private val weatherRepository: WeatherRepository) :
UseCase<Location, Unit>() {

override suspend fun run(params: Unit): Location = weatherRepository.getFusedLocation()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.edwin.domain.usecase.weather

import com.edwin.domain.WeatherRepository
import com.edwin.domain.model.WeatherDetails
import com.edwin.domain.usecase.UseCase

class GetWeatherDetailsUseCase(private val weatherRepository: WeatherRepository) :
UseCase<WeatherDetails?, GetWeatherDetailsUseCase.Params>() {

override suspend fun run(params: Params): WeatherDetails? =
weatherRepository.getWeatherResponse(
params.latitude, params.longitude
)

data class Params(
val latitude: Float,
val longitude: Float
)

}
17 changes: 0 additions & 17 deletions domain/src/main/java/com/edwin/domain/util/UseCaseWrapper.kt

This file was deleted.

0 comments on commit 4f2ded1

Please sign in to comment.