@@ -6,17 +6,13 @@ import androidx.activity.viewModels
6
6
import androidx.appcompat.app.AppCompatActivity
7
7
import androidx.core.content.ContextCompat
8
8
import androidx.core.view.isVisible
9
- import androidx.lifecycle.lifecycleScope
10
- import androidx.lifecycle.map
11
9
import com.bumptech.glide.Glide
12
10
import com.google.android.material.appbar.AppBarLayout
13
11
import com.riyaldi.core.data.Resource
14
12
import com.riyaldi.core.domain.model.Game
15
13
import com.riyaldi.gamekuy.R
16
14
import com.riyaldi.gamekuy.databinding.ActivityDetailBinding
17
15
import dagger.hilt.android.AndroidEntryPoint
18
- import kotlinx.coroutines.flow.map
19
- import kotlinx.coroutines.launch
20
16
import kotlin.math.abs
21
17
22
18
@AndroidEntryPoint
@@ -25,7 +21,6 @@ class DetailActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedListener
25
21
private val detailViewModel: DetailViewModel by viewModels()
26
22
27
23
private lateinit var binding: ActivityDetailBinding
28
- private lateinit var dataGame: Game
29
24
30
25
companion object {
31
26
const val EXTRA_GAME = " extra_game"
@@ -42,66 +37,25 @@ class DetailActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedListener
42
37
val game = intent.getParcelableExtra<Game >(EXTRA_GAME )
43
38
44
39
if (game != null ) {
45
- var isFav = true
46
-
47
- lifecycleScope.launch {
48
- isFav = detailViewModel.isFavorite(game.id) as Boolean
49
- }
50
- Toast .makeText(this @DetailActivity, isFav.toString(), Toast .LENGTH_SHORT ).show()
51
-
52
- if (isFav) {
53
- var isChanged = false
54
-
55
- detailViewModel.getDetailFilm(game.id).observe(this , { detailGame ->
56
- when (detailGame) {
57
- is Resource .Loading -> showLoading(true )
58
- is Resource .Success -> {
59
- dataGame = detailGame.data as Game
60
-
61
- if (dataGame.isFavorite != isFav && ! isChanged) {
62
- detailViewModel.setFavoriteGame(dataGame)
63
- setFavoriteState(dataGame.isFavorite)
64
- isChanged = true
65
- }
66
-
67
- populateData(dataGame)
68
- showLoading(false )
69
-
40
+ detailViewModel.getDetailFilm(game.id).observe(this , { detailGame ->
41
+ when (detailGame) {
42
+ is Resource .Loading -> showLoading(true )
43
+ is Resource .Success -> {
44
+ val dataGame = detailGame.data as Game
45
+ setFavoriteState(dataGame.isFavorite)
46
+ populateData(dataGame)
47
+ showLoading(false )
48
+ binding.fabAddFavorite.setOnClickListener {
49
+ detailViewModel.setFavoriteGame(dataGame)
70
50
setFavoriteState(dataGame.isFavorite)
71
- binding.fabAddFavorite.setOnClickListener {
72
- detailViewModel.setFavoriteGame(dataGame)
73
- setFavoriteState(dataGame.isFavorite)
74
- }
75
- }
76
- is Resource .Error -> {
77
- Toast .makeText(this , " error : ${detailGame.message} " , Toast .LENGTH_SHORT ).show()
78
- showLoading(false )
79
51
}
80
52
}
81
- })
82
- } else {
83
- detailViewModel.getDetailFilm(game.id).observe(this , { detailGame ->
84
- when (detailGame) {
85
- is Resource .Loading -> showLoading(true )
86
- is Resource .Success -> {
87
- dataGame = detailGame.data as Game
88
-
89
- populateData(dataGame)
90
- showLoading(false )
91
-
92
- setFavoriteState(dataGame.isFavorite)
93
- binding.fabAddFavorite.setOnClickListener {
94
- detailViewModel.setFavoriteGame(dataGame)
95
- setFavoriteState(dataGame.isFavorite)
96
- }
97
- }
98
- is Resource .Error -> {
99
- Toast .makeText(this , " error : ${detailGame.message} " , Toast .LENGTH_SHORT ).show()
100
- showLoading(false )
101
- }
53
+ is Resource .Error -> {
54
+ Toast .makeText(this , " error : ${detailGame.message} " , Toast .LENGTH_SHORT ).show()
55
+ showLoading(false )
102
56
}
103
- })
104
- }
57
+ }
58
+ })
105
59
}
106
60
}
107
61
0 commit comments