toast when instance unreachable

This commit is contained in:
Bnyro 2022-09-15 15:41:01 +02:00
parent efd95d95c3
commit 7c0b99f333
3 changed files with 15 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
@ -51,6 +52,11 @@ class SubscriptionsFragment : BaseFragment() {
if (viewModel.videoFeed.value == null) viewModel.fetchFeed() if (viewModel.videoFeed.value == null) viewModel.fetchFeed()
// listen for error responses
viewModel.errorResponse.observe(viewLifecycleOwner) {
if (it) Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
}
viewModel.videoFeed.observe(viewLifecycleOwner) { viewModel.videoFeed.observe(viewLifecycleOwner) {
if (!isShowingFeed()) return@observe if (!isShowingFeed()) return@observe
if (it == null) return@observe if (it == null) return@observe

View File

@ -13,10 +13,12 @@ import com.github.libretube.util.PreferenceHelper
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
class SubscriptionsViewModel : ViewModel() { class SubscriptionsViewModel : ViewModel() {
var errorResponse = MutableLiveData<Boolean>().apply {
value = false
}
var videoFeed = MutableLiveData<List<StreamItem>?>().apply { var videoFeed = MutableLiveData<List<StreamItem>?>().apply {
value = null value = null
} }
@ -37,12 +39,9 @@ class SubscriptionsViewModel : ViewModel() {
SubscriptionHelper.getFormattedLocalSubscriptions() SubscriptionHelper.getFormattedLocalSubscriptions()
) )
} }
} catch (e: IOException) { } catch (e: Exception) {
errorResponse.postValue(true)
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
Log.e(TAG(), "IOException, you might not have internet connection")
return@launch
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
return@launch return@launch
} }
this@SubscriptionsViewModel.videoFeed.postValue(videoFeed) this@SubscriptionsViewModel.videoFeed.postValue(videoFeed)
@ -65,12 +64,9 @@ class SubscriptionsViewModel : ViewModel() {
SubscriptionHelper.getFormattedLocalSubscriptions() SubscriptionHelper.getFormattedLocalSubscriptions()
) )
} }
} catch (e: IOException) { } catch (e: Exception) {
errorResponse.postValue(true)
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
Log.e(TAG(), "IOException, you might not have internet connection")
return@launch
} catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response")
return@launch return@launch
} }
this@SubscriptionsViewModel.subscriptions.postValue(subscriptions) this@SubscriptionsViewModel.subscriptions.postValue(subscriptions)

View File

@ -201,7 +201,7 @@ class BackgroundMode : Service() {
* Listens for changed playbackStates (e.g. pause, end) * Listens for changed playbackStates (e.g. pause, end)
* Plays the next video when the current one ended * Plays the next video when the current one ended
*/ */
player!!.addListener(object : Player.Listener { player?.addListener(object : Player.Listener {
override fun onPlaybackStateChanged(@Player.State state: Int) { override fun onPlaybackStateChanged(@Player.State state: Int) {
when (state) { when (state) {
Player.STATE_ENDED -> { Player.STATE_ENDED -> {