-
Notifications
You must be signed in to change notification settings - Fork 1
/
MovieInfoActivity.kt
123 lines (107 loc) · 4.71 KB
/
MovieInfoActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.template.nanamare.presentation.activity
import android.net.Uri
import android.os.Bundle
import androidx.lifecycle.Observer
import com.template.nanamare.BR
import com.template.nanamare.BuildConfig.VIDEO_URL
import com.template.nanamare.R
import com.template.nanamare.data.network.NetworkState
import com.template.nanamare.databinding.ItemActorBinding
import com.template.nanamare.databinding.MovieInfoActivityBinding
import com.template.nanamare.domain.model.CreditModel
import com.template.nanamare.domain.model.VideoModel
import com.template.nanamare.presentation.anim.SimpleAnimation
import com.template.nanamare.presentation.base.ui.BaseActivity
import com.template.nanamare.presentation.base.ui.SimpleRecyclerView
import com.template.nanamare.presentation.dialog.VideoFragment
import com.template.nanamare.presentation.mapper.ActorPresentationMapper
import com.template.nanamare.presentation.mapper.ResultPresentationMapper
import com.template.nanamare.presentation.model.ActorPresentation
import com.template.nanamare.presentation.vm.MovieInfoViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.util.*
class MovieInfoActivity : BaseActivity<MovieInfoActivityBinding>(R.layout.movie_info_activity) {
private val movieInfoViewModel by viewModel<MovieInfoViewModel>()
private val actorSimpleAdapter by lazy {
object :
SimpleRecyclerView.Adapter<ActorPresentation, ItemActorBinding>(
R.layout.item_actor,
BR.actor
) {
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.run {
vm = movieInfoViewModel
actorAdapter = actorSimpleAdapter
movieInfoViewModel.movie = intent.extras?.getParcelable(EXTRA_MOVIE_ITEM)
initAnim()
initActor()
}
movieInfoViewModel.run {
liveMovieVideoState.observe(this@MovieInfoActivity, Observer {
when (it) {
is NetworkState.Init -> hideLoadingPopup()
is NetworkState.Loading -> showLoadingPopup()
is NetworkState.Success<VideoModel> -> getUrl(it)
is NetworkState.Error -> showToast(it.throwable.toString())
is NetworkState.ServerError -> showToast(it.toString())
}
})
liveMovieCreditState.observe(this@MovieInfoActivity, Observer {
when (it) {
is NetworkState.Init -> hideLoadingPopup()
is NetworkState.Loading -> showLoadingPopup()
is NetworkState.Success<CreditModel> -> {
liveLoading.value = false
initActorAdapter(it.item)
}
is NetworkState.Error -> showToast(it.throwable.toString())
is NetworkState.ServerError -> showToast(it.toString())
}
})
}
}
private fun initActorAdapter(creditModel: CreditModel) {
movieInfoViewModel.liveActor.value =
creditModel.cast.map(ActorPresentationMapper::mapToPresentation)
}
private fun getUrl(it: NetworkState.Success<VideoModel>) {
it.item.results.map(ResultPresentationMapper::mapToPresentation)
.asSequence()
.filter { "YouTube".toLowerCase(Locale.getDefault()) == it.site.toLowerCase(Locale.getDefault()) }
.reduce { acc, result -> if (acc.size > result.size) acc else result }.let {
VideoFragment().show(
supportFragmentManager,
VideoFragment::class.java,
Bundle().apply {
putString(
VideoFragment.KEY_VIDEO_URL,
Uri.parse(VIDEO_URL).buildUpon().appendQueryParameter("v", it.key)
.build()
.toString()
)
},
VideoFragment::class.simpleName
)
}
}
private fun initActor() {
movieInfoViewModel.run {
movie?.let {
getMovieCredit(it.id)
}
}
}
private fun MovieInfoActivityBinding.initAnim() {
ivPoster.startAnimation(SimpleAnimation.leftToRight)
movieInfoBody.root.startAnimation(SimpleAnimation.rightToLeft)
tvNext.startAnimation(SimpleAnimation.bottomToTop)
movieSummaryBody.root.startAnimation(SimpleAnimation.rightToLeft)
movieActorInfoBody.root.startAnimation(SimpleAnimation.leftToRight)
}
companion object {
const val EXTRA_MOVIE_ITEM = "EXTRA_MOVIE_ITEM"
}
}