« first day (3998 days earlier)      last day (933 days later) » 

8:03 AM
Hello
How to get application context in the Kotlin top level function like
private fun hasInternetConnection(): Boolean { ... )
 
8:47 AM
@SagarKhurana private fun Context.hasInternetConnection(): Boolean { ... )
i just replied on Twitter lol :D
 
@Iva
then in order to access this function, I need some context component the repo layer
same guy ^_^
 
Yeah, you need context for that
 
but I dont have any context in top level function
 
i had an interesting implementation of that but I cant find it anymore
 
idk how hilt inject appliaction context in any class
@IvanMilisavljevic sed
class InternetCheck @Inject constructor(
@ApplicationContext private val context: Context
) {

fun hasInternetConnection(): Boolean {
val context = context
val connectivityManager = context.getSystemService(
Context.CONNECTIVITY_SERVICE
) as ConnectivityManager
val activeNetwork = connectivityManager.activeNetwork ?: return false
val capabilities =
connectivityManager.getNetworkCapabilities(activeNetwork) ?: return false

return when {
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
 
8:51 AM
Its quite easy with hilt actually
 
currently I have this
Yeah
but what if i want to do that without hilt and I have a extension funcation
 
You could save the application contrxt in the static instance
 
agree
but
that seems kind of a hack
 
But thats kinda an overkill
Yeah
 
indeed
thats why im afraid to use that
 
8:56 AM
Yeah, no other way to access those functions without a context
 
umm yeah i guess
The downside is that there is no guarantee that the non-static onCreate() will have been called before some static initialization code tries to fetch your Context object. That means your calling code will need to be ready to deal with null values which sort of defeats the whole point of this question. –
Melinda Green
Oct 19 '11 at 2:10
this is a comment from stackoverflow
about static aplication context
this is why I abandoned that approach
but is that could possibly be true?
isnt that
onCreate of application funcation is called first?
 
9:16 AM
Oh not necessarily, all lifecycle events are actually asynchronous
In 90%of the cases keeping a static application context would ve fine, if we talk about finding a solution to this specific problem
If we take into consideration everything else, it doesn't look pretty
Imho, checking if the phone is connected, belongs to the UI layer i would say, because domain layer is supposed to be platform agnostic anyways
 
9:52 AM
@DaveS noted
@SagarKhurana doubt you will ever need internet before any context is available so why scratch your head
 
10:28 AM
This was the most starred message last year on this day:
Hmm. Actually, nothing interesting was said on this day last year.
 
 
2 hours later…
12:23 PM
Ribbit
 
 
3 hours later…
3:13 PM
@asim Hey! Da heck are you doing pal!
 
 
1 hour later…
4:28 PM
posted on September 26, 2021

Android Weekly #485 🤖 #outlook a{ padding:0; } body{ width:100% !important; margin:0; padding:0; } body *{ -ms-text-size-adjust:none!important; -webkit-text-size-adjust:none!important; } body,.wrapper{ background:#ffffff; color:#505050; font-family:Arial, sans-serif; font-size:12px; } table,table td{ border-collaps

 
@RaymondArteaga you don't have any rights to ribbit so you can't sue me
 
5:27 PM
@asim In fact, I can, and in deed, I will
Ribbit ®
 
5:57 PM
this is copyright by @RaymondArteaga Ribbit ©
 
6:11 PM
@RaymondArteaga Waiting for your ILLEGAL notice
 
6:37 PM
You'll be punished without notice
 
Tim
7:12 PM
birthday congratulations to our #1 source of drama @MwBakker
 

« first day (3998 days earlier)      last day (933 days later) »