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 MainViewModel | |
@ViewModelInject | |
constructor( | |
private val mainRepository: MainRepository, | |
@Assisted private val savedStateHandle: SavedStateHandle | |
) : ViewModel() { | |
private val _dataState: MutableLiveData<DataState<List<Blog>>> = MutableLiveData() | |
val dataState: LiveData<DataState<List<Blog>>> | |
get() = _dataState |
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 BlogDao { | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
suspend fun insert(blogEntity: BlogCacheEntity): Long | |
@Query("SELECT * FROM blogs") | |
suspend fun get(): List<BlogCacheEntity> | |
} |
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
@Database(entities = [BlogCacheEntity::class], version = 1) | |
abstract class BlogDatabase : RoomDatabase() { | |
abstract fun blogDao(): BlogDao | |
companion object { | |
const val DATABASE_NAME: String = "blog_db" | |
} | |
} |
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 MainRepository | |
constructor( | |
private val blogDao: BlogDao, | |
private val blogApi: BlogApi, | |
private val cacheMapper: CacheMapper, | |
private val blogMapper: BlogMapper | |
) { | |
suspend fun getBlog(): Flow<DataState<List<Blog>>> = flow { | |
emit(DataState.Loading) | |
try { |
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 BlogMapper | |
@Inject | |
constructor() : EntityMapper<BlogObjectResponse, Blog> { | |
override fun mapFromEntity(entity: BlogObjectResponse): Blog { | |
return Blog( | |
id = entity.id, | |
title = entity.title, | |
body = entity.body, | |
image = entity.image, | |
category = entity.category |
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
interface BlogApi { | |
@GET("blogs") | |
suspend fun get(): List<BlogObjectResponse> | |
} |
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
data class BlogObjectResponse( | |
@SerializedName("pk") | |
@Expose | |
var id: Int, | |
@SerializedName("title") | |
@Expose | |
var title: String, | |
@SerializedName("body") |
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
sealed class DataState<out R> { | |
data class Success<out T>(val data: T) : DataState<T>() | |
data class Error(val exception: Exception) : DataState<Nothing>() | |
object Loading : DataState<Nothing>() | |
} |
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
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) { | |
setTheme(R.style.AppTheme.Base.Night); | |
} else { | |
setTheme(R.style.AppTheme.Base.Light); | |
} | |
setContentView(R.layout.activity_main); |