-
Notifications
You must be signed in to change notification settings - Fork 1
/
TopRatedFragment.kt
315 lines (236 loc) · 8.66 KB
/
TopRatedFragment.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.demo.themoviedb.view.fragments
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.util.Log
import android.view.*
import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.demo.themoviedb.MyApp
import com.demo.themoviedb.R
import com.demo.themoviedb.comman.BackButtonListener
import com.demo.themoviedb.comman.RouterProvider
import com.demo.themoviedb.data.model.MovieType
import com.demo.themoviedb.databinding.FragmentPopularBinding
import com.demo.themoviedb.databinding.FragmentTopRatedBinding
import com.demo.themoviedb.entity.MoviesResponse
import com.demo.themoviedb.presenter.TopRatedPresenter
import com.demo.themoviedb.utils.Coroutines
import com.demo.themoviedb.utils.MovieParser
import com.demo.themoviedb.view.acitivty.DetailActivity
import com.demo.themoviedb.view.adapters.MoviesAdapter
import com.demo.themoviedb.view.contract.TopRatedContract
import kotlinx.android.synthetic.main.fragment_top_rated.*
import ru.terrakok.cicerone.Navigator
import ru.terrakok.cicerone.Router
import ru.terrakok.cicerone.commands.Command
import ru.terrakok.cicerone.commands.Forward
class TopRatedFragment : Fragment(), TopRatedContract.View, BackButtonListener, RouterProvider {
// initialize Room db
private val movieDao = MyApp.database?.movieDao()
// initialize the navigator
private val navigator: Navigator? by lazy {
object : Navigator {
override fun applyCommand(command: Command) {
if (command is Forward) {
forward(command)
}
}
private fun forward(command: Forward) {
val data = (command.transitionData as MoviesResponse.Result)
when (command.screenKey) {
DetailActivity.TAG -> startActivity(
Intent(requireActivity(), DetailActivity::class.java)
.putExtra("data", data as Parcelable)
) // 4
else -> Log.e("Cicerone", "Unknown screen: " + command.screenKey)
}
}
}
}
var topRatedList: ArrayList<MoviesResponse.Result> = ArrayList()
var page = 1
var totalPages = 15
private var mPresenter: TopRatedContract.Presenter? = null
lateinit var mBinding: FragmentTopRatedBinding
private var mAdapter: MoviesAdapter? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
mBinding = FragmentTopRatedBinding.inflate(inflater)
// Set Menu
setHasOptionsMenu(true)
//initialize presenter
mPresenter = TopRatedPresenter(this)
return mBinding.root
}
companion object {
@JvmStatic
fun newInstance() =
TopRatedFragment().apply {
}
}
override val router: Router
get() = MyApp.INSTANCE.cicerone.router
//Show progress bar
override fun showLoading() {
try {
mBinding.rvTopRated.visibility = View.GONE
progressBarTopRated.visibility = View.VISIBLE
} catch (e: Exception) {
e.printStackTrace()
}
}
//Hide progress bar
override fun hideLoading() {
try {
mBinding.rvTopRated.visibility = View.VISIBLE
progressBarTopRated.visibility = View.GONE
} catch (e: Exception) {
e.printStackTrace()
}
}
//API data publish
override fun publishData(movies: List<MoviesResponse.Result>) {
setMovieAdapter(movies)
}
//Set the recyclerview adapter
private fun setMovieAdapter(movies: List<MoviesResponse.Result>) {
if (mAdapter == null) {
topRatedList.clear()
topRatedList.addAll(movies)
val layoutManager =
LinearLayoutManager(requireActivity())
mBinding.rvTopRated.isNestedScrollingEnabled = false
mBinding.rvTopRated.layoutManager = layoutManager
mAdapter = MoviesAdapter(
{ result -> mPresenter?.listItemClicked(result) },
topRatedList, mBinding.rvTopRated
)
mBinding.rvTopRated.adapter = mAdapter
mBinding.rvTopRated.itemAnimator = DefaultItemAnimator()
mAdapter!!.setOnLoadMoreListener(object : MoviesAdapter.OnLoadMoreListener {
override fun onLoadMore() {
if (totalPages > page) {
val pojo = MoviesResponse.Result(
false,
"",
"",
"",
"",
"",
0.0,
"",
"",
"",
false,
0.0,
0,
MovieType.TOP_RATED
)
topRatedList.add(pojo)
mAdapter!!.notifyItemInserted(topRatedList.size - 1)
page += 1
mPresenter?.onLoadNextPage(page)
}
}
})
} else {
try {
topRatedList.removeAt(topRatedList.size - 1)
mAdapter!!.notifyItemRemoved(topRatedList.size)
topRatedList.addAll(movies)
mAdapter!!.notifyDataSetChanged()
mAdapter!!.setLoaded()
} catch (e: Exception) {
e.printStackTrace()
}
}
for (i in topRatedList.indices) {
Log.d("Title $i", topRatedList[i].title)
}
}
//Show information
override fun showInfo(msg: String) {
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
}
// Delete movie list data from local db
override fun deleteTable() {
Coroutines.main {
movieDao?.deleteTopRateMovies()
}
}
// Add movie list data to local db
override fun addToTable(data: List<MoviesResponse.Result>) {
Coroutines.main {
data.iterator().forEach {
it.movieType = MovieParser.parseMovieType(MovieParser.TOP_RATED)
if (it.poster_path.isNullOrBlank()) {
it.poster_path = ""
}
if (it.backdrop_path.isNullOrBlank()) {
it.backdrop_path = ""
}
}
movieDao?.insertAll(data)
}
}
// Get movie list data from local db
override fun getFromTable() {
Coroutines.main {
movieDao?.getAllTopRateMovies()?.observe(this, Observer {
setMovieAdapter(it)
})
}
}
//Set the search view menu
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.search_menu, menu)
val menuItem = menu.findItem(R.id.menu_search)
val searchView = menuItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
try {
mAdapter?.filter?.filter(newText)
} catch (e: Exception) {
e.printStackTrace()
}
return false
}
})
}
override fun onResume() {
super.onResume()
MyApp.INSTANCE.cicerone.navigatorHolder.setNavigator(navigator)
mPresenter?.onViewCreated()
}
override fun onPause() {
super.onPause()
MyApp.INSTANCE.cicerone.navigatorHolder.removeNavigator()
}
override fun onDestroy() {
mPresenter?.onDestroyed()
mPresenter = null
super.onDestroy()
}
override fun onBackPressed(): Boolean {
val fragment = childFragmentManager.findFragmentById(R.id.root_top_rated)
return if (fragment != null && fragment is BackButtonListener
&& (fragment as BackButtonListener).onBackPressed()
) {
true
} else {
(activity as RouterProvider?)!!.router.exit()
true
}
}
}