Skip to content

Commit af56096

Browse files
committed
Add Chapters query
1 parent da3c146 commit af56096

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query Chapters {
2+
chapters {
3+
id
4+
number
5+
title
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package guide.graphql.toc.data
2+
3+
import com.apollographql.apollo.ApolloClient
4+
5+
object Apollo {
6+
val client: ApolloClient by lazy {
7+
ApolloClient.builder()
8+
.serverUrl("https://api.graphql.guide/graphql")
9+
.build()
10+
}
11+
}

app/src/main/java/guide/graphql/toc/ui/chapters/ChaptersAdapter.kt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@ package guide.graphql.toc.ui.chapters
22

33
import android.content.Context
44
import android.view.LayoutInflater
5+
import android.view.View
56
import android.view.ViewGroup
67
import androidx.recyclerview.widget.RecyclerView
8+
import guide.graphql.toc.ChaptersQuery
9+
import guide.graphql.toc.R
710
import guide.graphql.toc.databinding.ChapterBinding
811

912
class ChaptersAdapter(
1013
private val context: Context,
11-
private var chapters: List<String> = listOf(),
12-
private val onItemClicked: ((String) -> Unit)
14+
private var chapters: List<ChaptersQuery.Chapter> = listOf(),
15+
private val onItemClicked: ((ChaptersQuery.Chapter) -> Unit)
1316
) : RecyclerView.Adapter<ChaptersAdapter.ViewHolder>() {
1417

1518
class ViewHolder(val binding: ChapterBinding) : RecyclerView.ViewHolder(binding.root)
1619

17-
fun updateChapters(chapters: List<String>) {
20+
fun updateChapters(chapters: List<ChaptersQuery.Chapter>) {
1821
this.chapters = chapters
1922
notifyDataSetChanged()
2023
}
@@ -30,8 +33,20 @@ class ChaptersAdapter(
3033

3134
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
3235
val chapter = chapters[position]
33-
34-
holder.binding.chapterHeader.text = chapter
36+
val header =
37+
if (chapter.number == null) chapter.title else context.getString(
38+
R.string.chapter_number,
39+
chapter.number.toInt().toString()
40+
)
41+
42+
holder.binding.chapterHeader.text = header
43+
if (chapter.number == null) {
44+
holder.binding.chapterSubheader.visibility = View.GONE
45+
46+
} else {
47+
holder.binding.chapterSubheader.text = chapter.title
48+
holder.binding.chapterSubheader.visibility = View.VISIBLE
49+
}
3550

3651
holder.binding.root.setOnClickListener {
3752
onItemClicked.invoke(chapter)

app/src/main/java/guide/graphql/toc/ui/chapters/ChaptersFragment.kt

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import android.view.View
66
import android.view.ViewGroup
77
import android.widget.Toast
88
import androidx.fragment.app.Fragment
9+
import androidx.lifecycle.lifecycleScope
910
import androidx.navigation.fragment.findNavController
1011
import androidx.recyclerview.widget.DividerItemDecoration
1112
import androidx.recyclerview.widget.LinearLayoutManager
13+
import com.apollographql.apollo.coroutines.toDeferred
14+
import com.apollographql.apollo.exception.ApolloException
1215
import com.google.android.material.transition.MaterialSharedAxis
16+
import guide.graphql.toc.ChaptersQuery
1317
import guide.graphql.toc.R
18+
import guide.graphql.toc.data.Apollo
1419
import guide.graphql.toc.databinding.ChaptersFragmentBinding
1520

1621
class ChaptersFragment : Fragment() {
@@ -45,25 +50,42 @@ class ChaptersFragment : Fragment() {
4550
) { chapter ->
4651
findNavController().navigate(
4752
ChaptersFragmentDirections.viewSections(
48-
chapterId = 10,
49-
chapterNumber = 10,
50-
chapterTitle = getString(
53+
chapterId = chapter.id,
54+
chapterNumber = chapter.number?.toInt() ?: -1,
55+
chapterTitle = if (chapter.number == null) chapter.title else getString(
5156
R.string.chapter_title,
52-
"10",
53-
"Android Dev"
57+
chapter.number.toInt().toString(),
58+
chapter.title
5459
)
5560
)
5661
)
5762
}
5863

5964
val layoutManager = LinearLayoutManager(requireContext())
6065
binding.chapters.layoutManager = layoutManager
61-
66+
6267
val itemDivider = DividerItemDecoration(requireContext(), layoutManager.orientation)
6368
binding.chapters.addItemDecoration(itemDivider)
6469
binding.chapters.adapter = adapter
6570

66-
adapter.updateChapters(listOf("Android Dev"))
71+
lifecycleScope.launchWhenStarted {
72+
try {
73+
val response = Apollo.client.query(
74+
ChaptersQuery()
75+
).toDeferred().await()
76+
77+
if (response.hasErrors()) {
78+
throw Exception(response.errors?.get(0)?.message)
79+
}
80+
81+
val chapters = response.data?.chapters ?: throw Exception("Data is null")
82+
adapter.updateChapters(chapters)
83+
} catch (e: ApolloException) {
84+
showErrorMessage("GraphQL request failed")
85+
} catch (e: Exception) {
86+
showErrorMessage(e.message.orEmpty())
87+
}
88+
}
6789
}
6890

6991
override fun onDestroyView() {

0 commit comments

Comments
 (0)