Last active
October 4, 2020 14:55
-
-
Save gabrielbmoro/405a11364b5d49d3c45a511bde673371 to your computer and use it in GitHub Desktop.
Starting with Dagger Hilt
This file contains hidden or 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
apply plugin: 'kotlin-kapt' | |
apply plugin: 'dagger.hilt.android.plugin' | |
android { | |
... | |
} | |
dependencies{ | |
// Dagger | |
implementation "com.google.dagger:hilt-android:$dagger_hilt_version" | |
kapt "com.google.dagger:hilt-android-compiler:$dagger_hilt_version" | |
def androidx_dagger_hilt_version = '1.0.0-alpha02' | |
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$androidx_dagger_hilt_version" | |
kapt "androidx.hilt:hilt-compiler:$androidx_dagger_hilt_version" | |
} |
This file contains hidden or 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
import androidx.fragment.app.DialogFragment | |
import androidx.fragment.app.viewModels | |
import dagger.hilt.android.AndroidEntryPoint | |
@AndroidEntryPoint | |
class InputAlertDialogFragment : DialogFragment() { | |
private val viewModel by viewModels<ViewModelExample>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setStyle(STYLE_NO_TITLE, R.style.DialogStyle) | |
} | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_input_alert_dialog, container, false) | |
return binding.root | |
} | |
... | |
} |
This file contains hidden or 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
package com.gabrielbmoro.crazymath.core.di | |
import android.content.Context | |
import android.content.SharedPreferences | |
import com.gabrielbmoro.crazymath.core.repository.CrazyMathRepository | |
import com.gabrielbmoro.crazymath.core.repository.CrazyMathRepositoryImpl | |
import com.gabrielbmoro.crazymath.core.repository.FirebaseAuthRepositoryImpl | |
import com.gabrielbmoro.crazymath.core.repository.LocalDataSourceRepositoryImpl | |
import com.gabrielbmoro.crazymath.core.repository.FirebaseAuthRepositoryImpl | |
import com.gabrielbmoro.crazymath.core.navigation.NavigationController | |
import dagger.Module | |
import dagger.Provides | |
import dagger.hilt.InstallIn | |
import dagger.hilt.android.components.ApplicationComponent | |
import javax.inject.Singleton | |
@Module | |
@InstallIn(ApplicationComponent::class) | |
object FirebaseModule { | |
@Singleton | |
@Provides | |
fun provideFirebaseAuthRepository(): FirebaseAuthRepositoryImpl { | |
return FirebaseAuthRepositoryImpl() | |
} | |
} | |
@Module | |
@InstallIn(ApplicationComponent::class) | |
object NavigationModule { | |
@Singleton | |
@Provides | |
fun provideNavigationController(): NavigationController { | |
return NavigationController() | |
} | |
} | |
@Module | |
@InstallIn(ApplicationComponent::class) | |
object RepositoryModule { | |
@Provides | |
@Singleton | |
fun provideCrazyMathRepository( | |
localDataSourceRepositoryImpl: LocalDataSourceRepositoryImpl, | |
firebaseAuthRepositoryImpl: FirebaseAuthRepositoryImpl | |
): CrazyMathRepository { | |
return CrazyMathRepositoryImpl( | |
localDataSourceRepositoryImpl = localDataSourceRepositoryImpl, | |
firebaseAuthRepositoryImpl = firebaseAuthRepositoryImpl | |
) | |
} | |
} | |
@Module | |
@InstallIn(ApplicationComponent::class) | |
object SharedPreferencesModule { | |
private const val PREFERENCES_NAME = "crazy-math-preferences" | |
@Provides | |
@Named(PREFERENCES_NAME) | |
fun preferencesName(): String { | |
return "crazy-math-preferences" | |
} | |
@Provides | |
fun provideSharedPreferencesInstance(@ApplicationContext context: Context, @Named(PREFERENCES_NAME) preferencesName: String): SharedPreferences { | |
return context.getSharedPreferences( | |
preferencesName, | |
Context.MODE_PRIVATE | |
) | |
} | |
@Singleton | |
@Provides | |
fun provideLocalDataSourceRepository(preferences: SharedPreferences): LocalDataSourceRepositoryImpl { | |
return LocalDataSourceRepositoryImpl(preferences) | |
} | |
} |
This file contains hidden or 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
ext.dagger_hilt_version = '2.28-alpha' | |
dependencies { | |
classpath "com.google.dagger:hilt-android-gradle-plugin:$dagger_hilt_version" | |
} |
This file contains hidden or 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
import javax.inject.Inject | |
class UseCaseExample @Inject constructor( | |
private val repository: CrazyMathRepository | |
) { | |
fun execute() : String? { | |
return repository.getToken() | |
} | |
} |
This file contains hidden or 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
import androidx.hilt.lifecycle.ViewModelInject | |
class ViewModelExample @ViewModelInject constructor( | |
private val saveTokenUseCase: SaveTokenUseCase, | |
private val signUpUseCase: SignUpUseCase, | |
private val loginUseCase: SignInUseCase | |
) : ViewModel() { | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment