mirror of
https://github.com/imcarlost/Friendlists.git
synced 2026-04-10 10:56:54 -04:00
migrate to multidatabase structure and clean up code
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.hako.albumlist.di
|
||||
|
||||
import androidx.room.Room
|
||||
import com.hako.albumlist.BuildConfig
|
||||
import com.hako.albumlist.domain.clients.LocalClient
|
||||
import com.hako.albumlist.domain.datasource.AlbumlistRemoteApi
|
||||
import com.hako.albumlist.domain.usecase.GetAlbum
|
||||
import com.hako.albumlist.viewmodel.AlbumlistViewmodel
|
||||
@@ -8,8 +11,12 @@ import org.koin.androidx.viewmodel.dsl.viewModel
|
||||
import org.koin.dsl.module
|
||||
|
||||
val albumListModules = module {
|
||||
|
||||
single { Room.databaseBuilder(get(), LocalClient::class.java, BuildConfig.DB_NAME).build() }
|
||||
factory { get<LocalClient>().albumDao() }
|
||||
|
||||
factory { get<RemoteClient>().getClient(AlbumlistRemoteApi::class.java) }
|
||||
factory { GetAlbum(get()) }
|
||||
factory { GetAlbum(get(), get()) }
|
||||
|
||||
viewModel { AlbumlistViewmodel() }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.hako.albumlist.domain.clients
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import com.hako.albumlist.domain.datasource.AlbumDao
|
||||
import com.hako.albumlist.model.AlbumEntity
|
||||
|
||||
@Database(entities = [AlbumEntity::class], version = 1, exportSchema = false)
|
||||
abstract class LocalClient : RoomDatabase() {
|
||||
abstract fun albumDao(): AlbumDao
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.hako.albumlist.domain.datasource
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.hako.albumlist.model.AlbumEntity
|
||||
|
||||
@Dao
|
||||
interface AlbumDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun save(entity: AlbumEntity)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun saveAll(entities: List<AlbumEntity>)
|
||||
|
||||
@Query("SELECT * FROM ${AlbumEntity.TABLE_NAME} WHERE userId = :userId ORDER BY id ASC")
|
||||
fun getAlbums(userId: Int): List<AlbumEntity>
|
||||
|
||||
@Query("SELECT COUNT(*) FROM ${AlbumEntity.TABLE_NAME}")
|
||||
fun count(): Int
|
||||
|
||||
@Query("DELETE FROM ${AlbumEntity.TABLE_NAME}")
|
||||
fun nukeDatabase()
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.hako.albumlist.domain.datasource
|
||||
|
||||
import com.hako.albumlist.model.Album
|
||||
import com.hako.albumlist.model.AlbumRemote
|
||||
import io.reactivex.Single
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Query
|
||||
@@ -10,5 +10,5 @@ interface AlbumlistRemoteApi {
|
||||
@GET("/albums")
|
||||
fun getAlbums(
|
||||
@Query("userId") userId: Int
|
||||
): Single<List<Album>>
|
||||
): Single<List<AlbumRemote>>
|
||||
}
|
||||
@@ -1,22 +1,18 @@
|
||||
package com.hako.albumlist.domain.usecase
|
||||
|
||||
import com.hako.albumlist.domain.datasource.AlbumDao
|
||||
import com.hako.albumlist.domain.datasource.AlbumlistRemoteApi
|
||||
import com.hako.albumlist.model.AlbumViewable
|
||||
import com.hako.albumlist.model.Album
|
||||
import com.hako.albumlist.model.toAlbumEntity
|
||||
import com.hako.albumlist.model.toAlbumViewable
|
||||
import com.hako.base.domain.database.dao.AlbumDao
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import org.koin.core.KoinComponent
|
||||
import org.koin.core.get
|
||||
|
||||
class GetAlbum(private val dao: AlbumDao) : KoinComponent {
|
||||
|
||||
private val api: AlbumlistRemoteApi = get()
|
||||
class GetAlbum(private val dao: AlbumDao, private val api: AlbumlistRemoteApi) {
|
||||
|
||||
fun execute(
|
||||
userId: Int,
|
||||
onSuccess: (List<AlbumViewable>) -> Unit,
|
||||
onSuccess: (List<Album>) -> Unit,
|
||||
onError: (Throwable) -> Unit,
|
||||
onLoading: () -> Unit
|
||||
) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.hako.albumlist.R
|
||||
import com.hako.albumlist.model.AlbumViewable
|
||||
import com.hako.albumlist.model.Album
|
||||
import com.hako.albumlist.navigation.AlbumlistNavigation
|
||||
import com.hako.albumlist.viewmodel.AlbumlistViewmodel
|
||||
import com.hako.albumlist.widget.AlbumlistAdapter
|
||||
@@ -72,7 +72,7 @@ class AlbumlistFragment : Fragment() {
|
||||
Timber.e(throwable)
|
||||
}
|
||||
|
||||
private fun handleFetchSuccess(users: List<AlbumViewable>) {
|
||||
private fun handleFetchSuccess(users: List<Album>) {
|
||||
listAdapter.addAll(users)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,24 +1,38 @@
|
||||
package com.hako.albumlist.model
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import com.hako.base.domain.database.entities.AlbumEntity
|
||||
import kotlinx.android.parcel.Parcelize
|
||||
|
||||
@Parcelize
|
||||
data class Album(
|
||||
data class AlbumRemote(
|
||||
@SerializedName("id") val id: Int,
|
||||
@SerializedName("userId") val userId: Int,
|
||||
@SerializedName("title") val title: String
|
||||
) : Parcelable
|
||||
|
||||
data class AlbumViewable(
|
||||
@Entity(tableName = AlbumEntity.TABLE_NAME, indices = [Index(value = ["id"], unique = true)])
|
||||
data class AlbumEntity(
|
||||
@PrimaryKey
|
||||
val id: Int,
|
||||
val userId: Int,
|
||||
val title: String
|
||||
) {
|
||||
companion object {
|
||||
const val TABLE_NAME = "albums"
|
||||
}
|
||||
}
|
||||
|
||||
data class Album(
|
||||
val id: Int,
|
||||
val userId: Int,
|
||||
val title: String
|
||||
)
|
||||
|
||||
fun Album.toAlbumEntity() = AlbumEntity(this.id, this.userId, this.title)
|
||||
fun AlbumRemote.toAlbumEntity() = AlbumEntity(this.id, this.userId, this.title)
|
||||
|
||||
fun AlbumEntity.toAlbumViewable() = AlbumViewable(this.id, this.userId, this.title)
|
||||
fun AlbumEntity.toAlbumViewable() = Album(this.id, this.userId, this.title)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.hako.albumlist.viewmodel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.hako.albumlist.domain.usecase.GetAlbum
|
||||
import com.hako.albumlist.model.AlbumViewable
|
||||
import com.hako.albumlist.model.Album
|
||||
import com.hako.base.domain.network.RequestStatus
|
||||
import com.hako.base.domain.network.RequestStatus.Ready
|
||||
import com.hako.base.domain.network.RequestStatus.Loading
|
||||
@@ -14,7 +14,7 @@ import org.koin.core.get
|
||||
|
||||
class AlbumlistViewmodel : ViewModel(), KoinComponent {
|
||||
|
||||
val data = MutableLiveData<Either<Throwable, List<AlbumViewable>>>()
|
||||
val data = MutableLiveData<Either<Throwable, List<Album>>>()
|
||||
val requestStatus = MutableLiveData<RequestStatus>()
|
||||
|
||||
private val getAlbum: GetAlbum = get()
|
||||
|
||||
@@ -5,19 +5,19 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.hako.albumlist.R
|
||||
import com.hako.albumlist.model.AlbumViewable
|
||||
import com.hako.albumlist.model.Album
|
||||
import com.hako.base.extensions.autoNotify
|
||||
import kotlinx.android.synthetic.main.item_album_card.view.*
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class AlbumlistAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||||
|
||||
private var items by Delegates.observable(emptyList<AlbumViewable>()) { _, oldList, newList ->
|
||||
private var items by Delegates.observable(emptyList<Album>()) { _, oldList, newList ->
|
||||
autoNotify(oldList, newList) { old, new -> old.id == new.id }
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
var onItemClick: (AlbumViewable) -> Unit = { }
|
||||
var onItemClick: (Album) -> Unit = { }
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
|
||||
AlbumViewHolder(
|
||||
@@ -29,7 +29,7 @@ class AlbumlistAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||||
|
||||
fun getItem(position: Int) = items[position]
|
||||
|
||||
fun addAll(list: List<AlbumViewable>) {
|
||||
fun addAll(list: List<Album>) {
|
||||
items = list
|
||||
}
|
||||
|
||||
@@ -43,10 +43,10 @@ class AlbumlistAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||||
}
|
||||
|
||||
class AlbumViewHolder(private val view: View,
|
||||
private val onItemClick: (AlbumViewable) -> Unit) :
|
||||
private val onItemClick: (Album) -> Unit) :
|
||||
RecyclerView.ViewHolder(view) {
|
||||
|
||||
fun bind(album: AlbumViewable) = with(view) {
|
||||
fun bind(album: Album) = with(view) {
|
||||
item_album_card_album_name.text = album.title
|
||||
item_album_card_container.setOnClickListener {
|
||||
onItemClick(album)
|
||||
|
||||
Reference in New Issue
Block a user