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
class SampleService : Service() { | |
companion object { | |
private val LAUNCHER = ForegroundServiceLauncher(SampleService::class.java) | |
@JvmStatic | |
fun start(context: Context) = LAUNCHER.startService(context) | |
@JvmStatic | |
fun stop(context: Context) = LAUNCHER.stopService(context) | |
} |
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
class ForegroundServiceLauncher(private val serviceClass: Class<out Service>) { | |
private var isStarting = false | |
private var shouldStop = false | |
@Synchronized | |
fun startService(context: Context, block: Intent.() -> Unit = {}) { | |
isStarting = true | |
shouldStop = false | |
ContextCompat.startForegroundService(context, Intent(context, serviceClass).apply { block() }) |
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
import android.arch.lifecycle.GenericLifecycleObserver | |
import android.arch.lifecycle.Lifecycle | |
import android.arch.lifecycle.Lifecycle.Event.ON_DESTROY | |
import android.arch.lifecycle.LifecycleOwner | |
import kotlinx.coroutines.experimental.CoroutineScope | |
import kotlinx.coroutines.experimental.Dispatchers | |
import kotlinx.coroutines.experimental.Job | |
import kotlinx.coroutines.experimental.android.Main | |
fun Lifecycle.createJob(cancelEvent: Lifecycle.Event = ON_DESTROY): Job { |
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
import android.app.Activity | |
import android.support.annotation.IdRes | |
import android.view.View | |
fun <T : View> Activity.bind(@IdRes idRes: Int): Lazy<T> { | |
@Suppress("UNCHECKED_CAST") | |
return unsafeLazy { findViewById(idRes) as T } | |
} | |
fun <T : View> View.bind(@IdRes idRes: Int): Lazy<T> { |