Skip to content

Commit 128d052

Browse files
committed
Cleanup
1 parent b9df6cf commit 128d052

File tree

12 files changed

+69
-59
lines changed

12 files changed

+69
-59
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package guide.graphql.toc
22

3+
import android.content.Context
34
import android.view.LayoutInflater
45
import android.view.View
56
import android.view.ViewGroup
@@ -8,6 +9,7 @@ import guide.graphql.toc.databinding.ChapterBinding
89

910
class ChaptersAdapter(
1011
private val chapters: List<ChaptersQuery.Chapter>,
12+
private val context: Context,
1113
private val onItemClicked: ((ChaptersQuery.Chapter) -> Unit)
1214
) :
1315
RecyclerView.Adapter<ChaptersAdapter.ViewHolder>() {
@@ -26,7 +28,10 @@ class ChaptersAdapter(
2628
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
2729
val chapter = chapters[position]
2830
val header =
29-
if (chapter.number == null) chapter.title else "Chapter ${chapter.number.toInt()}"
31+
if (chapter.number == null) chapter.title else context.getString(
32+
R.string.chapter_number,
33+
chapter.number.toInt().toString()
34+
)
3035

3136
holder.binding.chapterHeader.text = header
3237
if (chapter.number == null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ChaptersFragment : Fragment() {
6161

6262
response.data?.chapters?.let { chapters ->
6363
val adapter =
64-
ChaptersAdapter(chapters) { chapter ->
64+
ChaptersAdapter(chapters, requireContext()) { chapter ->
6565
findNavController().navigate(
6666
ChaptersFragmentDirections.viewSections(
6767
chapterId = chapter.id

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ class SectionsAdapter(
2727
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
2828
val section = sections[position]
2929
section?.let {
30-
holder.binding.sectionTitle.text = context.getString(R.string.section_title, chapterNumber.toString(), section.number.toString(), section.title)
30+
holder.binding.sectionTitle.text = context.getString(
31+
R.string.section_title,
32+
chapterNumber.toString(),
33+
section.number.toString(),
34+
section.title
35+
)
3136
}
3237
}
3338
}

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,37 @@ class SectionsFragment : Fragment() {
6161
return@launchWhenResumed
6262
}
6363

64-
val chapter = response.data?.chapter
65-
if (chapter == null || response.hasErrors()) {
64+
if (response.hasErrors()) {
6665
showErrorMessage(response.errors?.get(0)?.message ?: getString(R.string.error))
6766
return@launchWhenResumed
6867
}
6968

70-
val chapterNumber = chapter.number?.toInt()
71-
binding.spinner.visibility = View.GONE
72-
binding.chapterHeader.title =
73-
if (chapter.number == null) chapter.title else getString(
74-
R.string.chapter_title,
75-
chapter.number.toString(),
76-
chapter.title
77-
)
78-
79-
if (chapter.sections.size > 1) {
80-
val adapter =
81-
SectionsAdapter(chapterNumber, chapter.sections, requireContext())
82-
val layoutManager = LinearLayoutManager(requireContext())
83-
binding.sections.layoutManager = layoutManager
84-
val itemDivider = DividerItemDecoration(requireContext(), layoutManager.orientation)
85-
binding.sections.addItemDecoration(itemDivider)
86-
binding.sections.adapter = adapter
87-
} else {
88-
binding.error.text = getString(R.string.no_section_error)
89-
binding.error.visibility = View.VISIBLE
69+
response.data?.chapter?.let { chapter ->
70+
val chapterNumber = chapter.number?.toInt()
71+
binding.spinner.visibility = View.GONE
72+
binding.chapterHeader.title =
73+
if (chapter.number == null) chapter.title else getString(
74+
R.string.chapter_title,
75+
chapter.number.toString(),
76+
chapter.title
77+
)
78+
79+
if (chapter.sections.size > 1) {
80+
val adapter =
81+
SectionsAdapter(chapterNumber, chapter.sections, requireContext())
82+
val layoutManager = LinearLayoutManager(requireContext())
83+
binding.sections.layoutManager = layoutManager
84+
val itemDivider =
85+
DividerItemDecoration(requireContext(), layoutManager.orientation)
86+
binding.sections.addItemDecoration(itemDivider)
87+
binding.sections.adapter = adapter
88+
} else {
89+
binding.error.text = getString(R.string.no_sections)
90+
binding.error.visibility = View.VISIBLE
91+
}
9092
}
93+
94+
9195
}
9296
}
9397

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/main_frame_layout"
56
android:layout_width="match_parent"
67
android:layout_height="match_parent"
7-
android:id="@+id/main_frame_layout"
88
tools:context=".MainActivity">
99

1010
<androidx.fragment.app.FragmentContainerView

app/src/main/res/layout/chapter.xml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:app="http://schemas.android.com/apk/res-auto"
5-
xmlns:tools="http://schemas.android.com/tools"
64
android:layout_width="match_parent"
75
android:layout_height="wrap_content"
6+
android:background="?selectableItemBackground"
87
android:paddingStart="20dp"
98
android:paddingTop="16dp"
10-
android:paddingBottom="16dp"
11-
android:background="?selectableItemBackground">
9+
android:paddingBottom="16dp">
1210

1311
<TextView
1412
android:id="@+id/chapter_header"
13+
style="@style/AppTheme.Heading"
1514
android:layout_width="0dp"
1615
android:layout_height="wrap_content"
17-
style="@style/AppTheme.Heading"
1816
app:layout_constraintBottom_toTopOf="@+id/chapter_subheader"
1917
app:layout_constraintEnd_toEndOf="parent"
2018
app:layout_constraintStart_toStartOf="parent"
@@ -23,8 +21,8 @@
2321

2422
<TextView
2523
android:id="@+id/chapter_subheader"
26-
android:layout_width="0dp"
2724
style="@style/AppTheme.Subheading"
25+
android:layout_width="0dp"
2826
android:layout_height="wrap_content"
2927
android:layout_marginTop="10dp"
3028
app:layout_constraintBottom_toBottomOf="parent"
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:layout_width="match_parent"
4-
android:layout_height="match_parent"
5-
xmlns:app="http://schemas.android.com/apk/res-auto">
6-
7-
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
86

97
<com.google.android.material.appbar.AppBarLayout
108
android:id="@+id/book_header_appbar"
@@ -14,15 +12,15 @@
1412
app:layout_constraintEnd_toEndOf="parent"
1513
app:layout_constraintStart_toStartOf="parent"
1614
app:layout_constraintTop_toTopOf="parent">
15+
1716
<androidx.appcompat.widget.Toolbar
18-
app:titleTextColor="?attr/colorOnPrimary"
1917
android:id="@+id/book_header"
20-
android:title="@string/app_name"
2118
android:layout_width="match_parent"
22-
android:layout_height="?attr/actionBarSize" />
19+
android:layout_height="?attr/actionBarSize"
20+
android:title="@string/app_name"
21+
app:titleTextColor="?attr/colorOnPrimary" />
2322
</com.google.android.material.appbar.AppBarLayout>
2423

25-
2624
<androidx.recyclerview.widget.RecyclerView
2725
android:id="@+id/chapters"
2826
android:layout_width="0dp"
@@ -31,4 +29,5 @@
3129
app:layout_constraintEnd_toEndOf="parent"
3230
app:layout_constraintStart_toStartOf="parent"
3331
app:layout_constraintTop_toBottomOf="@+id/book_header_appbar" />
32+
3433
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/section.xml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:app="http://schemas.android.com/apk/res-auto"
5-
xmlns:tools="http://schemas.android.com/tools"
64
android:layout_width="match_parent"
75
android:layout_height="wrap_content"
86
android:background="?selectableItemBackground"
9-
android:paddingTop="20dp"
10-
android:paddingBottom="20dp"
117
android:paddingStart="30dp"
12-
android:paddingEnd="30dp">
8+
android:paddingTop="20dp"
9+
android:paddingEnd="30dp"
10+
android:paddingBottom="20dp">
1311

1412
<TextView
1513
android:id="@+id/section_title"
14+
style="@style/AppTheme.Heading"
1615
android:layout_width="0dp"
1716
android:layout_height="wrap_content"
18-
style="@style/AppTheme.Heading"
1917
app:layout_constraintBottom_toBottomOf="parent"
2018
app:layout_constraintEnd_toEndOf="parent"
2119
app:layout_constraintStart_toStartOf="parent"

app/src/main/res/layout/sections_fragment.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
76
android:orientation="vertical">
@@ -14,11 +13,13 @@
1413
app:layout_constraintEnd_toEndOf="parent"
1514
app:layout_constraintStart_toStartOf="parent"
1615
app:layout_constraintTop_toTopOf="parent">
16+
1717
<androidx.appcompat.widget.Toolbar
18-
app:titleTextColor="?attr/colorOnPrimary"
1918
android:id="@+id/chapter_header"
2019
android:layout_width="match_parent"
21-
android:layout_height="?attr/actionBarSize" />
20+
android:layout_height="?attr/actionBarSize"
21+
app:titleTextColor="?attr/colorOnPrimary" />
22+
2223
</com.google.android.material.appbar.AppBarLayout>
2324

2425
<androidx.recyclerview.widget.RecyclerView

app/src/main/res/navigation/main.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
xmlns:tools="http://schemas.android.com/tools"
5-
android:id="@+id/nav"
6-
app:startDestination="@id/chapters_fragment">
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/nav"
6+
app:startDestination="@id/chapters_fragment">
77

88
<fragment
99
android:id="@+id/chapters_fragment"
@@ -18,6 +18,7 @@
1818
android:id="@+id/sections_fragment"
1919
android:name="guide.graphql.toc.SectionsFragment"
2020
tools:layout="@layout/sections_fragment">
21+
2122
<argument
2223
android:name="chapterId"
2324
app:argType="integer" />

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<string name="error">An error occurred.</string>
44
<string name="graphql_error">Error making the GraphQL request: %s</string>
55
<string name="chapter_title">Chapter %s: %s</string>
6-
<string name="no_section_error">This chapter has no sections.</string>
6+
<string name="no_sections">This chapter has no sections.</string>
77
<string name="section_title">%s.%s: %s</string>
8+
<string name="chapter_number">Chapter %s</string>
89
</resources>

app/src/main/res/values/styles.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<resources>
22

3-
<!-- Base application theme. -->
43
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
5-
<!-- Customize your theme here. -->
64
<item name="colorPrimary">@color/colorPrimary</item>
75
<item name="colorOnPrimary">#FFFFFF</item>
86
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

0 commit comments

Comments
 (0)