Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adrielcafe/AndroidCoroutineScopes
This lib implements the most common CoroutineScopes used in Android apps.
https://github.com/adrielcafe/AndroidCoroutineScopes
android android-library boilerplate coroutines kotlin kotlin-android kotlin-coroutines
Last synced: 3 months ago
JSON representation
This lib implements the most common CoroutineScopes used in Android apps.
- Host: GitHub
- URL: https://github.com/adrielcafe/AndroidCoroutineScopes
- Owner: adrielcafe
- License: mit
- Created: 2018-09-15T18:13:08.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-25T12:04:46.000Z (almost 5 years ago)
- Last Synced: 2024-08-01T01:30:55.254Z (6 months ago)
- Topics: android, android-library, boilerplate, coroutines, kotlin, kotlin-android, kotlin-coroutines
- Language: Kotlin
- Homepage:
- Size: 134 KB
- Stars: 17
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
- awesome-kotlin - AndroidCoroutineScopes - This lib implements the most common CoroutineScopes used in Android apps. (Libraries)
README
[![Version](https://jitpack.io/v/adrielcafe/AndroidCoroutineScopes.svg)](https://jitpack.io/#adrielcafe/AndroidCoroutineScopes)
[![Min API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat)]()
[![Android Arsenal]( https://img.shields.io/badge/Android%20Arsenal-AndroidCoroutineScopes-green.svg?style=flat )]( https://android-arsenal.com/details/1/7139 )# AndroidCoroutineScopes
Starting from [0.26.0 release](https://github.com/Kotlin/kotlinx.coroutines/releases/tag/0.26.0) we should define a scope for new coroutines ([docs](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/)).
To avoid this boilerplate code I've created this small library that implements the most common `CoroutineScope`s used in Android apps.
## How to use
Just extend your Activity/Fragment/ViewModel with the corresponding implementation.
```kotlin
// AppCompat scopes
class MyActivity : CoroutineScopedActivity()class MyFragment : CoroutineScopedFragment()
class MyDialogFragment : CoroutineScopedDialogFragment()
// ViewModel scopes
class MyViewModel : CoroutineScopedViewModel()class MyAndroidViewModel(app: Application) : CoroutineScopedAndroidViewModel(app)
```### Example
```kotlin
class MainActivity : CoroutineScopedActivity() {override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)// Don't use launch(UI), it's deprecated now
launch {
Log.d(TAG, "MAIN THREAD")
withContext(Dispatchers.Default){
Log.d(TAG, "OTHER THREAD")
}
}
}}
```## Import to your project
```gradle
// Add JitPack to your repositories if needed
repositories {
maven { url 'https://jitpack.io' }
}// AppCompat scopes
implementation 'com.github.adrielcafe.androidcoroutinescopes:appcompat:1.0.1'// ViewModel scopes
implementation 'com.github.adrielcafe.androidcoroutinescopes:viewmodel:1.0.1'
```