@@ -6,13 +6,17 @@ 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
9
11
import com.bumptech.glide.Glide
10
12
import com.google.android.material.appbar.AppBarLayout
11
13
import com.riyaldi.core.data.Resource
12
14
import com.riyaldi.core.domain.model.Game
13
15
import com.riyaldi.gamekuy.R
14
16
import com.riyaldi.gamekuy.databinding.ActivityDetailBinding
15
17
import dagger.hilt.android.AndroidEntryPoint
18
+ import kotlinx.coroutines.flow.map
19
+ import kotlinx.coroutines.launch
16
20
import kotlin.math.abs
17
21
18
22
@AndroidEntryPoint
@@ -21,6 +25,7 @@ class DetailActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedListener
21
25
private val detailViewModel: DetailViewModel by viewModels()
22
26
23
27
private lateinit var binding: ActivityDetailBinding
28
+ private lateinit var dataGame: Game
24
29
25
30
companion object {
26
31
const val EXTRA_GAME = " extra_game"
@@ -37,26 +42,66 @@ class DetailActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedListener
37
42
val game = intent.getParcelableExtra<Game >(EXTRA_GAME )
38
43
39
44
if (game != null ) {
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)
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
+
50
70
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 )
51
79
}
52
80
}
53
- is Resource .Error -> {
54
- Toast .makeText(this , " error : ${detailGame.message} " , Toast .LENGTH_SHORT ).show()
55
- showLoading(false )
56
- }
57
- }
58
- })
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 )
59
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
+ }
102
+ }
103
+ })
104
+ }
60
105
}
61
106
}
62
107
0 commit comments