Skip to content

Instantly share code, notes, and snippets.

@trinadhthatakula
Last active November 24, 2025 09:28
Show Gist options
  • Select an option

  • Save trinadhthatakula/9d8bc78f65adb78e38eb603da3a646e6 to your computer and use it in GitHub Desktop.

Select an option

Save trinadhthatakula/9d8bc78f65adb78e38eb603da3a646e6 to your computer and use it in GitHub Desktop.
⚡ Trinadh's Core Android Architecture (Kotlin + Koin)
/**
* 🚀 Core Architecture: Safe API Calling with Koin & Coroutines
* Stack: Kotlin | Koin | Flow | Result Pattern
* Author: Trinadh Thatakula
*/
// 1. type-safe Result wrapper
sealed interface DataResult<out T> {
data class Success<T>(val data: T) : DataResult<T>
data class Error(val exception: Throwable, val message: String? = null) : DataResult<Nothing>
data object Loading : DataResult<Nothing>
}
// 2. The Extension Function
// Safely executes a network call and catches exceptions automatically
suspend fun <T> safeApiCall(
dispatcher: CoroutineDispatcher = Dispatchers.IO,
apiCall: suspend () -> T
): DataResult<T> = withContext(dispatcher) {
try {
DataResult.Success(apiCall())
} catch (e: Exception) {
// Log to Crashlytics/Loki here
DataResult.Error(e, e.localizedMessage ?: "Unknown Error")
}
}
// 3. Koin Module Definition
val networkModule = module {
single {
HttpClient(Android) {
install(ContentNegotiation) { json() }
install(Logging) { level = LogLevel.ALL }
}
}
// Injecting the Repository with the safe caller
factory<AuthRepository> { AuthRepositoryImpl(client = get()) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment