Skip to content

Instantly share code, notes, and snippets.

View Alqueraf's full-sized avatar
👨‍💻
Remote

Alex Queudot Alqueraf

👨‍💻
Remote
View GitHub Profile
@Alqueraf
Alqueraf / Network.kt
Created April 7, 2021 13:59
Android Internet Connectivity
private fun hasNetwork(context: Context): Boolean {
var result = false
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
cm?.run {
cm.getNetworkCapabilities(cm.activeNetwork)?.run {
result = when {
hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
@Alqueraf
Alqueraf / coroutine_context_switch.kt
Created March 18, 2021 11:50
Coroutine Context Switch
scope.launch(Dispatchers.IO) {
val result = apiService.getListData()
withContext(Dispatchers.Main) {
adapter.submitList(result)
}
}
@Alqueraf
Alqueraf / AdapterClickListener.kt
Last active October 15, 2020 14:55
Adapter Click Listener Example
// Pass a click "listener" lambda function as a parameter
class Adapter(
private val clickListener: (item: Item) -> Unit,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
// Adapter code here ...
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
// Get Current Item
val item = list[position]
@Alqueraf
Alqueraf / OAuth Refresh Token Request.kt
Created August 26, 2020 16:57
OAuth Refresh Token Request.kt
val response = createNetworkClient().post<OAuthAccessTokenResponse>("https://id.twitch.tv/oauth2/token") {
parameter("client_id", clientID)
parameter("client_secret", clientSecret)
parameter("refresh_token", refreshToken)
parameter("grant_type", "refresh_token")
}
Log.d("OAuth", "Access Token: ${response.accessToken}. Refresh Token: ${response.refreshToken}")
@Alqueraf
Alqueraf / OAuthAccessTokenResponse.kt
Last active August 26, 2020 16:42
OAuth Access Token Response
@Serializable
data class OAuthAccessTokenResponse(
@SerialName("access_token") val accessToken: String,
@SerialName("refresh_token") val refreshToken: String? = null,
@SerialName("expires_in") val expiresInSeconds: Int? = null,
@SerialName("token_type") val tokenType: String? = null,
@SerialName("scope") val scopes: List<String>? = null,
)
@Alqueraf
Alqueraf / OAuth Tokens Request ktor.kt
Created August 26, 2020 16:11
OAuth Tokens Request ktor
val response = createNetworkClient().post<OAuthAccessTokenResponse>("https://id.twitch.tv/oauth2/token") {
parameter("client_id", clientID)
parameter("client_secret", clientSecret)
parameter("code", authorizationCode)
parameter("grant_type", "authorization_code")
parameter("redirect_uri", redirectUri)
}
Log.d("OAuth", "Access Token: ${response.accessToken}. Refresh Token: ${response.refreshToken}")
@Alqueraf
Alqueraf / OAuth Authorize Response URL
Created August 26, 2020 15:02
OAuth Authorize Response URL
http://localhost/
?code=oqb7y5csbv258j9lk3agyb55bcg9z1
&scope=user%3Aread%3Aemail+bits%3Aread+channel%3Aread%3Ahype_train+analytics%3Aread%3Agames
&state=26ee285f-b747-455c-99a8-2d73c4c76e5f
@Alqueraf
Alqueraf / OAuth Redirect Listener.kt
Last active June 20, 2021 17:06
OAuth Redirect Listener
// Set Redirect Listener
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
request?.let {
// Check if this url is our OAuth redirectUrl, otherwise ignore it
if (request.url.toString().startsWith(redirectUri)) {
// To prevent CSRF attacks, check that we got the same state value we sent, otherwise ignore it
val responseState = request.url.getQueryParameter("state")
if (responseState == uniqueState) {
// This is our request. Parse the redirect URL query parameters to get the code
@Alqueraf
Alqueraf / OAuth Authorize URL.kt
Last active September 3, 2020 18:33
OAuth Redirect Listener
private val uniqueState = UUID.randomUUID().toString()
// Prepare URL
val uri = Uri.parse(authorizationUrl)
.buildUpon()
.appendQueryParameter("client_id", clientID)
.appendQueryParameter("redirect_uri", redirectUri)
.appendQueryParameter("response_type", "code")
.appendQueryParameter("scope", scopes.joinToString(separator = " "))
.appendQueryParameter("state", uniqueState)
.build()
@Alqueraf
Alqueraf / OAuth Authorize Example
Last active August 26, 2020 14:53
OAuth Authorize Example
https://id.twitch.tv/oauth2/authorize
?client_id=xxxxxxxxxxxxxxxxxxxxxxxxxx
&redirect_uri=http%3A%2F%2Flocalhost
&response_type=code
&scope=user%3Aread%3Aemail%20bits%3Aread%20channel%3Aread%3Ahype_train%20analytics%3Aread%3Agames
&state=be725a22-bde4-4532-ae54-9bd2050d86c2