Bimba.git

ref: ef9c50aed3aab8d733a725c2aeb539cba7d3880e

app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.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
package ml.adamsprogs.bimba.activities

import android.content.DialogInterface
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_stop.*
import kotlinx.android.synthetic.main.banner.*
import ml.adamsprogs.bimba.*
import ml.adamsprogs.bimba.collections.FavouriteStorage
import ml.adamsprogs.bimba.datasources.TimetableDownloader
import ml.adamsprogs.bimba.datasources.VmService
import ml.adamsprogs.bimba.models.Departure
import ml.adamsprogs.bimba.models.Favourite
import ml.adamsprogs.bimba.models.Plate
import ml.adamsprogs.bimba.models.StopSegment
import ml.adamsprogs.bimba.models.adapters.DeparturesAdapter
import ml.adamsprogs.bimba.models.adapters.ServiceAdapter
import java.util.Calendar
import kotlin.collections.HashMap
import kotlin.collections.HashSet
import kotlin.collections.set

class StopActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadListener, ProviderProxy.OnDeparturesReadyListener {
    companion object {
        const val EXTRA_STOP_CODE = "stopCode"
        const val EXTRA_STOP_NAME = "stopName"
        const val EXTRA_FAVOURITE = "favourite"
        const val SOURCE_TYPE = "sourceType"
        const val SOURCE_TYPE_STOP = "stop"
        const val SOURCE_TYPE_FAV = "favourite"

        const val TIMETABLE_TYPE_DEPARTURE = "timetable_type_departure"
        const val TIMETABLE_TYPE_FULL = "timetable_type_full"
    }

    private var stopCode = ""
    private var favourite: Favourite? = null
    private var timetableType = TIMETABLE_TYPE_DEPARTURE
    private val context = this
    private val receiver = MessageReceiver.getMessageReceiver()
    private lateinit var providerProxy: ProviderProxy
    private val departures = HashMap<Plate.ID, List<Departure>>()
    private val fullDepartures = HashMap<String, List<Departure>>()
    private lateinit var subscriptionId: String
    private lateinit var adapter: DeparturesAdapter


    private lateinit var sourceType: String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_stop)

        providerProxy = ProviderProxy(this)

        sourceType = intent.getStringExtra(SOURCE_TYPE)

        setSupportActionBar(toolbar)

        when (sourceType) {
            SOURCE_TYPE_STOP -> {
                stopCode = intent.getSerializableExtra(EXTRA_STOP_CODE) as String
                supportActionBar?.title = intent.getSerializableExtra(EXTRA_STOP_NAME) as String
            }
            SOURCE_TYPE_FAV -> {
                favourite = intent.getParcelableExtra(EXTRA_FAVOURITE)
                supportActionBar?.title = favourite!!.name
            }
        }

        showFab()

        val layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        departuresList.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(departuresList.context, layoutManager.orientation))
        departuresList.adapter = DeparturesAdapter(this, null, true)
        adapter = departuresList.adapter as DeparturesAdapter
        departuresList.layoutManager = layoutManager

        departuresList.addOnScrollListener(object : androidx.recyclerview.widget.RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: androidx.recyclerview.widget.RecyclerView, newState: Int) {}
            override fun onScrolled(recyclerView: androidx.recyclerview.widget.RecyclerView, dx: Int, dy: Int) {
                updateFabVisibility(dy)
                super.onScrolled(recyclerView, dx, dy)
            }
        })

        if (stopCode != "")
            providerProxy.getVmMessage(stopCode) { message ->
                if (message != null) {
                    val rendered =
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY).toString()
                            } else {
                                @Suppress("DEPRECATION")
                                Html.fromHtml(message).toString()
                            }

                    banner.visibility = View.VISIBLE
                    banner_text.text = rendered
                    banner_more.setOnClickListener {
                        AlertDialog.Builder(context)
                                .setPositiveButton(context.getText(android.R.string.ok))
                                { dialog: DialogInterface, _: Int -> dialog.cancel() }
                                .setCancelable(true)
                                .setMessage(rendered)
                                .create().show()
                    }
                }
            }

        prepareOnDownloadListener()
        subscribeForDepartures()
    }

    private fun showFab() {
        if (sourceType == SOURCE_TYPE_FAV)
            return

        val favourites = FavouriteStorage.getFavouriteStorage(context)
        if (!favourites.has(stopCode)) {
            fab.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_favourite_empty, this.theme))
        }

        fab.setOnClickListener {
            if (!favourites.has(stopCode)) {
                val items = HashSet<StopSegment>()
                items.add(StopSegment(stopCode, null))
                favourites.add(stopCode, items, this@StopActivity)
                fab.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_favourite, this.theme))
            } else {
                Snackbar.make(it, getString(R.string.stop_already_fav), Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show()
            }
        }
    }

    //fixme<p:5> maybe better effects
    private fun updateFabVisibility(dy: Int) {
        if (fab == null)
            return
        if (dy > 0) {
            fab.hide()
        } else {
            fab.show()
        }
    }

    private fun prepareOnDownloadListener() {
        val filter = IntentFilter(TimetableDownloader.ACTION_DOWNLOADED)
        filter.addAction(VmService.ACTION_READY)
        filter.addCategory(Intent.CATEGORY_DEFAULT)
        registerReceiver(receiver, filter)
        receiver.addOnTimetableDownloadListener(context)
    }

    private fun subscribeForDepartures() {
        subscriptionId = if (sourceType == SOURCE_TYPE_STOP) {
            providerProxy.subscribeForDepartures(stopCode, this, this)
        } else
            favourite!!.subscribeForDepartures(this, context)
    }

    override fun onDeparturesReady(departures: List<Departure>, plateId: Plate.ID?, code: Int) {
        progressBar.visibility = View.GONE
        showError(stop_layout, code, this)
        if (plateId == null) {
            this.departures.clear()
            this.departures[Plate.ID.dummy] = departures
        } else {
            this.departures.remove(Plate.ID.dummy)
            this.departures[plateId] = departures
        }
        if (timetableType == TIMETABLE_TYPE_FULL)
            return
        refreshAdapter()
        if (adapter.departures?.isEmpty() != false) {
            emptyStateIcon.visibility = View.VISIBLE
            emptyStateText.visibility = View.VISIBLE
            departuresList.visibility = View.GONE
        } else {
            emptyStateIcon.visibility = View.GONE
            emptyStateText.visibility = View.GONE
            departuresList.visibility = View.VISIBLE
        }
    }

    private fun refreshAdapter() {
        if (timetableType == TIMETABLE_TYPE_FULL) {
            @Suppress("UNCHECKED_CAST")
            adapter.departures = fullDepartures[(dateSpinner.selectedItem as ServiceAdapter.RowItem).service]
        } else {
            val now = Calendar.getInstance()
            val seconds = now.secondsAfterMidnight()
            adapter.departures = this.departures.flatMap { it.value }.sortedBy { (if (it.onStop) 0 else 1) * 172800 + it.timeTill(seconds) }  // todo sorted by also onStop
        }
        adapter.notifyDataSetChanged()
    }

    override fun onTimetableDownload(result: String?) {
        val message: String = when (result) {
            TimetableDownloader.RESULT_NO_CONNECTIVITY -> getString(R.string.no_connectivity_cant_update)
            TimetableDownloader.RESULT_UP_TO_DATE -> getString(R.string.timetable_up_to_date)
            TimetableDownloader.RESULT_FINISHED -> getString(R.string.timetable_downloaded)
            else -> getString(R.string.error_try_later)
        }
        try {
            Snackbar.make(findViewById(R.id.stop_layout), message, Snackbar.LENGTH_LONG).show()
        } catch (e: IllegalArgumentException) {
        }
        providerProxy.refreshTimetable(this)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        if (providerProxy.mode == ProviderProxy.MODE_FULL)
            menuInflater.inflate(R.menu.menu_stop, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId

        if (id == R.id.action_change_type) {
            if (timetableType == TIMETABLE_TYPE_DEPARTURE) {
                timetableType = TIMETABLE_TYPE_FULL

                item.icon = (ResourcesCompat.getDrawable(resources, R.drawable.ic_timetable_departure, this.theme))
                adapter.relativeTime = false
                if (fullDepartures.isEmpty())
                    if (sourceType == SOURCE_TYPE_STOP)
                        fullDepartures.putAll(providerProxy.getFullTimetable(stopCode))
                    else
                        fullDepartures.putAll(favourite!!.fullTimetable())

                dateSpinner.let { spinner ->
                    spinner.adapter = ServiceAdapter(this, R.layout.toolbar_spinner_item, fullDepartures.keys.map {
                        ServiceAdapter.RowItem(it, providerProxy.describeService(it, this)!!)
                    }.sorted()).apply {
                        setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                    }
                    spinner.visibility = View.VISIBLE
                    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                            refreshAdapter()
                        }

                        override fun onNothingSelected(parent: AdapterView<*>?) {
                        }

                    }
                }

                refreshAdapter()
            } else {
                dateSpinner.visibility = View.GONE
                timetableType = TIMETABLE_TYPE_DEPARTURE
                item.icon = (ResourcesCompat.getDrawable(resources, R.drawable.ic_timetable_full, this.theme))
                adapter.relativeTime = true
                refreshAdapter()
            }
            return true
        }

        return super.onOptionsItemSelected(item)
    }

    override fun onDestroy() {
        super.onDestroy()
        receiver.removeOnTimetableDownloadListener(context)
        if (sourceType == SOURCE_TYPE_STOP)
            providerProxy.unsubscribeFromDepartures(subscriptionId, this)
        else
            favourite!!.unsubscribeFromDepartures(this)
        unregisterReceiver(receiver)
    }
}