Author: Adam Pioterek <adam.pioterek@protonmail.ch>
full timetable select service date
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt b/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt index a484c43a07383038d0b9f3b60c967bc4b030e26e..32dddcd358b1801b29be9440b9a0c0322cdb8460 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.kt @@ -193,6 +193,17 @@ else timetable.getStopName(stopCode) } + fun describeService(service: String, context: Context): String? { + return if (timetable.isEmpty()) + null + else + timetable.getServiceDescription(service, context) + } + + fun getServiceFirstDay(service: String): Int { + return timetable.getServiceFirstDay(service) + } + interface OnDeparturesReadyListener { fun onDeparturesReady(departures: List<Departure>, plateId: Plate.ID?) } diff --git a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt index 7bcd314cf54224f830474503d13805caf820e671..b6dbb3eeab4dbf3ca1b8ef2bd3077c7276377fd9 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt @@ -7,6 +7,7 @@ import android.view.* import android.support.v4.content.res.ResourcesCompat import android.support.v7.app.AppCompatActivity import android.support.v7.widget.* +import android.widget.AdapterView import java.util.Calendar import kotlinx.android.synthetic.main.activity_stop.* @@ -15,9 +16,9 @@ import ml.adamsprogs.bimba.collections.FavouriteStorage import ml.adamsprogs.bimba.datasources.* import ml.adamsprogs.bimba.models.* import ml.adamsprogs.bimba.models.adapters.DeparturesAdapter +import ml.adamsprogs.bimba.models.adapters.ServiceAdapter class StopActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadListener, ProviderProxy.OnDeparturesReadyListener { - companion object { const val EXTRA_STOP_CODE = "stopCode" const val EXTRA_STOP_NAME = "stopName" @@ -36,7 +37,7 @@ } private var stopCode = "" private var favourite: Favourite? = null - private var timetableType = "departure" + private var timetableType = TIMETABLE_TYPE_DEPARTURE private val context = this private val receiver = MessageReceiver.getMessageReceiver() private lateinit var providerProxy: ProviderProxy @@ -151,7 +152,8 @@ } private fun refreshAdapter() { if (timetableType == TIMETABLE_TYPE_FULL) { - // todo adapter.departures = fullDepartures + @Suppress("UNCHECKED_CAST") + adapter.departures = fullDepartures[(dateSpinner.selectedItem as ServiceAdapter.RowItem).service] } else { val now = Calendar.getInstance() val seconds = now.secondsAfterMidnight() @@ -186,6 +188,7 @@ 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()) @@ -193,8 +196,28 @@ 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 diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt b/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt index d2aad3409764302956a3650228b9fc792c495669..fa9209cc29bf5fa3f831edec5a208568095f370a 100644 --- a/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt +++ b/app/src/main/java/ml/adamsprogs/bimba/models/Timetable.kt @@ -178,6 +178,16 @@ return name } + fun getStopId(stopCode: String): String { + val cursor = db!!.rawQuery("select stop_id from stops where stop_code = ?", + arrayOf(stopCode)) + cursor.moveToNext() + val id = cursor.getString(0) + cursor.close() + + return id + } + fun getStopCode(stopId: String): String { val cursor = db!!.rawQuery("select stop_code from stops where stop_id = ?", arrayOf(stopId)) @@ -189,11 +199,12 @@ return code } fun getStopDepartures(stopCode: String): Map<String, List<Departure>> { + val stopID = getStopId(stopCode) val map = HashMap<String, ArrayList<Departure>>() val cursor = db!!.rawQuery("select route_id, service_id, departure_time, " + "wheelchair_accessible, stop_sequence, trip_id, trip_headsign, route_desc " + - "from stop_times natural join trips natural join routes where stop_code = ?", - arrayOf(stopCode)) + "from stop_times natural join trips natural join routes where stop_id = ?", + arrayOf(stopID)) while (cursor.moveToNext()) { val line = cursor.getString(0) @@ -478,25 +489,36 @@ return graphs } + fun getServiceFirstDay(service: String): Int { + val cursor = db!!.rawQuery("select * from calendar where service_id = ?", arrayOf(service)) + cursor.moveToFirst() + var i = 1 + while ((cursor.getString(i) == "0") and (i < 8)) i++ + cursor.close() + return i + } + fun getServiceDescription(service: String, context: Context): String { val dayNames = SparseArray<String>() dayNames.put(1, context.getString(R.string.Mon)) - dayNames.put(1, context.getString(R.string.Tue)) - dayNames.put(1, context.getString(R.string.Wed)) - dayNames.put(1, context.getString(R.string.Thu)) - dayNames.put(1, context.getString(R.string.Fri)) - dayNames.put(1, context.getString(R.string.Sat)) - dayNames.put(1, context.getString(R.string.Sun)) + dayNames.put(2, context.getString(R.string.Tue)) + dayNames.put(3, context.getString(R.string.Wed)) + dayNames.put(4, context.getString(R.string.Thu)) + dayNames.put(5, context.getString(R.string.Fri)) + dayNames.put(6, context.getString(R.string.Sat)) + dayNames.put(7, context.getString(R.string.Sun)) val cursor = db!!.rawQuery("select * from calendar where service_id = ?", arrayOf(service)) + cursor.moveToFirst() val days = SparseBooleanArray() for (i in 1..7) { days.append(i, cursor.getString(i) == "1") } + days.append(8, false) val description = ArrayList<String>() var start = 0 - for (i in 1..7) { + for (i in 1..8) { if (!days[i] and (start > 0)) { when { i - start == 1 -> description.add(dayNames[start]) diff --git a/app/src/main/java/ml/adamsprogs/bimba/models/adapters/ServiceAdapter.kt b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/ServiceAdapter.kt new file mode 100644 index 0000000000000000000000000000000000000000..014cd4a7cf014ff56ec0ca3e54a77c90a769f805 --- /dev/null +++ b/app/src/main/java/ml/adamsprogs/bimba/models/adapters/ServiceAdapter.kt @@ -0,0 +1,43 @@ +package ml.adamsprogs.bimba.models.adapters + +import android.annotation.SuppressLint +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import android.view.LayoutInflater +import ml.adamsprogs.bimba.R +import android.app.Activity +import android.widget.ArrayAdapter +import ml.adamsprogs.bimba.ProviderProxy + + +class ServiceAdapter(context: Activity, resourceId: Int, list: List<RowItem>) : ArrayAdapter<ServiceAdapter.RowItem>(context, resourceId, list) { + + private val inflater: LayoutInflater = context.layoutInflater + + @SuppressLint("ViewHolder", "InflateParams") + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val rowItem: RowItem = getItem(position) + val rowView = inflater.inflate(R.layout.toolbar_spinner_item, null, true) + rowView.findViewById<TextView>(R.id.text).text = rowItem.description + + return rowView + } + + @SuppressLint("InflateParams") + override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View { + val rowItem: RowItem = getItem(position) + val rowView = inflater.inflate(R.layout.toolbar_spinner_item, null, true) + rowView.findViewById<TextView>(R.id.text).text = rowItem.description + + return rowView + + } + + data class RowItem(val service: String, val description: String) : Comparable<RowItem> { + override fun compareTo(other: RowItem): Int { + val proxy = ProviderProxy() + return proxy.getServiceFirstDay(service).compareTo(proxy.getServiceFirstDay(other.service)) + } + } +} diff --git a/app/src/main/res/layout/activity_stop.xml b/app/src/main/res/layout/activity_stop.xml index 0aefb1bbdb0b77ef1893e948cfa161bb4e1bfb58..d6a0b92bfbdd7ad1d7be8b4b6481ca929b573770 100644 --- a/app/src/main/res/layout/activity_stop.xml +++ b/app/src/main/res/layout/activity_stop.xml @@ -28,6 +28,18 @@ app:title="@string/app_name"> </android.support.v7.widget.Toolbar> + <Spinner + android:id="@+id/dateSpinner" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginBottom="8dp" + android:layout_marginEnd="8dp" + android:layout_marginLeft="8dp" + android:layout_marginRight="8dp" + android:layout_marginStart="8dp" + android:layout_weight="1" + android:visibility="gone" /> + </android.support.design.widget.AppBarLayout> diff --git a/app/src/main/res/layout/toolbar_spinner_item.xml b/app/src/main/res/layout/toolbar_spinner_item.xml new file mode 100644 index 0000000000000000000000000000000000000000..1cb3890d88df2edaeec0cf238d7d7afb271c3f9c --- /dev/null +++ b/app/src/main/res/layout/toolbar_spinner_item.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="utf-8"?> +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/text" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:ellipsize="end" + android:singleLine="true" + android:textAlignment="inherit" + android:textColor="@color/text_on_toolbar" /> diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 9c139ca27b5819a8a45720dac6414ea95bab8fd7..5982e32ecdb6a838a1c7f45e3f82e9a77241302e 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -12,4 +12,5 @@#ffc107 <color name="tram">#00adef</color> <color name="bus">#c4212a</color> + <color name="text_on_toolbar">#ffffff</color> </resources>