-
Notifications
You must be signed in to change notification settings - Fork 4
/
AuthorRepository.kt
31 lines (28 loc) · 1015 Bytes
/
AuthorRepository.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.example.architecture.data.repository
import androidx.annotation.MainThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.architecture.data.datasource.HardCodedDataSource
import com.example.architecture.data.model.Author
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
/**
* @see ArticleRepository for more detailed documentation.
*
* Application-scoped: lives as long as an application itself. Is not recreated on configuration
* changes or on navigation.
*/
class AuthorRepository(
private val dataSource: HardCodedDataSource,
) {
private val _authors = MutableLiveData<List<Author>>(emptyList())
val authors: LiveData<List<Author>> = _authors
/**
* Loads author list. Result can be observed through [authors].
*/
@MainThread
suspend fun updateAuthors() {
val loadedList = withContext(Dispatchers.IO) { dataSource.loadAuthors() }
_authors.value = loadedList
}
}