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+
queryChapters{
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+
packageguide.graphql.toc.data
2+
3+
importcom.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
importandroid.content.Context
44
importandroid.view.LayoutInflater
5+
importandroid.view.View
56
importandroid.view.ViewGroup
67
importandroidx.recyclerview.widget.RecyclerView
8+
importguide.graphql.toc.ChaptersQuery
9+
importguide.graphql.toc.R
710
importguide.graphql.toc.databinding.ChapterBinding
811

912
classChaptersAdapter(
1013
privatevalcontext:Context,
11-
privatevarchapters:List<String> = listOf(),
12-
privatevalonItemClicked: ((String) ->Unit)
14+
privatevarchapters:List<ChaptersQuery.Chapter> = listOf(),
15+
privatevalonItemClicked: ((ChaptersQuery.Chapter) ->Unit)
1316
) : RecyclerView.Adapter<ChaptersAdapter.ViewHolder>(){
1417

1518
classViewHolder(valbinding:ChapterBinding) : RecyclerView.ViewHolder(binding.root)
1619

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

3134
overridefunonBindViewHolder(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
importandroid.view.ViewGroup
77
importandroid.widget.Toast
88
importandroidx.fragment.app.Fragment
9+
importandroidx.lifecycle.lifecycleScope
910
importandroidx.navigation.fragment.findNavController
1011
importandroidx.recyclerview.widget.DividerItemDecoration
1112
importandroidx.recyclerview.widget.LinearLayoutManager
13+
importcom.apollographql.apollo.coroutines.toDeferred
14+
importcom.apollographql.apollo.exception.ApolloException
1215
importcom.google.android.material.transition.MaterialSharedAxis
16+
importguide.graphql.toc.ChaptersQuery
1317
importguide.graphql.toc.R
18+
importguide.graphql.toc.data.Apollo
1419
importguide.graphql.toc.databinding.ChaptersFragmentBinding
1520

1621
classChaptersFragment : 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 elsegetString(
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+
throwException(response.errors?.get(0)?.message)
79+
}
80+
81+
val chapters = response.data?.chapters ?:throwException("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
overridefunonDestroyView(){

0 commit comments

Comments
(0)