This file contains hidden or 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 MyViewModel : ViewModel() { | |
/** | |
* This is the job for all coroutines started by this ViewModel. | |
* Cancelling this job will cancel all coroutines started by this ViewModel. | |
*/ | |
private val viewModelJob = SupervisorJob() | |
/** | |
* This is the main scope for all coroutines launched by MainViewModel. |
This file contains hidden or 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 MyViewModel : ViewModel() { | |
/** | |
* Heavy operation that cannot be done in the Main Thread | |
*/ | |
fun launchDataLoad() { | |
viewModelScope.launch { | |
sortList() | |
// Modify UI | |
} |
This file contains hidden or 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
private const val JOB_KEY = "androidx.lifecycle.ViewModelCoroutineScope.JOB_KEY" | |
val ViewModel.viewModelScope: CoroutineScope | |
get() { | |
val scope: CoroutineScope? = this.getTag(JOB_KEY) | |
if (scope != null) { | |
return scope | |
} | |
return setTagIfAbsent(JOB_KEY, | |
CloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)) |
This file contains hidden or 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
@MainThread | |
final void clear() { | |
mCleared = true; | |
// Since clear() is final, this method is still called on mock | |
// objects and in those cases, mBagOfTags is null. It'll always | |
// be empty though because setTagIfAbsent and getTag are not | |
// final so we can skip clearing it | |
if (mBagOfTags != null) { | |
for (Object value : mBagOfTags.values()) { | |
// see comment for the similar call in setTagIfAbsent |
This file contains hidden or 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
internal class CloseableCoroutineScope( | |
context: CoroutineContext | |
) : Closeable, CoroutineScope { | |
override val coroutineContext: CoroutineContext = context | |
override fun close() { | |
coroutineContext.cancel() | |
} | |
} |
This file contains hidden or 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
@ExperimentalCoroutinesApi | |
class CoroutinesTestRule( | |
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() | |
) : TestWatcher() { | |
override fun starting(description: Description?) { | |
super.starting(description) | |
Dispatchers.setMain(testDispatcher) | |
} |
This file contains hidden or 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 MainViewModelUnitTest { | |
@get:Rule | |
var coroutinesTestRule = CoroutinesTestRule() | |
@Test | |
fun test() { | |
... | |
} | |
} |
This file contains hidden or 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 MyViewModel(dependency: ExternalDependency) : ViewModel() { | |
fun run() { | |
viewModelScope.launch { | |
dependency.doStuff() | |
} | |
} | |
} | |
class MyViewModelTest { |
This file contains hidden or 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
diff --git a/generate/src/main/resources/i18Languages.xsd b/generate/src/main/resources/i18Languages.xsd | |
index 4e3fc1b..6825a20 100644 | |
--- a/generate/src/main/resources/i18Languages.xsd | |
+++ b/generate/src/main/resources/i18Languages.xsd | |
@@ -134,6 +134,7 @@ | |
<xsd:enumeration value="de-lu"/> | |
<xsd:enumeration value="del"/> | |
+ <xsd:enumeration value="dheading"/> | |
<xsd:enumeration value="den"/> |
This file contains hidden or 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 MainViewModel(private val dependency: Any): ViewModel { | |
fun sampleMethod() { | |
viewModelScope.launch { | |
val hashCode = dependency.hashCode() | |
// TODO: do something with hashCode | |
} | |
} | |
class MainViewModelUnitTest { |
OlderNewer