Skip to content

Commit b094b68

Browse files
committed
Cleanup
1 parent 37adaf9 commit b094b68

File tree

9 files changed

+62
-67
lines changed

9 files changed

+62
-67
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ package guide.graphql.toc
22

33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
5-
import androidx.navigation.NavController
65
import androidx.navigation.findNavController
7-
import androidx.navigation.ui.AppBarConfiguration
86
import androidx.navigation.ui.NavigationUI
97
import guide.graphql.toc.databinding.ActivityMainBinding
108

119
class MainActivity : AppCompatActivity() {
1210

1311
private lateinit var binding: ActivityMainBinding
1412

15-
1613
override fun onCreate(savedInstanceState: Bundle?) {
1714
super.onCreate(savedInstanceState)
15+
1816
binding = ActivityMainBinding.inflate(layoutInflater)
19-
val view = binding.root
20-
setContentView(view)
2117

18+
setContentView(binding.root)
2219
setSupportActionBar(binding.toolbar)
23-
NavigationUI.setupActionBarWithNavController(this, findNavController(R.id.nav_host_fragment))
2420

21+
NavigationUI.setupActionBarWithNavController(
22+
this,
23+
findNavController(R.id.nav_host_fragment)
24+
)
2525
}
2626

2727
override fun onSupportNavigateUp(): Boolean {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ data class Resource<out T>(val status: Status, val data: T?, val message: String
1616
}
1717
}
1818
}
19+
1920
enum class Status {
2021
SUCCESS,
2122
ERROR,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
77
import android.widget.Toast
8-
import androidx.appcompat.app.AppCompatActivity
98
import androidx.fragment.app.Fragment
109
import androidx.fragment.app.viewModels
1110
import androidx.lifecycle.Observer
1211
import androidx.navigation.fragment.findNavController
1312
import androidx.recyclerview.widget.DividerItemDecoration
1413
import androidx.recyclerview.widget.LinearLayoutManager
15-
import com.google.android.material.transition.MaterialFade
1614
import com.google.android.material.transition.MaterialSharedAxis
1715
import guide.graphql.toc.R
1816
import guide.graphql.toc.Status
@@ -63,8 +61,10 @@ class ChaptersFragment : Fragment() {
6361
)
6462
)
6563
}
64+
6665
val layoutManager = LinearLayoutManager(requireContext())
6766
binding.chapters.layoutManager = layoutManager
67+
6868
val itemDivider = DividerItemDecoration(requireContext(), layoutManager.orientation)
6969
binding.chapters.addItemDecoration(itemDivider)
7070
binding.chapters.adapter = adapter
@@ -78,11 +78,10 @@ class ChaptersFragment : Fragment() {
7878
}
7979
Status.ERROR -> Toast.makeText(
8080
requireContext(),
81-
"Error: ${chapterListResponse.message}",
81+
getString(R.string.graphql_error, chapterListResponse.message),
8282
Toast.LENGTH_SHORT
8383
).show()
8484
}
8585
})
86-
8786
}
8887
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ class ChaptersViewModel : ViewModel() {
2323
).toDeferred().await()
2424

2525
if (response.hasErrors()) {
26-
emit(Resource.error("Response has errors" ,null))
26+
emit(Resource.error("Response has errors", null))
2727
return@liveData
2828
}
29-
response.data?.chapters?.let{
29+
response.data?.chapters?.let {
3030
emit(Resource.success(response.data!!.chapters))
3131
return@liveData
3232
}
33-
emit(Resource.error("Data is null" ,null))
33+
emit(Resource.error("Data is null", null))
3434
return@liveData
3535
} catch (e: ApolloException) {
3636
Log.d("ChaptersQuery", "GraphQL request failed", e)
37-
emit(Resource.error("GraphQL request failed", null))
37+
emit(Resource.error("GraphQL request failed", null))
3838
return@liveData
3939
}
4040
}

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@ import android.os.Bundle
44
import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
7-
import androidx.appcompat.app.AppCompatActivity
87
import androidx.fragment.app.Fragment
98
import androidx.fragment.app.viewModels
109
import androidx.lifecycle.Observer
11-
import androidx.lifecycle.lifecycleScope
12-
import androidx.navigation.fragment.findNavController
1310
import androidx.navigation.fragment.navArgs
1411
import androidx.recyclerview.widget.DividerItemDecoration
1512
import androidx.recyclerview.widget.LinearLayoutManager
16-
import com.apollographql.apollo.coroutines.toDeferred
17-
import com.apollographql.apollo.exception.ApolloException
18-
import com.google.android.material.transition.MaterialFade
19-
import com.google.android.material.transition.MaterialFadeThrough
2013
import com.google.android.material.transition.MaterialSharedAxis
21-
import guide.graphql.toc.*
14+
import guide.graphql.toc.Status
2215
import guide.graphql.toc.databinding.SectionsFragmentBinding
2316

2417
class SectionsFragment : Fragment() {
@@ -54,35 +47,35 @@ class SectionsFragment : Fragment() {
5447
requireContext(),
5548
args.chapterNumber
5649
)
50+
5751
val layoutManager = LinearLayoutManager(requireContext())
5852
binding.sections.layoutManager = layoutManager
53+
5954
val itemDivider =
6055
DividerItemDecoration(requireContext(), layoutManager.orientation)
6156
binding.sections.addItemDecoration(itemDivider)
6257
binding.sections.adapter = adapter
6358

6459
viewModel.sectionsList.observe(viewLifecycleOwner, Observer { sectionsResource ->
6560
when (sectionsResource.status) {
66-
Status.SUCCESS -> {
67-
sectionsResource.data?.let {
68-
adapter.updateSections(it)
69-
binding.spinner.visibility = View.GONE
70-
binding.error.visibility = View.GONE
71-
}
61+
Status.SUCCESS -> {
62+
sectionsResource.data?.let {
63+
adapter.updateSections(it)
64+
binding.spinner.visibility = View.GONE
65+
binding.error.visibility = View.GONE
66+
}
7267
}
7368
Status.ERROR -> {
74-
showErrorMessage(sectionsResource.message?: "")
69+
showErrorMessage(sectionsResource.message ?: "")
7570
}
7671
Status.LOADING -> {
7772
binding.spinner.visibility = View.VISIBLE
7873
binding.error.visibility = View.GONE
7974
}
8075
}
81-
})
82-
76+
})
8377

8478
viewModel.chapterId = args.chapterId
85-
8679
}
8780

8881
private fun showErrorMessage(error: String) {
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package guide.graphql.toc.ui.sections
22

33
import android.util.Log
4-
import androidx.lifecycle.*
4+
import androidx.lifecycle.LiveData
5+
import androidx.lifecycle.MutableLiveData
6+
import androidx.lifecycle.ViewModel
57
import com.apollographql.apollo.coroutines.toDeferred
68
import com.apollographql.apollo.exception.ApolloException
79
import guide.graphql.toc.Resource
810
import guide.graphql.toc.SectionsQuery
911
import guide.graphql.toc.apolloClient
1012

11-
class SectionsViewModel: ViewModel() {
13+
class SectionsViewModel : ViewModel() {
1214

1315
private val _chapterId: MutableLiveData<Int> = MutableLiveData()
1416

@@ -22,34 +24,35 @@ class SectionsViewModel: ViewModel() {
2224
}
2325
}
2426

25-
val sectionsList: LiveData<Resource<List<SectionsQuery.Section?>>> = _chapterId.switchMap { sectionId ->
26-
return@switchMap liveData {
27-
emit(Resource.loading(null))
28-
try {
29-
val response = apolloClient.query(
30-
SectionsQuery(id = sectionId)
31-
).toDeferred().await()
27+
val sectionsList: LiveData<Resource<List<SectionsQuery.Section?>>> =
28+
_chapterId.switchMap { sectionId ->
29+
return@switchMap liveData {
30+
emit(Resource.loading(null))
31+
try {
32+
val response = apolloClient.query(
33+
SectionsQuery(id = sectionId)
34+
).toDeferred().await()
3235

33-
if (response.hasErrors()) {
34-
emit(Resource.error("Response has errors" ,null))
35-
return@liveData
36-
}
37-
response.data?.chapter?.sections?.let { sections ->
38-
if (sections.size > 1) {
39-
emit(Resource.success(sections))
40-
} else {
41-
emit(Resource.error("No sections", null))
36+
if (response.hasErrors()) {
37+
emit(Resource.error("Response has errors", null))
38+
return@liveData
4239
}
40+
response.data?.chapter?.sections?.let { sections ->
41+
if (sections.size > 1) {
42+
emit(Resource.success(sections))
43+
} else {
44+
emit(Resource.error("No sections", null))
45+
}
46+
return@liveData
47+
}
48+
emit(Resource.error("Chapter has no sections", null))
49+
return@liveData
50+
} catch (e: ApolloException) {
51+
Log.d("Sections Query", "GraphQL request failed", e)
52+
emit(Resource.error("GraphQL request failed", null))
4353
return@liveData
4454
}
45-
emit(Resource.error("Chapter has no sections" ,null))
46-
return@liveData
47-
} catch (e: ApolloException) {
48-
Log.d("Sections Query", "GraphQL request failed", e)
49-
emit(Resource.error("GraphQL request failed", null))
50-
return@liveData
5155
}
5256
}
53-
}
5457

5558
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
android:id="@+id/toolbar"
2121
android:layout_width="match_parent"
2222
android:layout_height="?attr/actionBarSize"
23-
android:theme="@style/AppTheme.Toolbar"/>
23+
android:theme="@style/AppTheme.Toolbar" />
2424
</com.google.android.material.appbar.AppBarLayout>
2525

26-
2726
<fragment
2827
android:id="@+id/nav_host_fragment"
29-
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
3028
android:name="androidx.navigation.fragment.NavHostFragment"
3129
android:layout_width="match_parent"
3230
android:layout_height="match_parent"
3331
app:defaultNavHost="true"
32+
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
3433
app:navGraph="@navigation/main" />
3534

3635
</androidx.coordinatorlayout.widget.CoordinatorLayout>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
android:layout_height="wrap_content"
66
android:background="?selectableItemBackground"
77
android:paddingStart="20dp"
8+
android:paddingEnd="20dp"
89
android:paddingTop="16dp"
910
android:paddingBottom="16dp">
1011

@@ -31,5 +32,4 @@
3132
app:layout_constraintStart_toStartOf="parent"
3233
app:layout_constraintTop_toBottomOf="@+id/chapter_header" />
3334

34-
3535
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +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"
12-
android:label="@string/app_name">
11+
android:label="@string/app_name"
12+
tools:layout="@layout/chapters_fragment">
1313
<action
1414
android:id="@+id/view_sections"
1515
app:destination="@id/sections_fragment" />
@@ -18,16 +18,16 @@
1818
<fragment
1919
android:id="@+id/sections_fragment"
2020
android:name="guide.graphql.toc.ui.sections.SectionsFragment"
21-
tools:layout="@layout/sections_fragment"
22-
android:label="{chapterTitle}">
21+
android:label="{chapterTitle}"
22+
tools:layout="@layout/sections_fragment">
2323

2424
<argument
2525
android:name="chapterId"
2626
app:argType="integer" />
2727
<argument
2828
android:name="chapterNumber"
29-
app:argType="integer"
30-
android:defaultValue="-1" />
29+
android:defaultValue="-1"
30+
app:argType="integer" />
3131
<argument
3232
android:name="chapterTitle"
3333
app:argType="string" />

0 commit comments

Comments
 (0)