Skip to content

Instantly share code, notes, and snippets.

Avatar

Carlos Daniel cdmunoz

View GitHub Profile
@cdmunoz
cdmunoz / TestNetworkModule.kt
Created Sep 4, 2020
Koin's test network module
View TestNetworkModule.kt
fun testNetworkModule(baseUrl: String) = module {
single { provideTestRetrofit(baseUrl) }
single { provideTestApiService(get(), ApiService::class.java) }
}
fun provideTestRetrofit(baseUrl: String): Retrofit =
Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create()).build()
fun provideTestApiService(retrofit: Retrofit, apiService: Class<ApiService>) =
@cdmunoz
cdmunoz / NasaRoverPhotosApp.kt
Created Sep 3, 2020
Koin's start within Application class
View NasaRoverPhotosApp.kt
class NasaRoverPhotosApp : Application() {
override fun onCreate() {
super.onCreate()
Timber.plant(DebugTree())
startKoin {
androidContext(this@NasaRoverPhotosApp)
modules(appModules)
}
}
@cdmunoz
cdmunoz / ViewModelModule.kt
Created Sep 3, 2020
Koin's view model module
View ViewModelModule.kt
val viewModelModule = module {
viewModel { PhotosViewModel(get()) }
}
@cdmunoz
cdmunoz / RepositoryModule.kt
Created Sep 3, 2020
Koin's Repository module
View RepositoryModule.kt
val repositoryModule = module {
factory {
PhotosRepository(get())
}
}
@cdmunoz
cdmunoz / NetworkModule.kt
Created Sep 3, 2020
Koin's network module
View NetworkModule.kt
val networkModule = module {
single { provideOkHttpClient() }
single { provideRetrofit(get()) }
single { provideApiService(get(), ApiService::class.java) }
}
fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder().build()
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit =
Retrofit.Builder().baseUrl(BuildConfig.BASE_URL)
View OpenSettings.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.cameraIcon.setOnClickListener {
openSettings()
}
}
private fun openSettings() {
View MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.cameraIcon.setOnClickListener {
cameraPermission.launch(Manifest.permission.CAMERA)
}
}
View MainActivity.kt
private val cameraPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
with(binding.root) {
when {
granted -> snackBar("Permission granted!")
shouldShowRequestPermissionRationale(Manifest.permission.CAMERA) -> {
//this option is available starting in API 23
snackBar("Permission denied, show more info!")
}
else -> snackBar("Permission denied")
View build.gradle
def activityKtxVersion = "1.2.0-alpha07"
implementation "androidx.activity:activity-ktx:$activityKtxVersion"
def fragmentKtxVersion = "1.3.0-alpha07"
implementation "androidx.fragment:fragment-ktx:$fragmentKtxVersion"
View PhotosRepositoryTest.kt
@RunWith(JUnit4::class)
class PhotosRepositoryTest : MockWebServerBaseTest() {
@get:Rule
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
private lateinit var photosRepository: PhotosRepository
private lateinit var apiService: ApiService
private val sol = anyInt()
private val page = anyInt()