-
-
Save vaimalaviya1233/ce560342496e522e170bb5e00d8f99dc to your computer and use it in GitHub Desktop.
⚡ Trinadh's Core Android Architecture (Kotlin + Koin)
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
| /** | |
| * 🚀 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