Last active
July 8, 2020 23:11
-
-
Save DoomMortal/fa2c243d13d72a37988d12096cf8312a to your computer and use it in GitHub Desktop.
Convert room to domain model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
data class MainModel( | |
@PrimaryKey @Json(name = "id") val id: Int, | |
@Json(name = "userId") val userId: String, | |
@Json(name = "ortId") val ortId: String, | |
@Json(name = "plz") val plz: String, | |
@Json(name = "name") val name: String, | |
@Json(name = "picture") val picture: String, | |
@Json(name = "streetId") val streetId: Int, | |
@Json(name = "streetName") val streetName: String | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dao | |
interface PlaceDao { | |
@Query("SELECT * from place") | |
fun getPlaces(): DataSource.Factory<Int, MainModel> | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
fun insertAll( places: PagedList<MainModel>) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var properties: LiveData<PagedList<com.trashmaster.muellmister.network.Place>> | |
≠ | |
properties = videosRepository.data | |
// New for merged ViewModel: | |
videosRepository.bufferPlaces(_properties.value!!) | |
properties = videosRepository.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PlaceRepository(private val database: AppDatabase) { | |
val data: LiveData<PagedList<com.trashmaster.muellmister.data.Place>> = | |
database.placeDao().getPlaces().toLiveData(pageSize = 50) | |
suspend fun bufferPlaces(list: PagedList<MainModel>) { | |
withContext(Dispatchers.IO) { | |
database.placeDao().insertAll(list) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment