Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 5fa93e0

Browse files
committed
[ADDED] Error handling for the resources section
1 parent 906d80d commit 5fa93e0

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2019 Hossain Khan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hossainkhan.android.demo.data
18+
19+
20+
/**
21+
* A class to represent external resource request with data or error on fail.
22+
*/
23+
class ResourceResult(
24+
val resources: List<ResourceInfo> = emptyList(),
25+
val error: Exception? = null
26+
)

app/src/main/java/com/hossainkhan/android/demo/ui/resource/LearningResourceActivity.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import android.content.Intent
2121
import android.net.Uri
2222
import android.os.Bundle
2323
import androidx.appcompat.app.AppCompatActivity
24+
import androidx.constraintlayout.widget.ConstraintLayout
2425
import androidx.databinding.DataBindingUtil
2526
import androidx.lifecycle.Observer
2627
import androidx.lifecycle.ViewModelProvider
2728
import androidx.recyclerview.widget.LinearLayoutManager
2829
import androidx.recyclerview.widget.RecyclerView
30+
import com.google.android.material.snackbar.Snackbar
2931
import com.hossainkhan.android.demo.R
3032
import com.hossainkhan.android.demo.data.ResourceInfo
3133
import com.hossainkhan.android.demo.databinding.ActivityLearningResourceBinding
@@ -34,6 +36,9 @@ import dagger.android.AndroidInjection
3436
import timber.log.Timber
3537
import javax.inject.Inject
3638

39+
/**
40+
* This activity lists external resources on [ConstraintLayout], such as tech talks on youtube.
41+
*/
3742
class LearningResourceActivity : AppCompatActivity() {
3843

3944
@Inject
@@ -69,7 +74,13 @@ class LearningResourceActivity : AppCompatActivity() {
6974
}
7075

7176
viewModel.data.observe(this, Observer { result ->
72-
ideaListAdapter.submitList(result)
77+
if (result?.error != null) {
78+
Timber.w(result.error, "Unable to load resources.")
79+
Snackbar.make(binding.root, R.string.message_resource_load_failed, Snackbar.LENGTH_INDEFINITE).show()
80+
return@Observer
81+
} else {
82+
ideaListAdapter.submitList(result.resources)
83+
}
7384
})
7485
}
7586

app/src/main/java/com/hossainkhan/android/demo/ui/resource/LearningResourceViewModel.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import com.google.firebase.firestore.FirebaseFirestore
2424
import com.google.firebase.firestore.Query
2525
import com.google.firebase.firestore.QuerySnapshot
2626
import com.hossainkhan.android.demo.data.ResourceInfo
27+
import com.hossainkhan.android.demo.data.ResourceResult
2728
import timber.log.Timber
28-
import java.lang.Exception
2929
import javax.inject.Inject
30+
import kotlin.Exception
3031

3132
class LearningResourceViewModel @Inject constructor(
3233
firestore: FirebaseFirestore
@@ -37,8 +38,8 @@ class LearningResourceViewModel @Inject constructor(
3738

3839
val isLoading = ObservableField(true)
3940

40-
private val _data = MutableLiveData<List<ResourceInfo>>()
41-
val data: LiveData<List<ResourceInfo>> = _data
41+
private val _data = MutableLiveData<ResourceResult>()
42+
val data: LiveData<ResourceResult> = _data
4243

4344
private val resourceData = mutableListOf<ResourceInfo>()
4445

@@ -49,6 +50,12 @@ class LearningResourceViewModel @Inject constructor(
4950
.get()
5051
.addOnSuccessListener(this::updateResources)
5152
.addOnFailureListener(this::onLoadFailed)
53+
.addOnCompleteListener {
54+
if (it.result?.isEmpty == true) {
55+
Timber.i("Completed with no data. This is likely due to no internet.")
56+
onLoadFailed(Exception("No data"))
57+
}
58+
}
5259
}
5360

5461
private fun updateResources(result: QuerySnapshot) {
@@ -58,11 +65,12 @@ class LearningResourceViewModel @Inject constructor(
5865
resourceData.add(x)
5966
}
6067
isLoading.set(false)
61-
_data.value = resourceData
68+
_data.value = ResourceResult(resourceData)
6269
}
6370

6471
private fun onLoadFailed(exception: Exception) {
6572
Timber.w(exception, "Error getting documents.")
6673
isLoading.set(false)
74+
_data.value = ResourceResult(error = exception)
6775
}
6876
}

app/src/main/res/values/dimens.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
- https://developer.android.com/guide/topics/resources/providing-resources
2525
- https://developer.android.com/training/multiscreen/screensizes
2626
-->
27-
<resources>
27+
<resources xmlns:tools="http://schemas.android.com/tools">
28+
<!--
29+
Overrides snackbar text truncation.
30+
https://stackoverflow.com/questions/32228300/how-to-prevent-my-snackbar-text-from-being-truncated-on-android/32228529
31+
-->
32+
<integer tools:override="true" name="design_snackbar_text_max_lines">4</integer>
33+
2834
<dimen name="box_size_small">40dp</dimen>
2935
<dimen name="box_size_medium">80dp</dimen>
3036
<dimen name="box_size_large">120dp</dimen>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
<string name="additional_resource_title">External Resources</string>
2727
<string name="tech_talk_author_and_venue">by %1$s at %2$s</string>
2828
<string name="tech_talk_publish_date">Published on %1$s</string>
29-
29+
<string name="message_resource_load_failed">Unable to load external resources. This is likely due to no internet connectivity. Please try re-loading this screen with network connection.</string>
3030
</resources>

0 commit comments

Comments
 (0)