Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iamkingalvarado/95109a3558dbd038cd6ddf0e774c0d47 to your computer and use it in GitHub Desktop.
Save iamkingalvarado/95109a3558dbd038cd6ddf0e774c0d47 to your computer and use it in GitHub Desktop.
Example.kt
package io.topcars.feat.profile
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModel
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.http.GET
import javax.inject.Inject
data class User1(
val id: String
)
interface AuthApi {
@GET("/login")
fun login(): User1
}
interface AuthRepository {
fun login(email: String, password: String)
}
class DefaultAuthRepository @Inject constructor() : AuthRepository {
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://api.suputamadre.com")
.build()
private val api = retrofit.create(AuthApi::class.java)
override fun login(email: String, password: String) {
api.login()
}
}
class MockAuthRepository @Inject constructor() : AuthRepository {
override fun login(email: String, password: String) {
TODO("Not yet implemented")
}
}
@HiltViewModel
class LoginViewModel2 @Inject constructor(
@Default
private val authRepository: AuthRepository
) : ViewModel() {
fun login(email: String, password: String) {
authRepository.login(email, password)
}
}
@HiltViewModel
class LoginViewModel3 @Inject constructor(
@Google
private val authRepository: AuthRepository
) : ViewModel() {
fun login(email: String, password: String) {
authRepository.login(email, password)
}
}
class SignUpFragment : Fragment() {
private val viewModel: LoginViewModel3 by viewModels()
}
@AndroidEntryPoint
class LoginFragment2 : Fragment() {
private val viewModel: LoginViewModel2 by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.login("", "")
view.findViewById<View>(R.id.loading).setOnClickListener {
viewModel.login("", "")
}
}
}
class GoogleAuthRepository @Inject constructor(): AuthRepository {
override fun login(email: String, password: String) {
TODO("Not yet implemented")
}
}
@Module
@InstallIn(SingletonComponent::class)
object AuthModule {
@Default
@Provides
fun provideAuthRepository(): AuthRepository {
return if (BuildConfig.DEBUG) {
MockAuthRepository()
} else {
DefaultAuthRepository()
}
}
@Google // SCOPE
@Provides
fun provideGoogleAuthRepository(): AuthRepository {
return GoogleAuthRepository()
}
}
annotation class Google
annotation class Default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment