/
DetailRestoActivity.kt
259 lines (224 loc) · 11.4 KB
/
DetailRestoActivity.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
package com.azhar.restofinder.activities
import android.annotation.SuppressLint
import android.app.Activity
import android.app.ProgressDialog
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.androidnetworking.AndroidNetworking
import com.androidnetworking.common.Priority
import com.androidnetworking.error.ANError
import com.androidnetworking.interfaces.JSONObjectRequestListener
import com.azhar.restofinder.R
import com.azhar.restofinder.activities.DetailRestoActivity
import com.azhar.restofinder.adapter.HighlightsAdapter
import com.azhar.restofinder.adapter.ReviewAdapter
import com.azhar.restofinder.model.ModelHighlights
import com.azhar.restofinder.model.ModelMain
import com.azhar.restofinder.model.ModelReview
import com.azhar.restofinder.networking.ApiEndpoint
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.android.synthetic.main.activity_detail_resto.*
import org.json.JSONException
import org.json.JSONObject
import java.util.*
class DetailRestoActivity : AppCompatActivity() {
private var mProgressBar: ProgressDialog? = null
private var highlightsAdapter: HighlightsAdapter? = null
private var reviewAdapter: ReviewAdapter? = null
private val modelHighlights: MutableList<ModelHighlights> = ArrayList()
private val modelReview: MutableList<ModelReview> = ArrayList()
var RatingResto = 0.0
var IdResto: String? = null
var ImageCover: String? = null
var Title: String? = null
var Rating: String? = null
var RestoName: String? = null
var modelMain: ModelMain? = null
@SuppressLint("Assert", "SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail_resto)
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
if (Build.VERSION.SDK_INT >= 21) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
window.statusBarColor = resources.getColor(R.color.colorPrimary)
}
mProgressBar = ProgressDialog(this)
mProgressBar?.setTitle("Mohon Tunggu")
mProgressBar?.setCancelable(false)
mProgressBar?.setMessage("Sedang menampilkan data...")
toolbar.setTitle("")
setSupportActionBar(toolbar)
assert(supportActionBar != null)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
modelMain = intent.getSerializableExtra(DETAIL_RESTO) as ModelMain
if (modelMain != null) {
IdResto = modelMain?.idResto
ImageCover = modelMain?.thumbResto
RatingResto = modelMain!!.aggregateRating
Title = modelMain?.nameResto
Rating = modelMain?.ratingText
RestoName = modelMain?.nameResto
tvTitle.setText(Title)
tvRestoName.setText(RestoName)
tvRating.setText("$RatingResto | $Rating")
tvTitle.setSelected(true)
val newValue = RatingResto.toFloat()
ratingResto.setNumStars(5)
ratingResto.setStepSize(0.5.toFloat())
ratingResto.setRating(newValue)
Glide.with(this)
.load(ImageCover)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imgCover)
//method get Highlight
showRecyclerViewList()
//method get Detail
getDetailResto()
//method get Review
getReviewResto()
}
}
private fun showRecyclerViewList() {
highlightsAdapter = HighlightsAdapter(modelHighlights)
reviewAdapter = ReviewAdapter(this, modelReview)
rvHighlights.setLayoutManager(LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false))
rvHighlights.setHasFixedSize(true)
rvHighlights.setAdapter(highlightsAdapter)
rvReviewResto.setLayoutManager(LinearLayoutManager(this))
rvReviewResto.setHasFixedSize(true)
rvReviewResto.setAdapter(reviewAdapter)
}
private fun getDetailResto() {
mProgressBar?.show()
AndroidNetworking.get(ApiEndpoint.BASEURL + ApiEndpoint.DetailRestaurant + IdResto)
.addHeaders("user-key", "ZOMATO API KEY")
.setPriority(Priority.HIGH)
.build()
.getAsJSONObject(object : JSONObjectRequestListener {
@SuppressLint("SetTextI18n")
override fun onResponse(response: JSONObject) {
try {
mProgressBar?.dismiss()
val jsonArrayOne = response.getJSONArray("highlights")
for (i in 0 until jsonArrayOne.length()) {
val dataApi = ModelHighlights()
val highlights = jsonArrayOne[i].toString()
dataApi.highlights = highlights
modelHighlights.add(dataApi)
}
val jsonObjectData = response.getJSONObject("location")
val jsonArrayTwo = response.getJSONArray("establishment")
for (x in 0 until jsonArrayTwo.length()) {
val Establishment = jsonArrayTwo[x].toString()
tvEstablishment.text = Establishment
}
val AverageCost = response.getString("average_cost_for_two")
val PriceRange = response.getString("price_range")
val Currency = response.getString("currency")
val Timings = response.getString("timings")
val LocalityVerbose = jsonObjectData.getString("locality_verbose")
val Address = jsonObjectData.getString("address")
val Telepon = response.getString("phone_numbers")
val Website = response.getString("url")
val Latitude = jsonObjectData.getDouble("latitude")
val Longitude = jsonObjectData.getDouble("longitude")
tvLocalityVerbose.text = LocalityVerbose
tvAverageCost.text = "$Currency $AverageCost / $PriceRange orang"
tvAddress.text = Address
tvOpenTime.text = Timings
llRoute.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?daddr=$Latitude,$Longitude"))
startActivity(intent)
}
llTelpon.setOnClickListener {
val intent: Intent
intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$Telepon"))
startActivity(intent)
}
llWebsite.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(Website))
startActivity(intent)
}
highlightsAdapter?.notifyDataSetChanged()
} catch (e: JSONException) {
e.printStackTrace()
Toast.makeText(this@DetailRestoActivity,
"Gagal menampilkan data!", Toast.LENGTH_SHORT).show()
}
}
override fun onError(anError: ANError) {
mProgressBar?.dismiss()
Toast.makeText(this@DetailRestoActivity,
"Tidak ada jaringan internet!", Toast.LENGTH_SHORT).show()
}
})
}
private fun getReviewResto() {
mProgressBar?.show()
AndroidNetworking.get(ApiEndpoint.BASEURL + ApiEndpoint.ReviewRestaurant + IdResto)
.addHeaders("user-key", "ZOMATO API KEY")
.setPriority(Priority.HIGH)
.build()
.getAsJSONObject(object : JSONObjectRequestListener {
override fun onResponse(response: JSONObject) {
try {
mProgressBar?.dismiss()
val jsonArray = response.getJSONArray("user_reviews")
for (i in 0 until jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(i)
val dataApi = ModelReview()
val jsonObjectDataOne = jsonObject.getJSONObject("review")
val jsonObjectDataTwo = jsonObjectDataOne.getJSONObject("user")
dataApi.ratingReview = jsonObjectDataOne.getDouble("rating")
dataApi.reviewText = jsonObjectDataOne.getString("review_text")
dataApi.reviewTime = jsonObjectDataOne.getString("review_time_friendly")
dataApi.nameUser = jsonObjectDataTwo.getString("name")
dataApi.profileImage = jsonObjectDataTwo.getString("profile_image")
modelReview.add(dataApi)
}
reviewAdapter?.notifyDataSetChanged()
} catch (e: JSONException) {
e.printStackTrace()
Toast.makeText(this@DetailRestoActivity, "Gagal menampilkan data!", Toast.LENGTH_SHORT).show()
}
}
override fun onError(anError: ANError) {
mProgressBar?.dismiss()
Toast.makeText(this@DetailRestoActivity, "Tidak ada jaringan internet!", Toast.LENGTH_SHORT).show()
}
})
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
finish()
return true
}
return super.onOptionsItemSelected(item)
}
companion object {
const val DETAIL_RESTO = "detailResto"
fun setWindowFlag(activity: Activity, bits: Int, on: Boolean) {
val window = activity.window
val layoutParams = window.attributes
if (on) {
layoutParams.flags = layoutParams.flags or bits
} else {
layoutParams.flags = layoutParams.flags and bits.inv()
}
window.attributes = layoutParams
}
}
}