This file contains 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
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 |
This file contains 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
scope.launch(Dispatchers.IO) { | |
val result = apiService.getListData() | |
withContext(Dispatchers.Main) { | |
adapter.submitList(result) | |
} | |
} |
This file contains 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
// 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] |
This file contains 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
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}") |
This file contains 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
@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, | |
) |
This file contains 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
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}") |
This file contains 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
// 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 |
NewerOlder