Skip to content

Commit de4acbb

Browse files
Use labels to change toolbar instead of programatically
1 parent 14746ff commit de4acbb

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ class MainActivity : AppCompatActivity() {
1212

1313
private lateinit var binding: ActivityMainBinding
1414

15-
private val navigationListener = NavController.OnDestinationChangedListener { controller, destination, arguments ->
16-
if (destination.id == R.id.chapters_fragment) {
17-
this.setTitle(R.string.app_name)
18-
}
19-
}
2015

2116
override fun onCreate(savedInstanceState: Bundle?) {
2217
super.onCreate(savedInstanceState)
@@ -32,14 +27,4 @@ class MainActivity : AppCompatActivity() {
3227
override fun onSupportNavigateUp(): Boolean {
3328
return findNavController(R.id.nav_host_fragment).navigateUp() || super.onSupportNavigateUp()
3429
}
35-
36-
override fun onResume() {
37-
super.onResume()
38-
findNavController(R.id.nav_host_fragment).addOnDestinationChangedListener(navigationListener)
39-
}
40-
41-
override fun onPause() {
42-
super.onPause()
43-
findNavController(R.id.nav_host_fragment).removeOnDestinationChangedListener(navigationListener)
44-
}
4530
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.navigation.fragment.findNavController
1313
import androidx.recyclerview.widget.DividerItemDecoration
1414
import androidx.recyclerview.widget.LinearLayoutManager
1515
import com.google.android.material.transition.MaterialFade
16+
import guide.graphql.toc.R
1617
import guide.graphql.toc.Status
1718
import guide.graphql.toc.databinding.ChaptersFragmentBinding
1819

@@ -51,7 +52,11 @@ class ChaptersFragment : Fragment() {
5152
ChaptersFragmentDirections.viewSections(
5253
chapterId = chapter.id,
5354
chapterNumber = chapter.number?.toInt() ?: -1,
54-
chapterTitle = chapter.title
55+
chapterTitle = if (chapter.number == null) chapter.title else getString(
56+
R.string.chapter_title,
57+
chapter.number.toString(),
58+
chapter.title
59+
)
5560
)
5661
)
5762
}
@@ -68,7 +73,11 @@ class ChaptersFragment : Fragment() {
6873
adapter.updateChapters(it)
6974
}
7075
}
71-
Status.ERROR -> Toast.makeText(requireContext(), "Error: ${chapterListResponse.message}", Toast.LENGTH_SHORT).show()
76+
Status.ERROR -> Toast.makeText(
77+
requireContext(),
78+
"Error: ${chapterListResponse.message}",
79+
Toast.LENGTH_SHORT
80+
).show()
7281
}
7382
})
7483

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
<fragment
99
android:id="@+id/chapters_fragment"
1010
android:name="guide.graphql.toc.ui.chapters.ChaptersFragment"
11-
tools:layout="@layout/chapters_fragment">
11+
tools:layout="@layout/chapters_fragment"
12+
android:label="@string/app_name">
1213
<action
1314
android:id="@+id/view_sections"
1415
app:destination="@id/sections_fragment" />
@@ -17,7 +18,8 @@
1718
<fragment
1819
android:id="@+id/sections_fragment"
1920
android:name="guide.graphql.toc.ui.sections.SectionsFragment"
20-
tools:layout="@layout/sections_fragment">
21+
tools:layout="@layout/sections_fragment"
22+
android:label="{chapterTitle}">
2123

2224
<argument
2325
android:name="chapterId"

0 commit comments

Comments
 (0)