Skip to content

Instantly share code, notes, and snippets.

Avatar

Toshihiro Yagi sys1yagi

View GitHub Profile
@sys1yagi
sys1yagi / Main.kt
Last active May 17, 2019
Observation of state change of View by Flow
View Main.kt
// in Activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launchWhenCreated {
editNickName.textChangeAsFlow()
.map { it?.isNotEmpty() ?: false }
.collect {
sendButton.isEnabled = it
}
}
View intent.kt
// deprecatedだけど新しいのは API 23からなので仕方ない
val intent = AccountManager.newChooseAccountIntent(
null,
null,
arrayOf("com.google"),
true,
null,
null,
null,
null
View coroutine_statemachine.kt
fun delay(delayTime: Long, stateMachine: StateMachine) {
Thread.sleep(delayTime)
stateMachine.resume()
}
class StateMachine {
var state = 0
var start = 0L
fun resume() {
when (state) {
View WorkerService.kt
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
View EditTextChannel.kt
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 {
View StateLiveData.kt
class StateLiveData<T> : MutableLiveData<T>() {
override fun onInactive() {
super.onInactive()
value = null
}
}
@sys1yagi
sys1yagi / actor_fizzbuzz.kt
Last active Dec 3, 2017
Implement fizzbuzz with actor of kotlin coroutine.
View actor_fizzbuzz.kt
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
}
})
}
View ide.kt
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
}
View coroutine_extensions.kt
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())
View request.md
@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)