Skip to content

Commit af2bcdd

Browse files
committed
Remove data fetching
1 parent a730e97 commit af2bcdd

File tree

10 files changed

+15
-201
lines changed

10 files changed

+15
-201
lines changed

README.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

app/src/main/graphql/guide/graphql/toc/Chapters.graphql

Lines changed: 0 additions & 7 deletions
This file was deleted.

app/src/main/graphql/guide/graphql/toc/Sections.graphql

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/src/main/graphql/guide/graphql/toc/schema.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/src/main/java/guide/graphql/toc/data/Apollo.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

app/src/main/java/guide/graphql/toc/data/Resource.kt

Lines changed: 0 additions & 39 deletions
This file was deleted.

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

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

33
import android.content.Context
44
import android.view.LayoutInflater
5-
import android.view.View
65
import android.view.ViewGroup
76
import androidx.recyclerview.widget.RecyclerView
8-
import guide.graphql.toc.ChaptersQuery
9-
import guide.graphql.toc.R
107
import guide.graphql.toc.databinding.ChapterBinding
118

129
class ChaptersAdapter(
1310
private val context: Context,
14-
private var chapters: List<ChaptersQuery.Chapter> = listOf(),
15-
private val onItemClicked: ((ChaptersQuery.Chapter) -> Unit)
11+
private var chapters: List<String> = listOf(),
12+
private val onItemClicked: ((String) -> Unit)
1613
) :
1714
RecyclerView.Adapter<ChaptersAdapter.ViewHolder>() {
1815

1916
class ViewHolder(val binding: ChapterBinding) : RecyclerView.ViewHolder(binding.root)
2017

21-
fun updateChapters(chapters: List<ChaptersQuery.Chapter>) {
18+
fun updateChapters(chapters: List<String>) {
2219
this.chapters = chapters
2320
notifyDataSetChanged()
2421
}
@@ -34,20 +31,8 @@ class ChaptersAdapter(
3431

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

5237
holder.binding.root.setOnClickListener {
5338
onItemClicked.invoke(chapter)

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

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,17 @@ 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
109
import androidx.navigation.fragment.findNavController
1110
import androidx.recyclerview.widget.DividerItemDecoration
1211
import androidx.recyclerview.widget.LinearLayoutManager
13-
import com.apollographql.apollo.coroutines.toDeferred
14-
import com.apollographql.apollo.exception.ApolloException
1512
import com.google.android.material.transition.MaterialSharedAxis
16-
import guide.graphql.toc.ChaptersQuery
1713
import guide.graphql.toc.R
18-
import guide.graphql.toc.data.apolloClient
1914
import guide.graphql.toc.databinding.ChaptersFragmentBinding
2015

2116
class ChaptersFragment : Fragment() {
2217
private var _binding: ChaptersFragmentBinding? = null
23-
// This property is only valid between onCreateView and
24-
// onDestroyView.
2518
private val binding get() = _binding!!
2619

27-
2820
override fun onCreateView(
2921
inflater: LayoutInflater,
3022
container: ViewGroup?,
@@ -53,12 +45,12 @@ class ChaptersFragment : Fragment() {
5345
) { chapter ->
5446
findNavController().navigate(
5547
ChaptersFragmentDirections.viewSections(
56-
chapterId = chapter.id,
57-
chapterNumber = chapter.number?.toInt() ?: -1,
58-
chapterTitle = if (chapter.number == null) chapter.title else getString(
48+
chapterId = 10,
49+
chapterNumber = 10,
50+
chapterTitle = getString(
5951
R.string.chapter_title,
60-
chapter.number.toInt().toString(),
61-
chapter.title
52+
"10",
53+
"Android Dev"
6254
)
6355
)
6456
)
@@ -71,22 +63,7 @@ class ChaptersFragment : Fragment() {
7163
binding.chapters.addItemDecoration(itemDivider)
7264
binding.chapters.adapter = adapter
7365

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

9269
override fun onDestroyView() {

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,24 @@ 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
98
import guide.graphql.toc.databinding.SectionBinding
109

1110
class SectionsAdapter(
1211
private val context: Context,
13-
private val chapterNumber: Int,
14-
private var sections: List<SectionsQuery.Section?> = listOf()
12+
private val chapterNumber: Int
1513
) :
1614
RecyclerView.Adapter<SectionsAdapter.ViewHolder>() {
1715

18-
fun updateSections(sections: List<SectionsQuery.Section?>) {
19-
this.sections = sections
20-
notifyDataSetChanged()
21-
}
22-
2316
class ViewHolder(val binding: SectionBinding) : RecyclerView.ViewHolder(binding.root)
2417

2518
override fun getItemCount(): Int {
26-
return sections.size
19+
return 0
2720
}
2821

2922
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
3023
val binding = SectionBinding.inflate(LayoutInflater.from(parent.context), parent, false)
3124
return ViewHolder(binding)
3225
}
3326

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

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,16 @@ 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
98
import androidx.navigation.fragment.navArgs
109
import androidx.recyclerview.widget.DividerItemDecoration
1110
import androidx.recyclerview.widget.LinearLayoutManager
12-
import com.apollographql.apollo.coroutines.toDeferred
13-
import com.apollographql.apollo.exception.ApolloException
1411
import com.google.android.material.transition.MaterialSharedAxis
15-
import guide.graphql.toc.SectionsQuery
16-
import guide.graphql.toc.data.apolloClient
1712
import guide.graphql.toc.databinding.SectionsFragmentBinding
1813

1914
class SectionsFragment : Fragment() {
20-
21-
2215
private var _binding: SectionsFragmentBinding? = null
23-
// This property is only valid between onCreateView and
24-
// onDestroyView.
2516
private val binding get() = _binding!!
2617

27-
2818
private val args: SectionsFragmentArgs by navArgs()
2919

3020
override fun onCreateView(
@@ -62,33 +52,7 @@ class SectionsFragment : Fragment() {
6252
binding.sections.addItemDecoration(itemDivider)
6353
binding.sections.adapter = adapter
6454

65-
lifecycleScope.launchWhenStarted {
66-
binding.spinner.visibility = View.VISIBLE
67-
binding.error.visibility = View.GONE
68-
try {
69-
val response = apolloClient.query(
70-
SectionsQuery(id = args.chapterId)
71-
).toDeferred().await()
72-
73-
if (response.hasErrors()) {
74-
throw Exception("Response has errors")
75-
}
76-
77-
val sections = response.data?.chapter?.sections ?: throw Exception("Data is null")
78-
79-
if (sections.size > 1) {
80-
adapter.updateSections(sections)
81-
binding.spinner.visibility = View.GONE
82-
binding.error.visibility = View.GONE
83-
} else {
84-
throw Exception("No sections")
85-
}
86-
} catch (e: ApolloException) {
87-
showErrorMessage("GraphQL request failed")
88-
} catch (e: Exception) {
89-
showErrorMessage(e.message.orEmpty())
90-
}
91-
}
55+
showErrorMessage("No sections")
9256
}
9357

9458
override fun onDestroyView() {

0 commit comments

Comments
 (0)