ref: 3ddf8ece77302b3b0578d6e7c40e9f0716364485
app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.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 |
package ml.adamsprogs.bimba.search import android.content.Context import android.content.Intent import android.location.Location import android.location.LocationListener import android.location.LocationManager import android.net.ConnectivityManager import android.os.Bundle import android.os.Handler import android.os.Looper import android.util.Log import android.view.View import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.content.res.AppCompatResources import androidx.core.view.WindowCompat import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.coroutines.* import ml.adamsprogs.bimba.R import ml.adamsprogs.bimba.api.* import ml.adamsprogs.bimba.databinding.ActivityResultsBinding import ml.adamsprogs.bimba.departures.DeparturesActivity import java.io.InputStream class ResultsActivity : AppCompatActivity(), LocationListener { enum class Mode { MODE_LOCATION, MODE_SEARCH } private var _binding: ActivityResultsBinding? = null private val binding get() = _binding!! private lateinit var adapter: BimbaResultsAdapter private val handler = Handler(Looper.getMainLooper()) private var runnable = Runnable {} override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) _binding = ActivityResultsBinding.inflate(layoutInflater) setContentView(binding.root) binding.resultsRecycler.layoutManager = LinearLayoutManager(this) adapter = BimbaResultsAdapter(layoutInflater, this, listOf()) { when (it) { is Stop -> { val intent = Intent(this, DeparturesActivity::class.java).apply { putExtra("code", it.code) putExtra("name", it.name) } startActivity(intent) } is Line -> { TODO("[3.1] start line graph activity") } } } binding.resultsRecycler.adapter = adapter setSupportActionBar(binding.topAppBar) WindowCompat.setDecorFitsSystemWindows(window, false) @Suppress("DEPRECATION") // fixme later getSerializable in API>=33 when (intent.extras?.get("mode")) { Mode.MODE_LOCATION -> { supportActionBar?.title = getString(R.string.stops_nearby) locate() } Mode.MODE_SEARCH -> { val query = intent.extras?.getString("query")!! supportActionBar?.title = getString(R.string.results_for, query) getItemsByQuery(Server.get(this), query) } } } private fun locate() { try { val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000 * 60 * 10, 100f, this ) handler.removeCallbacks(runnable) runnable = Runnable { showError(Error(0, R.string.error_gps, R.drawable.error_gps)) } handler.postDelayed(runnable, 60 * 1000) locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) ?.let { onLocationChanged(it) } } catch (_: SecurityException) { // this won’t happen because we don’t start this activity without location permission } } override fun onLocationChanged(location: Location) { handler.removeCallbacks(runnable) getItemsByLocation(Server.get(this), Position(location.latitude, location.longitude)) } override fun onResume() { super.onResume() @Suppress("DEPRECATION") // fixme later getSerializable in API>=33 if (intent.extras?.get("mode") == Mode.MODE_LOCATION) { locate() } } override fun onPause() { super.onPause() val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager locationManager.removeUpdates(this) handler.removeCallbacks(runnable) } override fun onDestroy() { super.onDestroy() val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager locationManager.removeUpdates(this) handler.removeCallbacks(runnable) } private fun getItemsByQuery(server: Server, query: String) { val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager MainScope().launch { val itemsResult = queryItems(cm, server, query) if (itemsResult.error != null) { Log.e("Results.query", "$itemsResult") showError(itemsResult.error) } else { val response = unmarshallItemResponse(itemsResult.stream!!) updateItems((response as ItemsSuccess).items) } } } private fun getItemsByLocation(server: Server, position: Position) { val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager MainScope().launch { val itemsResult = locateItems(cm, server, position) if (itemsResult.error != null) { Log.e("Results.location", "$itemsResult") showError(itemsResult.error) } else { val response = unmarshallItemResponse(itemsResult.stream!!) updateItems((response as ItemsSuccess).items) } } } private fun showError(error: Error) { binding.resultsProgress.visibility = View.GONE binding.resultsRecycler.visibility = View.GONE binding.errorImage.visibility = View.VISIBLE binding.errorText.visibility = View.VISIBLE binding.errorText.text = getString(error.stringResource) binding.errorImage.setImageDrawable(AppCompatResources.getDrawable(this, error.imageResource)) } private fun updateItems(items: List<Item>) { binding.resultsProgress.visibility = View.GONE adapter.update(items) if (items.isEmpty()) { binding.errorImage.visibility = View.VISIBLE binding.errorText.visibility = View.VISIBLE binding.resultsRecycler.visibility = View.GONE binding.errorText.text = getString(R.string.error_404) binding.errorImage.setImageDrawable( AppCompatResources.getDrawable( this, R.drawable.error_search ) ) } else { binding.resultsOverlay.visibility = View.GONE binding.errorImage.visibility = View.GONE binding.errorText.visibility = View.GONE binding.resultsRecycler.visibility = View.VISIBLE } } private suspend fun unmarshallItemResponse(stream: InputStream): ItemsResponse { return withContext(Dispatchers.IO) { ItemsResponse.unmarshal(stream) } } } |