@@ -5,10 +5,15 @@ import android.view.LayoutInflater
5
5
import android.view.View
6
6
import android.view.ViewGroup
7
7
import androidx.fragment.app.Fragment
8
+ import androidx.lifecycle.lifecycleScope
8
9
import androidx.navigation.fragment.navArgs
9
10
import androidx.recyclerview.widget.DividerItemDecoration
10
11
import androidx.recyclerview.widget.LinearLayoutManager
12
+ import com.apollographql.apollo.coroutines.toDeferred
13
+ import com.apollographql.apollo.exception.ApolloException
11
14
import com.google.android.material.transition.MaterialSharedAxis
15
+ import guide.graphql.toc.SectionsQuery
16
+ import guide.graphql.toc.data.Apollo
12
17
import guide.graphql.toc.databinding.SectionsFragmentBinding
13
18
14
19
class SectionsFragment : Fragment () {
@@ -52,7 +57,33 @@ class SectionsFragment : Fragment() {
52
57
binding.sections.addItemDecoration(itemDivider)
53
58
binding.sections.adapter = adapter
54
59
55
- showErrorMessage(" No sections" )
60
+ lifecycleScope.launchWhenStarted {
61
+ binding.spinner.visibility = View .VISIBLE
62
+ binding.error.visibility = View .GONE
63
+ try {
64
+ val response = Apollo .client.query(
65
+ SectionsQuery (id = args.chapterId)
66
+ ).toDeferred().await()
67
+
68
+ if (response.hasErrors()) {
69
+ throw Exception (" Response has errors" )
70
+ }
71
+
72
+ val sections = response.data?.chapter?.sections ? : throw Exception (" Data is null" )
73
+
74
+ if (sections.size > 1 ) {
75
+ adapter.updateSections(sections)
76
+ binding.spinner.visibility = View .GONE
77
+ binding.error.visibility = View .GONE
78
+ } else {
79
+ throw Exception (" No sections" )
80
+ }
81
+ } catch (e: ApolloException ) {
82
+ showErrorMessage(" GraphQL request failed" )
83
+ } catch (e: Exception ) {
84
+ showErrorMessage(e.message.orEmpty())
85
+ }
86
+ }
56
87
}
57
88
58
89
override fun onDestroyView () {
0 commit comments