Brak opisu

DiscoverActivity.kt 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.example.floradex20.activities
  2. import android.content.Context
  3. import android.content.Intent
  4. import android.content.SharedPreferences
  5. import android.os.Bundle
  6. import android.util.Log
  7. import android.view.Menu
  8. import android.view.MenuInflater
  9. import android.view.MenuItem
  10. import android.widget.Button
  11. import android.widget.EditText
  12. import androidx.appcompat.app.AppCompatActivity
  13. import androidx.recyclerview.widget.LinearLayoutManager
  14. import androidx.recyclerview.widget.RecyclerView
  15. import com.example.floradex20.ApiManager
  16. import com.example.floradex20.R
  17. import com.example.floradex20.adapters.FlowerAdapter
  18. import com.example.floradex20.models.Flower
  19. class DiscoverActivity : AppCompatActivity(), FlowerAdapter.OnItemClickListener {
  20. private lateinit var recyclerView: RecyclerView
  21. private lateinit var plants: List<Flower>
  22. //Para desplegar el boton de filtro en Discover Page
  23. override fun onCreateOptionsMenu(menu: Menu?): Boolean {
  24. val inflater: MenuInflater = menuInflater
  25. inflater.inflate(R.menu.filter_menu, menu)
  26. return true
  27. }
  28. override fun onCreate(savedInstanceState: Bundle?) {
  29. super.onCreate(savedInstanceState)
  30. setContentView(R.layout.activity_discover)
  31. //Donde se guarda el input del search bar y el search button
  32. val enterButton = findViewById<Button>(R.id.search_enter)
  33. val input = findViewById<EditText>(R.id.search)
  34. // Get user_id
  35. val sharedPref: SharedPreferences = applicationContext.getSharedPreferences("user_info", Context.MODE_PRIVATE)
  36. val userID = sharedPref.getString("user_id", "")!!
  37. Log.i("preferences", userID)
  38. // Get flowers with user_id
  39. // val thisActivity = this@Discover_activity
  40. val apimanager = ApiManager(findViewById(R.id.discover_activity))
  41. apimanager.getPlants(userID) {
  42. recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
  43. recyclerView.apply {
  44. setHasFixedSize(true)
  45. plants = it.data
  46. adapter = FlowerAdapter(plants, this@DiscoverActivity)
  47. layoutManager = LinearLayoutManager(this@DiscoverActivity)
  48. //Aqui hay que chequiar si el API response llega bien
  49. recyclerView.setAdapter(adapter)
  50. recyclerView.setLayoutManager(layoutManager)
  51. }
  52. }
  53. //Esto se ejecuta cuando el usuario hace un search con el search bar
  54. enterButton?.setOnClickListener {
  55. val search_input = input.text.toString()
  56. apimanager.getPlantsByName(userID, search_input) {
  57. // progress_bar.visibility = View.GONE
  58. recyclerView.apply {
  59. // Log.i("velllllllllllllllllllll", response.body().toString())
  60. setHasFixedSize(true)
  61. plants = it.data
  62. adapter = FlowerAdapter(plants, this@DiscoverActivity)
  63. layoutManager = LinearLayoutManager(this@DiscoverActivity)
  64. recyclerView.setAdapter(adapter)
  65. recyclerView.setLayoutManager(layoutManager)
  66. }
  67. }
  68. }
  69. }
  70. override fun onItemClick(position: Int){
  71. //Empezar el activity de Jose
  72. val plantInfo = arrayOf<String>(plants[position].common_name.toString(), plants[position].scientific_name.toString()
  73. , plants[position].year.toString(), plants[position].author.toString(), plants[position].image_url.toString())
  74. val intent = Intent(this, FlowerDetailActivity::class.java).apply {
  75. putExtra("plant_info", plantInfo)
  76. }
  77. startActivity(intent)
  78. Log.i("Position", plantInfo.get(0))
  79. }
  80. //Esto es lo que se ejecuta cuando se preciona el boton de filter en el UI.
  81. //Ejecuta Filtering.kt
  82. override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
  83. R.id.filterIcon -> {
  84. val intent = Intent(this, FilteringActivity::class.java)
  85. startActivityForResult(intent, 1)
  86. true
  87. }
  88. else -> {
  89. super.onOptionsItemSelected(item) //Error handling?
  90. }
  91. }
  92. //Esta es la funcion que se ejecuta cuando se recibe los filtros de
  93. //filtering.kt. Hace un call al api con los filtros.
  94. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  95. super.onActivityResult(requestCode, resultCode, data)
  96. val edible = data?.getStringExtra("edible").toString().toLowerCase()
  97. val vegetable = data?.getStringExtra("vegetable").toString().toLowerCase()
  98. val scientificName = data?.getStringExtra("cientifico").toString().toLowerCase()
  99. val color = data?.getStringExtra("color").toString().toLowerCase()
  100. // Get user_id
  101. val sharedPref: SharedPreferences = applicationContext.getSharedPreferences("user_info", Context.MODE_PRIVATE)
  102. val userID = sharedPref.getString("user_id", "")!!
  103. Log.i("preferences", userID)
  104. // Get flowers (with filters)
  105. val apiManager = ApiManager(findViewById(R.id.discover_activity))
  106. apiManager.getFiltered(userID, edible, vegetable, color, scientificName) {
  107. recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
  108. recyclerView.apply {
  109. setHasFixedSize(true)
  110. plants = it.data
  111. adapter = FlowerAdapter(plants, this@DiscoverActivity)
  112. layoutManager = LinearLayoutManager(this@DiscoverActivity)
  113. //Aqui hay que chequiar si el API response llega bien
  114. recyclerView.setAdapter(adapter)
  115. recyclerView.setLayoutManager(layoutManager)
  116. }
  117. }
  118. }
  119. }