Skip to content

Instantly share code, notes, and snippets.

View sys1yagi's full-sized avatar

Toshihiro Yagi sys1yagi

View GitHub Profile
@sys1yagi
sys1yagi / Main.kt
Last active May 17, 2019 01:19
Observation of state change of View by Flow
// in Activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launchWhenCreated {
editNickName.textChangeAsFlow()
.map { it?.isNotEmpty() ?: false }
.collect {
sendButton.isEnabled = it
}
}
@sys1yagi
sys1yagi / intent.kt
Last active February 28, 2019 23:25
// deprecatedだけど新しいのは API 23からなので仕方ない
val intent = AccountManager.newChooseAccountIntent(
null,
null,
arrayOf("com.google"),
true,
null,
null,
null,
null
fun delay(delayTime: Long, stateMachine: StateMachine) {
Thread.sleep(delayTime)
stateMachine.resume()
}
class StateMachine {
var state = 0
var start = 0L
fun resume() {
when (state) {
import android.app.IntentService
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.work.Data
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import androidx.work.Worker
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.ReceiveChannel
import kotlinx.coroutines.experimental.channels.sendBlocking
import kotlinx.coroutines.experimental.launch
object EditTextChannel {
fun empty(editText: EditText): ReceiveChannel<Boolean> = ConflatedChannel<Boolean>().apply {
class StateLiveData<T> : MutableLiveData<T>() {
override fun onInactive() {
super.onInactive()
value = null
}
}
@sys1yagi
sys1yagi / actor_fizzbuzz.kt
Last active December 3, 2017 07:47
Implement fizzbuzz with actor of kotlin coroutine.
class Value(val value: Int, var output: String = "")
fun rule(seed: Int, print: String, next: SendChannel<Value>) = actor<Value> {
for (value in channel) {
next.send(value.apply {
if (value.value % seed == 0) {
this.output += print
}
})
}
fun shouldOpenShop(shopId: Long): Boolean {
if( shopId <= 0L ){
return false
}
if( this.shop == null ) {
return false
}
if( this.shop.id == shopId ) { // これを return this.shop.id == shopId にしろって言われる
return false
}
fun <T> async(context: CoroutineContext = CommonPool, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T)
= kotlinx.coroutines.experimental.async(context, start, block)
fun ui(start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit)
= launch(UI, start, block)
suspend fun <T, U> parallel(c1: suspend CoroutineScope.() -> T, c2: suspend CoroutineScope.() -> U): Pair<T, U> {
val c1Job = async(block = c1)
val c2Job = async(block = c2)
return Pair(c1Job.await(), c2Job.await())
@startuml

start

if (Have an access token?) then (yes)
else (no)
  if (Have an refresh token?) then (yes)
    while (Request access token) is (error)
 if (retry?) then (yes)