Skip to content

Instantly share code, notes, and snippets.

View Audhil's full-sized avatar
🎯
Focusing

Mohammed Audhil Audhil

🎯
Focusing
View GitHub Profile
@Audhil
Audhil / DependencyInversion.java
Created September 29, 2020 19:54
SOLID - get it break it!
package solid;
public class _5DI {
// dependency inversion principle
// wrong
private class Email {
private void sendEmail() {
}
@Audhil
Audhil / api_chaining.kt
Last active September 23, 2020 15:14
Coroutines in NutShell! - (pay attention to file names)
lifecycleScope.launch(Dispatchers.IO) {
val time = measureTimeMillis {
val res1 = async {
println("yup 1st launched: ${Thread.currentThread().name}")
getNetworkResp1()
}.await()
try {
val res2 = async {
println("yup 2nd launched: ${Thread.currentThread().name}")
@Audhil
Audhil / API.kt
Last active September 19, 2020 18:42
Kotlin CoRoutines in NutShell!
package com.example.croutinesdemo
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
// api
interface API {
@GET("/comments")
fun getComments(): Call<List<Comment>> // apiCallWithRetrofitDemo1(), apiCallWithRetrofitDemo2()
@Audhil
Audhil / Event.kt
Created September 13, 2020 17:12
handle livedata only once!
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
@Audhil
Audhil / writetofile.kt
Created September 4, 2020 17:11
handy func to write content to internal storage file - useful to print logs, when it gets truncated by logcat!
private fun writeToFile(data: String, context: Context) {
try {
val outputStreamWriter = OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE))
outputStreamWriter.write(data)
outputStreamWriter.close()
} catch (e: IOException) {
showELog("File write failed: $e")
}
}
@Audhil
Audhil / MainActivityTest.kt
Created August 23, 2020 17:13
instrumentation tests - actual test cases
@UninstallModules(APIModule::class, OtherModule::class)
@HiltAndroidTest
class MainActivityTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Rule
@JvmField
val activityRule = ActivityTestRule(MainActivity::class.java, true, false)
@Audhil
Audhil / TestOtherModule.kt
Created August 23, 2020 17:09
instrumentation tests - Test equivalent for OtherModule.kt
@Module
@InstallIn(ActivityComponent::class)
class TestOtherModule {
@Provides
fun giveFeedListAdapter(): FeedListAdapter = FeedListAdapter()
}
@Audhil
Audhil / TestAPIModule.kt
Created August 23, 2020 17:02
instrumentation tests - Test equivalent of app module
@Module
@InstallIn(ApplicationComponent::class)
class TestAPIModule {
@Provides
fun giveRetrofitAPIService(): API =
Retrofit.Builder()
.baseUrl("http://localhost:8080/")
// .baseUrl("http://127.0.0.1:8080") // this too works
.addConverterFactory(MoshiConverterFactory.create())
@Audhil
Audhil / build.gradle
Created August 23, 2020 16:54
instrumentation tests - app/build.gradle
android {
...
defaultConfig {
...
testInstrumentationRunner "com.example.newsapp.runner.NewsRunner"
...
}
}
@Audhil
Audhil / NewsRunner.kt
Created August 23, 2020 16:50
instrumentation tests - custom runner
class NewsRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
}