mirror of
https://github.com/imcarlost/Friendlists.git
synced 2026-04-10 02:46:54 -04:00
implement viewmodel, domain layer and view layer for userlist
This commit is contained in:
@@ -12,8 +12,6 @@ android {
|
||||
minSdkVersion build_versions.min_sdk
|
||||
targetSdkVersion build_versions.target_sdk
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
||||
buildConfigField "String", "DB_NAME", '"friendlists.db"'
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
|
||||
@@ -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() }
|
||||
|
||||
}
|
||||
33
base/src/main/java/com/hako/base/domain/Either.kt
Normal file
33
base/src/main/java/com/hako/base/domain/Either.kt
Normal 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))
|
||||
5
base/src/main/java/com/hako/base/domain/UseCase.kt
Normal file
5
base/src/main/java/com/hako/base/domain/UseCase.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.hako.base.domain
|
||||
|
||||
interface UseCase <T> {
|
||||
fun execute(onSuccess: (List<T>) -> Unit, onError: (Throwable) -> Unit, onLoading: () -> Unit)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 {
|
||||
@@ -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 {
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.hako.base.domain.network
|
||||
|
||||
sealed class RequestStatus {
|
||||
object Ready : RequestStatus()
|
||||
object Loading : RequestStatus()
|
||||
object Errored : RequestStatus()
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user