implement viewmodel, domain layer and view layer for userlist

This commit is contained in:
Carlos Martinez
2020-02-03 13:01:55 -03:00
parent 7d4432278e
commit 259955ed5d
26 changed files with 305 additions and 76 deletions

View File

@@ -1,16 +0,0 @@
package com.hako.base.di
import androidx.room.Room
import com.hako.base.BuildConfig
import com.hako.base.room.BaseDatabase
import org.koin.dsl.module
val baseModule = module {
// Room database
single { Room.databaseBuilder(get(), BaseDatabase::class.java, BuildConfig.DB_NAME).build() }
factory { get<BaseDatabase>().userDao() }
factory { get<BaseDatabase>().albumDao() }
factory { get<BaseDatabase>().photoDao() }
}

View File

@@ -0,0 +1,33 @@
package com.hako.base.domain
sealed class Either<out L, out R> {
data class Left<out L>(val a: L) : Either<L, Nothing>()
data class Right<out R>(val b: R) : Either<Nothing, R>()
val isRight get() = this is Right<R>
val isLeft get() = this is Left<L>
fun <L> left(a: L) = Left(a)
fun <R> right(b: R) = Right(b)
fun either(fnL: (L) -> Any, fnR: (R) -> Any): Any =
when (this) {
is Left -> fnL(a)
is Right -> fnR(b)
}
}
fun <A, B, C> ((A) -> B).c(f: (B) -> C): (A) -> C = {
f(this(it))
}
fun <T, L, R> Either<L, R>.flatMap(fn: (R) -> Either<L, T>): Either<L, T> =
when (this) {
is Either.Left -> Either.Left(
a
)
is Either.Right -> fn(b)
}
fun <T, L, R> Either<L, R>.map(fn: (R) -> (T)): Either<L, T> = this.flatMap(fn.c(::right))

View File

@@ -0,0 +1,5 @@
package com.hako.base.domain
interface UseCase <T> {
fun execute(onSuccess: (List<T>) -> Unit, onError: (Throwable) -> Unit, onLoading: () -> Unit)
}

View File

@@ -0,0 +1,17 @@
package com.hako.base.domain.database
import androidx.room.Database
import androidx.room.RoomDatabase
import com.hako.base.domain.database.dao.AlbumDao
import com.hako.base.domain.database.dao.PhotoDao
import com.hako.base.domain.database.dao.UserDao
import com.hako.base.domain.database.entities.AlbumEntity
import com.hako.base.domain.database.entities.PhotoEntity
import com.hako.base.domain.database.entities.UserEntity
@Database(entities = [UserEntity::class, AlbumEntity::class, PhotoEntity::class], version = 1, exportSchema = false)
abstract class DatabaseClient : RoomDatabase() {
abstract fun userDao(): UserDao
abstract fun albumDao(): AlbumDao
abstract fun photoDao(): PhotoDao
}

View File

@@ -1,10 +1,10 @@
package com.hako.base.room.dao
package com.hako.base.domain.database.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.hako.base.room.entities.AlbumEntity
import com.hako.base.domain.database.entities.AlbumEntity
@Dao
interface AlbumDao {

View File

@@ -1,10 +1,10 @@
package com.hako.base.room.dao
package com.hako.base.domain.database.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.hako.base.room.entities.PhotoEntity
import com.hako.base.domain.database.entities.PhotoEntity
@Dao
interface PhotoDao {

View File

@@ -1,10 +1,10 @@
package com.hako.base.room.dao
package com.hako.base.domain.database.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.hako.base.room.entities.UserEntity
import com.hako.base.domain.database.entities.UserEntity
@Dao
interface UserDao {
@@ -18,6 +18,9 @@ interface UserDao {
@get:Query("SELECT * FROM ${UserEntity.TABLE_NAME}")
val all: List<UserEntity>
@Query("SELECT * FROM ${UserEntity.TABLE_NAME}")
fun getAllUsers(): List<UserEntity>
@Query("SELECT COUNT(*) FROM ${UserEntity.TABLE_NAME}")
fun count(): Int

View File

@@ -1,4 +1,4 @@
package com.hako.base.room.entities
package com.hako.base.domain.database.entities
import androidx.room.Entity
import androidx.room.Index

View File

@@ -1,4 +1,4 @@
package com.hako.base.room.entities
package com.hako.base.domain.database.entities
import androidx.room.Entity
import androidx.room.Index

View File

@@ -1,4 +1,4 @@
package com.hako.base.room.entities
package com.hako.base.domain.database.entities
import androidx.room.Entity
import androidx.room.Index

View File

@@ -0,0 +1,35 @@
package com.hako.base.domain.network
import com.hako.base.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import timber.log.Timber
class RemoteClient(endpoint: String) {
private val logger = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Timber.d(message)
}
}).setLevel(getLoggerLevel())
private val client = OkHttpClient.Builder()
.addInterceptor(logger)
.build()
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(endpoint)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build()
fun <T> getClient(api: Class<T>): T = retrofit.create(api)
private fun getLoggerLevel() = when (BuildConfig.DEBUG) {
true -> HttpLoggingInterceptor.Level.BASIC
false -> HttpLoggingInterceptor.Level.NONE
}
}

View File

@@ -0,0 +1,7 @@
package com.hako.base.domain.network
sealed class RequestStatus {
object Ready : RequestStatus()
object Loading : RequestStatus()
object Errored : RequestStatus()
}

View File

@@ -0,0 +1,13 @@
package com.hako.base.extensions
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
fun <T> LiveData<T>.observeNonNull(owner: LifecycleOwner, func: (T) -> Unit) {
observe(owner, Observer {
it?.let {
func(it)
}
})
}

View File

@@ -1,17 +0,0 @@
package com.hako.base.room
import androidx.room.Database
import androidx.room.RoomDatabase
import com.hako.base.room.dao.AlbumDao
import com.hako.base.room.dao.PhotoDao
import com.hako.base.room.dao.UserDao
import com.hako.base.room.entities.AlbumEntity
import com.hako.base.room.entities.PhotoEntity
import com.hako.base.room.entities.UserEntity
@Database(entities = [UserEntity::class, AlbumEntity::class, PhotoEntity::class], version = 1, exportSchema = false)
abstract class BaseDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
abstract fun albumDao(): AlbumDao
abstract fun photoDao(): PhotoDao
}