Skip to content

Commit

Permalink
Put LiveData loads on the right context
Browse files Browse the repository at this point in the history
  • Loading branch information
ccomeaux committed Jan 10, 2024
1 parent 05f118f commit 8192da2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.boardgamegeek.model.Person
import com.boardgamegeek.extensions.*
import com.boardgamegeek.repository.ArtistRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
Expand Down Expand Up @@ -42,7 +43,7 @@ class ArtistsViewModel @Inject constructor(
}

val artists = _sort.switchMap {
liveData {
liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
sort.value?.let {
val sort = when(it) {
SortType.NAME -> ArtistRepository.SortType.NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import androidx.lifecycle.*
import com.boardgamegeek.repository.CategoryRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject

@HiltViewModel
Expand All @@ -25,7 +26,7 @@ class CategoriesViewModel @Inject constructor(
}

val categories = sort.switchMap {
liveData {
liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
sort.value?.let {
val sort = when (it) {
SortType.NAME -> CategoryRepository.SortType.NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.boardgamegeek.model.Person
import com.boardgamegeek.extensions.*
import com.boardgamegeek.repository.DesignerRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
Expand Down Expand Up @@ -42,7 +43,7 @@ class DesignersViewModel @Inject constructor(
}

val designers = _sort.switchMap {
liveData {
liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
sort.value?.let {
val sort = when(it) {
SortType.NAME -> DesignerRepository.SortType.NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import androidx.lifecycle.*
import com.boardgamegeek.repository.MechanicRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject

@HiltViewModel
Expand All @@ -25,7 +26,7 @@ class MechanicsViewModel @Inject constructor(
}

val mechanics = sort.switchMap {
liveData {
liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
sort.value?.let {
val sort = when (it) {
SortType.NAME -> MechanicRepository.SortType.NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.boardgamegeek.repository.ArtistRepository
import com.boardgamegeek.repository.DesignerRepository
import com.boardgamegeek.repository.PublisherRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand Down Expand Up @@ -74,7 +75,7 @@ class PersonViewModel @Inject constructor(
}

val details = _personInfo.switchMap { person ->
liveData {
liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
when (person.id) {
BggContract.INVALID_ID -> emit(RefreshableResource.success(null))
else -> {
Expand Down Expand Up @@ -134,7 +135,7 @@ class PersonViewModel @Inject constructor(
val collectionSort: LiveData<CollectionSort> = _personInfo.map { it.sort }

val collection = _personInfo.switchMap { person ->
liveData {
liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
emitSource(
when (person.type) {
PersonType.ARTIST -> artistRepository.loadCollection(person.id, if (person.sort == CollectionSort.RATING) ArtistRepository.CollectionSortType.RATING else ArtistRepository.CollectionSortType.NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.boardgamegeek.model.Company
import com.boardgamegeek.extensions.*
import com.boardgamegeek.repository.PublisherRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
Expand Down Expand Up @@ -42,7 +43,7 @@ class PublishersViewModel @Inject constructor(
}

val publishers = _sort.switchMap {
liveData {
liveData(viewModelScope.coroutineContext + Dispatchers.IO) {
sort.value?.let {
val sort = when(it) {
SortType.NAME -> PublisherRepository.SortType.NAME
Expand Down

0 comments on commit 8192da2

Please sign in to comment.