Skip to content

Instantly share code, notes, and snippets.

@kohendrix
kohendrix / HttpRetryPolicy.kt
Created February 1, 2019 03:44
Volley retry policy
import android.util.Log
import com.android.volley.DefaultRetryPolicy
import com.android.volley.VolleyError
class HttpRetryPolicy(timeoutMs: Int = defaultTimeoutMs, maxRetry: Int = defaultMaxRetryCount) :
DefaultRetryPolicy(timeoutMs, maxRetry, DEFAULT_BACKOFF_MULT) {
companion object {
private const val defaultTimeoutMs = 1000 * 10
private const val defaultMaxRetryCount = 1
@kohendrix
kohendrix / MainViewModel.kt
Created February 2, 2019 03:29
TwitterSample search() updated
class MainViewModel : ViewModel() {
private val repository = TweetsRepository()
private val searchWords = MutableLiveData<String>() // trigger
// tweet data exposed to Views
val tweetDataResults: LiveData<TweetDataResult>
= Transformations.switchMap(searchWords) { str -> repository.loadTweets(str) }
/**
* search trigger
@kohendrix
kohendrix / VolleyRequest.kt
Created February 2, 2019 04:05
Volley Coroutines Sample request() no coroutines
fun request(method: Int, url: String, headers: Map<String, String>, jsonRequest: JSONObject?,
successListener: (json: JSONObject) -> Unit, errorListener: (error: Exception) -> Unit) {
val request = object : JsonObjectRequest(method, url, jsonRequest,
Response.Listener<JSONObject> { json -> successListener(json) },
Response.ErrorListener { e -> errorListener(e) }) {
override fun getHeaders(): Map<String, String> = headers
override fun parseNetworkError(volleyError: VolleyError): VolleyError {
return volleyError.networkResponse?.let {
Log.e("VolleyRequest", volleyError.networkResponse.headers.toString())
@kohendrix
kohendrix / VolleyRequest.kt
Created February 2, 2019 04:06
Volley Coroutines Sample request() coroutines
suspend fun request(method: Int, url: String, headers: Map<String, String>, jsonRequest: JSONObject?): JSONObject {
return suspendCancellableCoroutine { continuation ->
val success = Response.Listener<JSONObject> { response -> continuation.resume(response) }
val error = Response.ErrorListener { error -> continuation.resumeWithException(error) }
val request = object : JsonObjectRequest(method, url, jsonRequest, success, error) {
override fun getHeaders(): Map<String, String> = headers
override fun parseNetworkError(volleyError: VolleyError): VolleyError {
return volleyError.networkResponse?.let {
@kohendrix
kohendrix / VolleyTestClasses.kt
Created February 2, 2019 04:08
Volley Coroutines Sample
class TestRequest : HttpRequest() {
companion object {
private const val HEADER_AUTHORIZATION_KEY = "Authorization"
private const val QUERY_KEY = "q"
}
override val method: Int = Request.Method.GET
override val url: String = "https://www.testrequestforfun.com"
override val queryParams: Map<String, String> = mapOf(QUERY_KEY to "paramTest")
@kohendrix
kohendrix / AndroidManifest.xml
Created February 2, 2019 04:15
ReproSample
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.koheiando.reprosample">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".ReproApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
class DeepLinkTargetONEActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_deeplink_one)
}
}
class DeepLinkTargetTWOActivity : AppCompatActivity() {
companion object {
const val EXTRA_TEXT = "text"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_deeplink_two)
findViewById<TextView>(R.id.tv_to_replace).text = intent.getStringExtra(EXTRA_TEXT) ?: "text not passed"
}
@kohendrix
kohendrix / MainActivity.kt
Created February 2, 2019 04:23
ReproSample
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onResume() {
super.onResume()
Repro.track("main_open") // send tracking
@kohendrix
kohendrix / MainActivity.kt
Created February 2, 2019 04:25
ReproSample ReproTrigger version
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onResume() {
super.onResume()
Handler().postDelayed({