· 7 years ago · Nov 29, 2018, 07:54 PM
1package com.example.cis400assignment2
2
3import android.support.v7.app.AppCompatActivity
4import android.os.Bundle
5import android.util.Log
6import android.view.MotionEvent
7import android.widget.ImageView
8import android.widget.SeekBar
9import android.widget.Toast
10import com.google.gson.Gson
11import kotlinx.android.synthetic.main.activity_movie_list.*
12import java.lang.Math.abs
13
14class MovieListActivity : AppCompatActivity() {
15
16 val movies = """
17 [
18 {
19 "vote_count": 222,
20 "id": 346910,
21 "video": false,
22 "vote_average": 5.7,
23 "title": "The Predator",
24 "popularity": 252.73,
25 "poster_path": "/wMq9kQXTeQCHUZOG4fAe5cAxyUA.jpg",
26 "original_language": "en",
27 "original_title": "The Predator",
28 "genre_ids": [
29 27,
30 878,
31 28,
32 35
33 ],
34 "backdrop_path": "/f4E0ocYeToEuXvczZv6QArrMDJ.jpg",
35 "adult": false,
36 "overview": "From the outer reaches of space to the small-town streets of suburbia, the hunt comes home. Now, the universe’s most lethal hunters are stronger, smarter and deadlier than ever before, having genetically upgraded themselves with DNA from other species. When a young boy accidentally triggers their return to Earth, only a ragtag crew of ex-soldiers and a disgruntled science teacher can prevent the end of the human race.",
37 "release_date": "2018-09-13"
38 },
39 {
40 "vote_count": 1669,
41 "id": 348350,
42 "video": false,
43 "vote_average": 6.7,
44 "title": "Solo: A Star Wars Story",
45 "popularity": 242.322,
46 "poster_path": "/4oD6VEccFkorEBTEDXtpLAaz0Rl.jpg",
47 "original_language": "en",
48 "original_title": "Solo: A Star Wars Story",
49 "genre_ids": [
50 28,
51 12,
52 878
53 ],
54 "backdrop_path": "/96B1qMN9RxrAFu6uikwFhQ6N6J9.jpg",
55 "adult": false,
56 "overview": "Through a series of daring escapades deep within a dark and dangerous criminal underworld, Han Solo meets his mighty future copilot Chewbacca and encounters the notorious gambler Lando Calrissian.",
57 "release_date": "2018-05-15"
58 },
59 {
60 "vote_count": 471,
61 "id": 439079,
62 "video": false,
63 "vote_average": 6,
64 "title": "The Nun",
65 "popularity": 198.619,
66 "poster_path": "/sFC1ElvoKGdHJIWRpNB3xWJ9lJA.jpg",
67 "original_language": "en",
68 "original_title": "The Nun",
69 "genre_ids": [
70 27,
71 9648,
72 53
73 ],
74 "backdrop_path": "/fgsHxz21B27hOOqQBiw9L6yWcM7.jpg",
75 "adult": false,
76 "overview": "When a young nun at a cloistered abbey in Romania takes her own life, a priest with a haunted past and a novitiate on the threshold of her final vows are sent by the Vatican to investigate. Together they uncover the order’s unholy secret. Risking not only their lives but their faith and their very souls, they confront a malevolent force in the form of the same demonic nun that first terrorized audiences in “The Conjuring 2,†as the abbey becomes a horrific battleground between the living and the damned.",
77 "release_date": "2018-09-05"
78 },
79 {
80 "vote_count": 8117,
81 "id": 299536,
82 "video": false,
83 "vote_average": 8.3,
84 "title": "Avengers: Infinity War",
85 "popularity": 193.69,
86 "poster_path": "/7WsyChQLEftFiDOVTGkv3hFpyyt.jpg",
87 "original_language": "en",
88 "original_title": "Avengers: Infinity War",
89 "genre_ids": [
90 12,
91 878,
92 28
93 ],
94 "backdrop_path": "/lmZFxXgJE3vgrciwuDib0N8CfQo.jpg",
95 "adult": false,
96 "overview": "As the Avengers and their allies have continued to protect the world from threats too large for any one hero to handle, a new danger has emerged from the cosmic shadows: Thanos. A despot of intergalactic infamy, his goal is to collect all six Infinity Stones, artifacts of unimaginable power, and use them to inflict his twisted will on all of reality. Everything the Avengers have fought for has led up to this moment - the fate of Earth and existence itself has never been more uncertain.",
97 "release_date": "2018-04-25"
98 },
99 {
100 "vote_count": 3414,
101 "id": 351286,
102 "video": false,
103 "vote_average": 6.5,
104 "title": "Jurassic World: Fallen Kingdom",
105 "popularity": 178.036,
106 "poster_path": "/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg",
107 "original_language": "en",
108 "original_title": "Jurassic World: Fallen Kingdom",
109 "genre_ids": [
110 28,
111 12,
112 878
113 ],
114 "backdrop_path": "/3s9O5af2xWKWR5JzP2iJZpZeQQg.jpg",
115 "adult": false,
116 "overview": "Three years after the demise of Jurassic World, a volcanic eruption threatens the remaining dinosaurs on the isla Nublar, so Claire Dearing, the former park manager, recruits Owen Grady to help prevent the extinction of the dinosaurs once again.",
117 "release_date": "2018-06-06"
118 },
119 {
120 "vote_count": 703,
121 "id": 442249,
122 "video": false,
123 "vote_average": 6,
124 "title": "The First Purge",
125 "popularity": 151.976,
126 "poster_path": "/2slvblTroiT1lY9bYLK7Amigo1k.jpg",
127 "original_language": "en",
128 "original_title": "The First Purge",
129 "genre_ids": [
130 28,
131 878,
132 53,
133 27
134 ],
135 "backdrop_path": "/r4clWIxKeWoqrHspQP0XunuUmB7.jpg",
136 "adult": false,
137 "overview": "To push the crime rate below one percent for the rest of the year, the New Founding Fathers of America test a sociological theory that vents aggression for one night in one isolated community. But when the violence of oppressors meets the rage of the others, the contagion will explode from the trial-city borders and spread across the nation.",
138 "release_date": "2018-07-04"
139 },
140 {
141 "vote_count": 21,
142 "id": 135870,
143 "video": false,
144 "vote_average": 4,
145 "title": "Rebel",
146 "popularity": 142.332,
147 "poster_path": "/dNTzorrVmcJ25fUGzs9FfkoXb0P.jpg",
148 "original_language": "te",
149 "original_title": "రెబెలà±",
150 "genre_ids": [
151 28
152 ],
153 "backdrop_path": "/yCGEkEqy7uOo7NwlbgOzGrI8i43.jpg",
154 "adult": false,
155 "overview": "Rebel is a romantic action revenge drama, which revolves around the story of a happy-go-lucky guy. The hero (Prabhas) is the son of a good-natured mafia don (Krishnam Raju). The hero becomes Rebel after his father is killed. How he takes revenge upon the people, who killed his father, will form the second half of the movie. Apart from directing, Raghava Lawrence also handles the choreography, screenplay and music direction for the film. The film is being produced by J. Bhaghawan and J. Pulla Rao under Sri Balaji Cine Media banner.",
156 "release_date": "2012-09-28"
157 },
158 {
159 "vote_count": 907,
160 "id": 345940,
161 "video": false,
162 "vote_average": 6.1,
163 "title": "The Meg",
164 "popularity": 140.252,
165 "poster_path": "/eyWICPcxOuTcDDDbTMOZawoOn8d.jpg",
166 "original_language": "en",
167 "original_title": "The Meg",
168 "genre_ids": [
169 28,
170 878,
171 53
172 ],
173 "backdrop_path": "/rH79sB6Nkx4cMW3JzsUy7wK0rhX.jpg",
174 "adult": false,
175 "overview": "A deep sea submersible pilot revisits his past fears in the Mariana Trench, and accidentally unleashes the seventy foot ancestor of the Great White Shark believed to be extinct.",
176 "release_date": "2018-08-09"
177 },
178 {
179 "vote_count": 33,
180 "id": 414919,
181 "video": false,
182 "vote_average": 6.5,
183 "title": "Hart Beat",
184 "popularity": 124.218,
185 "poster_path": "/h1PBdhD1c6xnTV6c7B4d58OESYB.jpg",
186 "original_language": "nl",
187 "original_title": "Hart Beat",
188 "genre_ids": [
189 10751,
190 35,
191 18
192 ],
193 "backdrop_path": "/pJG7QnbqUeFNKre3BTVvrv96ZUX.jpg",
194 "adult": false,
195 "overview": "Making music means everything for the shy Zoë. But since her father left, she never played guitar again. When she is hit by the bus from Mik, superstar Bieber size, sparks fly allthough she is not a fan at first. Off course, people around the two love birds dont exactly cooperate to make things work...",
196 "release_date": "2016-10-12"
197 },
198 {
199 "vote_count": 27,
200 "id": 507569,
201 "video": false,
202 "vote_average": 5.6,
203 "title": "The Seven Deadly Sins: Prisoners of the Sky",
204 "popularity": 112.839,
205 "poster_path": "/r6pPUVUKU5eIpYj4oEzidk5ZibB.jpg",
206 "original_language": "ja",
207 "original_title": "The Seven Deadly Sins: Prisoners of the Sky",
208 "genre_ids": [
209 28,
210 12,
211 14,
212 16
213 ],
214 "backdrop_path": "/uKwOX7MtKlAaGeCQe6c4jc1vZpj.jpg",
215 "adult": false,
216 "overview": "The Seven Deadly Sins travel to a remote land in search of the phantom ingredient \"sky fish.\" Meliodas and Hawk end up at a \"Sky Palace\" that exists above the clouds, where all the residents have wings. Meliodas is mistaken for a boy who committed a crime and is thrown in prison. Meanwhile, the residents are preparing a ceremony for defense against a ferocious beast that awakens once every 3,000 years. But the Six Knights of Black, a Demon Clan army, arrives and removes the seal on the beast in order to threaten the lives of the residents of Sky Palace. Meliodas and his allies meet the Six Knights of Black in battle.",
217 "release_date": "2018-08-18"
218 }
219 ]
220 """.trimIndent()
221
222 // declare variables for the movie list and poster table
223 lateinit var movieList: List<MovieData>
224 lateinit var posterTable:MutableMap<String, Int>
225 // original height and width of the poster images
226 var originalWidth: Int = 154
227 var originalHeight: Int = 231
228 // downX variable needed for gesture math
229 var downX: Float = Float.NaN
230 // needed to keep track of the current movie displayed
231 var movieIndex: Int = 0
232
233
234 override fun onCreate(savedInstanceState: Bundle?) {
235 super.onCreate(savedInstanceState)
236 setContentView(R.layout.activity_movie_list)
237
238 // create the movie list from JSON string using Gson
239 this.movieList = Gson().fromJson(movies,
240 Array<MovieData>::class.java).asList()
241
242 // create the poster table, mapping title and image id
243 posterTable = mutableMapOf()
244 posterTable["The Predator"] = R.drawable.poster_predator
245 posterTable["Solo: A Star Wars Story"] = R.drawable.poster_starwars
246 posterTable["The Nun"] = R.drawable.poster_nun
247 posterTable["Avengers: Infinity War"] = R.drawable.poster_avengers
248 posterTable["Jurassic World: Fallen Kingdom"] = R.drawable.poster_jurassicworld
249 posterTable["The First Purge"] = R.drawable.poster_purge
250 posterTable["Rebel"] = R.drawable.poster_rebel
251 posterTable["The Meg"] = R.drawable.poster_meg
252 posterTable["Hart Beat"] = R.drawable.poster_hartbeat
253 posterTable["The Seven Deadly Sins: Prisoners of the Sky"] = R.drawable.poster_sevendeadlysins
254
255 // load/display the first movie info
256 loadMovieInfo(movieIndex)
257
258 // set the seekbar to 50 percent
259 movieSeekBar.progress = 50
260
261 // seekbar listener
262 movieSeekBar.setOnSeekBarChangeListener(
263 object: SeekBar.OnSeekBarChangeListener {
264 override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
265 // Make the image change sizes here!
266
267 changePosterSize(154 * (progress/25), 231 * (progress/25))
268// Toast.makeText(this@MovieListActivity,
269// "Progress is " + seekBar?.progress + "%", Toast.LENGTH_LONG).show()
270
271 }
272
273 override fun onStartTrackingTouch(seekBar: SeekBar?) {
274 //Log.i("Seekbar", "touched!!!")
275 }
276
277 override fun onStopTrackingTouch(seekBar: SeekBar?) {
278 // perform action when stop touching
279// Toast.makeText(this@MovieListActivity,
280// "Progress is " + seekBar?.progress + "%", Toast.LENGTH_LONG).show()
281 }
282 }
283 )
284
285 // set long click listener for the image
286 posterImageView.setOnLongClickListener{
287 this.movieSeekBar.progress = 50
288 //posterImageView.requestLayout()
289 originalHeight = posterImageView.layoutParams.height
290 originalWidth = posterImageView.layoutParams.width
291 changePosterSize(originalWidth, originalHeight)
292 true
293 }
294
295 posterImageView.setOnClickListener {
296 Toast.makeText(this@MovieListActivity,
297 "Poster image clicked!", Toast.LENGTH_LONG).show()
298 }
299
300 posterImageView.setOnTouchListener { v, event ->
301 when (event?.action) {
302 MotionEvent.ACTION_DOWN -> {
303 downX = event.x
304 }
305 MotionEvent.ACTION_UP -> {
306 val deltaX = downX - event.x
307
308 if (abs(deltaX) > 20F) {
309 if (deltaX > 0) {
310 //right to left
311// Toast.makeText(this@MovieListActivity,
312// "Right to left!", Toast.LENGTH_LONG).show()
313 if (movieIndex < 9) {
314 movieIndex++
315 loadMovieInfo(movieIndex)
316 }
317 }
318 else {
319 // left to right
320// Toast.makeText(this@MovieListActivity,
321// "Left to right!", Toast.LENGTH_LONG).show()
322
323 if (movieIndex > 0) {
324 movieIndex--
325 loadMovieInfo(movieIndex)
326 }
327 }
328 }
329 }
330 }
331 v?.onTouchEvent(event) ?: true
332 }
333
334 }
335
336 // cited https://stackoverflow.com/questions/3144940/set-imageview-width-and-height-programmatically
337 private fun changePosterSize(width: Int, height: Int) {
338 posterImageView.requestLayout()
339 posterImageView.layoutParams.height = height
340 posterImageView.layoutParams.width = width
341 }
342
343 private fun loadMovieInfo(movieIndex: Int) {
344 // load the image
345 posterImageView.setImageResource(posterTable.get(movieList[movieIndex].title)!!)
346 movieTitleView.text = movieList[movieIndex].title
347 movieRatingView.text = movieList[movieIndex].vote_average.toString()
348 movieIdView.text = movieList[movieIndex].id.toString()
349 movieOverviewView.text = movieList[movieIndex].overview
350
351 }
352}