Skip to content

Instantly share code, notes, and snippets.

View gabriel-TheCode's full-sized avatar
🏠
Working from home

Gabriel TEKOMBO gabriel-TheCode

🏠
Working from home
View GitHub Profile
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
@Dao
interface BlogDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(blogEntity: BlogCacheEntity): Long
@Query("SELECT * FROM blogs")
suspend fun get(): List<BlogCacheEntity>
}
@Database(entities = [BlogCacheEntity::class], version = 1)
abstract class BlogDatabase : RoomDatabase() {
abstract fun blogDao(): BlogDao
companion object {
const val DATABASE_NAME: String = "blog_db"
}
}
@gabriel-TheCode
gabriel-TheCode / MainRepository.kt
Last active March 10, 2022 15:57
Main Repository
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 {
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
interface BlogApi {
@GET("blogs")
suspend fun get(): List<BlogObjectResponse>
}
data class BlogObjectResponse(
@SerializedName("pk")
@Expose
var id: Int,
@SerializedName("title")
@Expose
var title: String,
@SerializedName("body")
@gabriel-TheCode
gabriel-TheCode / DataState.kt
Created June 22, 2021 16:06
A utility class that will be responsible to communicate the current state of Network Call to the UI Layer
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>()
}
@gabriel-TheCode
gabriel-TheCode / ButtonComposable.kt
Created February 3, 2021 12:57
Custom Icon Button with Jetpack Compose
@Composable
fun IconButton(onClick: () -> Unit) {
Button(onClick = { onClick() }){
Icon(asset = Icons.Filled.Refresh, modifier = Modifier.padding(8.dp))
Text(text = stringResource(id = R.string.resend_code))
}
}
@gabriel-TheCode
gabriel-TheCode / ChangingTheme_setTheme.java
Created June 10, 2020 11:09
Apply a theme according to the current theme
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);