Skip to content

Commit 83b5473

Browse files
committed
Add Sections query
1 parent af56096 commit 83b5473

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
query Sections($id: Int!) {
2+
chapter(id: $id) {
3+
sections {
4+
number
5+
title
6+
}
7+
}
8+
}

app/src/main/java/guide/graphql/toc/ui/sections/SectionsAdapter.kt

+20-3
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,40 @@ import android.view.LayoutInflater
55
import android.view.ViewGroup
66
import androidx.recyclerview.widget.RecyclerView
77
import guide.graphql.toc.R
8+
import guide.graphql.toc.SectionsQuery
89
import guide.graphql.toc.databinding.SectionBinding
910

1011
class SectionsAdapter(
1112
private val context: Context,
12-
private val chapterNumber: Int
13+
private val chapterNumber: Int,
14+
private var sections: List<SectionsQuery.Section> = listOf()
1315
) : RecyclerView.Adapter<SectionsAdapter.ViewHolder>() {
1416

17+
fun updateSections(sections: List<SectionsQuery.Section>) {
18+
this.sections = sections
19+
notifyDataSetChanged()
20+
}
21+
1522
class ViewHolder(val binding: SectionBinding) : RecyclerView.ViewHolder(binding.root)
1623

1724
override fun getItemCount(): Int {
18-
return 0
25+
return sections.size
1926
}
2027

2128
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
2229
val binding = SectionBinding.inflate(LayoutInflater.from(parent.context), parent, false)
2330
return ViewHolder(binding)
2431
}
2532

26-
override fun onBindViewHolder(holder: ViewHolder, position: Int) {}
33+
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
34+
val section = sections[position]
35+
section.let {
36+
holder.binding.sectionTitle.text = context.getString(
37+
R.string.section_title,
38+
chapterNumber.toString(),
39+
section.number.toString(),
40+
section.title
41+
)
42+
}
43+
}
2744
}

app/src/main/java/guide/graphql/toc/ui/sections/SectionsFragment.kt

+32-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
77
import androidx.fragment.app.Fragment
8+
import androidx.lifecycle.lifecycleScope
89
import androidx.navigation.fragment.navArgs
910
import androidx.recyclerview.widget.DividerItemDecoration
1011
import androidx.recyclerview.widget.LinearLayoutManager
12+
import com.apollographql.apollo.coroutines.toDeferred
13+
import com.apollographql.apollo.exception.ApolloException
1114
import com.google.android.material.transition.MaterialSharedAxis
15+
import guide.graphql.toc.SectionsQuery
16+
import guide.graphql.toc.data.Apollo
1217
import guide.graphql.toc.databinding.SectionsFragmentBinding
1318

1419
class SectionsFragment : Fragment() {
@@ -52,7 +57,33 @@ class SectionsFragment : Fragment() {
5257
binding.sections.addItemDecoration(itemDivider)
5358
binding.sections.adapter = adapter
5459

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+
}
5687
}
5788

5889
override fun onDestroyView() {

0 commit comments

Comments
 (0)