-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from shiki/issue/8627-giphy-picker-media-lib-im…
…port Import from Giphy in Media Library
- Loading branch information
Showing
15 changed files
with
559 additions
and
45 deletions.
There are no files selected for viewing
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
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
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
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
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
40 changes: 40 additions & 0 deletions
40
WordPress/src/main/java/org/wordpress/android/viewmodel/giphy/CoroutineScopedViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.wordpress.android.viewmodel.giphy | ||
|
||
import android.arch.lifecycle.ViewModel | ||
import kotlinx.coroutines.experimental.CoroutineScope | ||
import kotlinx.coroutines.experimental.Dispatchers | ||
import kotlinx.coroutines.experimental.Job | ||
import kotlin.coroutines.experimental.CoroutineContext | ||
|
||
/** | ||
* A base class for implementing Android [ViewModel] classes that also act as a [CoroutineScope]. | ||
* | ||
* One advantage of using this is that coroutine builders like [async] or [launch] will become children of the parent | ||
* [coroutineJob] declared in this [ViewModel]. If this [ViewModel] is cleared from memory, all the active child | ||
* coroutines will be automatically cancelled. | ||
* | ||
* This strategy is called _Structured Concurrency_. Learn more about it here: | ||
* | ||
* - [Structured concurrency, lifecycle and coroutine parent-child hierarchy](https://goo.gl/rH3rmt) | ||
* - [KotlinConf 2018 - Kotlin Coroutines in Practice by Roman Elizarov](https://www.youtube.com/watch?v=a3agLJQ6vt8) | ||
*/ | ||
abstract class CoroutineScopedViewModel : ViewModel(), CoroutineScope { | ||
/** | ||
* The default parent [CoroutineContext] of all coroutine builders under this [ViewModel] ([CoroutineScope]). | ||
*/ | ||
protected val coroutineJob = Job() | ||
|
||
/** | ||
* Sets it up so that all coroutine builders like [async] and [launch] will become children of [coroutineJob] | ||
* and use the background thread dispatcher defined by [Dispatchers.Default]. | ||
*/ | ||
override val coroutineContext: CoroutineContext get() = coroutineJob + Dispatchers.Default | ||
|
||
/** | ||
* Cancel all active child coroutines. | ||
*/ | ||
override fun onCleared() { | ||
super.onCleared() | ||
coroutineJob.cancel() | ||
} | ||
} |
Oops, something went wrong.