Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / HttpRequestQueue.kt
Created February 1, 2019 03:43
Volley RequestQueue singleton
import android.content.Context
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.Volley
/**
* Singleton
*/
class HttpRequestQueue constructor(context: Context) {
import org.json.JSONObject
abstract class HttpResponse {
companion object {
const val HEADER_KEY = "header"
const val RESPONSE_KEY = "response"
}
internal abstract fun parseJson(json: JSONObject): HttpResponse
}
@kohendrix
kohendrix / HttpRequest.kt
Created February 1, 2019 03:42
HttpRequest client using Volley and Coroutines
import android.util.Log
import com.android.volley.Response
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonObjectRequest
import com.example.koheiando.twittervolleysample.TvsApplication
import kotlinx.coroutines.suspendCancellableCoroutine
import org.json.JSONObject
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import com.example.koheiando.reprosample.DeepLinkTargetTWOActivity.Companion.EXTRA_TEXT
/**
* does not have UI, just start the right activity requested from a browser
*/
@kohendrix
kohendrix / ReproTrigger.kt
Created January 25, 2019 09:04
triggers Repro events
import io.repro.android.Repro
class ReproTrigger {
companion object {
private var instance: ReproTrigger? = null
fun getInstance(): ReproTrigger {
if (instance == null)
instance = ReproTrigger()
return instance!!
}
@kohendrix
kohendrix / ReproApplication.kt
Last active January 25, 2019 07:47
Implementing Repro SDK for Android
import android.app.Application
import android.util.Log
import io.repro.android.Repro
import org.xmlpull.v1.XmlPullParser
import java.lang.Exception
class ReproApplication : Application() {
companion object {
private const val REPRO_TOKEN_KEY = "repro_sdk_token"
}
@kohendrix
kohendrix / NotDialogFragment.kt
Last active December 27, 2018 00:59
Android Fragment that shows a dialog but not a DialogFragment
class NotDialogFragment : Fragment() {
private var reserveDismiss = false // resume時にこのdialogを閉じる
private var reserveShow = false // resume時に表示
private lateinit var dialogMain: LinearLayout
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater?.inflate(R.layout.fragment_load, container, false)?.apply {
dialogMain = findViewById<FrameLayout>(R.id.dialog_main_layout).also {
it.visibility = View.GONE
@kohendrix
kohendrix / promise_array_test.js
Created November 30, 2018 07:33
Simple tests of the Promise array execution order.
'use strict';
var p = console.log;
var FS = require('fs');
// fs appendFile Promise wrapper
function write(path, data){
return new Promise((res, rej) => FS.appendFile(path, data, err =>
err ? rej(err) : res(p(data, 'is done!'))
));