From 7c0b99f33386ad1220322eda59f7efffe0ce2ddb Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 15 Sep 2022 15:41:01 +0200 Subject: [PATCH] toast when instance unreachable --- .../fragments/SubscriptionsFragment.kt | 6 ++++++ .../models/SubscriptionsViewModel.kt | 20 ++++++++----------- .../libretube/services/BackgroundMode.kt | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/github/libretube/fragments/SubscriptionsFragment.kt b/app/src/main/java/com/github/libretube/fragments/SubscriptionsFragment.kt index 3018664c3..fe89aa229 100644 --- a/app/src/main/java/com/github/libretube/fragments/SubscriptionsFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/SubscriptionsFragment.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Toast import androidx.core.view.isVisible import androidx.fragment.app.activityViewModels import androidx.recyclerview.widget.GridLayoutManager @@ -51,6 +52,11 @@ class SubscriptionsFragment : BaseFragment() { 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) { if (!isShowingFeed()) return@observe if (it == null) return@observe diff --git a/app/src/main/java/com/github/libretube/models/SubscriptionsViewModel.kt b/app/src/main/java/com/github/libretube/models/SubscriptionsViewModel.kt index 4640b2a03..7cd7213b7 100644 --- a/app/src/main/java/com/github/libretube/models/SubscriptionsViewModel.kt +++ b/app/src/main/java/com/github/libretube/models/SubscriptionsViewModel.kt @@ -13,10 +13,12 @@ import com.github.libretube.util.PreferenceHelper import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import retrofit2.HttpException -import java.io.IOException class SubscriptionsViewModel : ViewModel() { + var errorResponse = MutableLiveData().apply { + value = false + } + var videoFeed = MutableLiveData?>().apply { value = null } @@ -37,12 +39,9 @@ class SubscriptionsViewModel : ViewModel() { SubscriptionHelper.getFormattedLocalSubscriptions() ) } - } catch (e: IOException) { + } catch (e: Exception) { + errorResponse.postValue(true) 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 } this@SubscriptionsViewModel.videoFeed.postValue(videoFeed) @@ -65,12 +64,9 @@ class SubscriptionsViewModel : ViewModel() { SubscriptionHelper.getFormattedLocalSubscriptions() ) } - } catch (e: IOException) { + } catch (e: Exception) { + errorResponse.postValue(true) 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 } this@SubscriptionsViewModel.subscriptions.postValue(subscriptions) diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index c48d59a78..4fc253363 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -201,7 +201,7 @@ class BackgroundMode : Service() { * Listens for changed playbackStates (e.g. pause, end) * 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) { when (state) { Player.STATE_ENDED -> {