Skip to content

Instantly share code, notes, and snippets.

View ThomasGorisse's full-sized avatar

Thomas Gorisse ThomasGorisse

View GitHub Profile
view.doOnResume {
startWelcomeAnimation(view)
}
view.doOnPause {
stopWelcomeAnimation(view)
}
val lifecycle = findViewById(R.id.frameLayout).lifecycle
// Or
val imageView = ImageView(context)
imageView.lifecycleScope.launchWhenCreated {
(imageView.parent as View).isVisible = true
loadSlideShow(imageView.width, imageView.height)
}
textView.lifecycleScope.launchWhenCreated {
while (isActive) {
view.text = "${System.currentTimeMillis()}" // Dispatchers.Main
withContext(Dispatchers.IO) { // Dispatchers.IO for waiting in background
delay(1000) // Wait 1 second
}
}
}
texView.lifecycleScope.launchWhenCreated { // Launch on Dispatchers.Main
val data = withContext(Dispatchers.IO) { // Dispatchers.IO for background task
loadNetworkData() // Suspend function making http calls
}
texView.text = data // Dispatchers.Main for UI change
}
view.lifecycle.addObserver(onResume = {
refreshData()
})
dependencies {
implementation "com.gorisse.thomas:android-view-lifecycle:1.0.1"
}
val View.lifecycle get() = lifecycleOwner.lifecycle
val View.lifecycleScope get() = lifecycleOwner.lifecycleScope
val View.lifecycleOwner: LifecycleOwner
get() = getTag(R.id.view_lifecycle_owner) as? LifecycleOwner ?: object : LifecycleOwner,
LifecycleEventObserver {
private val lifecycle = LifecycleRegistry(this)
init {
doOnAttach {
findViewTreeLifecycleOwner()?.lifecycle?.addObserver(this)
}
doOnDetach {
@ThomasGorisse
ThomasGorisse / build.gradle
Last active October 27, 2021 09:17
Sceneform Maintained - Dependencies
dependencies {
implementation("com.gorisse.thomas.sceneform:sceneform:1.20.1")
}
@ThomasGorisse
ThomasGorisse / MainActivity.kt
Last active October 27, 2021 09:20
Sceneform Maintained - Edit your Activity or Fragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Load model.glb from assets folder or http url
(supportFragmentManager.findFragmentById(R.id.arFragment) as ArFragment)
.setOnTapPlaneGlbModel("model.glb")
}