Use viewModelScope extension.

This commit is contained in:
Isira Seneviratne 2023-02-19 19:52:37 +05:30
parent 3171c82dbc
commit d1fd030ab0

View File

@ -3,16 +3,15 @@ package com.github.libretube.ui.models
import android.util.Log import android.util.Log
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.CommentsPage import com.github.libretube.api.obj.CommentsPage
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.ui.extensions.filterNonEmptyComments import com.github.libretube.ui.extensions.filterNonEmptyComments
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class CommentsViewModel : ViewModel() { class CommentsViewModel : ViewModel() {
private val scope = CoroutineScope(Dispatchers.IO)
private var isLoading = false private var isLoading = false
val commentsPage = MutableLiveData<CommentsPage?>() val commentsPage = MutableLiveData<CommentsPage?>()
@ -25,7 +24,7 @@ class CommentsViewModel : ViewModel() {
fun fetchComments() { fun fetchComments() {
videoId ?: return videoId ?: return
scope.launch { viewModelScope.launch(Dispatchers.IO) {
isLoading = true isLoading = true
val response = try { val response = try {
RetrofitInstance.api.getComments(videoId!!) RetrofitInstance.api.getComments(videoId!!)
@ -42,7 +41,7 @@ class CommentsViewModel : ViewModel() {
fun fetchNextComments() { fun fetchNextComments() {
if (isLoading || nextPage == null || videoId == null) return if (isLoading || nextPage == null || videoId == null) return
scope.launch { viewModelScope.launch(Dispatchers.IO) {
isLoading = true isLoading = true
val response = try { val response = try {
RetrofitInstance.api.getCommentsNextPage(videoId!!, nextPage!!) RetrofitInstance.api.getCommentsNextPage(videoId!!, nextPage!!)