@@ -8,6 +8,7 @@ import android.util.TypedValue
8
8
import android.view.*
9
9
import android.widget.Toast
10
10
import androidx.appcompat.widget.SearchView
11
+ import androidx.core.view.isInvisible
11
12
import androidx.core.view.isVisible
12
13
import androidx.fragment.app.Fragment
13
14
import androidx.fragment.app.viewModels
@@ -28,7 +29,7 @@ class ExploreFragment : Fragment() {
28
29
private val exploreViewModel: ExploreViewModel by viewModels()
29
30
30
31
private var _binding : FragmentExploreBinding ? = null
31
- private val binding get() = _binding !!
32
+ private val binding get() = _binding as FragmentExploreBinding
32
33
33
34
private lateinit var gameAdapter: GameAdapter
34
35
@@ -46,6 +47,7 @@ class ExploreFragment : Fragment() {
46
47
if (activity != null ) {
47
48
setHasOptionsMenu(true )
48
49
showLoading(false )
50
+ showSearchGame(true )
49
51
50
52
gameAdapter = GameAdapter ()
51
53
@@ -58,6 +60,23 @@ class ExploreFragment : Fragment() {
58
60
startActivity(intent)
59
61
}
60
62
63
+ exploreViewModel.games.observe(viewLifecycleOwner, { games ->
64
+ if (games != null ) {
65
+ when (games) {
66
+ is Resource .Loading -> showLoading(true )
67
+ is Resource .Success -> {
68
+ gameAdapter.setData(games.data)
69
+ showLoading(false )
70
+ }
71
+ is Resource .Error -> {
72
+ showNoGame(true )
73
+ Toast .makeText(context, " error" , Toast .LENGTH_SHORT ).show()
74
+ showLoading(false )
75
+ }
76
+ }
77
+ }
78
+ })
79
+
61
80
val marginVertical = TypedValue .applyDimension(TypedValue .COMPLEX_UNIT_DIP , 16f , resources.displayMetrics)
62
81
63
82
with (binding.rvExplore){
@@ -81,25 +100,13 @@ class ExploreFragment : Fragment() {
81
100
searchView.queryHint = " Cari game"
82
101
searchView.setOnQueryTextListener(object : SearchView .OnQueryTextListener {
83
102
override fun onQueryTextSubmit (query : String? ): Boolean {
103
+ showSearchGame(false )
104
+ showNoGame(false )
105
+ showLoading(true )
84
106
85
107
if (query != null ) {
86
108
lifecycleScope.launch {
87
109
exploreViewModel.searchGames(query)
88
- exploreViewModel.games.observe(viewLifecycleOwner, { games ->
89
- if (games != null ) {
90
- when (games) {
91
- is Resource .Loading -> showLoading(true )
92
- is Resource .Success -> {
93
- gameAdapter.setData(games.data)
94
- showLoading(false )
95
- }
96
- is Resource .Error -> {
97
- Toast .makeText(context, " error" , Toast .LENGTH_SHORT ).show()
98
- showLoading(false )
99
- }
100
- }
101
- }
102
- })
103
110
}
104
111
}
105
112
return true
@@ -113,6 +120,18 @@ class ExploreFragment : Fragment() {
113
120
114
121
private fun showLoading (state : Boolean ) {
115
122
binding.pbExplore.isVisible = state
123
+ binding.rvExplore.isInvisible = state
124
+ }
125
+
126
+ private fun showSearchGame (state : Boolean ) {
127
+ binding.ivSearchGame.isVisible = state
128
+ binding.tvSearchGame.isVisible = state
129
+ binding.rvExplore.isVisible = ! state
130
+ }
131
+
132
+ private fun showNoGame (state : Boolean ) {
133
+ binding.ivNoGame.isVisible = state
134
+ binding.tvNoGame.isVisible = state
116
135
binding.rvExplore.isVisible = ! state
117
136
}
118
137
0 commit comments