Skip to content

Instantly share code, notes, and snippets.

View chiragthummar's full-sized avatar
🎯
Focusing

Chirag Thummar chiragthummar

🎯
Focusing
View GitHub Profile
@HiltViewModel
class HomeScreenViewModel @Inject constructor(
private val imageRepository: ImageRepository
) : ViewModel() {
var state by mutableStateOf(HomeScreenState())
private set
fun onEvent(event: HomeScreenEvents) {
when (event) {
@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
@Binds
@Singleton
abstract fun bindImageRepository(
imageRepositoryImpl: ImageRepositoryImpl
): ImageRepository
}
@Singleton
class ImageRepositoryImpl @Inject constructor(
private val imageApi: ImageApi
) : ImageRepository {
override fun getImages(text: String): Flow<Resources<List<Image>>> {
return flow {
emit(Resources.Loading(true))
val remoteList = try {
interface ImageRepository {
fun getImages(
text: String
): Flow<Resources<List<Image>>>
}
fun ImageDto.toImage(): Image {
return Image(
id = id,
width = width,
height = height,
srcSmall = srcSmall,
src = src
)
}
data class Image(
var id: String,
var src: String,
var srcSmall: String,
var width: Int,
var height: Int,
)
import com.md.jetpackcomposemviarchitecture.data.remote.ImageApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import com.md.jetpackcomposemviarchitecture.data.remote.dto.ImageListDto
import retrofit2.http.GET
import retrofit2.http.Query
interface ImageApi {
@GET("search")
suspend fun getInfiniteApiImages(
@Query("q") q : String
): ImageListDto
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
@chiragthummar
chiragthummar / App.kt
Created December 22, 2023 13:17
Create Hilt App
import android.app.Application
import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class App : Application() {
override fun onCreate() {
super.onCreate()
}
}